Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
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 | #include "AudioMixer.h" |
michael@0 | 7 | #include <assert.h> |
michael@0 | 8 | |
michael@0 | 9 | using mozilla::AudioDataValue; |
michael@0 | 10 | using mozilla::AudioSampleFormat; |
michael@0 | 11 | |
michael@0 | 12 | /* In this test, the different audio stream and channels are always created to |
michael@0 | 13 | * cancel each other. */ |
michael@0 | 14 | void MixingDone(AudioDataValue* aData, AudioSampleFormat aFormat, uint32_t aChannels, uint32_t aFrames, uint32_t aSampleRate) |
michael@0 | 15 | { |
michael@0 | 16 | bool silent = true; |
michael@0 | 17 | for (uint32_t i = 0; i < aChannels * aFrames; i++) { |
michael@0 | 18 | if (aData[i] != 0.0) { |
michael@0 | 19 | if (aFormat == mozilla::AUDIO_FORMAT_S16) { |
michael@0 | 20 | fprintf(stderr, "Sample at %d is not silent: %d\n", i, (short)aData[i]); |
michael@0 | 21 | } else { |
michael@0 | 22 | fprintf(stderr, "Sample at %d is not silent: %f\n", i, (float)aData[i]); |
michael@0 | 23 | } |
michael@0 | 24 | silent = false; |
michael@0 | 25 | } |
michael@0 | 26 | } |
michael@0 | 27 | if (!silent) { |
michael@0 | 28 | MOZ_CRASH(); |
michael@0 | 29 | } |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | /* Helper function to give us the maximum and minimum value that don't clip, |
michael@0 | 33 | * for a given sample format (integer or floating-point). */ |
michael@0 | 34 | template<typename T> |
michael@0 | 35 | T GetLowValue(); |
michael@0 | 36 | |
michael@0 | 37 | template<typename T> |
michael@0 | 38 | T GetHighValue(); |
michael@0 | 39 | |
michael@0 | 40 | template<> |
michael@0 | 41 | float GetLowValue<float>() { |
michael@0 | 42 | return -1.0; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | template<> |
michael@0 | 46 | short GetLowValue<short>() { |
michael@0 | 47 | return -INT16_MAX; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | template<> |
michael@0 | 51 | float GetHighValue<float>() { |
michael@0 | 52 | return 1.0; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | template<> |
michael@0 | 56 | short GetHighValue<short>() { |
michael@0 | 57 | return INT16_MAX; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | void FillBuffer(AudioDataValue* aBuffer, uint32_t aLength, AudioDataValue aValue) |
michael@0 | 61 | { |
michael@0 | 62 | AudioDataValue* end = aBuffer + aLength; |
michael@0 | 63 | while (aBuffer != end) { |
michael@0 | 64 | *aBuffer++ = aValue; |
michael@0 | 65 | } |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | int main(int argc, char* argv[]) { |
michael@0 | 69 | const uint32_t CHANNEL_LENGTH = 256; |
michael@0 | 70 | const uint32_t AUDIO_RATE = 44100; |
michael@0 | 71 | AudioDataValue a[CHANNEL_LENGTH * 2]; |
michael@0 | 72 | AudioDataValue b[CHANNEL_LENGTH * 2]; |
michael@0 | 73 | FillBuffer(a, CHANNEL_LENGTH, GetLowValue<AudioDataValue>()); |
michael@0 | 74 | FillBuffer(a + CHANNEL_LENGTH, CHANNEL_LENGTH, GetHighValue<AudioDataValue>()); |
michael@0 | 75 | FillBuffer(b, CHANNEL_LENGTH, GetHighValue<AudioDataValue>()); |
michael@0 | 76 | FillBuffer(b + CHANNEL_LENGTH, CHANNEL_LENGTH, GetLowValue<AudioDataValue>()); |
michael@0 | 77 | |
michael@0 | 78 | { |
michael@0 | 79 | int iterations = 2; |
michael@0 | 80 | mozilla::AudioMixer mixer(MixingDone); |
michael@0 | 81 | |
michael@0 | 82 | fprintf(stderr, "Test AudioMixer constant buffer length.\n"); |
michael@0 | 83 | |
michael@0 | 84 | while (iterations--) { |
michael@0 | 85 | mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 86 | mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 87 | mixer.FinishMixing(); |
michael@0 | 88 | } |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | { |
michael@0 | 92 | mozilla::AudioMixer mixer(MixingDone); |
michael@0 | 93 | |
michael@0 | 94 | fprintf(stderr, "Test AudioMixer variable buffer length.\n"); |
michael@0 | 95 | |
michael@0 | 96 | FillBuffer(a, CHANNEL_LENGTH / 2, GetLowValue<AudioDataValue>()); |
michael@0 | 97 | FillBuffer(a + CHANNEL_LENGTH / 2, CHANNEL_LENGTH / 2, GetLowValue<AudioDataValue>()); |
michael@0 | 98 | FillBuffer(b, CHANNEL_LENGTH / 2, GetHighValue<AudioDataValue>()); |
michael@0 | 99 | FillBuffer(b + CHANNEL_LENGTH / 2, CHANNEL_LENGTH / 2, GetHighValue<AudioDataValue>()); |
michael@0 | 100 | mixer.Mix(a, 2, CHANNEL_LENGTH / 2, AUDIO_RATE); |
michael@0 | 101 | mixer.Mix(b, 2, CHANNEL_LENGTH / 2, AUDIO_RATE); |
michael@0 | 102 | mixer.FinishMixing(); |
michael@0 | 103 | FillBuffer(a, CHANNEL_LENGTH, GetLowValue<AudioDataValue>()); |
michael@0 | 104 | FillBuffer(a + CHANNEL_LENGTH, CHANNEL_LENGTH, GetHighValue<AudioDataValue>()); |
michael@0 | 105 | FillBuffer(b, CHANNEL_LENGTH, GetHighValue<AudioDataValue>()); |
michael@0 | 106 | FillBuffer(b + CHANNEL_LENGTH, CHANNEL_LENGTH, GetLowValue<AudioDataValue>()); |
michael@0 | 107 | mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 108 | mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 109 | mixer.FinishMixing(); |
michael@0 | 110 | FillBuffer(a, CHANNEL_LENGTH / 2, GetLowValue<AudioDataValue>()); |
michael@0 | 111 | FillBuffer(a + CHANNEL_LENGTH / 2, CHANNEL_LENGTH / 2, GetLowValue<AudioDataValue>()); |
michael@0 | 112 | FillBuffer(b, CHANNEL_LENGTH / 2, GetHighValue<AudioDataValue>()); |
michael@0 | 113 | FillBuffer(b + CHANNEL_LENGTH / 2, CHANNEL_LENGTH / 2, GetHighValue<AudioDataValue>()); |
michael@0 | 114 | mixer.Mix(a, 2, CHANNEL_LENGTH / 2, AUDIO_RATE); |
michael@0 | 115 | mixer.Mix(b, 2, CHANNEL_LENGTH / 2, AUDIO_RATE); |
michael@0 | 116 | mixer.FinishMixing(); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | FillBuffer(a, CHANNEL_LENGTH, GetLowValue<AudioDataValue>()); |
michael@0 | 120 | FillBuffer(b, CHANNEL_LENGTH, GetHighValue<AudioDataValue>()); |
michael@0 | 121 | |
michael@0 | 122 | { |
michael@0 | 123 | mozilla::AudioMixer mixer(MixingDone); |
michael@0 | 124 | fprintf(stderr, "Test AudioMixer variable channel count.\n"); |
michael@0 | 125 | |
michael@0 | 126 | mixer.Mix(a, 1, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 127 | mixer.Mix(b, 1, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 128 | mixer.FinishMixing(); |
michael@0 | 129 | mixer.Mix(a, 1, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 130 | mixer.Mix(b, 1, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 131 | mixer.FinishMixing(); |
michael@0 | 132 | mixer.Mix(a, 1, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 133 | mixer.Mix(b, 1, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 134 | mixer.FinishMixing(); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | { |
michael@0 | 138 | mozilla::AudioMixer mixer(MixingDone); |
michael@0 | 139 | fprintf(stderr, "Test AudioMixer variable stream count.\n"); |
michael@0 | 140 | |
michael@0 | 141 | mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 142 | mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 143 | mixer.FinishMixing(); |
michael@0 | 144 | mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 145 | mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 146 | mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 147 | mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 148 | mixer.FinishMixing(); |
michael@0 | 149 | mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 150 | mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE); |
michael@0 | 151 | mixer.FinishMixing(); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | return 0; |
michael@0 | 155 | } |
michael@0 | 156 |