web-audio-api icon indicating copy to clipboard operation
web-audio-api copied to clipboard

Implement copyFromChannel/copyToChannel

Open 1j01 opened this issue 9 years ago • 0 comments

I was able to work around the lack of copyFromChannel/copyToChannel by using getChannelData and Float32Array::slice and AudioBuffer.fromArray when trying to slice an AudioBuffer:


sliceAudioBuffer = (audioBuffer, startOffset, endOffset, audioContext)->
		# {numberOfChannels, duration, sampleRate, frameCount} = audioBuffer
		# 
		# newAudioBuffer = audioContext.createBuffer(numberOfChannels, endOffset - startOffset, sampleRate)
		# tempArray = new Float32Array(frameCount)
		# 
		# for channel in [0..numberOfChannels]
		# 	audioBuffer.copyFromChannel(tempArray, channel, startOffset)
		# 	newAudioBuffer.copyToChannel(tempArray, channel, 0)
		# 
		# newAudioBuffer
		
		{numberOfChannels, sampleRate} = audioBuffer
		
		array =
			for channel in [0...numberOfChannels]
				samples = audioBuffer.getChannelData(channel)
				samples.slice(startOffset * sampleRate, endOffset * sampleRate)
			
		AudioBuffer.fromArray(array, sampleRate)

But it seems like these should be pretty simple to add.

1j01 avatar Oct 31 '16 04:10 1j01