Newbie question about making a specific kind of sampler


Hey guys,
I’m doing a sampler for my SC introductory course. The specific thing about it is that every note on the keyboard (88 notes needed) has a designated sound and is supposed to have variations coming randomly with each notepress of that sound. So what I have is 88 buffers with 5 sounds (recorded words, actually) in them each, so every time I for example press the C4 note on a keyboard, some random variation of that word will sound, choosing one the 5 of that specific buffer.

While I’m able to make it work like this…
MIDIdef.noteOn(\sAmple, {
arg vel, note, chan, src;
Synth(\sampleRandom, [\buf, ~buffers1.choose]);

…This MIDIdef gets triggered no matter what note I press. I just can’t figure out a easy way to make it work so that C4 triggers ~buffers1, and C#4 triggers ~buffers2, etc.

Is it something I have to specify here already?
~buffers1 = SoundFile.collectIntoBuffers("/Users/blabla/Desktop/Sounds/Buffer1sounds/*");

Any ideas?




A general rule of thumb in programming: If you ever find yourself writing name1, name2, name3, name4 etc., in almost every case this calls for an array. Adding numbers onto variable names should be a red flag telling you to recast the data structure as an array.

Arrays access things by numbers.

If you’ve got Buffer1sounds, Buffer2sounds etc., then the pattern is “Buffer++number++sounds” which we can write as "Buffer%sounds".format(index). So it’s easy to go from the number to the directory name.

SoundFile.collectIntoBuffers gives you an array. It’s no problem to put arrays into a bigger array – two dimensions.


// note index+1 -- arrays count from 0, I guess your folders start with 1
~buffers = Array.fill(88, { |index|

Then, in the MIDIdef, you can use note to calculate the index. ~buffers[noteIndex] gives you the array of possible samples for that key, and .choose will work after that. This way, you don’t need 88 separate MIDIdefs. You can have just one, which programmatically accesses the appropriate row of samples.



Thank you for the great explanation! This was just what I needed.