cloud-game/static/test.html
2019-04-15 19:20:47 +08:00

59 lines
No EOL
2.3 KiB
HTML

<html>
<body>
<button onclick="play()">hihi</button>
<script>
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// Stereo
var channels = 1;
var sampleRate = 16000;
var bitDepth = 16;
// Create an empty two second stereo buffer at the
// sample rate of the AudioContext
var req = new XMLHttpRequest();
var sound;
req.open('GET', "/static/sinewave.raw", false);
req.overrideMimeType('text\/plain; charset=x-user-defined');
req.onreadystatechange = function (aEvt) {
if (req.readyState == 4) {
if(req.status == 200) {
sound = req.responseText;
console.log("loaded");
} else {
alert('failed loading audio');
}
}
};
req.send(null);
function play(){
frameCount = sound.length / 2;
myAudioBuffer = audioCtx.createBuffer(channels, frameCount, sampleRate);
for (var channel = 0; channel < channels; channel++) {
var nowBuffering = myAudioBuffer.getChannelData(channel, bitDepth, sampleRate);
for (var i = 0; i < frameCount; i++) {
// audio needs to be in [-1.0; 1.0]
// for this reason I also tried to divide it by 32767
// as my pcm sample is in 16-Bit. It plays still the
// same creepy sound less noisy.
var word = (sound.charCodeAt(i * 2) & 0xff) + ((sound.charCodeAt(i * 2 + 1) & 0xff) << 8);
nowBuffering[i] = ((word + 32768) % 65536 - 32768) / 32768.0;
}
}
// Get an AudioBufferSourceNode.
// This is the AudioNode to use when we want to play an AudioBuffer
var source = audioCtx.createBufferSource();
// set the buffer in the AudioBufferSourceNode
source.buffer = myAudioBuffer;
// connect the AudioBufferSourceNode to the
// destination so we can hear the sound
source.connect(audioCtx.destination);
// start the source playing
source.start();
}
</script>
</body>
</html>