Today on The Csound Blog, we’re going to learn how to build a loop-based sampler out of common household ingredients.
Listen: mp3
Download: splice_and_stutter.csd, BT Sample Pack (13.2 MB)
Here’s a brief rundown of today’s example. A drum loop is loaded into an f-table with the instrument LoadSample. The instrument SampleEngine plays back selective parts of the loop. Instruments Basic, Stutter and Random are interface instruments that simplify the process of triggering samples.
The LoadSample instrument loads a sample into an f-table, while storing information about the sample into an ad hoc data structure created from chn busses. Here isn’t the place to go into detail. I will say that it is akin to a C struct, and stores the file name, sample rate, length of file (in samples), the tempo, and the number of beats (quarter notes) in the loop. All the user-defined opcodes are support opcodes for the data structure.
SampleEngine is the heart of this piece. It works by triggering discreet notes from within the loop, with the loop offset being determined by input it receives via p-field 7. The offset unit is in beats. Let’s say the loop is 16 beats long. A passed value of 0 plays the first quarter note. A value of 1 plays the second quarter note of the loop, etc.
This instrument is designed to be played by other instruments, rather than being triggered directly by a score i-event. That is…
Instead of having multiple samplers that do various things, I created a single complex sampler engine that is capable of a wider range of tricks. The problem with complex instruments in Csound is that writing score events can be cumbersome to write and certainly hard to read, especially when dealing with several parameters. This is where the interface instruments come into play.
The interface instruments Basic, Stutter and Random help us tame the complexity of SampleEngine by reducing the number of p-fields needed by the score, and by defining clear behaviors. Basic is a no thrills controller that simply triggers part of the loop. Stutter, well, stutters. Random randomly picks a beat and plays it.
A greatly added benefit to this approach is that the score is much easier to read. Instead of trying to figure out if a particular i-event stutters or not by scanning a row of numbers, one can just casually look at the name of the instrument used. To put it another way, does this stutter?
i 5 7 1 0.25 0.5 100 12 0.083 1 0
How about this?
i $Stutter 7 1 0.25 0.5 100 12 [1 / 12]
There are a lots of ins and outs to today’s example. And I admit, I skipped over most of them. If there is a particular issue or issues you wish for me to expand on, comment below, and I’ll make it a priority to blog about it in the future.
This sampler is a derivitive work based on an instrument co-developed by Jean-Luc Sinclair (aka Jean-Luc Cohen) and myself back in 2006. The loop in today’s example is by BT (aka Brian Transeau), released under a Creative Commons attribution license, and released as part of the OLPC Sample Library. You can obtain this sample and others here. (13.2 MB) You will need the loop “105 Blanketed Lama.wav” in order to run the csd file.
Update: There is an issue with the CSD file not running on Windows Windows Me (or earlier). There is now a fix. Download the new splice_and_stutter.csd, and see line 269 for details. This only applies to users of Windows Windows Me (or earlier versions).
Thanks to everyone on the Csound Mailing List who helped straighten this out.
It’s cool being able to use Csound as a loop-based sampler, but I’m also interested in pulling loop points directly from the ‘smpl’ chunk in a .wav file so that more complete sample-based instruments could be made possible. Do you know if there is a way to do this in Csound, without resorting to something hackish or inelegant?
To my knowledge, no. This sounds like a question for the list. If a solution doesn’t exist, I’m sure an opcode could be developed to do so.
I’m really interested in trying to make a sample-based instrument that is played via MIDI, like a typical soft-synth. Any chance this might be in the cards for a future blog post? Thank you!
Darren: Yes. A Csound MIDI sampler is definitely in the cards.
Cool!
Or I could finally get off my butt and learn how to code it myself. ;)
I’ve downloaded the csd and samples and I’m getting the following error when I try and run it:-
error: input arg ‘Sout’ of type string not allowed when expecting w, line 113:
xout Sout
Is anyone else experiencing this problem?
The full output is reproduced below:-
PortMIDI real time MIDI plugin for Csound
PortAudio real-time audio module for Csound
virtual_keyboard real time MIDI plugin for Csound
0dBFS level = 32768.0
Csound version 5.11 (double samples) Oct 1 2009
libsndfile-1.0.20
Reading options from $CSOUNDRC: C:Csound.csoundrc
UnifiedCSD: splice_and_stutter.csd
STARTING FILE
Creating orchestra
Creating score
orchname: C:UsersAnthonyAppDataLocalTempcs3.orc
scorename: C:UsersAnthonyAppDataLocalTempcs4.sco
RAWWAVE_PATH: C:Csoundsamples/
rtaudio: PortAudio module enabled … using callback interface
rtmidi: PortMIDI module enabled
orch compiler:
Macro definition for LoadSample
Macro definition for Basic
Macro definition for Stutter
Macro definition for Random
Macro definition for SampleEngine
208 lines read
opcode objcreate_k 0 SS
opcode objcreate_a 0 SS
opcode objcreate_S 0 SS
opcode objset_i 0 iSS
opcode objset_k 0 kSS
opcode objset_a 0 aSS
opcode objset_S 0 SSS
opcode objget_i i SS
opcode objget_k k SS
opcode objget_a a SS
opcode objget_S S SS
error: input arg ‘Sout’ of type string not allowed when expecting w, line 113:
xout Sout
instr 1
instr 2
instr 3
instr 4
instr 5
1 syntax errors in orchestra. compilation invalid
Anthony: I just downloaded the example I placed on the net, tried rendering it, and it works fine. I’m using OS X. Based on the paths listed in your output, I’m guessing you’re on windows. This might be a path issue. I’ll email this to the list, and see if I can get an answer.
In the mean time, you might try this. Place the file into the same folder as the csd. Or, place the file in C:Csoundsamples/. That is, if you haven’t tried these two options yet.
And thanks for reporting this. I can’t fix something I don’t know about. :)
Anthony: One more thing to try. It might be because there are spaces in the filename.
Try changing the file name “105 Blanketed Lama.wav” to “105BlanketedLama.wav”. You’ll have to update the CSD to reflect this as well. Go to line 254, and substitute that line with:
i $LoadSample 0 1 100 “105BlanketedLama.wav” 1 16
Jacob: Thanks for the suggestions. I’ve tried both moving the sample file into the csd directory and the sample directory and renaming it to remove spaces, and neither fix the problem alas.
Anthony: Thanks for checking. I’ve put in an email to the Csound mailing list. Hopefully I’ll have an answer before the weekend is out, if not sooner.
Anthony: I replaced the old file with a new one. Can you please try again? And here is a link to the conversation we’re having on the Csound Mailing List:
http://www.nabble.com/CSD-error-bug-with-stutter-instrument-on-windows–tt26041900.html#a26043977
Anthony: The problem has been identified. Download the latest CSD, and see line 269 for details on how to get it working.
There are some platform inconsistencies between OS X and windows I was not aware of. I’m preparing a small report on these to the Csound devs today. And future blog examples will be designed with these differences in mind, so that people don’t have to tweak code to get them running.
Jacob: I’ve downloaded the newest csd file and made the changes as mentioned in the comments. It works just fine now. Thanks for putting in all the extra work. Now I just need to get my head around how it works :)
Pingback: The Csound Blog » Patterns, Gestures and Behaviors