concept SqrOsc in category chuck

appears as: SqrOsc, SqrOsc
Programming for Musicians and Digital Artists: Creating music with ChucK

This is an excerpt from Manning's book Programming for Musicians and Digital Artists: Creating music with ChucK.

You know how to make and fill arrays, so now you can finally use your array to control sound, playing the melody you’ve stored. You start out in listing 3.4 by declaring and connecting a SqrOsc as the source of your audio signal chain . Then you set up some gains to turn your notes on and off (0.7 for on and 0.0 for off) . Next you declare and initialize an array just as you did before, but this time it has more data (more notes of your song!) .

Remember when you wrote your very first ChucK program to make sound (“Hello Sine!”)? You used a SinOsc UGen, hooking it to the dac to make your first note (pure pitched sound). We also talked about and used a few of the other oscillator-type UGens: TriOsc, SawOsc, and SqrOsc. ChucK has more oscillator-type UGens, and you’re going to use one of those now to make great electronic dance-type sounds and music.

The PulseOsc UGen generates a square pulse (like SqrOsc), but you can also control the fraction of each period that’s high versus low (this is called the pulse width, or the duty cycle). You can set or vary the duty cycle of PulseOsc anywhere between 0.0 and 1.0, to create varied spectral sounds (a small duty cycle yields a very bright spectrum; 0.5 yields less bright). Figure 6.1 shows the output of PulseOsc for two different widths: 0.1 (high only 10% of the time) and 0.5 (equal 50% high and low times, like a square wave).

Figure 6.1. PulseOsc waveforms for 0.1 (10% text high time) and 0.5 (50%) duty cycles

The 0.5 setting, also called 50% duty cycle, generates the same waveform that SqrOsc does. Such oscillators with varying pulse widths are commonly used in electronic dance music, often changing the pulse width in rhythm to the music. Sweeping the pulse width dynamically can make cool science-fiction sound effects as well.

6.3.1. Making a clarinet sound using SqrOsc and Envelope

In chapter 1 we talked about how a square wave can sound somewhat like a clarinet and how the stick-slip physics of bowed strings generate something like a sawtooth wave. Now, by applying an Envelope to a SqrOsc, you can build a super-simple clarinet that more properly (gradually) starts and stops notes.

Listing 6.4 shows a simple example of using the Envelope and SqrOsc UGens to make a clarinet sound. Note that you connect the oscillator through the Envelope to the dac . After setting an initial frequency , you use a while loop to play individual notes by triggering the Envelope , waiting a bit to let it smoothly rise, and then turning the Envelope off (again waiting a bit to let it ramp down). We use a loop to play what’s called a “harmonic series,” increasing the pitch by 55 Hz. each time .

Listing 6.4. Simple clarinet synthesis with Envelope applied to SqrOsc