Opts on Custom Functions?

Hello I was interested in telling the computer to play sine waves with specific frequencies, and I found a custom made function that allows for this. However unlike the Play function, the “Fplay” function doesn’t allow for options. Is there a way for me to change the function, or to achieve this goal another way? Below is the function, with an example telling it to play a note with a frequency of exactly 256. The biggest problem is with the Play function I can decide what the sustain will be, and with Fplay it is the same short notes over and over. Any help would be greatly appreciated, Making functions is a little outside my skill range. I found this function on GitHub but the thread was closed so I couldn’t ask the maker.

define :fplay do |x|
play hz_to_midi(x)
end

fplay 256

You can add in opts like this

define :fplay do |x,*args|
  play hz_to_midi(x),*args
end
use_synth :tri
fplay 440, sustain:3, pan: 1
3 Likes

Hi Chris,

Here’s a short example for you to look over.

The only clue I’ll give you… |x, y=0.25, z=1|
gives default values to use if nothing is passed
to the function, which is why: fplay 250 and
fplay 256, 0.5, 2 will both work.

define :fplay do |x, y=0.25, z=1|
  play hz_to_midi(x), rel: y, sustain: z
end

fplay 256, 0.5, 2
sleep 3
fplay 250

sleep 5

notes = [112, 114, 118, 106, 114, 112, 114, 118, 130, 114].ring
releases = [0.25, 0.25, 0.5, 1,0.25, 0.25, 0.5, 1, 0.5, 0.5].ring
sustains = [0.5].ring
sleeps = [0.75, 1, 1.5, 0.5, 1,0.75, 1, 1.5, 0.5, 1 ].ring

live_loop :tune do
  fplay notes.tick, releases.look, sustains.look
  sleep sleeps.look
end

Eli…

Would it be possible to use this code to create a joystick that “Yokos” aka it makes a sustained note that goes up and down in pitch based on the y axis of the joystick?

Yes it should be possible. I did a control program for Sonic Pi using a ps3 controller and a python script back in 2017 which could be modified.

EDIT the program is several years old, and you would need to modify detection of incoming osc calls. The sync statements now need to use
sync "/osc*/... instead of sync "/osc/....." to allow for new syntax. Also the incoming port is now 4560 not 4559 which it was previously.

2 Likes

Amazing! thank you so much!

1 Like