Using MIDI controller to trigger sliced up samples

I took a longer sample and chopped it up using the Slicer effect and then used a MIDI keyboard to trigger each slice of the sample.

Here’s a link to the video on Twitter. (Couldn’t figure how to post it so the video appears here)

https://twitter.com/mrbombmusic/status/919591178957414401

Here’s the source code

t ="/Users/admin/Music/SAmples/TATcut.aiff"
sample t, finish: 0.5 #plays initial sample one time
live_loop :midi_piano do
  use_real_time
  note, velocity = sync "/midi/q25/0/1/note_on"
  sample t, num_slices: 32, slice: note, note: note - 2, amp: velocity / 127.0, rate: 1.25
   puts note - 2
end

Just some notes about the code.

I also have some questions about how to make this work better.

  1. How could I make a sample stop playing when I trigger another sample. As it is now, the sample plays through till the end, so if you press multiple keys, there is overlap as they all play out and it can get a bit messy.

  2. Despite changing with the num_slice feature, It always seems to slice samples up in to the same number (Seems like 16) When I try to change it to something higher like 32 or lower like 4, i would assume the sample would run shorter or longer, but it always stays the same length which I assume is because what I am doing isn’t actually changing the number of slices.

Thanks!

Hi @mrbombmusic,

great video thanks!

w.r.t. including source code, as it’s just Markdown, you just need to start and finish the code block with three backticks: ``` (I’ve edited your post accordingly so you can see what I mean).

Also, w.r.t. the issue with number of slices - have you noticed that you’ve misspelled the opt? It’s num_slices: not num_slice:. Incorrectly spelled opts are ignored by Sonic Pi which is probably why your’e not seeing any change of behaviour.

Additionally, you might want to take a look at sample's onset: opt - it might blow your mind :wink:

@samaaron answered why the number of slices didn’t work.
Below shows how I got the samples to stop using the kill function, saving the reference to the sample playing using the set function and retrieving it on note off (velocity set to 0) uisng a get function.

t ="~/Downloads/TATcut.aiff"
set :kv,0 #store an initial value for k
#sample t, finish: 0.5 #plays initial sample one time
live_loop :midi_piano do
  use_real_time
  note, velocity = sync  "/midi/steinberg_ur22mkii__port1/3/1/note_on"
  puts velocity
  if velocity>0 then
    k=sample t, num_slices: 48, slice: note, note: note - 48, amp: velocity / 127.0, rate: 1.25
    set :kv,k
  else
    kill get(:kv)
  end
  puts note - 48
end

I used the middle section of my midi keyboard for input hence the -48 offset, also located your sample in the download folder on my Mac.

Argh! Misspelling! What a rookie mistake! I can’t wait to tell my students about that one!

That onset opt does look exciting. Correct me if I’m wrong but it looks very similar to chopping up samples using transient markers in a more traditional DAW.

Thanks for the tip!

Yep, onset: uses an automatic onset detection system to find all the transients :slight_smile:

Thanks for the code. However, when I ran it I am still getting overlapping samples when I do multiple triggers or try to play the same one several times in a row. It does seem like the kill function would be what I’m looking for but I’m not sure what went wrong. The only things I changed were the sample and midi controller directory and the note offset for the midi controller. Thoughts?

Here’s the code I ran.


t ="/Users/admin/Music/SAmples/TATcut.aiff"


set :kv,0 #store an initial value for k
#sample t, finish: 0.5 #plays initial sample one time
live_loop :midi_piano do
 use_real_time
 note, velocity = sync  "/midi/q25/0/1/note_on"
 puts velocity
 if velocity>0 then
   k=sample t, num_slices: 48, slice: note, note: note, amp: velocity / 127.0, rate: 1.25
   set :kv,k
 else
   kill get(:kv)
 end
end

Apologies - but I’m not sure what you’re trying to do. If you assign samples to react to incoming MIDI and hit a chord on the keyboard, then your’e going to hear multiple samples at the same time.

Are you trying to make a monophonic sampler?

I think my example here will only really work properly for a single note input AND released BEFORE the next input. Otherwise kv can’t keep tabs on multiple overlapping inputs. However I did do some work previously which allowed up to 8 note polyphony in this article which includes a video but it is very involved, and requires the power of a Mac to run (doesn’t work on a Raspberry Pi) In essence it keeps track on up to 8 different “k” values and matches up the sample (or in this case note) to kill when the note is released.

Yes, that sounds about right. On the old MPC2000XL, the drum pads had a parameter to play each sample as mono, poly, or note off. I forget if it was mono or note off, but if you hit the drumpad and a sample played and then hit it again before the first sample was finished, it would kill the first sample and play the one that was just triggered. This resulted in no overlap of samples. I am not trying to play multiple samples at once in this case.

Figured it out. I’m sure there’s a more efficient way to do this, but it works! @robin.newman

t ="/Users/admin/Music/SAmples/TATcut.aiff"
k = sample t, finish: 0.0001 #Couldn't figure out how to declare this without it playing so I just made is really short
k2 = nil

live_loop :midi_piano do
  use_real_time
  note, velocity = sync  "/midi/q25/0/1/note_on"
  
  if k
    kill k
    k2 = sample t, num_slices: 16, slice: note, note: note, amp: velocity / 127.0, rate: 1.25
    k = nil
  else if k2
    kill k2
    k = sample t, num_slices: 16, slice: note, note: note, amp: velocity / 127.0, rate: 1.25
    k2 = nil
  end
end
end

I have to admit “Mono Mode” and cut channels is my top wish in SP. I just haven’t gotten around to asking if there was an idiomatic way I was totally missing. Kill does the trick, but I’m very likely to be using it on a deep bass which clicks like crazy. Great for CHH/OHH pairs though.

I’d love to know more about what mono mode might be. How would you use it, and what would it do?

The original reason I was seeking it in SP was to create as convincing a 303 line as possible while still having it randomly generated. The idea would be that in a thread in which something like “mono true” is called (or globally if you wanna get real weird) any synth or sample call would kill the previous sound with a little bit of smoothing to eliminate a click. The two places I use this most are with basslines, as stacked low notes can be very unforgiving, and with open and closed hi hats to create the illusion of closing the pedal while the open is ringing. Would also be awesome with what what MrBombMusic is doing above so that any slice can run for its usual duration or until interrupted by another slice, would make breakcore easier. :smiley:
Cut channels is the same idea, but would be defined by an option to assign a channel independent of threads.

Please feel free to ignore this, or tell me to open my own thread…
but it… ‘sorta’ fits in with this.

Why do we have to do so much work to ‘convert’ an SP based tune
to Midi-based…

Could there not be a global use_output :midi / use_output :internal
which treats all plays as either SP or converts them to midi, and
sends them (with options if needs be)…

That way people get to concentrate on coding music. not
learning a transport mechanism which they really dont need
to know about,

Just asking because basically I dont know what the heck I’m talking about.
Hey, rough week, its late and I’m tired… time for bed.

Eli…

It’s complicated. MIDI is standardized as far as its hardware protocol, but what’s on the other end is a mystery. There are a few common CCs, but those are just a suggestion, not a standard. I’ll gladly help you out with MIDI issues if you wanna message me, I’m in the thick of getting all my gear together for the first time so I’m using MIDI more than synths/samples right now.

Now… ‘standards’ I understand, living in the computer hardware
world, as I do.

RS232 is a classic ‘standard’… until you try and actually connect
product from 2 different manufacturers.

So… because: reasons.

I can live with that. :slight_smile:

Eli…

MIDI is not too different from RS232, really.