Posted on

Audio File Playback in ChucK

As of today, we’re going to be four sections deep into my new guide on ChucK programming.

If you’ve never heard of it before – ChucK is an audio programming language similar to Pure Data, Max/MSP, CSound, SuperCollider, or Sonic Pi.  At its core, it’s a giant sound-tweaking sandbox that you play with using code to synthesize waves, play back recorded sounds, use built in (or your own) instruments, and generally tweak all of that to your heart’s content.

After getting through today’s post, you’ll have completed the foundation of how to use ChucK to do simple playback of audio files (and if you’ve read the previous entries, you know all about generating sine waves as well!).  With all of this knowledge, you’re steps away from creating things like completely randomized drum beats from a single loop, or your own FM synthesis instruments!

Below, I walk you through how to load up an audio file from your own computer into ChucK, play it back, and mess around with it just a bit.  Hope you enjoy it – and if you do, don’t forget to scroll to the bottom of the page and join the wait list for the new guide.  It will go up for presale very shortly and those on the waitlist will be getting a special treat for jumping aboard early.

Enjoy!

Audio File Playback in ChucK

At this point, we’ve already covered a lot of basic ground in ChucK.  You may not think so, but with the concepts you’ve already learned you’ll very quickly be able to generate square, triangle, and sawtooth waves as well as use a number of ChucK’s built in instruments too.

Before we get to that point though, I want to equip you with the ability to play back any .wav file on your computer through ChucK.  Once you can play a file back, you can then learn how to manipulate your audio files.  What you can do at that point is limited only by your imagination, knowledge of code, and digital signal processing.

Check out the code below and enter it into a new file called “WavPlayback.ck”

SndBuf buffer => dac;

"/path/to/your/file.wav" => buffer.read;
buffer.samples() => buffer.pos;

0 => buffer.pos;
buffer.length() => now;

Once you’ve gotten this written, make sure to change “/path/to/your/file.wav” to the correct path to the .wav file on your audio system that you want to play.

With Mac and Linux machines, your directory path will look like it usually does with forward slashes.  Most likely it will look something like “/Users/YourName/AudioFilePath/File.wav”

For Windows users, when you copy your path it may look like “C:\Users\YourName\Documents\File.wav” – you need to change this to make sure you’re using forward slashes.  So, the previous directory would instead look like “C:/Users/YourName/Documents?File.wav”.  It’s a little weird for Windows users, but that’s the way ChucK wants to see the path.

Once you’ve got that all set – hit “Add Shred” and you should hear your file play back!

As usual – let me walk you through this so you understand what’s going on.

SndBuf buffer => dac;

Much like before with our “SinOsc” object, we’re just creating another object here and sending it to our computer’s sound card.  In this case, we’re creating a “SndBuf” or sound buffer and naming it “buffer”.

Depending on how far down the audio programming rabbit hold you’ve gone, you may or may not know what a sound buffer is yet.  In short, it’s a simple array that holds all of the audio samples in your file.  So here we’re essentially creating a custom ChucK object that is representative of an array that can hold all the samples that make up an audio file.

You’re likely already aware that .wav audio has basic sampling rates of 44,100 or 48,000 samples per second (and upwards of those two numbers, of course).  So, once we load a sound file into our buffer, that buffer will hold 220,500 values between 1 and -1 if said sound file is 5 seconds of mono audio at a 44.1k sample rate – pretty cool, right?  That should make quite a bit of sense if you’re plenty used to playing with audio.

What’s even more interesting is this – once you learn that digital audio is simply number value data points between 1 and -1 stored in an array and then read by your computer during playback, you now know the basics of how audio files are stored, read, and played back on your machine in any situation!

"/path/to/your/file.wav" => buffer.read;
buffer.samples() => buffer.pos;

In these two lines, we’re telling ChucK where the audio file you want to play back is actually located on your machine, and to read all of the samples from that audio file into memory via the “buffer” object.

The line below that is a little more tricky.  First, you need to understand that a SndBuf object inherently knows total quantity of audio samples it is made up of as soon as we read an audio file into it.  In addition, the SndBuf object has a “pos” or “position” value – this is essentially a virtual play head.  So the SndBuf object tracks what sample in the array it should be playing back from next – and in fact, we can set that value at any time.  That’s exactly what we’re doing here.

“buffer.samples()” in this case will give us the total number of samples our audio file is made up of.  With the ChucK operator (=>) we then send that number to our SndBuf’s play head position.  This is a mildly technical way of saying that with the second line of code we’re telling ChucK to move the play head to the end of the file.  If we don’t do that, ChucK will play our file back immediately no matter what – completely removing all of the control we have over the initial playback of our audio file.  We obviously don’t want that, so this line almost always comes after reading in an audio file.

0 => buffer.pos;
buffer.length() => now;

Here we’re setting the play head position to zero – or, the very first sample of our audio file.  Remember, we always want to have control of our audio file playback.  It may seem a bit redundant to move the play head to the end of our file and back to the beginning (well, it is) – but it’s simply a good habit to get into.  We’re all about building habits.

With the last line, “buffer.length()” returns the total duration of the audio file that we’ve now read into our buffer object.  With that number, we’re using the ChucK operator and “now” keyword to tell ChucK to play back the entire length of your audio file.

Pretty neat, right?

I know it seems like we haven’t done anything super cool yet, and you’re right – we haven’t!  But be a little patient, you now have most all of the building blocks you need to do really cool things.

In fact, I encourage you to play with this ChucK file a little bit.  Manually set the buffer play head position to different samples and see if you can get ChucK to play your file back from different positions in your audio file.  If you’re really adventurous, see if you can generate a random number like we did in the previous chapter and set that to the play head position so your audio file always starts from a different place!

Get on the Wait List

Congratulations on making your very first audio player – I hope it’s as satisfying for you as it was for me!

Good news – that’s just scratching the surface.  In the guide we’ll go through playing back your own audio files, modifying their playback, randomizing variations, modulation, ChucK’s custom instruments, using MIDI, OSC, and more.

If this made you stoked and surprised you could handle it – then this guide is for you.  For now, I’m not letting you put your money down.  That will come soon.  But if you’re super interested in more, sign up for the wait list below.

By signing up, you’ll be added to my main email list.  If you’re already on it, sign up again as it will update your profile to tell me about your interest and I can email things specifically to you.


Copyright 2016-2021, NIR LLC, all rights reserved.