If you followed along last week, I helped walk you through making a sine wave generator with code using ChucK. I led out that post saying –
“But, I guarantee it’ll whet your appetite and give you that “Oh, this is cool!” moment when you make sound by typing words for the first time.”
I meant it, and I got this response from a reader:
“That was AWESOME!
It definitely left me wanting more.”
I try and keep my promises – and I’m definitely going to give you more today. I really want to help you (yes, you specifically) feel like making sound with code isn’t some crazy impossible thing that’s out of your hands. So, below is another unedited chapter out of my upcoming ChucK guide. In it, I walk you through how to take that simple sine wave generator and change it into an infinitely looping sound effect that sounds like a computer “talking” straight out of a 70’s sci-fi show!
If you go through it and really enjoy it – make sure to scroll all the way to the bottom of the page to sign up for the wait list for my new ChucK guide.
Using Randomization to Make Your Computer “Talk”
In this chapter you’re going to be introduced to two of the most important concepts within ChucK – the infinite loop and randomization.
Let’s get started by opening up your “HelloChuck.ck” file and saving it as “ComputerTalk.ck”. We’re going to modify this file to randomly generate a frequency for our sine oscillator and make it play until you tell it to stop.
Once you’re ready, modify your code to make it look like what I have below:
SinOsc oscillator => dac;
440 => oscillator.freq;
.3 => oscillator.gain;
while(true)
{
Math.random2(60, 10000) => oscillator.freq;
100::ms => now;
}
Once you’ve done that, make sure your ChucK virtual machine is running and click “Add Shred”. You should hear what sounds like robot-speak straight out of an old science fiction show!
Let’s walk through what all of this means.
SinOsc oscillator => dac;
440 => oscillator.freq;
.3 => oscillator.gain;
This code we’ve seen before, so I’m going to go over it quickly. We’re again creating a sine wave oscillator and connecting it to our sound card output. We’re then setting the initial frequency of our oscillator to 440 hertz (which, technically we don’t even need here as it will get randomized before playback… but… repetition helps learning) and setting the initial gain level to .3 on a scale of 0 to 1.
while(true)
{
Math.random2(60, 10000) => oscillator.freq;
100::ms => now;
}
This next part is what we’re most interested in. If you’ve programmed anything before, you’re likely familiar with while loops. If not – what we’ve got above it a block of code that will continue run over and over on loop until the condition in the parenthesis next to “while” isn’t true anymore.
In this case, that won’t ever happen – because the condition is just “true”. So, we’ve got what’s called an “infinite loop” in computer terminology.
Often times this is bad, because the computer can easily lock up and run out of memory. In our case (so long as you’re not running ChucK forever) it works as a fantastic mechanism to play our sound until we decide to stop it. Of course, when you want that to happen just click “Remove Shred”.
Within our while loop block, we have two separate statements:
Math.random2(60, 10000) => oscillator.freq;
100::ms => now;
In the first line, we’re doing something similar to what you’ve already seen – setting the frequency of the sine wave oscillator. ChucK comes pre-loaded with a “Math” library of functions that we can use at any time, and we’re taking advantage of that here. One of the functions from the “Math” library is called “random2” and it generates a random number between minimum and maximum values that you provide. In this first line we’re telling ChucK –
Give me a random whole number between 60 and 10000 and set that to our oscillator’s frequency.
So, the random number generator here will not generate a number with a decimal point in it (just so we don’t get too crazy and the numbers are easy to follow). If you ever wanted that, don’t worry, ChucK has another library function for that called “random2f” where the “f” stands for “float” (a number with a decimal point in ChucK).
You’ve also seen the second line before, but just in seconds rather than milliseconds. In this case, we’re telling ChucK to generate a random number between 60 and 10000 for our oscillator and then play that tone back for 100 milliseconds. After that’s done, the while loop runs again – generating another random number and playing back for 100 milliseconds. This will happen forever until you tell ChucK to stop the shred.
Pretty cool, huh?!
It may seem silly and trivial to you, but at one point this type of sound was used as a very legitimate, serious sound effect. In the right context, it still would be! Now, to aid your learning, take some time to play around with this file and make it your own! Change the range in which the random number generates and change the amount of time playback occurs each time ChucK goes through the loop. Have fun with it! I bet, if nothing else, you could come up with some wild atonal bass lines.
Get on the Wait List
Congratulations on making a very nice sci-fi sound effect! I bet you’re already starting to get more comfortable with this.
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.