1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/WebAudioUtils.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "WebAudioUtils.h" 1.11 +#include "AudioNodeStream.h" 1.12 +#include "AudioParamTimeline.h" 1.13 +#include "blink/HRTFDatabaseLoader.h" 1.14 +#include "speex/speex_resampler.h" 1.15 + 1.16 +namespace mozilla { 1.17 + 1.18 +namespace dom { 1.19 + 1.20 +struct ConvertTimeToTickHelper 1.21 +{ 1.22 + AudioNodeStream* mSourceStream; 1.23 + AudioNodeStream* mDestinationStream; 1.24 + 1.25 + static int64_t Convert(double aTime, void* aClosure) 1.26 + { 1.27 + ConvertTimeToTickHelper* This = static_cast<ConvertTimeToTickHelper*> (aClosure); 1.28 + MOZ_ASSERT(This->mSourceStream->SampleRate() == This->mDestinationStream->SampleRate()); 1.29 + return This->mSourceStream-> 1.30 + TicksFromDestinationTime(This->mDestinationStream, aTime); 1.31 + } 1.32 +}; 1.33 + 1.34 +void 1.35 +WebAudioUtils::ConvertAudioParamToTicks(AudioParamTimeline& aParam, 1.36 + AudioNodeStream* aSource, 1.37 + AudioNodeStream* aDest) 1.38 +{ 1.39 + MOZ_ASSERT(!aSource || aSource->SampleRate() == aDest->SampleRate()); 1.40 + ConvertTimeToTickHelper ctth; 1.41 + ctth.mSourceStream = aSource; 1.42 + ctth.mDestinationStream = aDest; 1.43 + aParam.ConvertEventTimesToTicks(ConvertTimeToTickHelper::Convert, &ctth, aDest->SampleRate()); 1.44 +} 1.45 + 1.46 +void 1.47 +WebAudioUtils::Shutdown() 1.48 +{ 1.49 + WebCore::HRTFDatabaseLoader::shutdown(); 1.50 +} 1.51 + 1.52 +int 1.53 +WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler, 1.54 + uint32_t aChannel, 1.55 + const float* aIn, uint32_t* aInLen, 1.56 + float* aOut, uint32_t* aOutLen) 1.57 +{ 1.58 +#ifdef MOZ_SAMPLE_TYPE_S16 1.59 + nsAutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp1; 1.60 + nsAutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp2; 1.61 + tmp1.SetLength(*aInLen); 1.62 + tmp2.SetLength(*aOutLen); 1.63 + ConvertAudioSamples(aIn, tmp1.Elements(), *aInLen); 1.64 + int result = speex_resampler_process_int(aResampler, aChannel, tmp1.Elements(), aInLen, tmp2.Elements(), aOutLen); 1.65 + ConvertAudioSamples(tmp2.Elements(), aOut, *aOutLen); 1.66 + return result; 1.67 +#else 1.68 + return speex_resampler_process_float(aResampler, aChannel, aIn, aInLen, aOut, aOutLen); 1.69 +#endif 1.70 +} 1.71 + 1.72 +int 1.73 +WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler, 1.74 + uint32_t aChannel, 1.75 + const int16_t* aIn, uint32_t* aInLen, 1.76 + float* aOut, uint32_t* aOutLen) 1.77 +{ 1.78 + nsAutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp; 1.79 +#ifdef MOZ_SAMPLE_TYPE_S16 1.80 + tmp.SetLength(*aOutLen); 1.81 + int result = speex_resampler_process_int(aResampler, aChannel, aIn, aInLen, tmp.Elements(), aOutLen); 1.82 + ConvertAudioSamples(tmp.Elements(), aOut, *aOutLen); 1.83 + return result; 1.84 +#else 1.85 + tmp.SetLength(*aInLen); 1.86 + ConvertAudioSamples(aIn, tmp.Elements(), *aInLen); 1.87 + int result = speex_resampler_process_float(aResampler, aChannel, tmp.Elements(), aInLen, aOut, aOutLen); 1.88 + return result; 1.89 +#endif 1.90 +} 1.91 + 1.92 +int 1.93 +WebAudioUtils::SpeexResamplerProcess(SpeexResamplerState* aResampler, 1.94 + uint32_t aChannel, 1.95 + const int16_t* aIn, uint32_t* aInLen, 1.96 + int16_t* aOut, uint32_t* aOutLen) 1.97 +{ 1.98 +#ifdef MOZ_SAMPLE_TYPE_S16 1.99 + return speex_resampler_process_int(aResampler, aChannel, aIn, aInLen, aOut, aOutLen); 1.100 +#else 1.101 + nsAutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp1; 1.102 + nsAutoTArray<AudioDataValue, WEBAUDIO_BLOCK_SIZE*4> tmp2; 1.103 + tmp1.SetLength(*aInLen); 1.104 + tmp2.SetLength(*aOutLen); 1.105 + ConvertAudioSamples(aIn, tmp1.Elements(), *aInLen); 1.106 + int result = speex_resampler_process_float(aResampler, aChannel, tmp1.Elements(), aInLen, tmp2.Elements(), aOutLen); 1.107 + ConvertAudioSamples(tmp2.Elements(), aOut, *aOutLen); 1.108 + return result; 1.109 +#endif 1.110 +} 1.111 + 1.112 +} 1.113 +}