Created
June 27, 2017 20:52
-
-
Save montyanderson/df618eea35910cb9bc957ff936cd21cf to your computer and use it in GitHub Desktop.
This file contains 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
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