Use Ruby Array Methods in Sonic PI

#29Nov2018

#Using Ruby array methods with SonicPI
# Google ruby array methods
# not every Ruby method is implemented in Sonic PI
# https://ruby-doc.org/core-2.2.0/Array.html

use_synth :piano
rel= 1.5 # note release
nle=[0.125] # note length
nle=nle.ring
amplitude= 1
key= 'C4'
scaletype='major'
cscale=knit(key,16)

with_fx :level, amp: amplitude do
  play_pattern_timed cscale.zip(scale(key,scaletype)+scale(key,scaletype).
                                reverse).flatten,nle,release: nle[0]*rel
end

with_fx :level, amp: amplitude do
  play_pattern_timed cscale.zip(scale(key,scaletype)+scale(key,scaletype).
                                reverse),nle,release: nle[0]*rel
end
1 Like

I’ve taken the liberty of editing your post and adding ``` before and after the code.
As you had it before, just pasting the code in directly, the single quotes around C4 had been converted to smart quotes.
If you precede and follow your code with three reverse ticks then it posts correctly. It also has the bonus of
added colour to show the syntax.
PS it sounds nice :grinning:

undefined method `zip’

Runtime Error: [buffer 2, line 15] - NoMethodError

Thread death!

undefined method `zip' for (ring "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4"):SonicPi::Core::RingVector

This is a good example of having to be careful with some ruby methods with Sonic Pi. The problem is that cscale defined as knit(key.16) has to be converted to an array list using .to_a if you want to user .zip with it. So change the program as below and it works.

#Using Ruby array methods with SonicPI
# Google ruby array methods
# not every Ruby method is implemented in Sonic PI
# https://ruby-doc.org/core-2.2.0/Array.html

use_synth :piano
rel= 1.5 # note release
nle=[0.125] # note length
nle=nle.ring
amplitude= 1
key= 'C4'
scaletype='major'
cscale=knit(key,16).to_a

with_fx :level, amp: amplitude do
  play_pattern_timed cscale.zip(scale(key,scaletype)+scale(key,scaletype).
                                reverse).flatten,nle,release: nle[0]*rel
end

with_fx :level, amp: amplitude do
  play_pattern_timed cscale.zip(scale(key,scaletype)+scale(key,scaletype).
                                reverse),nle,release: nle[0]*rel
end

It shows that you have to be careful using ruby constructs and sometimes be prepared to play around with them to get them to work in Sonic PI EDIT I think this worked as per the orginal with earlier versions of SP several years ago.