Last active
August 29, 2015 14:03
-
-
Save chipbell4/b7410af164253bb0452f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* | |
* welcome to wavepot | |
* ------------------ | |
* | |
* this is a live editor. you create a function named `dsp` | |
* that accepts the parameter `t`, the coefficient of time, | |
* which you use to generate a single sample (range -1..1) | |
* | |
* below is the smallest example possible, a simple sine wave | |
* generator. check out more complex demos on the right ----> | |
* | |
* have fun! | |
* | |
*/ | |
var tempo = 100; | |
var tempo_frequency = tempo / 60.0; | |
function dsp(t) { | |
return 0.1 * OctaveBass(t, BassFrequency(t)) + 0.2 * Kick(t) + 0.3 * Chords(t); | |
//return 0.2 * Kick(t); | |
} | |
var Sine = function(t, freq) { | |
return Math.sin(2 * Math.PI * freq * t); | |
}; | |
var Square = function(t, freq) { | |
var sine_value = Sine(t, freq); | |
if(sine_value < 0) { | |
return -1; | |
} | |
else { | |
return 1; | |
} | |
}; | |
var Triangle = function(t, freq) { | |
var period = 1.0 / freq; | |
var a = period / 2; | |
return 2 / a * (t - a * Math.floor(t / a + 0.5)) * Math.pow(-1, Math.floor(t / a - 0.5)); | |
}; | |
var Noise = function(t) { | |
return Math.random(); | |
} | |
var OctaveBass = function(t, freq) { | |
return MixBass(FullBass(t, freq), EmptyBass(t, freq), t); | |
} | |
var FullBass = function(t, freq) { | |
return ( 0.3 * Square(t, freq) + 0.3 * Square(t, freq + 1) + 0.3 * Triangle(t, freq * 2) + 0.1 * Noise(t) ); | |
}; | |
var EmptyBass = function(t, freq) { | |
return 0.9 * Square(t, freq) + 0.1 * Noise(t); | |
}; | |
var MixBass = function(full, empty, t) { | |
var ratio = -Sine(t, WobbleFrequency(t)); | |
return ratio * full + (1-ratio) * empty; | |
} | |
var ClampTimeToBeat = function(t) { | |
return Math.floor(t * tempo_frequency); | |
}; | |
var WobbleFrequency = function(t) { | |
var freqs = [2, 2, 2, 1, 2, 2, 3, 4, 2, 2, 2, 1, 2, 2, 3, 1]; | |
var beat = ClampTimeToBeat(t) % freqs.length; | |
return tempo_frequency * freqs[beat]; | |
}; | |
var BassFrequency = function(t) { | |
var A = 55.0000; | |
var F = 87.3071; | |
var C = 65.4064; | |
var Bb = 58.2705; | |
var B = 61.7354; | |
var freqs = [A, A, F, F, C, C, B, Bb]; | |
var beat = ClampTimeToBeat(t) % freqs.length; | |
return freqs[beat]; | |
}; | |
var Kick = function(t) { | |
var beat_location = (t * tempo_frequency) % 1; | |
return Math.sin(2 * Math.PI * Math.sqrt(5000*beat_location)); | |
} | |
var Chords = function(t) { | |
var eight_location = (2 * t *tempo_frequency) % 1; | |
var G4 = 391.995; | |
var A4 = 440; | |
var B4 = 493.883; | |
var Bb4 = 466.164; | |
var C5 = 523.251; | |
var D5 = 587.330; | |
var E5 = 659.255; | |
var chords = [ | |
[A4, C5, E5], | |
[], | |
[A4, C5, E5], | |
[], | |
[G4, D5, E5], | |
[], | |
[G4, B4, D5], | |
[G4, Bb4, D5] | |
]; | |
var beat = ClampTimeToBeat(t); | |
var chord_to_use = chords[ (beat) % chords.length ]; | |
var waveform = 0; | |
for(var i = 0; i < chord_to_use.length; i++) { | |
waveform += ChordNote(t, chord_to_use[i]); | |
} | |
return waveform / Math.max(chord_to_use.length, 1); | |
} | |
var ChordNote = function(t, freq) { | |
var beat_location = (t * tempo_frequency) % 1; | |
var waveform = Sine(t, freq) + Sine(t, freq * 1.01); | |
return waveform * Math.exp(-beat_location / 0.75); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment