Okay, still a newbie, but I’m starting to get a handle on things.
I have a script that mostly works, but after it’s been running for a few minutes, it occasionally crashes because (apparently) one or more notes in a scale/chord has a midi note < 0 – which of course doesn’t exist in midi.
Two questions: 1. Why is this happening? and 2. How can I fix it?
I know there’s a way to filter out elements of a list based on some sort of logical test, but I can’t find it in the docs. Ruby has a delete_if method for arrays, but sonic pi apparently doesn’t support this for rings. (I’d assumed they subclassed rings from arrays, but apparently not so.)
How do I do this?
myring.deleteifbelowzero.dostuffwith cleanlist (e.g., each, choose, etc)
Thanks!
Hi
I think that, instead of delete_if, you should try to ensure the <0 never happens. Can we see your code, or the relevant excerpt?
use the preformatted text widget above ;)
PD_Pi
-
If you post some code where you see this, someone can have a look and work out where they’re coming from.
-
If you can’t prevent them at the source, you can use
filter
to filter them out:
puts (ring 1, -2, 3, -4, 5, -6).filter {|x| x >= 0} # => (ring 1, 3, 5)
I think (like most Ruby stuff) filter
is not officially supported by Sonic Pi though, so it might stop working in a future version.
@emlyn provides an elegant solution; mines a bit hacky:
myScale = (ring -1,0,1,2,3,4,5)
live_loop :test do
tick
if myScale.choose < 0
else
play myScale.look + 60, release: 0.4
end
sleep 0.5
end
creates a gap for any value <0, whereas:
live_loop :test2 do
tick
puts myScale.choose
play myScale.choose.abs + 60, release: 0.4
sleep 0.5
end
doesn’t.
PD-Pi
Perfect!
That’s exactly what I was looking for. myring.filter {filter block}. Simple and elegant.
The code is long & complicated, and seems to be related to changing the key signature. I’m using set/get/sync, but some combination of events seems to be messing with the scales in the middle of a loop. I commented out the key switching block, and the bug went away.
brendanmac, I like the idea of using absolute value too. Never would have occurred to me.