Created
May 1, 2014 23:16
-
-
Save joshcarr/11953a16f899a8dbab2f to your computer and use it in GitHub Desktop.
Creating sound waves with JavaScript
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
// Creating sound waves with JavaScript | |
// from http://js.do/blog/sound-waves-with-javascript/ | |
var samples = [] | |
// | |
// | |
// | |
// Achord | |
var frequency = [392, 659.26, 783.99, 1046.5]; // "C4+E4+G4+C5" notes | |
var samples_length = 44100; // Plays for 1 second (44.1 KHz) | |
for (var i=0; i < samples_length ; i++) { // fills array with samples | |
var t = i/samples_length; // time from 0 to 1 | |
samples[i] = (sin( frequency[0] * 2*PI*t ) + sin( frequency[1] * 2*PI*t )+ sin( frequency[2] * 2*PI*t ) + + sin( frequency[3] * 2*PI*t ))/4 ; | |
} | |
// | |
// | |
// | |
// laser echo | |
var f1 = 900; | |
var f2 = 5; | |
var samples_length = 44100; | |
for (var i=0; i < samples_length ; i++) { | |
var t = i/samples_length; | |
samples[i] = cos(2*PI*f1*t + 1500*cos(2*PI*f2*t) ); | |
samples[i] *= exp(-t*4); | |
} | |
// | |
// | |
// | |
// bell 1 | |
var f = 880; | |
var samples_length = 44100; | |
for (var i=0; i < samples_length ; i++) { | |
var t = i/samples_length; | |
var w = 2*PI*f*t; | |
samples[i] = cos(w + 8*sin(w*7/5) * exp(-t*4) ); | |
samples[i] *= exp(-t*3); | |
} | |
// | |
// | |
// | |
// bell 2 | |
var f = 220; | |
var samples_length = 44100; | |
for (var i=0; i < samples_length ; i++) { | |
var t = i/samples_length; | |
var w = 2*PI*f*t; | |
samples[i] = cos(w + 8*sin(w*2) * exp(-t*4) ); | |
samples[i] *= exp(-t*3); | |
} | |
// | |
// | |
// | |
// clarinet | |
var f = 261.63; // 261.63 Hertz = C3 note | |
var samples_length = 44100; | |
for (var i=0; i < samples_length ; i++) { | |
var t = i/samples_length; | |
var w = f*2*PI*t; | |
// Odd harmonics | |
samples[i] = (sin(w) + 0.75*sin(w*3) + 0.5*sin(w*5)+0.14*sin(w*7)+0.5*sin(w*9)+0.12*sin(11*w)+0.17*sin(w*13))/(1+.75+.5+.14+.17); | |
samples[i] *= exp(t/1.5); | |
samples[i] *= exp(-t*1.5); | |
} | |
// | |
// | |
// | |
// violin | |
var f = 698.46; // 698.46 Hertz = F4 note | |
var samples_length = 44100; | |
for (var i=0; i < samples_length ; i++) { | |
var t = i/samples_length; | |
var y=0; | |
var A_total = 0; | |
for (var harm=1;harm<=7;harm++) { | |
var f2 = f*harm; | |
var A = 1/harm; | |
A_total += A; | |
y += A*sin(f2*2*PI*t); | |
} | |
samples[i] = y/A_total; | |
samples[i] *= (1-0.5*sin(2*PI*6*t)); // Add a low frequency amplitude modulation | |
samples[i] *= (1-exp(-t*3)); | |
} | |
// | |
// | |
// | |
//Chord creator | |
var frequency = 82.42; // 220 Hz = "E" note | |
//set the base chord note (E = 0, F =1, F# = 2, etc...) | |
var base = 0; | |
//richness (1-4 work best, 4+ the volume dwindles with no noticeable tonal gain) | |
var rich = 1; | |
//set the chord type (Major is (0,4,7), Minor is (0,3,7), Aug+ is (0,5,8) | |
var note1 = 4; | |
var note2 = 7; | |
frequency *= Math.pow(2,base/12+2-rich); | |
//generate base frequency based off of parameters | |
var samples_length = 44100; | |
// Plays for 1 second (44.1 KHz) | |
for (var i=0; i < samples_length ; i++) { | |
// fills array with samples | |
var t = i/samples_length; | |
// time from 0 to 1 | |
samples[i] = 0; | |
//richness increases the number of octaves the chord ranges over | |
for (var j=1; j<1+rich; j++){ | |
//add the 3 notes to create the chord | |
samples[i] += sin( frequency * PI*t *Math.pow(2,1+ j ))/3; | |
samples[i] += sin( frequency * PI*t *Math.pow(2,1+ j +note1/12) )/3; | |
samples[i] += sin( frequency * PI*t *Math.pow(2,1+ j +note2/12) )/3; | |
} | |
samples[i] /= Math.pow(rich, .7); | |
//reduced some of the effect of the parameter changes on the volume | |
} |
See Demo of similar or same code at:
https://www.isnbreizh.fr/ixn/technicalSubject/sound/soundGenerator2.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am unable to play the sound wave so generated using js please help...