I have some code and whenever I use the function snap_midi_to_scale() all my threads (besides the thread that is calling the function) get killed because they fall to far behind. I have checked that the function is somewhat fast (prob. takes like 100ms at most to finish). I have no idea what is happening. Here’s my code:
# Welcome to Sonic Pi
use_midi_defaults port: "iac_driver_surgext", velocity: 70, channel: 1
surge = "iac_driver_surgext"
dexed = "iac_driver_dexed"
def play_notes(notes, length, velocity, port, spacing = 0.01)
for n in notes do
midi n, sustain: length - spacing, vel: velocity, port: port
end
sleep length
end
def play_note(note, length, velocity, port, spacing = 0.01)
midi note, sustain: length - spacing, vel: velocity, port: port
sleep length
end
def snap_midi_to_scale(scale_key, scale_type, note)
if scale_key >= 12 then
raise StandardError.new("Please use a scale key from 0 to 11")
end
out_note = note
if scale_type == "major" then
scale_intervals = [2, 4, 5, 7, 9, 11] # intervals for maj (all intervals in half steps from root)
elsif scale_type == "minor" then
scale_intervals = [2, 3, 5, 7, 8, 10] # intervals for min
elsif scale_type == "harmonicminor" then
scale_intervals = [2, 3, 5, 7, 8, 11] # intervals for harmonic min
else
raise StandardError.new("#{scale_type} is not a valid scale")
end
# NOTE: Not actual octave, just octaves from midi note zero
note_octave = (note / 12).floor()
# Generate two octaves to account for cases where scale starts above
# inputed note
note_octave -= 1
scale_notes = [scale_key + note_octave * 12]
for interval in scale_intervals do
scale_notes.append( (scale_key + interval) + 12 * note_octave)
end
scale_notes.append(scale_key + 12 * (note_octave + 1))
for interval in scale_intervals do
scale_notes.append( (scale_key + interval) + 12 * (note_octave + 1))
end
while not scale_notes.include? out_note do
out_note -= 1
end
return out_note
end
set :i, 0
in_thread(name: :clock) do
loop do
set :i, get[:i] + 1
sleep 1
end
end
in_thread(name: :chords) do
notesring = range(48, 52, 1).ring()
loop do
sync :i
key = notesring[(get :i)]
chord_notes = chord(key, :minor)
puts chord_notes
snapped_notes = []
for note in chord_notes do
#puts snap_midi_to_scale(1, "major", i)
snapped_notes.append(snap_midi_to_scale(0, "major", note))
end
play_notes(chord_notes, 3.95, 90, dexed)
end
end
in_thread(name: :drums) do
loop do
sync :i
4.times do
sample :bd_ada, amp: 2
sleep 0.5
sample :hat_bdu, amp: 0.3
sleep 0.495
end
end
end
Please respond ASAP I need to get my code working for a project