Trigger Buffer via MIDI/OSC?

It would be very handy to be able to ‘run’ a buffer via MIDI or OSC. Many times I’ll have one-off sound effects that I run from various buffers while a ‘main’ buffer plays the main song. As it stands now, I have to click (or use the keyboard) to choose the correct buffer, then hit Cmd-R to play the sample referenced by that buffer. Being able to trigger buffers via a MIDI controller would be a game changer…

Bryan

1 Like

Hi Bryan,

It may not be possible at the moment to run a specific ‘buffer’ in response to an event, in the sense of something like run_buffer(1)… but In fact, you can do things that are pretty much equivalent. For example, due to the ‘global’ nature of the built in Time-State event store, it is absolutely possible to sync (wait) on an OSC or MIDI event in one buffer, trigger this event from wherever, and then run whatever code in response. The key is that you need to Run the buffer where you are syncing on the event at least once initially to ensure that the code begins waiting for your events.

It’s definitely worth checking out the MIDI and OSC sections of the tutorial for more details: Sonic Pi - Tutorial and Sonic Pi - Tutorial.

Let us know if you have any further questions :slightly_smiling_face:

2 Likes

hi @bcgreen24

Have a look at this post. It seems to be an example to illustrate @ethancrawford explanation.

3 Likes

Hi @bcgreen24 yes I’d echo the above - my example may not be exactly what you’re after, but the principle of having code inside live loops, synced on midi or osc cues - that’s it.

Quite often I’ll have a buffer dedicated to running one-off samples mapped to a midi keyboard. I just cmd-R it once to get the loops going, then it’s good until a hard stop.

I remember many years ago going to see Thomas Dolby who had one of the first Fairlight’s in the UK and he had a party piece of playing humerous sounds from his keyboard. Apparently it cost several times the cost of his London flat. Now, it’s all free! Who said modern life is rubbish?

3 Likes

Thank you all for this information!

Bryan

1 Like

Hope it helps :slightly_smiling_face: Will you let us know how you get on?

So far, so good! I did a quick test and created some code that basically does one of the things I was setting out to accomplish: trigger samples via a MIDI controller.

 live_loop :midi_sfx do
   use_real_time
   note = sync "/midi:launchkey_mk2_61_launchkey_midi:10/note_on"
   
   if note[0] == 40 then
     sample :ambi_lunar_land, release: 2, sustain: 2
   end

   if note[0] == 41 then
     sample :ambi_dark_woosh, release: 2, sustain: 2
   end

   if note[0] == 42 then
     sample :ambi_drone, release: 2, sustain: 2
   end
 end

The above code allows me to map various samples to performance pads on my MIDI controller, and works soooooo well. :slight_smile:

Again, thanks for your guidance!

Bryan

2 Likes

Excellent! It’s great isn’t it?

For less code you could use the Ruby case-when-else-end form, something like

case note[0]
when 40
    sample...
when 41
    sample...
else
    sample...
end

All kinds of possibilities :smile: Or put the samples in an array and index into them…

x=[:ambi_lunar_land, 
   :ambi_dark_woosh]
sample x[note[0]-40]

Yeah, I need a crash course in Ruby; haven’t used it much, other than in Sonic Pi :slight_smile:

I love your idea of triggering samples held in an array!

Bryan

1 Like

Me too. Sam sometimes reminds us that ‘Sonic Pi is not Ruby’ so he’s not necessarily supporting all Ruby constructs. We have to be a bit cautious at investing too much in clever-clever code, but hopefully arrays and things basic things will stay in there.

Ahhhh, yes. Good point-- I keep forgetting that Sonic Pi ain’t Ruby. :slight_smile: