Skip to content

Instantly share code, notes, and snippets.

@dalcib
Last active July 20, 2021 18:24
Show Gist options
  • Save dalcib/aa1cc533ff67a53f4d7bea3825ed8645 to your computer and use it in GitHub Desktop.
Save dalcib/aa1cc533ff67a53f4d7bea3825ed8645 to your computer and use it in GitHub Desktop.
Ramdom music notes generator
var context=new AudioContext();
var o=null;
var g=null;
var note
var notes = [130.8, 138.6, 146.8, 155.6, 164.8, 174.6, 185.0, 196.0, 207.7, 220.0, 233.1, 246.9, 261.6, 277.2, 293.7, 311.1, 329.6, 349.2, 370, 392, 415.3, 440, 466.2, 493.9] //, 523.3, 554.4, 587.3, 622.3, 659.3, 698.5, 740, 784, 830.6, 880, 932.3, 987.8]
var swaras = ['Sa','re','Re','ga','Ga','Ma','MA','Pa','dha','Dha','ni','Ni','Sa*','re','Re*','ga*','Ga*','Ma*','MA*','Pa*','dha*','Dha*','ni*','Ni*', 'Sa**']
function playNote(frequency) {
var type = 'sine' // 'square' 'triangle', 'sawtooth');
setTimeout(function(){
o = context.createOscillator();
g = context.createGain();
o.type = type;
o.connect(g);
o.frequency.value = frequency;
g.connect(context.destination);
o.start(0);
g.gain.exponentialRampToValueAtTime(0.00001, context.currentTime + 7);
},1000)
}
function play() {
playNote(notes[Math.floor(Math.random()*notes.length)])
}
function gen() {
note = Math.floor(Math.random()*notes.length)
console.log(swaras[note])
}
function p(){
playNote(notes[note])
}
//https://reeteshghimire.com.np/2020/03/27/play-musical-notes-with-javascript/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment