Skip to content

Instantly share code, notes, and snippets.

@yoeran
Forked from mbarton/Web Audio Cue Tests
Last active December 19, 2015 16:29
Show Gist options
  • Save yoeran/5983887 to your computer and use it in GitHub Desktop.
Save yoeran/5983887 to your computer and use it in GitHub Desktop.
Little test to combine microphone input (for recording) with multi-channel playback. Multi-channel playback probably only works on Chrome 28+ or Chrome Canary
window.AudioContext = window.AudioContext || window.webkitAudioContext;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
var mixer_outputs = [];
var sources = [];
// setup context
var ctx = new AudioContext();
ctx.destination.channelCount = 4;
ctx.destination.channelCountMode = "explicit";
ctx.destination.channelInterpretation = "discrete";
// setup merger
var merger = ctx.createChannelMerger(2);
merger.connect(ctx.destination);
// functions
function createMixerOutput(channel_id, index){
mixer_outputs[channel_id] = ctx.createGainNode();
mixer_outputs[channel_id].channelCount = 2;
mixer_outputs[channel_id].channelCountMode = "explicit";
mixer_outputs[channel_id].channelInterpretation = "speakers";
mixer_outputs[channel_id].connect(merger, 0, index);
return mixer_outputs[channel_id];
}
function downloadAudio(channel_id, filename){
var xhr = new XMLHttpRequest();
xhr.open('GET', filename, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
ctx.decodeAudioData(this.response, function(data){
sources[channel_id] = ctx.createBufferSource();
sources[channel_id].buffer = data;
sources[channel_id].loop = true;
sources[channel_id].connect( mixer_outputs[channel_id] );
});
};
xhr.send();
}
// You'll need multiple outputs on your soundcard for this
createMixerOutput("main", 0); // will output to 1/2
createMixerOutput("cue", 1); // will output to 3/4
downloadAudio("main","1.wav"); // load file into main channel
downloadAudio("cue","2.wav"); // load file into cue channel
// start specific sample after click
$("button").click(function(){
sources[$(this).attr("id")].start(0);
});
// Request access to microphone
navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
console.log('No live audio input: ' + e);
});
// Get the microphone stream
function startUserMedia(stream)
{
// this breaks the merging of the outputs, resulting in "main" and "cue" being outputted to the same output channel again... HELP!
// var mic = ctx.createMediaStreamSource(stream);
}
<head>
<title>Cue Test</title>
</head>
<body>
<button id="main">Start Main</button>
<button id="cue">Start Cue</button>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="cue-test.js"></script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment