Skip to content

Instantly share code, notes, and snippets.

@yo-to
Last active December 30, 2015 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yo-to/7843030 to your computer and use it in GitHub Desktop.
Save yo-to/7843030 to your computer and use it in GitHub Desktop.
Mac用 Firefox 25.0.1 のスクラッチパッドで音を鳴らす (矩形波、メロディを奏でる)
var ctx = new AudioContext();
var vol = ctx.createGain();
vol.gain.value = 0.2; // 0.0 - 1.0
vol.connect(ctx.destination);
osc_freq = new Array(0, 262, 294, 330, 349, 392, 440);
melody = new Array(
1, 2, 3, 0, 1, 2, 3, 0, 5, 3, 2, 1, 2, 3, 2, 0,
1, 2, 3, 0, 1, 2, 3, 0, 5, 3, 2, 1, 2, 3, 1, 0,
5, 5, 3, 5, 6, 6, 5, 0, 3, 3, 2, 2, 1
);
length = 0.8;
for(i=0; i<melody.length; i++) {
osc = ctx.createOscillator();
osc.type = 'square';
osc.frequency.value = osc_freq[melody[i]];
osc.connect(vol);
osc.start(i * length);
osc.stop((i + 1) * length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment