Dynamically set octave while playing note

How can I play a note with an octave saved in a variable, like so:

my_octave = 4

play :C my_octave

Hello @musicninja!

There are several ways to do this. Here’s a couple:

  • the use_octave and with_octave functions
  • the note function (which accepts an octave: opt)

More details are available in the ‘Lang’ section of the in-app help panel, under those particular function references :+1:

Happy to elaborate if you need any further help :slightly_smiling_face:

1 Like

Brilliant, thank you. I’ll use the note function. That’s exactly what I was looking for.

2 Likes
# play a note and send a midi note with a different octave


use_midi_defaults ip: "127.0.0.1",
  port: "tobespoke_1",
  channel: "1"

use_synth :piano
foo = 0
8.times do
  
  n = note(:C, octave: foo)
  play n
  midi n
  foo = foo + 1
  sleep 0.5
end