Bug 921695 - Part 2: Modify callers. r=karlt

This commit is contained in:
JW Wang 2013-10-01 14:22:57 +08:00
parent 11b47d7a8d
commit 4ffa278343
4 changed files with 21 additions and 87 deletions

View File

@ -79,39 +79,6 @@ AudioNodeExternalInputStream::GetTrackMapEntry(const StreamBuffer::Track& aTrack
static const uint32_t SPEEX_RESAMPLER_PROCESS_MAX_OUTPUT = 1000; static const uint32_t SPEEX_RESAMPLER_PROCESS_MAX_OUTPUT = 1000;
template <typename T> static int
SpeexResamplerProcess(SpeexResamplerState* aResampler,
uint32_t aChannel,
const T* aInput, uint32_t* aIn,
float* aOutput, uint32_t* aOut);
template <> int
SpeexResamplerProcess<float>(SpeexResamplerState* aResampler,
uint32_t aChannel,
const float* aInput, uint32_t* aIn,
float* aOutput, uint32_t* aOut)
{
NS_ASSERTION(*aOut <= SPEEX_RESAMPLER_PROCESS_MAX_OUTPUT, "Bad aOut");
return speex_resampler_process_float(aResampler, aChannel, aInput, aIn, aOutput, aOut);
}
template <> int
SpeexResamplerProcess<int16_t>(SpeexResamplerState* aResampler,
uint32_t aChannel,
const int16_t* aInput, uint32_t* aIn,
float* aOutput, uint32_t* aOut)
{
NS_ASSERTION(*aOut <= SPEEX_RESAMPLER_PROCESS_MAX_OUTPUT, "Bad aOut");
int16_t tmp[SPEEX_RESAMPLER_PROCESS_MAX_OUTPUT];
int result = speex_resampler_process_int(aResampler, aChannel, aInput, aIn, tmp, aOut);
if (result == RESAMPLER_ERR_SUCCESS) {
for (uint32_t i = 0; i < *aOut; ++i) {
aOutput[i] = AudioSampleToFloat(tmp[i]);
}
}
return result;
}
template <typename T> static void template <typename T> static void
ResampleChannelBuffer(SpeexResamplerState* aResampler, uint32_t aChannel, ResampleChannelBuffer(SpeexResamplerState* aResampler, uint32_t aChannel,
const T* aInput, uint32_t aInputDuration, const T* aInput, uint32_t aInputDuration,
@ -131,9 +98,9 @@ ResampleChannelBuffer(SpeexResamplerState* aResampler, uint32_t aChannel,
float* output = aOutput->AppendElements(SPEEX_RESAMPLER_PROCESS_MAX_OUTPUT); float* output = aOutput->AppendElements(SPEEX_RESAMPLER_PROCESS_MAX_OUTPUT);
uint32_t in = aInputDuration - processed; uint32_t in = aInputDuration - processed;
uint32_t out = aOutput->Length() - prevLength; uint32_t out = aOutput->Length() - prevLength;
SpeexResamplerProcess(aResampler, aChannel, WebAudioUtils::SpeexResamplerProcess(aResampler, aChannel,
aInput + processed, &in, aInput + processed, &in,
output, &out); output, &out);
processed += in; processed += in;
aOutput->SetLength(prevLength + out); aOutput->SetLength(prevLength + out);
} }

View File

@ -223,9 +223,9 @@ public:
static_cast<float*>(const_cast<void*>(aOutput->mChannelData[i])) + static_cast<float*>(const_cast<void*>(aOutput->mChannelData[i])) +
aBufferOffset; aBufferOffset;
speex_resampler_process_float(resampler, i, WebAudioUtils::SpeexResamplerProcess(resampler, i,
inputData, &inSamples, inputData, &inSamples,
outputData, &outSamples); outputData, &outSamples);
aFramesRead = inSamples; aFramesRead = inSamples;
aFramesWritten = outSamples; aFramesWritten = outSamples;

View File

@ -19,6 +19,7 @@
#include "nsIScriptError.h" #include "nsIScriptError.h"
#include "nsMimeTypes.h" #include "nsMimeTypes.h"
#include "nsCxPusher.h" #include "nsCxPusher.h"
#include "WebAudioUtils.h"
namespace mozilla { namespace mozilla {
@ -350,37 +351,21 @@ MediaDecodeTask::Decode()
static_cast<uint64_t>(audioData->mFrames) / static_cast<uint64_t>(audioData->mFrames) /
static_cast<uint64_t>(sampleRate) static_cast<uint64_t>(sampleRate)
); );
#ifdef MOZ_SAMPLE_TYPE_S16
AudioDataValue* resampledBuffer = new(fallible) AudioDataValue[channelCount * expectedOutSamples];
#endif
for (uint32_t i = 0; i < audioData->mChannels; ++i) { for (uint32_t i = 0; i < audioData->mChannels; ++i) {
uint32_t inSamples = audioData->mFrames; uint32_t inSamples = audioData->mFrames;
uint32_t outSamples = expectedOutSamples; uint32_t outSamples = expectedOutSamples;
#ifdef MOZ_SAMPLE_TYPE_S16 WebAudioUtils::SpeexResamplerProcess(
speex_resampler_process_int(resampler, i, &bufferData[i * audioData->mFrames], &inSamples, resampler, i, &bufferData[i * audioData->mFrames], &inSamples,
&resampledBuffer[i * expectedOutSamples], mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
&outSamples); &outSamples);
ConvertAudioSamples(&resampledBuffer[i * expectedOutSamples],
mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
outSamples);
#else
speex_resampler_process_float(resampler, i, &bufferData[i * audioData->mFrames], &inSamples,
mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
&outSamples);
#endif
if (i == audioData->mChannels - 1) { if (i == audioData->mChannels - 1) {
mDecodeJob.mWriteIndex += outSamples; mDecodeJob.mWriteIndex += outSamples;
MOZ_ASSERT(mDecodeJob.mWriteIndex <= resampledFrames); MOZ_ASSERT(mDecodeJob.mWriteIndex <= resampledFrames);
} }
} }
#ifdef MOZ_SAMPLE_TYPE_S16
delete[] resampledBuffer;
#endif
} else { } else {
for (uint32_t i = 0; i < audioData->mChannels; ++i) { for (uint32_t i = 0; i < audioData->mChannels; ++i) {
ConvertAudioSamples(&bufferData[i * audioData->mFrames], ConvertAudioSamples(&bufferData[i * audioData->mFrames],
@ -399,12 +384,7 @@ MediaDecodeTask::Decode()
int outputLatency = speex_resampler_get_output_latency(resampler); int outputLatency = speex_resampler_get_output_latency(resampler);
AudioDataValue* zero = (AudioDataValue*)calloc(inputLatency, sizeof(AudioDataValue)); AudioDataValue* zero = (AudioDataValue*)calloc(inputLatency, sizeof(AudioDataValue));
#ifdef MOZ_SAMPLE_TYPE_S16
AudioDataValue* resampledBuffer = new(fallible) AudioDataValue[channelCount * outputLatency];
if (!resampledBuffer || !zero) {
#else
if (!zero) { if (!zero) {
#endif
// Out of memory! // Out of memory!
ReportFailureOnMainThread(WebAudioDecodeJob::UnknownError); ReportFailureOnMainThread(WebAudioDecodeJob::UnknownError);
return; return;
@ -414,19 +394,10 @@ MediaDecodeTask::Decode()
uint32_t inSamples = inputLatency; uint32_t inSamples = inputLatency;
uint32_t outSamples = outputLatency; uint32_t outSamples = outputLatency;
#ifdef MOZ_SAMPLE_TYPE_S16 WebAudioUtils::SpeexResamplerProcess(
speex_resampler_process_int(resampler, i, zero, &inSamples, resampler, i, zero, &inSamples,
&resampledBuffer[i * outputLatency], mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
&outSamples); &outSamples);
ConvertAudioSamples(&resampledBuffer[i * outputLatency],
mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
outSamples);
#else
speex_resampler_process_float(resampler, i, zero, &inSamples,
mDecodeJob.mChannelBuffers[i] + mDecodeJob.mWriteIndex,
&outSamples);
#endif
if (i == channelCount - 1) { if (i == channelCount - 1) {
mDecodeJob.mWriteIndex += outSamples; mDecodeJob.mWriteIndex += outSamples;
@ -435,10 +406,6 @@ MediaDecodeTask::Decode()
} }
free(zero); free(zero);
#ifdef MOZ_SAMPLE_TYPE_S16
delete[] resampledBuffer;
#endif
} }
mPhase = PhaseEnum::AllocateBuffer; mPhase = PhaseEnum::AllocateBuffer;

View File

@ -106,9 +106,9 @@ public:
MOZ_ASSERT(mBuffer.Length() == outSamples); MOZ_ASSERT(mBuffer.Length() == outSamples);
speex_resampler_process_float(mUpSampler, aChannel, WebAudioUtils::SpeexResamplerProcess(mUpSampler, aChannel,
aInputData, &inSamples, aInputData, &inSamples,
outputData, &outSamples); outputData, &outSamples);
MOZ_ASSERT(inSamples == WEBAUDIO_BLOCK_SIZE && outSamples == WEBAUDIO_BLOCK_SIZE*aBlocks); MOZ_ASSERT(inSamples == WEBAUDIO_BLOCK_SIZE && outSamples == WEBAUDIO_BLOCK_SIZE*aBlocks);
@ -123,9 +123,9 @@ public:
MOZ_ASSERT(mBuffer.Length() == inSamples); MOZ_ASSERT(mBuffer.Length() == inSamples);
speex_resampler_process_float(mDownSampler, aChannel, WebAudioUtils::SpeexResamplerProcess(mDownSampler, aChannel,
inputData, &inSamples, inputData, &inSamples,
aOutputData, &outSamples); aOutputData, &outSamples);
MOZ_ASSERT(inSamples == WEBAUDIO_BLOCK_SIZE*aBlocks && outSamples == WEBAUDIO_BLOCK_SIZE); MOZ_ASSERT(inSamples == WEBAUDIO_BLOCK_SIZE*aBlocks && outSamples == WEBAUDIO_BLOCK_SIZE);
} }