michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "AudioMixer.h" michael@0: #include michael@0: michael@0: using mozilla::AudioDataValue; michael@0: using mozilla::AudioSampleFormat; michael@0: michael@0: /* In this test, the different audio stream and channels are always created to michael@0: * cancel each other. */ michael@0: void MixingDone(AudioDataValue* aData, AudioSampleFormat aFormat, uint32_t aChannels, uint32_t aFrames, uint32_t aSampleRate) michael@0: { michael@0: bool silent = true; michael@0: for (uint32_t i = 0; i < aChannels * aFrames; i++) { michael@0: if (aData[i] != 0.0) { michael@0: if (aFormat == mozilla::AUDIO_FORMAT_S16) { michael@0: fprintf(stderr, "Sample at %d is not silent: %d\n", i, (short)aData[i]); michael@0: } else { michael@0: fprintf(stderr, "Sample at %d is not silent: %f\n", i, (float)aData[i]); michael@0: } michael@0: silent = false; michael@0: } michael@0: } michael@0: if (!silent) { michael@0: MOZ_CRASH(); michael@0: } michael@0: } michael@0: michael@0: /* Helper function to give us the maximum and minimum value that don't clip, michael@0: * for a given sample format (integer or floating-point). */ michael@0: template michael@0: T GetLowValue(); michael@0: michael@0: template michael@0: T GetHighValue(); michael@0: michael@0: template<> michael@0: float GetLowValue() { michael@0: return -1.0; michael@0: } michael@0: michael@0: template<> michael@0: short GetLowValue() { michael@0: return -INT16_MAX; michael@0: } michael@0: michael@0: template<> michael@0: float GetHighValue() { michael@0: return 1.0; michael@0: } michael@0: michael@0: template<> michael@0: short GetHighValue() { michael@0: return INT16_MAX; michael@0: } michael@0: michael@0: void FillBuffer(AudioDataValue* aBuffer, uint32_t aLength, AudioDataValue aValue) michael@0: { michael@0: AudioDataValue* end = aBuffer + aLength; michael@0: while (aBuffer != end) { michael@0: *aBuffer++ = aValue; michael@0: } michael@0: } michael@0: michael@0: int main(int argc, char* argv[]) { michael@0: const uint32_t CHANNEL_LENGTH = 256; michael@0: const uint32_t AUDIO_RATE = 44100; michael@0: AudioDataValue a[CHANNEL_LENGTH * 2]; michael@0: AudioDataValue b[CHANNEL_LENGTH * 2]; michael@0: FillBuffer(a, CHANNEL_LENGTH, GetLowValue()); michael@0: FillBuffer(a + CHANNEL_LENGTH, CHANNEL_LENGTH, GetHighValue()); michael@0: FillBuffer(b, CHANNEL_LENGTH, GetHighValue()); michael@0: FillBuffer(b + CHANNEL_LENGTH, CHANNEL_LENGTH, GetLowValue()); michael@0: michael@0: { michael@0: int iterations = 2; michael@0: mozilla::AudioMixer mixer(MixingDone); michael@0: michael@0: fprintf(stderr, "Test AudioMixer constant buffer length.\n"); michael@0: michael@0: while (iterations--) { michael@0: mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.FinishMixing(); michael@0: } michael@0: } michael@0: michael@0: { michael@0: mozilla::AudioMixer mixer(MixingDone); michael@0: michael@0: fprintf(stderr, "Test AudioMixer variable buffer length.\n"); michael@0: michael@0: FillBuffer(a, CHANNEL_LENGTH / 2, GetLowValue()); michael@0: FillBuffer(a + CHANNEL_LENGTH / 2, CHANNEL_LENGTH / 2, GetLowValue()); michael@0: FillBuffer(b, CHANNEL_LENGTH / 2, GetHighValue()); michael@0: FillBuffer(b + CHANNEL_LENGTH / 2, CHANNEL_LENGTH / 2, GetHighValue()); michael@0: mixer.Mix(a, 2, CHANNEL_LENGTH / 2, AUDIO_RATE); michael@0: mixer.Mix(b, 2, CHANNEL_LENGTH / 2, AUDIO_RATE); michael@0: mixer.FinishMixing(); michael@0: FillBuffer(a, CHANNEL_LENGTH, GetLowValue()); michael@0: FillBuffer(a + CHANNEL_LENGTH, CHANNEL_LENGTH, GetHighValue()); michael@0: FillBuffer(b, CHANNEL_LENGTH, GetHighValue()); michael@0: FillBuffer(b + CHANNEL_LENGTH, CHANNEL_LENGTH, GetLowValue()); michael@0: mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.FinishMixing(); michael@0: FillBuffer(a, CHANNEL_LENGTH / 2, GetLowValue()); michael@0: FillBuffer(a + CHANNEL_LENGTH / 2, CHANNEL_LENGTH / 2, GetLowValue()); michael@0: FillBuffer(b, CHANNEL_LENGTH / 2, GetHighValue()); michael@0: FillBuffer(b + CHANNEL_LENGTH / 2, CHANNEL_LENGTH / 2, GetHighValue()); michael@0: mixer.Mix(a, 2, CHANNEL_LENGTH / 2, AUDIO_RATE); michael@0: mixer.Mix(b, 2, CHANNEL_LENGTH / 2, AUDIO_RATE); michael@0: mixer.FinishMixing(); michael@0: } michael@0: michael@0: FillBuffer(a, CHANNEL_LENGTH, GetLowValue()); michael@0: FillBuffer(b, CHANNEL_LENGTH, GetHighValue()); michael@0: michael@0: { michael@0: mozilla::AudioMixer mixer(MixingDone); michael@0: fprintf(stderr, "Test AudioMixer variable channel count.\n"); michael@0: michael@0: mixer.Mix(a, 1, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.Mix(b, 1, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.FinishMixing(); michael@0: mixer.Mix(a, 1, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.Mix(b, 1, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.FinishMixing(); michael@0: mixer.Mix(a, 1, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.Mix(b, 1, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.FinishMixing(); michael@0: } michael@0: michael@0: { michael@0: mozilla::AudioMixer mixer(MixingDone); michael@0: fprintf(stderr, "Test AudioMixer variable stream count.\n"); michael@0: michael@0: mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.FinishMixing(); michael@0: mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.FinishMixing(); michael@0: mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE); michael@0: mixer.FinishMixing(); michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: