Rising number function?

Hi there, still feeling pretty n00b, sorry if this is pretty arbitrary but I cant find the answer for the life of me.

I’m trying to control cutoff or lpf or something using a rising set of numbers defined by a function, once it gets to a top threshold the idea is for it to stay there rather than loop back down.

I’ve got this…

define :rise do |rise|
  rise += 10
  if rise > 120
    rise = 120
    rise = rise

live_loop :BD do
sample ate_08_BD, beat_stretch: 2, cutoff: rise 0
sleep 2

what am I doing wrong?

Hey @DeceptiveCake :slight_smile:
There were a few problems getting this to run - firstly, due to the structure of Sonic Pi code, Sonic Pi did not understand that you were trying to pass 0 as the parameter to the rise function - it just saw a number stuck on the end of that line for some unknown reason. Here, in order to make it see 0 as the parameter to that function, you would need to write it as rise(0).

Having said that, after I replaced ate_08_BD with the name of one of the samples built in to Sonic Pi, the loop then runs for me. However, it will still not have the desired result. That’s because we’re passing 0 into the function when playing the sample, sleeping for 2, and then doing exactly the same thing the next time around the loop. (Every time around the loop, rise is passed 0.

Luckily, in this case Sonic Pi actually makes it quite easy to do exactly what you want, without having to worry about if conditions and adding values etc :grinning: Enter: Rings, tick/look, and the ramp function :slightly_smiling_face:

live_loop :BD do
sample ate_08_BD, beat_stretch: 2, cutoff: (line 0, 120, steps: 16, inclusive: true).ramp.tick
sleep 2

I don’t know exactly what your sample is meant to sound like, but the mechanism of what you’re after is what follows cutoff above anyway :slight_smile: In a nutshell: line creates a ring of values, ramp causes anything stepping through the ring to stay on the last item when it reaches there, and tick is what steps through each value.
I’m quite happy to explain it in detail if you want, or still have questions - otherwise, you can read about it in the app help (See line, tick, look and ramp under ‘Lang’). Rings (which the line function also creates) are also described in Chapter 8.4 of the Tutorial. (Or see https://sonic-pi.net/tutorial#section-8-4).

You set rise to 0 every time you start the loop.
I would try something like this (I used a different sample) and used Ruby’s min method

define :rise do |x|
  return [60+10*x,120].min

live_loop :BD do
  sample :loop_amen, beat_stretch: 2, cutoff: rise(tick)
  sleep 2
1 Like

Think I obviously missed this bit when I read your message earlier :joy:
My solution above is one way of achieving a rising value that holds at the end - Robin’s does just as well also. It all depends on what you choose for your particular use case :slight_smile:

Thank you!
defined as a function or built in, s’all good either way right now :smile:

You’re both stars!