From 9a6e4c2741f46586a0db527d6e0a7eee00449dfa Mon Sep 17 00:00:00 2001 From: Karl Tomlinson Date: Thu, 17 Aug 2017 17:23:27 +1200 Subject: [PATCH] bug 1391482 accept int16_t-sample initialization of AudioBuffer r=padenot MozReview-Commit-ID: 5UaVZYneN2b --HG-- extra : rebase_source : b6a5e28572c44f14a1bcdb13a950e78bb46eda41 --- dom/media/webaudio/AudioBuffer.cpp | 41 ++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/dom/media/webaudio/AudioBuffer.cpp b/dom/media/webaudio/AudioBuffer.cpp index 5bee7c701136..f43233750667 100644 --- a/dom/media/webaudio/AudioBuffer.cpp +++ b/dom/media/webaudio/AudioBuffer.cpp @@ -271,6 +271,22 @@ AudioBuffer::WrapObject(JSContext* aCx, JS::Handle aGivenProto) return AudioBufferBinding::Wrap(aCx, this, aGivenProto); } +static void +CopyChannelDataToFloat(const AudioChunk& aChunk, uint32_t aChannel, + uint32_t aSrcOffset, float* aOutput, uint32_t aLength) +{ + MOZ_ASSERT(aChunk.mVolume == 1.0f); + if (aChunk.mBufferFormat == AUDIO_FORMAT_FLOAT32) { + mozilla::PodCopy(aOutput, + aChunk.ChannelData()[aChannel] + aSrcOffset, + aLength); + } else { + MOZ_ASSERT(aChunk.mBufferFormat == AUDIO_FORMAT_S16); + ConvertAudioSamples(aChunk.ChannelData()[aChannel] + aSrcOffset, + aOutput, aLength); + } +} + bool AudioBuffer::RestoreJSChannelData(JSContext* aJSContext) { @@ -291,13 +307,11 @@ AudioBuffer::RestoreJSChannelData(JSContext* aJSContext) if (!mSharedChannels.IsNull()) { // "4. Attach ArrayBuffers containing copies of the data to the // AudioBuffer, to be returned by the next call to getChannelData." - MOZ_ASSERT(mSharedChannels.mVolume == 1.0f); - const float* data = mSharedChannels.ChannelData()[i]; JS::AutoCheckCannotGC nogc; bool isShared; - mozilla::PodCopy(JS_GetFloat32ArrayData(array, &isShared, nogc), - data, Length()); + float* jsData = JS_GetFloat32ArrayData(array, &isShared, nogc); MOZ_ASSERT(!isShared); // Was created as unshared above + CopyChannelDataToFloat(mSharedChannels, i, 0, jsData, Length()); } mJSChannels[i] = array; } @@ -325,7 +339,6 @@ AudioBuffer::CopyFromChannel(const Float32Array& aDestination, uint32_t aChannel JS::AutoCheckCannotGC nogc; JSObject* channelArray = mJSChannels[aChannelNumber]; - const float* sourceData = nullptr; if (channelArray) { if (JS_GetTypedArrayLength(channelArray) != Length()) { // The array's buffer was detached. @@ -334,20 +347,22 @@ AudioBuffer::CopyFromChannel(const Float32Array& aDestination, uint32_t aChannel } bool isShared = false; - sourceData = JS_GetFloat32ArrayData(channelArray, &isShared, nogc); + const float* sourceData = + JS_GetFloat32ArrayData(channelArray, &isShared, nogc); // The sourceData arrays should all have originated in // RestoreJSChannelData, where they are created unshared. MOZ_ASSERT(!isShared); - } else if (!mSharedChannels.IsNull()) { - MOZ_ASSERT(mSharedChannels.mVolume == 1.0f); - sourceData = mSharedChannels.ChannelData()[aChannelNumber]; + PodMove(aDestination.Data(), sourceData + aStartInChannel, length); + return; } - if (sourceData) { - PodMove(aDestination.Data(), sourceData + aStartInChannel, length); - } else { - PodZero(aDestination.Data(), length); + if (!mSharedChannels.IsNull()) { + CopyChannelDataToFloat(mSharedChannels, aChannelNumber, aStartInChannel, + aDestination.Data(), length); + return; } + + PodZero(aDestination.Data(), length); } void