Confused about `play_pattern_timed`


the docs of the play_pattern_timed function state, that

play_pattern_timed [40, 42, 44, 46], [1, 2, 3]

is equivalent to

play 40
sleep 1
play 42
sleep 2
play 44
sleep 3
play 46

But it’s not. The play_pattern_timed function changes the sustain of the played notes, based on their associated sleep value.
An older post confirms this and people in the accompanying thread also confirm that this is the way they use the function. Which I guess is fine. Still it’s not what the docs say, and so I’m wondering how I can get the behaviour that’s described in the docs for the play_pattern_timed function.

Any pointers are much appreciated :slight_smile:

Cheers, Benjamin


I think the documentation is wrong here, I don’t know of a way to make play_pattern_timed behave like that. Probably this is how it worked in an old version of Sonic Pi and it was never updated.

However, you can make your own function that does what you want:

define :play_timed do |notes, times, **args|
  ts = times.ring
  notes.each_with_index do |note, i|
    play note, **args
    sleep ts[i]

play_timed [40, 42, 44, 46], [1, 2, 3], amp: 2

This will play each note without altering the individual durations. It’s not exactly like the example though, in that it includes a final sleep after the last note, which I think makes more sense, but if you don’t want that you can change the sleep line to:

    sleep ts[i] if i != notes.length - 1

Thanks, that’s helpful. I also assume, the docs are wrong, and I’d be willing to write a PR to correct it.

@samaaron If you agree the docs are wrong on play_pattern_timed I’d prepare a PR to correctly describe what the function is currently doing.

You can also do something like

play_pattern_timed(scale :c4,:major),[1,1,1,1,2,2,3,1],sustain: 0,release: 1

This gives 0 sustain and 1 release to all the notes.
or you could do

play_pattern_timed(scale :c4,:major),[1,1,1,1,2,2,3,1],sustain: 1,release: 0

which sounds a bit clicky

play_pattern_timed(scale :c4,:major),[1,1,1,1,2,2,3,1],sustain: 0

is the same as the first example as the default release is 1

you can also set this all up in synth defaults like this

use_synth_defaults sustain: 0

play_pattern_timed(scale :c4,:major),[1,1,1,1,2,2,3,1]
1 Like

Indeed, looks like that was the behavior before so docs should probably get updated.

Of course, I should have looked into the code’s history before.
The discussion in the PR makes lots of sense (at least the part I could follow :wink: )

I’ll prep a PR to update the docs for play_pattern_timed then.