michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "WebAudioUtils.h" michael@0: #include "AudioNodeStream.h" michael@0: #include "AudioParamTimeline.h" michael@0: #include "blink/HRTFDatabaseLoader.h" michael@0: #include "speex/speex_resampler.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: namespace dom { michael@0: michael@0: struct ConvertTimeToTickHelper michael@0: { michael@0: AudioNodeStream* mSourceStream; michael@0: AudioNodeStream* mDestinationStream; michael@0: michael@0: static int64_t Convert(double aTime, void* aClosure) michael@0: { michael@0: ConvertTimeToTickHelper* This = static_cast (aClosure); michael@0: MOZ_ASSERT(This->mSourceStream->SampleRate() == This->mDestinationStream->SampleRate()); michael@0: return This->mSourceStream-> michael@0: TicksFromDestinationTime(This->mDestinationStream, aTime); michael@0: } michael@0: }; michael@0: michael@0: void michael@0: WebAudioUtils::ConvertAudioParamToTicks(AudioParamTimeline& aParam, michael@0: AudioNodeStream* aSource, michael@0: AudioNodeStream* aDest) michael@0: { michael@0: MOZ_ASSERT(!aSource || aSource->SampleRate() == aDest->SampleRate()); michael@0: ConvertTimeToTickHelper ctth; michael@0: ctth.mSourceStream = aSource; michael@0: ctth.mDestinationStream = aDest; michael@0: aParam.ConvertEventTimesToTicks(ConvertTimeToTickHelper::Convert, &ctth, aDest->SampleRate()); michael@0: } michael@0: michael@0: void michael@0: WebAudioUtils::Shutdown() michael@0: { michael@0: WebCore::HRTFDatabaseLoader::shutdown(); michael@0: } michael@0: michael@0: int michael@0: WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler, michael@0: uint32_t aChannel, michael@0: const float* aIn, uint32_t* aInLen, michael@0: float* aOut, uint32_t* aOutLen) michael@0: { michael@0: #ifdef MOZ_SAMPLE_TYPE_S16 michael@0: nsAutoTArray tmp1; michael@0: nsAutoTArray tmp2; michael@0: tmp1.SetLength(*aInLen); michael@0: tmp2.SetLength(*aOutLen); michael@0: ConvertAudioSamples(aIn, tmp1.Elements(), *aInLen); michael@0: int result = speex_resampler_process_int(aResampler, aChannel, tmp1.Elements(), aInLen, tmp2.Elements(), aOutLen); michael@0: ConvertAudioSamples(tmp2.Elements(), aOut, *aOutLen); michael@0: return result; michael@0: #else michael@0: return speex_resampler_process_float(aResampler, aChannel, aIn, aInLen, aOut, aOutLen); michael@0: #endif michael@0: } michael@0: michael@0: int michael@0: WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler, michael@0: uint32_t aChannel, michael@0: const int16_t* aIn, uint32_t* aInLen, michael@0: float* aOut, uint32_t* aOutLen) michael@0: { michael@0: nsAutoTArray tmp; michael@0: #ifdef MOZ_SAMPLE_TYPE_S16 michael@0: tmp.SetLength(*aOutLen); michael@0: int result = speex_resampler_process_int(aResampler, aChannel, aIn, aInLen, tmp.Elements(), aOutLen); michael@0: ConvertAudioSamples(tmp.Elements(), aOut, *aOutLen); michael@0: return result; michael@0: #else michael@0: tmp.SetLength(*aInLen); michael@0: ConvertAudioSamples(aIn, tmp.Elements(), *aInLen); michael@0: int result = speex_resampler_process_float(aResampler, aChannel, tmp.Elements(), aInLen, aOut, aOutLen); michael@0: return result; michael@0: #endif michael@0: } michael@0: michael@0: int michael@0: WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler, michael@0: uint32_t aChannel, michael@0: const int16_t* aIn, uint32_t* aInLen, michael@0: int16_t* aOut, uint32_t* aOutLen) michael@0: { michael@0: #ifdef MOZ_SAMPLE_TYPE_S16 michael@0: return speex_resampler_process_int(aResampler, aChannel, aIn, aInLen, aOut, aOutLen); michael@0: #else michael@0: nsAutoTArray tmp1; michael@0: nsAutoTArray tmp2; michael@0: tmp1.SetLength(*aInLen); michael@0: tmp2.SetLength(*aOutLen); michael@0: ConvertAudioSamples(aIn, tmp1.Elements(), *aInLen); michael@0: int result = speex_resampler_process_float(aResampler, aChannel, tmp1.Elements(), aInLen, tmp2.Elements(), aOutLen); michael@0: ConvertAudioSamples(tmp2.Elements(), aOut, *aOutLen); michael@0: return result; michael@0: #endif michael@0: } michael@0: michael@0: } michael@0: }