Sound Basics

Return to ProMusic2015

Sine Wave

Frequency: 440Hz
Level: -12 dB

var sineOsc = new Tone.Oscillator({
	frequency: 440,
	type: "sine",
	volume: -12
}).toMaster();

//Toggle On/Off
sineToggle.on('*', function(data){
	if (data.value == 1){
		sineOsc.start();
	}else if (data.value == 0){
		sineOsc.stop();
	}
});

//Set initial slider values
sineFreqSlider.set({
	value: 0.330
}, true);

sineVolSlider.set({
	value: 0.8
}, true);

//Frequency slider function
sineFreqSlider.on('*', function(data){
	//Scale and offset
	var frequency = data.value * 900 + 100;
	sineOsc.frequency.value = frequency;
});

//Volume slider function
sineVolSlider.on('*', function(data){
	/*Another scale and offset (0-1 mapped to -60-0).  Math.floor is a built-in
	JS function for rounding.*/
	var volume = Math.floor(data.value * 60 - 60);
	sineOsc.volume.value = volume;
});
				

Adding Partials of a Sawtooth Wave


var sawOsc = new Tone.Oscillator({
	frequency: 440,
	volume: -12
}).toMaster();

sawPartialNumber.set({
	value: 1
}, true);

/*
Sawtooth partial selector function.  Tone.js has
built in functionality that allows us to specify how many
overtones (partials) the wave will have.
*/
sawPartialNumber.on('*', function(data){
if (data.value >= 1){
		sawOsc.type = "sawtooth" + (data.value + 1);
	}
});
				

Filtering a Sawtooth Wave

Cutoff Frequency: 5000 Hz

/*
Using an oscillator with a lot of harmonics (like a sawtooth wave) and
removing some of the high harmonics using a lowpass filter is probably the
most common form of synthesis.
*/
var lowPassFilter = new Tone.Filter({
	type: "lowpass",
	frequency: 5000,
	Q: 10
}).toMaster();

var filterOsc = new Tone.Oscillator({
	type: "sawtooth",
	frequency: 440,
	volume: -12
}).connect(lowPassFilter);

cutoffFrequencyDial.set({
	value: 1
}, true);

cutoffFrequencyDial.on('*', function(data){
	var cutoff = Math.floor(data.value * 5000);
	lowPassFilter.frequency.value = cutoff;
});
				

Phase Cancellation

 Sine  Triangle

0° Out of Phase

var phaseOsc1 = new Tone.Oscillator({
	frequency: 440,
	volume: -12
}).toMaster();

var phaseOsc2 = new Tone.Oscillator({
	frequency: 440,
	volume: -12
}).toMaster();

phaseSlider.set({
	value: 0
}, true);

//Oscillator toggles
phaseSineToggle.on('*', function(data){
	if (data.value == 1){
		phaseOsc1.type = "sine";
		phaseOsc2.type = "sine";
		phaseOsc1.start();
		phaseOsc2.start();
		phaseTriToggle.set({
			value: 0
		}, false);
	}else if (data.value == 0){
		phaseOsc1.stop();
		phaseOsc2.stop();
	}
});

phaseTriToggle.on('*', function(data){
	if (data.value == 1){
		phaseOsc1.type = "triangle";
		phaseOsc2.type = "triangle";
		phaseOsc1.start();
		phaseOsc2.start();
		phaseSineToggle.set({
			value: 0
		}, false);
	}else if (data.value == 0){
		phaseOsc1.stop();
		phaseOsc2.stop();
	}
});

/*
Phase slider function.  There are actually 2 oscillators playing at the
same time.  The slider changes the second oscillator's phase from 0° - 180°
*/
phaseSlider.on('*', function(data){
	var phase = Math.floor(data.value * 180);
	phaseOsc2.phase = phase;
});
				

Beating


/*
When 2 tones are very close together in frequency, they will produce a phenomenon known as beating.
The beating frequency is equivalent to the difference in frequency between the two tones.
*/
var beatOsc1 = new Tone.Oscillator({
	type: "sine",
	frequency: 440,
	volume: -12
}).toMaster();

var beatOsc2 = new Tone.Oscillator({
	type: "sine",
	frequency: 441,
	volume: -12
}).toMaster();

beatFreqNumber.set({
	value: 1
}, true);

beatFreqNumber.on('*', function(data){
	beatOsc2.frequency.value = beatOsc1.frequency.value + data.value;
});
				

Frequency Modulation


//Fun with FM synthesis

var fmSynth = new Tone.FMSynth({
	harmonicity: 0,
	modulationIndex: 0,
	modulator: {
		oscillator: {
			type: "sine"
		}
	}
}).toMaster();

fmPosition.set({
	x: 0,
	y: 0
}, true);

fmToggle.on('*', function(data){
	if (data.value == 1){
		fmSynth.triggerAttack("A4");
	}else if (data.value == 0){
		fmSynth.triggerRelease();
	}
});

fmPosition.on('*', function(data){
	var index = data.y * 10;
	var harmonicity = data.x * 3;

	fmSynth.modulationIndex.value = index;
	fmSynth.harmonicity.value = harmonicity;
});