Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef MOZILLA_AUDIOMIXER_H_ |
michael@0 | 7 | #define MOZILLA_AUDIOMIXER_H_ |
michael@0 | 8 | |
michael@0 | 9 | #include "AudioSampleFormat.h" |
michael@0 | 10 | #include "nsTArray.h" |
michael@0 | 11 | #include "mozilla/PodOperations.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | typedef void(*MixerFunc)(AudioDataValue* aMixedBuffer, |
michael@0 | 15 | AudioSampleFormat aFormat, |
michael@0 | 16 | uint32_t aChannels, |
michael@0 | 17 | uint32_t aFrames, |
michael@0 | 18 | uint32_t aSampleRate); |
michael@0 | 19 | |
michael@0 | 20 | /** |
michael@0 | 21 | * This class mixes multiple streams of audio together to output a single audio |
michael@0 | 22 | * stream. |
michael@0 | 23 | * |
michael@0 | 24 | * AudioMixer::Mix is to be called repeatedly with buffers that have the same |
michael@0 | 25 | * length, sample rate, sample format and channel count. |
michael@0 | 26 | * |
michael@0 | 27 | * When all the tracks have been mixed, calling FinishMixing will call back with |
michael@0 | 28 | * a buffer containing the mixed audio data. |
michael@0 | 29 | * |
michael@0 | 30 | * This class is not thread safe. |
michael@0 | 31 | */ |
michael@0 | 32 | class AudioMixer |
michael@0 | 33 | { |
michael@0 | 34 | public: |
michael@0 | 35 | AudioMixer(MixerFunc aCallback) |
michael@0 | 36 | : mCallback(aCallback), |
michael@0 | 37 | mFrames(0), |
michael@0 | 38 | mChannels(0), |
michael@0 | 39 | mSampleRate(0) |
michael@0 | 40 | { } |
michael@0 | 41 | |
michael@0 | 42 | /* Get the data from the mixer. This is supposed to be called when all the |
michael@0 | 43 | * tracks have been mixed in. The caller should not hold onto the data. */ |
michael@0 | 44 | void FinishMixing() { |
michael@0 | 45 | mCallback(mMixedAudio.Elements(), |
michael@0 | 46 | AudioSampleTypeToFormat<AudioDataValue>::Format, |
michael@0 | 47 | mChannels, |
michael@0 | 48 | mFrames, |
michael@0 | 49 | mSampleRate); |
michael@0 | 50 | PodZero(mMixedAudio.Elements(), mMixedAudio.Length()); |
michael@0 | 51 | mSampleRate = mChannels = mFrames = 0; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | /* Add a buffer to the mix. aSamples is interleaved. */ |
michael@0 | 55 | void Mix(AudioDataValue* aSamples, |
michael@0 | 56 | uint32_t aChannels, |
michael@0 | 57 | uint32_t aFrames, |
michael@0 | 58 | uint32_t aSampleRate) { |
michael@0 | 59 | if (!mFrames && !mChannels) { |
michael@0 | 60 | mFrames = aFrames; |
michael@0 | 61 | mChannels = aChannels; |
michael@0 | 62 | mSampleRate = aSampleRate; |
michael@0 | 63 | EnsureCapacityAndSilence(); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | MOZ_ASSERT(aFrames == mFrames); |
michael@0 | 67 | MOZ_ASSERT(aChannels == mChannels); |
michael@0 | 68 | MOZ_ASSERT(aSampleRate == mSampleRate); |
michael@0 | 69 | |
michael@0 | 70 | for (uint32_t i = 0; i < aFrames * aChannels; i++) { |
michael@0 | 71 | mMixedAudio[i] += aSamples[i]; |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | private: |
michael@0 | 75 | void EnsureCapacityAndSilence() { |
michael@0 | 76 | if (mFrames * mChannels > mMixedAudio.Length()) { |
michael@0 | 77 | mMixedAudio.SetLength(mFrames* mChannels); |
michael@0 | 78 | } |
michael@0 | 79 | PodZero(mMixedAudio.Elements(), mMixedAudio.Length()); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | /* Function that is called when the mixing is done. */ |
michael@0 | 83 | MixerFunc mCallback; |
michael@0 | 84 | /* Number of frames for this mixing block. */ |
michael@0 | 85 | uint32_t mFrames; |
michael@0 | 86 | /* Number of channels for this mixing block. */ |
michael@0 | 87 | uint32_t mChannels; |
michael@0 | 88 | /* Sample rate the of the mixed data. */ |
michael@0 | 89 | uint32_t mSampleRate; |
michael@0 | 90 | /* Buffer containing the mixed audio data. */ |
michael@0 | 91 | nsTArray<AudioDataValue> mMixedAudio; |
michael@0 | 92 | }; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | #endif // MOZILLA_AUDIOMIXER_H_ |