Playing Sampled Audio
In this tutorial we are going to look at the sample playback features of Bidule and eventually build ourselves a simple midi-controller sample player.
To avoid confusion what you would normally call a ‘sample’ i.e. a piece of recorded audio I will refer to as an Audio File because I will use the term ‘sample’ to refer to a single slice of time. e.g. if the sample rate is set at 44.1 KHz then there will be 44100 samples per second.
First we’ll start by getting to know some of the modules available for working with audio files within bidule.
Open the application and the palette. You’ll see that there is a whole section devoted to Audio Files, also there are a couple of modules available in the?Building Blocks->Audio File section. We’ll start with the main Audio File section first, as we go through the modules here add them to your layout so you can see how many ins and outs they have and what their UI panels look like.
In / Out
Stereo Audio Out
This is a nice module for recording and playing back audio within your layout. If you open the panel you will see that it has a Mode control where you can set it to record, play or bypass. You can also set the buffer size (how much audio you want to record in one go). You can also set the bit depth to 16,24 or 32 bits.
The file selector is for saying where you want to *save* the data in the buffer, not for loading in audio files. So, thats why there is a button called ‘Save’ (although it has Send Command written on it..).
Finally you can control the volume level for the audio being played.
Boboche Looper (syncable)
In / Out
Stereo Audio Out
This module is a simple way to playback looped audio in your layout. I dont know why it has this particular name, maybe Boboche means simple in french..?
Anyway, if you pop open the panel you’ll see that there are just a few controls. The first is a drop down list of audio files you can choose from (probably none at the moment), and the button underneath gives you a handy way to add audio files to what is known as the ‘Media Pool’.
The Media Pool is where all of the various files you use in your layout will be stored. You can also access it from the ‘Media’ button on the toolbar at the top of the application.
You can use a wide variety of audio file types within Bidule, but remember you cant use mp3’s, so if you want to work with mp3s using bidules modules its best to convert it to wav/aif first.
Back to the panel, the other important controls here are for setting the playback speed and controlling the volume. You can also set how bidule will interpolate sample data, dont worry about that for now but if you are interested in such things you can head to http://en.wikipedia.org/wiki/Sample_rate_conversion and find out more.
You’ll probably also notice that this module has a little ‘s’ on the left hand side. This means that it can be synchronized to other modules (ones that generate sync such as the Sync Transport). If the module is ‘synced’ then the pitch is set to match the tempo of the sync generator and the pitch control on the panel wont have any effect. This module doesnt do timestretching.
Have a play around with the module now, hit the ‘Add file(s) to media pool’ button, load in some sounds / loops then select one from the drop down list to hear it in action (dont forget to connect the module to your audio output via a mixer..)
You should notice that the speed control has negative settings to play the audio in reverse..instant rewind.
We’ll leave this one for now, but feel free to connect it up and have a play to hear what it does to your sounds.
In / Out
Stereo Audio Out
This is the big brother to the previous Boboche Looper. If you open the panel you’ll see there are a lot of controls here.
The first set on the left are for loading audio files as we saw before and 2 amplitude controls. The first one allows us to set a volume for an individual sound file, while the second is the overall volume for this module.
The last control on the left side is a choice to control how we want to ‘update loop points’. This affects the behaviour of the loop editing window which we will come to shortly.
The middle section is for controlling how the looper works with the timing of the audio file. You can define the time signature and set the BPM, which will cause the looper to calculate how many beats there are in your file, and you can also set the number of beats by hand (although at the moment this doesnt update the BPM information).
For handling different time signature there are two options in the ‘Meter Adaptation’ control. ‘None’ or ‘Trunc-and-mute’ (truncate and mute). If you want your loop to play continually no matter what values you put into the meter boxes set this to ‘none’. The other setting will either cut off (truncate) the end or add space (mute) to adapt this meter to the meter setting of the sync generator.
Right at the bottom of this section is a button to launch a handy little wave viewer where you can set the start and end loop points of your audio file. Just drag the coloured lines where you want them, also you can use the middle mouse button to zoom in and out for fine tuning. The control for ‘update loop points’ determines whether bidule will update the sound as you move them around a.k.a ‘While Dragging’ or will wait until you have put the marker where you want it ‘On Release’.
The next control is the interpolation setting as we saw in the Boboche looper.
Finally there are 3 controls for dealing with time stretching and pitch adjustment. By default the ‘Stretch Type’ is off so the looper will automatically adjust the pitch of your sound to match the tempo of the sync generator, but if you select ‘CHEAP-OLA’ you can control the pitch and length of the sound independently. As you might guess by the name ‘Cheap-ola’ the timestretching algorithm is fairly basic, but its not without its own charm and usefulness.
Once the timestretching is activated you can set the ‘grain size’ and the pitch of the sound. To understand the grain size control it will first help to understand the basics of timestretching.
There are various techniques for changing the duration of a sound without affecting its pitch, but the basic method requires chopping the sound up into tiny little pieces (also known as grains) and either repeating a grain a certain number of times (to lengthen the sound) or skipping grains (to shorten the sound). We’ll see how to build our own later, but for now just remember that the grain size is how big the little pieces are. If you set the tempo low and the grain size high you’ll be able to hear the individual grains easier as the sound plays.
After the grain control we have the familiar pitch shift setting, to control the pitch of the sample but this time without affecting the length.
I use this module all the time when I want a continuous audio stream to work with, e.g. for building effects. Its worth getting to know it.
Next up are the players, they are all the same except for the number of output channels so i’ll only describe one.
Player (2 Channels)
In / Out
Stereo Audio Out
This is the simplest way of playing audio files, it doesnt use the Medial Pool and it doesnt have a pitch control.
All you can do is load a sample and set it playing (and loop it if need be)
In / Out
Stereo Audio Out
This is more of a tool for detroying your sounds than anything else. You can load sounds from the Media Pool and control how they are played via the wave modulation points. It basically loops between a start point and end point which are themselves controlled by modulators, much like LFO’s. You can set the shape and speed of the modulators and also control the speed of playback (which just changes the pitch, again no timestretching here).
Its fun if slightly uncontrollable although its actually a group so you can go inside and see how its put together.
Recorder (2 Channels)
In / Out
Stereo Audio In
This is the a simple way of recording your audio in bidule and saving it to disk
Before you start recording you choose where you want to save the files and what to call them, then just hit the record button to start then the same button again to stop. Now you will have a fresh audio file on your hard drive.
One nice feature of this module is that if you tick the box that says ‘Create new files for each record’ then bidule will automatically name the files each time you start a recording by putting a number on the end of the name (which increases by one each time you make a recording)
You can also choose to write multiple mono files, or one file for each input.
Thats it for the modules in the Audio File section, last but not least we have the Audio File modules in the Building Blocks section. Theres only two, the Basic Audio Buffer and the Basic Audio File Player.
They may look slightly daunting at first as they feature more inputs and outputs than most other modules, but you will soon get used to them and appreciate the level of control this provides.
Basic Audio Buffer
This is for recording and playing back audio within bidule. Note that it doesnt write audio files.
This is where you feed in the audio you wish to record.
This controls which part of the recording is being played. It can range from 0 to the number of samples in the buffer minus 1. (Remember samples as in a slice of time, not a complete audio recording)
When this is 1 the module will output the audio from the buffer at the play position.
When this is 1 the module will record the audio from the first input into the position set by the next control..
Like the play position this controls where in the buffer the incoming audio is stored. For simple recording purposes you will just increase this over time until you want to stop, but the great thing about it being an input is that you can record any way you like, in small loops, backwards etc..
Clear Contents Trigger
This is used to empty the buffer.
If they play gate is 1 then this will output whatever is in the buffer at the position set by the Play Position input.
Basic Audio File Player
This is the module to use when you want ultimate control over audio file playback. Much like the other audio file playing modules it uses files from the media pool, but the only control on the UI panel is the interpolation setting, everything else is done via the inputs.
Triggers the playback if the gate signal is also present
If the signal to this input is at 1 then when a trigger signal occurs the output will be whatever is in the buffer at the position set by the Play Position input (the next input along)
Where in the buffer is the sample data read from. This is specified in samples and starts from 0, so the maximum value is 1 less than the number of samples in?the audio file
How loud the output signal is
Controlling playback of?an audio file
So for our simple sample player we’ll be using the last module, the Basic Audio File Player.
Open Bidule if you havent already or just hit ‘new’ on the toolbar. Now from the palette find the Basic Audio File Player (its in Building Blocks -> Audio File) and add one to the canvas.
Im going to assume you have a midi controller and that its working. First make sure you have an input module for you controller on the canvas so we can trigger the audio from your keyboard / trigger finger / etc. If you read the previous tutorials about building a synth you will probably remember that we need to convert the midi data from the controller into signals we can use within our layout and to do that we need to use the?Midi Note Extractor. Its also in the Building Blocks section of the palette, under Midi.
Ok, now wire the output of your midi controller module to the note extractor and lets look at the inputs to the audio file player. We’ve got a trigger, a gate, the play position and amplitude. If you now check the outputs from the note extractor we have frequency, amplitude, gate and trigger. Looks like we’ve almost got a match..theres already 3 matching connections. No reason not to get them wired up now, just match them up by type (unfortunately they arent laid out in the same order so you end up with a little bundle of crossed wires).
So, this just leaves us with two ports to think about, the play position of the audio file player and the frequency output of the note extractor, how can we convert the frequency into the play position?
First of all we need to consider that the frequency output from the note extractor is a static value, in other words, while we hold down a key the output doesnt change. If you connect an audio monitor (Building Blocks -> Monitoring) to the frequency output and watch the monitor window you’ll see what happens. Press C3 and the monitor displays ‘261.625580’.
For controlling the play position control of the audio file player we need to first think about how sample playback works.
As I mentioned right at the start of this article an audio file has a ‘sample rate’, this is how many slices there are in 1 second. The most common audio file sample rate at the moment is 44100, but thats not all…we also have to consider that bidule itself has a sample rate. This is how many slices of audio it calculates per second. This setting will depend on your audio hardware and your configuration, I have mine set to 44100, but 48000, 88200 and 96000 are quite common.
To keep things simple for now I will assume that both the audio file and bidule share the same sample rate of 44100 (we’ll see later how to handle? the differences).
So if we have an audio file at 44100 and bidule at 44100 to play an audio file at its original speed we need to change the play position by counting from 1 to 44100 every second. How do we count? Fortunately bidule has a handy module just for counting, its called ‘accum’ (for accumulator, as in it accumulates numbers). Its in my favourite section ‘Building Blocks -> Math’. Find it in the palette and add one to the layout so we can see how it works.
The 5 inputs from left to right are
Value To Accumulate
Reset To Start
Inclusive Lower Bound
Exclusive Higher Bound
Value To Accumulate
This is how much we want to change the output (accumulated) value by every sample (according to the bidule sample rate..)
If you send a ‘1’ to this input then every sample the output increases by 1. Simple.
This is where we want to start counting from
Reset To Start
Sends the output back to the start value as we set above (if we dont set it, then its zero)
Inclusive Lower Bound / Exclusive Higher Bound
To understand these two values first open the panel for the Accum. In there you will see it has a check box ‘Loop Values between low and high’
If that box is checked then the Accum will use these two inputs for looping. The inclusive lower bound is the value it jumps back to when it loops, its inclusive because it is ‘included’ in the range, so if the lower bound is 10, when it loops the output goes to 10 and carries on from there.
The higher bound is where the counting will loop and its exclusive because it is not part of the output. If you set the higher bound to 100, as soon as the accumulated value gets to 99 it will loop back to the lower bound.
So how do we put this Accum together with what we know to be able to play a sound file? You might have guessed from the description of the ‘Value to Accumulate’ input that if we need to count to 44100 in one second and bidule runs at a sample rate of 44100 then we can just set this input to 1 and it will happily count up to that value in one second.
Lets do just that, add a constant module to the layout and set its value to 1. Connect this constant to the first input of the Accum module. Add an audio monitor (or use one you have from earlier) and connect it to the first output of the accum to see whats happening. (make sure that the loop switch is off in the accum panel). You should see the monitor showing an ever increasing number. Just what we need.
Now we can load a file into the audio file player and start triggering it from our keyboard. I’ve supplied a file here which you can use, its just a basic synth sample.
Open the panel for the Basic Audio File Player and click the ‘Select file(s)’ button. Find the sample and hit ok. Now you have to select it from the list just above the button.
All thats left is to connect the note extractor, accum and file player together. When we play a midi note we want the sample to start from the beginning so we can use the trigger output of the note extractor to reset the accum to its start value. Connect those two together (its the last pin of the extractor to the 3rd of the accum). If you play some notes and check the audio monitor you should see the number jump back to a small number when you play (its unlikely that you will see the zero at its gone very quickly)
Now just connect the first output of the accum to the play position of the file player and make sure that the audio output of the file player is connected to your sound card module (via a mixer preferably).
The whole layout should look something like this :
And if you play a key on your keyboard you should hopefully hear the sample being played back.
This is great, but what about playing it back at a different pitch? This isnt actually that difficult, its just a simple bit of maths.
First, try changing the value in the constant box to something else, like 0.5 or 2 then play the keys again. You’ll notice that the note changes octave.
So all we need to do is to convert the frequency value into a value suitable for the accumulator. Earlier we saw that the frequency of the C3 key is 261.625580. What we want is to make this into an input of 1 for the accumulator so our sample plays back at normal pitch when C3 is pressed. Thats easy, we just divide the frequency by 261.625580.
Add a new module, the ‘Binary Operator’ from the maths section. Open the panel and set it to divide (the ‘/’ symbol).
The left input comes from the 1st output of the note extractor, the frequency. For the second input, disconnect the constant from the accum, open the constant panel and enter 261.625580. Then connect the constant to the right input of the divide module and finally the output of the divider to the first input of the accum.
The layout should look like this :
And when you play the keyboard you should hear the sample being played back at the correct pitch for each key.
You can download the layout so far here (the zip also includes the sample im using)
You might be thinking that now is a good time to start wiring this up like its an oscillator in a synth (and you can do if you’re really keen) but first we’ll cover?looping the sample.
When you start playing the sample at higher pitches you’ll notice that it ends sooner and sooner because we are travelling through the file at a faster speed. What would be cool is if we can just loop a part of the sample over and over while we hold down a key.
The Accum module already features most what we need to implement looping. It has a switch on the panel to turn looping on and off and inputs to set the start and end points (lower bound and upper bound). All we need to do is make sure that those start and end points are set to sensible values.
To do this we need one vital piece of information : How long is the audio file. And again, bidule delivers what we need easily, check the third output of the audio file player ‘# of samples in audio file’. Perfect.
We want to be able to loop any part of the audio file so we need to allow the loop start and loop end to be anywhere from 0 -> # of samples in the file. To do this we can use a variable for each loop point where the value ranges from 0 to 1 and then multiply it with # of samples in the file. Then no matter what file we load in we know that?the loop points are in range.
Lets put together the start and end loop controls and take it from there. We’ll need a couple of multipliers (Binary Operator set to *) and a couple of variables. Add all those to the layout. Connect the first variable to the left of the first * and the second variable to the left of the second *. Rename the variables to ‘Loop Start’ and ‘Loop End’.
Next connect the third output of the audio file player to the right side of both *’s. Then connect the output of the * after the loop start to the 4th accum input (lower bound) and the other * to the 5th input (higher bound). Open the panel of the accum and check the ‘Loop values between low / high box’
Your layout should look something like this :
If you now try playing some notes you probably wont hear anything. This is because both the start and end points are set to zero.
Open the panels for both Loop Start and Loop End and while holding down a key (preferably a higer note, say C5) try adjusting the values and listen to the effect that the loop points has on the playback.
With some careful adjustment you can find a point where the note loops smoothly while you keep your finger on the key. (If youre having trouble try these values : 0.629 and 0.7205)
This is pretty good, but before we go and group this into a module of its own lets just add the last piece we need before we can make it polyphonic. If you remember from the synthesizer tutorial the polyphonic adapter only works when we have the ‘activity gate’ of an envelope as one of the group outputs, and of course a nice ADSR envelope would be pretty useful in here so we can shape the sound a bit.
If you read the previous tutorials you will have a good idea about how to add an ADSR envelope as this is quite similar. First add the module from the palette (The ADSR not the Basic ADSR) and connect the gate / trigger from the note extractor to the ADSR inputs. This time they are in the same order so you can hold down CTRL as you connect the gate to make both connections.
To modulate the output of the file player with the envelope we’ll need two more multiplies as its a stereo signal, so add those and connect the envelope output of the ADSR to the left input of both *’s then rewire the two outputs of the Audio File Player to go into the right inputs of the *’s (one in each, not both to both). Then connect the output of the *’s to the mixer.
If you try changing the envelope settings you’ll notice that they all work correctly apart from release, this is because the output of the file player is controlled by the gate of the note and stops as soon as the key is released, we need to change that so its controlled by the activity gate of the ADSR. Disconnect the gate input to the file player (the second input) and connect the activity gate from the envelope to the file player.
Your layout might look a bit like this :
If you play some notes now the ADSR controls should all be working.
You can download the layout so far from here :
Sample Player with looping and ADSR
Now we can bundle it all up into a nice module, set some controls for the panel and make it polyphonic.
Select everything between the Midi In and the mixer (not including those modules) then right click a selected module and navigate to Group -> Group Selected Objects.In the dialog we want 1 midi input and 3 sample outputs.
Once you’ve made the group, lets go inside and wire up the inputs and outputs.
The top midi in goes to the note extractor. The first two outputs are the audio and are connected to the two * modules. The final output is the activity gate from the adsr. Once they are wired, rename the outputs to something useful (e.g. Audio L, Audio R, Acitivty Gate)
Then we can add the controls for the panel. Right click the canvas and select ‘Group -> Parameters’. Then add these parameters. Also dont forget set their names, i’ve shown the names I use in brackets if its different from the default.
Audio File Player
MediaPool Shortcut (Browse)
Value??? (Loop Start)
Value??? (Loop End)
Now go back to the parent and you can right-click your new module and select Polyphonic adapter. Choose a suitable number of voices then click OK. Next just re-connect the midi input and the audio output and play some notes. Also try loading in other sound files and playing round with the loop settings.
You can download this layout from here :
Thats all for this tutorial. Go ahead and experiment with wiring the player in whatever way you like..try adding a filter section or lfos like the synthesizer from the previous tutorials.
As always let me know if there are mistakes or something isnt clear or any suggestions.