Skip to content

Instantly share code, notes, and snippets.

@montyanderson
Created June 27, 2017 20:52
Show Gist options
  • Save montyanderson/df618eea35910cb9bc957ff936cd21cf to your computer and use it in GitHub Desktop.
Save montyanderson/df618eea35910cb9bc957ff936cd21cf to your computer and use it in GitHub Desktop.
function mix(streams) {
const length = Math.max(...streams.map(s => s.length));
const numberOfChannels = Math.max(...streams.map(s => s.numberOfChannels));
const sampleRate = streams[0].sampleRate;
// assume all streams have the same sample rate for now
const result = new AudioBuffer({
length,
numberOfChannels,
sampleRate
});
streams.forEach(stream => {
for(let channel = 0; channel < stream.numberOfChannels; channel++) {
const buffer = result.getChannelData(channel);
const data = stream.getChannelData(channel);
for(let i = 0; i < data.length; i++) {
buffer[i] += data[i];
}
}
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment