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/. */
6 #ifndef MOZILLA_AUDIOMIXER_H_
7 #define MOZILLA_AUDIOMIXER_H_
9 #include "AudioSampleFormat.h"
10 #include "AudioStream.h"
12 #include "mozilla/LinkedList.h"
13 #include "mozilla/NotNull.h"
14 #include "mozilla/PodOperations.h"
18 struct MixerCallbackReceiver
{
19 virtual void MixerCallback(AudioDataValue
* aMixedBuffer
,
20 AudioSampleFormat aFormat
, uint32_t aChannels
,
21 uint32_t aFrames
, uint32_t aSampleRate
) = 0;
24 * This class mixes multiple streams of audio together to output a single audio
27 * AudioMixer::Mix is to be called repeatedly with buffers that have the same
28 * length, sample rate, sample format and channel count. This class works with
29 * interleaved and plannar buffers, but the buffer mixed must be of the same
30 * type during a mixing cycle.
32 * When all the tracks have been mixed, calling FinishMixing will call back with
33 * a buffer containing the mixed audio data.
35 * This class is not thread safe.
39 AudioMixer() : mFrames(0), mChannels(0), mSampleRate(0) {}
43 while ((cb
= mCallbacks
.popFirst())) {
48 void StartMixing() { mSampleRate
= mChannels
= mFrames
= 0; }
50 /* Get the data from the mixer. This is supposed to be called when all the
51 * tracks have been mixed in. The caller should not hold onto the data. */
53 MOZ_ASSERT(mChannels
&& mSampleRate
, "Mix not called for this cycle?");
54 for (MixerCallback
* cb
= mCallbacks
.getFirst(); cb
!= nullptr;
56 MixerCallbackReceiver
* receiver
= cb
->mReceiver
;
58 receiver
->MixerCallback(mMixedAudio
.Elements(),
59 AudioSampleTypeToFormat
<AudioDataValue
>::Format
,
60 mChannels
, mFrames
, mSampleRate
);
62 PodZero(mMixedAudio
.Elements(), mMixedAudio
.Length());
63 mSampleRate
= mChannels
= mFrames
= 0;
66 /* Add a buffer to the mix. The buffer can be null if there's nothing to mix
67 * but the callback is still needed. */
68 void Mix(AudioDataValue
* aSamples
, uint32_t aChannels
, uint32_t aFrames
,
69 uint32_t aSampleRate
) {
70 if (!mFrames
&& !mChannels
) {
72 mChannels
= aChannels
;
73 mSampleRate
= aSampleRate
;
74 EnsureCapacityAndSilence();
77 MOZ_ASSERT(aFrames
== mFrames
);
78 MOZ_ASSERT(aChannels
== mChannels
);
79 MOZ_ASSERT(aSampleRate
== mSampleRate
);
85 for (uint32_t i
= 0; i
< aFrames
* aChannels
; i
++) {
86 mMixedAudio
[i
] += aSamples
[i
];
90 void AddCallback(NotNull
<MixerCallbackReceiver
*> aReceiver
) {
91 mCallbacks
.insertBack(new MixerCallback(aReceiver
));
94 bool FindCallback(MixerCallbackReceiver
* aReceiver
) {
95 for (MixerCallback
* cb
= mCallbacks
.getFirst(); cb
!= nullptr;
97 if (cb
->mReceiver
== aReceiver
) {
104 bool RemoveCallback(MixerCallbackReceiver
* aReceiver
) {
105 for (MixerCallback
* cb
= mCallbacks
.getFirst(); cb
!= nullptr;
106 cb
= cb
->getNext()) {
107 if (cb
->mReceiver
== aReceiver
) {
117 void EnsureCapacityAndSilence() {
118 if (mFrames
* mChannels
> mMixedAudio
.Length()) {
119 mMixedAudio
.SetLength(mFrames
* mChannels
);
121 PodZero(mMixedAudio
.Elements(), mMixedAudio
.Length());
124 class MixerCallback
: public LinkedListElement
<MixerCallback
> {
126 explicit MixerCallback(NotNull
<MixerCallbackReceiver
*> aReceiver
)
127 : mReceiver(aReceiver
) {}
128 NotNull
<MixerCallbackReceiver
*> mReceiver
;
131 /* Function that is called when the mixing is done. */
132 LinkedList
<MixerCallback
> mCallbacks
;
133 /* Number of frames for this mixing block. */
135 /* Number of channels for this mixing block. */
137 /* Sample rate the of the mixed data. */
138 uint32_t mSampleRate
;
139 /* Buffer containing the mixed audio data. */
140 nsTArray
<AudioDataValue
> mMixedAudio
;
143 } // namespace mozilla
145 #endif // MOZILLA_AUDIOMIXER_H_