Making a Sine Oscillator From Scratch and Additive Synthesis

Goals

Sine Function in Faust


process = sin(0);

will output 0.

To verify this, you could click on the truck (export function) in the Faust Online IDE and then choose misc/csv to get a table containing the first n samples output by the program.


import("stdfaust.lib");
process = sin(ma.PI/2);

will output 1.

Note that stdfaust.lib is imported here in order to use ma.pi.


import("stdfaust.lib");
process = sin(2*ma.PI);

will output 0.

Implementing a Phasor


process = +(1) ~ _;

Don't forget that you can always print the output of a Faust program by using the in the Faust Online IDE misc/csv


process = +(0.01) ~ _;



import("stdfaust.lib");
process = +(0.01) ~ ma.decimal;

Note the use of ma.decimal in the loop here to prevent numerical errors.


import("stdfaust.lib");
freq = 440;
process = (+(freq/ma.SR) ~ ma.decimal);


import("stdfaust.lib");
freq = hslider("freq",440,50,2000,0.01);
process = (+(freq/ma.SR) ~ ma.decimal);


import("stdfaust.lib");
f = hslider("freq",440,50,2000,0.01);
phasor(freq) = (+(freq/ma.SR) ~ ma.decimal);
process = phasor(f);


Generating a Sine Wave


import("stdfaust.lib");
f = hslider("freq",440,50,2000,0.01);
phasor(freq) = (+(freq/ma.SR) ~ ma.decimal);
osc(freq) = sin(phasor(freq)*2*ma.PI);
process = osc(f);

Note that we created an osc function in order to have a cleaner code.

Additive Synthesis


import("stdfaust.lib");
f = hslider("freq",440,50,2000,0.01);
phasor(freq) = (+(freq/ma.SR) ~ ma.decimal);
osc(freq) = sin(phasor(freq)*2*ma.PI);
organ(freq) = (osc(freq) + osc(freq*2) + osc(freq*3))/3;
process = organ(f);

Making a Synthesizer


import("stdfaust.lib");
f = hslider("freq",440,50,2000,0.01);
g = hslider("gain",1,0,1,0.01);
t = button("gate");
phasor(freq) = (+(freq/ma.SR) ~ ma.decimal);
osc(freq) = sin(phasor(freq)*2*ma.PI);
organ(freq) = (osc(freq) + osc(freq*2) + osc(freq*3))/3;
process = organ(f)*g*t;


import("stdfaust.lib");
f = hslider("freq",440,50,2000,0.01);
g = hslider("gain",1,0,1,0.01);
t = si.smoo(button("gate"));
phasor(freq) = (+(freq/ma.SR) ~ ma.decimal);
osc(freq) = sin(phasor(freq)*2*ma.PI);
organ(freq) = (osc(freq) + osc(freq*2) + osc(freq*3))/3;
process = organ(f)*g*t;

Turn it Into an Android App

import("stdfaust.lib");
f = hslider("freq[acc: 0 0 -10 0 10]",1000,50,2000,0.01) : si.smoo;
phasor(freq) = (+(freq/ma.SR) ~ ma.decimal);
osc(freq) = sin(phasor(freq)*2*ma.PI);
organ(freq) = (osc(freq) + osc(freq*2) + osc(freq*3))/3;
process = organ(f);