Musical scale: Difference between revisions

→‎{{header|J}}: Added JS/HTML
(removed 'crotchet' because it doesn't really make sense (tempo and time signature is unspecified anyway).)
(→‎{{header|J}}: Added JS/HTML)
Line 478:
0.25 is the duration of each note (in seconds).
 
=={{header|JavaScript}}/{{header|HTML}}==
Using the Web Audio API
<lang javascript><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Sample Page</title>
</head>
<body>
Upon loading the page you should hear the scale.
<script type="text/javascript">
function musicalScale(freqArr){
// create web audio api context
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = new AudioContext();
 
// create oscillator and gain node
var oscillator = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();
 
// connect oscillator to gain node to speakers
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
// set frequencies to play
duration = 0.5 // seconds
freqArr.forEach(function (freq, i){
oscillator.frequency.setValueAtTime(freq, audioCtx.currentTime + i * duration);
});
// start playing!
oscillator.start();
// stop playing!
oscillator.stop(audioCtx.currentTime + freqArr.length * duration);
}
 
musicalScale([261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25]);
</script>
</body>
</html></lang>
 
=={{header|Julia}}==
535

edits