Hello.
I was trying to make a triplet rhythm, but when I do the following, I got an error.
What should I do?
t = 1
loop do
use_synth :pretty_bell
play 70
sleep t/3
end
Hello.
I was trying to make a triplet rhythm, but when I do the following, I got an error.
What should I do?
t = 1
loop do
use_synth :pretty_bell
play 70
sleep t/3
end
Hello @hyeroni
The Ruby language (and by extension, Sonic Pi, which is based on it) is in this case treating the value of t/3
as an integer, since both t
and 3
are integers - therefore, instead of something like 0.3
, (to however many decimal places) it is treating the result as 0
.
Since an integer result (which in this results in dividing by zero) is not what you want, the solution here is to cause Ruby/Sonic Pi to give you a floating point number answer instead. You do this by making sure that at least one of the numbers in the division is a floating point number, which you can do by writing something like this: t/3.0
. (note the decimal point and zero), or you could also convert t
to a float by saying something like t.to_f/3
What an interesting quirk of the language and a good fix. Iām inclined to go with making it 3.0
Thank you so much~~ ^^*