### 28.2.18 Mixing audio clips together

The mixdown command is used for mixing several audio clips down together into one clip.

- mixdown takes an even number of arguments. Each pair of consecutive arguments is a sequence consisting of the following:
  - A, a stereo or mono audio clip.
  - parm, which is either a nonnegative real number or a list consisting of at most five elements. In the first case, parm is the offset at which A is mixed in, and in the second case parm consists of zero to five elements in the following order:
    - offset, a nonnegative real number representing the clip offset in seconds (by default 0).
    - gain, a real number representing the clip gain in dB (by default 0).
    - pan, a real number in [−1,1] (by default 0).
    - fadein, a nonnegative real number representing the fade-in length in seconds (by default 0).
    - fadeout, a nonnegative real number representing the fade-out length in seconds (by default 0).
- mixdown(A,parm⟨,… ⟩) returns an audio clip which is the mix of the given clips. The audio is not automatically normalized, so calling normalize (see Section 28.2.15) may be required afterwards. If clipping occurs while mixing, a warning is printed in the message area.
- All audio clips involved in the mix must have the same sample rate, but may have different bit depths. The largest bit depth is selected for the resulting audio. If at least one of the clips is a stereo (two-channel) sound, then the entire mix is a stereo sound as well, otherwise it is a mono (single-channel) sound.
- Each of the offset, fadein and fadeout parameters must be entered either as a nonnegative integer or as a floating-point number. In the first case the value is assumed to be in sample units, while in the second case it is assumed to be in seconds.
- The gain parameter is entered in decibels (dB) with negative values resulting in a softer mix, and positive values resulting in a louder mix.
- If fadein and/or fadeout parameters are larger than zero, then fade-in and/or fade-out will be applied to the respective clip while mixing. The fade function is f(x)=cos(π(1−x)/2)2 for 0≤ x≤ 1.
- In case of clipping, try calling mixdown again with lower gain values.

#### Example

In the following example we create a music track with six individual tones. First we write a function for creating tones with vibrato.

vibrato:=proc(f,t) local f1,f2,dl,dr,s; purge(s); f1:=rand(0.5,2.0); f2:=rand(0.5,2.0); dl:=[]; dr:=[]; for s in t do dl.append(sin(2*pi*(f+0.2*sin(2*pi*f1*s))*s)); dr.append(sin(2*pi*(f+5+0.2*sin(2*pi*f1*s))*s)); od; return createwav([dl,dr]); end:;

Now create several tones of different lengths…

s:=2.0^(1/12):; clip1:=vibrato(880,soundsec(7.5)):; clip2:=vibrato(880*s,soundsec(5)):; clip3:=vibrato(880/s^2,soundsec(2.5)):; clip4:=vibrato(880/s^3,soundsec(6)):; clip5:=vibrato(880/s^11,soundsec(6)):; clip6:=vibrato(880/(2*s^7),soundsec(6)):;

… and mix them down at different positions.

g:=-6:; mix:=mixdown(clip1,[0,g,-0.3,2.0,0.2], clip2,[5.0,g,0.7,0.5,2.0], clip3,[7.3,g,-0.3,0.2,0.2], clip4,[9.6,g,-0.3,0.2,3.0], clip5,[9.8,-6.0,0.0,0.5,4.0],clip6,[10.0,-8.0,0.15,0.5,2.0])

a sound clip with 705600 samples at 44100 Hz (16 bit, stereo) |  |  |  |  |  |  |  |  |  |

Finally, play the music:

playsnd(normalize(mix,-1))

Output: a 16 seconds of synthesized music.