Created
November 18, 2014 21:00
-
-
Save BrianAdams/5b3e249f49c1210c6ee2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<html> | |
<head> | |
<script src="/socket.io/socket.io.js"></script> | |
<script src="socket.io-stream.js"></script> | |
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> | |
<script> | |
$(function() { | |
var socket = io.connect(); | |
var stream = ss.createStream(); | |
// receive data | |
window.AudioContext = window.AudioContext || window.webkitAudioContext; | |
var audioCtx = new AudioContext(); | |
var audioBuffer = audioCtx.createBuffer(2, 22050, 44100); | |
var source = audioCtx.createBufferSource(); | |
source.buffer = audioBuffer; | |
// source.connect(audioCtx.destination); | |
gainNode = audioCtx.createGain(); | |
gainNode.gain.value = 0; | |
gainNode.connect(audioCtx.destination); | |
source.connect(gainNode); | |
source.start(0); | |
socket.on('audio', function(audioChunk) { | |
// Create/set audio buffer for each chunk | |
//audioBuffer.getChannelData(0).set(audioChunk); | |
//source.buffer = audioBuffer; | |
console.log("."); | |
playsound(audioChunk); | |
}); | |
function playsound( raw ) { | |
// i'll assume you know how to convert in this direction | |
// since you have convertFloat32ToInt16 | |
// var buffer = convertInt16ToFloat32( raw ), | |
var context = audioCtx; | |
var buffer = new Int16Array(raw); | |
var channel0buffer = new Float32Array(buffer.length/2); | |
var channel1buffer = new Float32Array(buffer.length/2); | |
var j = 0; | |
for (var i =0;i < buffer.length;i++){ | |
channel0buffer[j] = buffer[i]; | |
i++; | |
channel1buffer[j] = buffer[i]; | |
j++; | |
} | |
// var buffer = new Int16Array(raw), | |
var src = context.createBufferSource(), | |
audioBuffer = context.createBuffer( 2, buffer.length, context.sampleRate ); | |
audioBuffer.getChannelData( 0 ).set( channel1buffer ); | |
audioBuffer.getChannelData( 1 ).set( channel0buffer ); | |
src.buffer = audioBuffer; | |
src.connect( gainNode ); | |
src.start( 0 ); | |
} | |
}); | |
</script> | |
<head> | |
<body> | |
<p>Volume</p> | |
<input id="volume" type="range" min="0" max="1" step="0.1" value="0.0"/> | |
<script> | |
document.getElementById('volume').addEventListener('change', function() { | |
gainNode.gain.value = this.value; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment