Skip to content

Instantly share code, notes, and snippets.

@Zepheus
Last active August 29, 2015 14:02
Show Gist options
  • Save Zepheus/bd761cc15ba89e070e0d to your computer and use it in GitHub Desktop.
Save Zepheus/bd761cc15ba89e070e0d to your computer and use it in GitHub Desktop.
/*!
* Cedric Van Goethem
* Rudimentary Twinkle Twinkle Little Star demo for
* http://wavepot.com/
*/
var transpose = 0.5;
var volume = 0.1;
var tempo = 2;
var notes = {'A': 440, 'B': 493.88, 'C': 523.25, 'D': 587.33, 'E': 659.25, 'F': 698.46, 'G': 783.99 };
var start = 0.0;
var pause = 0.1;
var pauseStart = 0;
var isPausing = false;
var i = 0;
var song = [['C', 0.5], ['C', 0.5], ['G', 0.5], ['G', 0.5], ['A2', 0.5], ['A2', 0.5], ['G', 1],
['F1', 0.25], ['F1', 0.25], ['F1', 0.25], ['F1', 0.25], ['E', 0.5], ['E', 0.5], ['D', 0.5], ['D', 0.5], ['C', 1],
['G', 0.5], ['G', 0.5], ['F', 0.5], ['F', 0.5], ['E', 0.5], ['E', 0.5], ['D', 1],
['G', 0.5], ['G', 0.5], ['F', 0.5], ['F', 0.5], ['E', 0.5], ['E', 0.5], ['D', 1],
['C', 0.5], ['C', 0.5], ['G', 0.5], ['G', 0.5], ['A2', 0.5], ['A2', 0.5], ['G', 1],
['F1', 0.25], ['F1', 0.25], ['F1', 0.25], ['F1', 0.25], ['E', 0.5], ['E', 0.5], ['D', 0.5], ['D', 0.5], ['C', 1]];
function dsp(t) {
if(isPausing){
if(t - pauseStart >= pause / tempo){
isPausing = false;
start = t;
}
} else {
if(t - start >= (song[i][1]/tempo)){
i = (i + 1) % song.length;
isPausing = true;
pauseStart = t;
}
var noteString = song[i][0];
var note = noteString[0];
var xtraTranspose = noteString.length > 1 ? parseInt(noteString[1]) : 1;
var freq = notes[note] * transpose * xtraTranspose;
return volume * Math.sin(2 * Math.PI * t * freq);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment