# Designing Sound in SuperCollider/Print version

    Note: the latest version of this book can be found at http://en.wikibooks.org/wiki/Designing_Sound_in_SuperCollider
This book is an independent project based on Designing Sound by Andy Farnell, all about the principles and techniques needed to design sound effects for real-time synthesis. The original book provides examples in the PureData language - here we have re-created some of the examples using SuperCollider.
The original book includes much more than what you see here - we're only recreating the examples and not the text! So in a sense this is not a stand-alone book and you'll get the most out of it if you have the original book with you. But we hope the examples are illustrative in themselves.
Any defects in the code we present should be assumed to be our own mistakes, and no reflection on Andy's fine book!

## Table of contents
### Introduction

* Equivalents for Pure Data Objects

### Tools & Technique

* 14. Schroeder reverb
* 17. Additive synthesis

### Practicals: Artificial sounds

* 24. Pedestrians
* 25. Phone tones
* 26. DTMF
* 27. Alarms
* 28. Sirens

### Practicals: Idiophonics

* 29. Telephone bell
* 30. Bouncing ball
* 31. Rolling can
* 32. Creaking door
* 33. Boing

### Practicals: Nature

* 34. Fire
* 35. Bubbles
* 36. Running water
* 38. Rain
* 39. Electricity

### Practicals: Machines

* 44. Motors
* 45. Cars

### Practicals: Lifeforms

* 50. Insects

### Practicals: Sci-Fi

* 56. Transporter
* 57. R2D2
* 58. Red alert

# Tools & Technique
## Fig 14.28: recirculating schroeder reverb

First start the server, if you haven't already:
Then so we have some source material, we'll load the standard sound file that comes bundled with SC:
Now here's the code which creates the reverb in a single Synth, with four separate delay lines cross-fertilising each other:
And here's an alternative way of doing exactly the same thing, this time using a matrix to represent the cross-mixing of the delayed streams. The single matrix replaces all those plusses and minuses so it's a neat way to represent the mixing - see which you find most readable.

## Fig 17.4: Additive synthesis

Additive synthesis using eqn 17.2, taken from Moorer.
First start the server, if you haven't already - this time making sure it's the server which lets use the oscilloscopes:
In the following we use ".sin" and ".cos" as mathematical operations, so we can stick closely to the original design - these are much less efficient than using SC's SinOsc oscillator, which is what most people would use for additive synthesis in SuperCollider.
In the original diagram, freq starts as 122, index as 0.42. Here we connect these parameters to the mouse - left/right for freq, up/down for harmonic decay.
The amplitude will vary with the decay coefficient, so practically, you'd want some kind of normalisation.

# Practicals: Artificial sounds
## Fig 24.4: pedestrian crossing beeps (UK-style)

The LFPulse here is used to provide a sharp-edged on/off for the sound. (LFPulse outputs a low of zero and a high of one, which is handy in this case so we don't need to re-scale it before we can use it as the multiplier.)
Designing Sound in SuperCollider/thiscodeisby

## Fig 25.3: Filter to approximate the transmission medium

Leave it running while you run the other blocks of code below...

## Fig 25.6: Pulse dialling, before DTMF

Designing Sound in SuperCollider/thiscodeisby

## Fig 27.2: Alarm with two alternating tones

Note that we have separated this into multiple lines for clarity. Exercise: rewrite it as a single line - and use array expansion rather than writing "SinOsc" twice.

## Fig 27.3: Alarm with three alternating tones

Or we can write exactly the same thing a bit more generically using Demand units. The frequencies are simply given as an Array - change the values, or add new ones to the end:

## Fig 27.4: A choice of timbral settings

This using .cos and .sin to perform waveshaping - move the mouse left and right to choose from our 4 timbral options.
Exercise: alter the "Duty" line so that you can have different durations for each of the notes.(Hint: you can use Dseq to specify times just as easily as to specify frequencies.)

## Fig 27.7: A multi-ringer

This SynthDef is capable of a wide variety of sequences, as you'll see below:
...and now we can play it, using the sequences used in the book:
Exercise: work out why some of these multialarm examples don't quite sound like Andy's audio examples! ;)

## 28: US-style police siren

We'll use the internal server to make sure we can use the oscilloscope etc.:

## Fig 28.6: imitation of capacitor charge/discharge

In SuperCollider we can provide an almost-direct physical model: the LFPulse represents a "raw" on/off signal before smoothing by capacitors, and the "lagud" provides exponential smoothing, with the handy feature of allowing different time-periods for the "on" and "off" convergence. This one-line example will plot the curve for you:

## The siren

Now let's use this technique both for the pitch curve, and for waveform synthesis.
Exercise: instead of using the lagged-pulse implementation of the waveform, do as the book says and try using a simple triangle oscillator (LFTri) - this loses the physical-modelling "realism" of the authentic circuit but will be more efficient, and reasonably similar. How much of an effect on the tone quality do you get?

# Practicals: Idiophonics
## 29 Old-style telephone bell

In the book, one partial of the resonance is modelled using a sinewave, modulated by an envelope that dies away on a quadratic-shaped curve. Groups of three such oscillators are then made.
Here we take advantage of a built-in unit in SuperCollider which does quite a lot of the work for us: Klank. A difference: it gives exponential rather than quadratic decay - which is not too bad since exponential decay is often what you want.

## Fig 29.12: a striker sound

As described in the book, a brief click is all we need to approximate this part of the sound.

## Fig 29.13: many resonators to create the various modes of the bell

Note: leave the bell playing while you add the following effect:

## Fig 29.14: resonant effects of bakelite phone casing

If you have sc3.3 or later, you can visualise a frequency representation of the filter's effect:

## Fig 29.14: putting it all together

We will re-use the SynthDef from fig 29.14, so make sure you have run that so that the server has that SynthDef. (But stop the sound, if it is still running.)

## Fig 31.3: simple approximation to a drink can

The four clear resonances are identified using a spectrogram of a can.

## Fig 31.6: a repeating roll pattern

This simulates the regular turning of the drinks can. Since it's a control signal we'll plot it rather than play, for now.

## Fig 31.8: putting it all together

(You must have run figs 31.6 and 31.7 to get their functions defined.)
The sound of a can rolling a little and coming to a stop.
Designing Sound in SuperCollider/thiscodeisby

## Fig 32.6: putting it all together

The following re-uses the functions defined further up this page in combination, to create a door which you can push yourself!
Put the mouse over to the left of the screen before you run this code. Then move the mouse to the right and to the left, to control the amount of force on the door.

## Motion of the twanged ruler

Here we use Env to create an asymmetric waveshape - when the value is below zero, the ruler is touching the table and so is practically "shorter". Therefore it has a higher frequency (shorter wavelength) in the lower cycle than in the upper cycle.

## Let's hear it

OK, so now we need to make sound from this data. The base frequency for the resonator is higher if isDown==true, since the effective length is shorter. So we need to modulate the base frequency at the same time as pushing the thwacks through the resonators.

# Practicals: Nature
## Fig 34.4: square it

Squaring the noise gives it a more useful dynamic range, with rare loud bursts

## Fig 34.5: more

Add another .squared for even stronger effect on the dynamics, and use a high-pass filter to make the hiss more hissy

## Fig 34.7: many crackles

If we use a proper envelope generator instead of a simple line, we can retrigger it randomly with Dust to give random crackles of a controllable density

## Fig 34.8: more variation

Add more variation by modulating the duration and the filter frequency for each event

## Fig 34.9: woof

Simple attempt at the low "woofing" noise made by the flames themselves

## Fig 34.10: woosh

// another component we could use to build up a flamey sound

## Fig 34.11: shape

Shaping the dynamic range and discarding some of the lower frequencies, allowing a little clipping to give a less static sound.

## Fig 34.13: poly

Let's have four of the above, each filtered differently, for a composite effect

## Fig 35.5: producing a repeating but random-seeming pattern of triggers

First we'll create a reusable synthdef that outputs triggers (but not sound):

## Fig 35.9: four bubble systems, simply triggered at random.

Note, instead of using the "bubbletrigs" synth (which is a direct port of the pd example) we could use Patterns to trigger bubble synths. This is a different model for resource management: instead of having four always-running synths which re-trigger to create a new bubble, we create one synth whenever we need a bubble, and it frees itself after.
This next one's a bit more complex - we do as the book does and make smaller bubbles have (a) higher pitch (b) lower volume (c) shorter duration. To connect these values together we define a "sizefactor" and use Pkey to reuse it in each of the args.

## Fig 36.3: random-frequency sines

Not a physically realistic model, but notice how it has some of the same aural qualities:

## Fig 36.4: rising pitches

Tweak the above so that the sounds drift in pitch, tending to rise:

## Fig 38.1: Fluid sphere impact

The waveform produced by impact of fluid sphere on hard surface - we'll create it as a Function here so we can reuse it:

## Fig 38.3: obtaining a Gaussian noise distribution.

The Central Limit Theorem tells us that adding together independent noise sources will eventually give us gaussian noise. Twelve is certainly enough for auditory purposes.
In the book, Andy says that it is more efficient to use two white noise sources and to create a gaussian shape using the Box-Muller transform. However, this involves some relatively heavy mathematical operations (log, cos, sqrt) so it's not actually obvious which approach is more efficient on a given system. Compare the CPU usage (and the audio result) of the above, with the following: