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