content/media/AudioSampleFormat.h

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

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 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6 #ifndef MOZILLA_AUDIOSAMPLEFORMAT_H_
michael@0 7 #define MOZILLA_AUDIOSAMPLEFORMAT_H_
michael@0 8
michael@0 9 #include "nsAlgorithm.h"
michael@0 10 #include <algorithm>
michael@0 11
michael@0 12 namespace mozilla {
michael@0 13
michael@0 14 /**
michael@0 15 * Audio formats supported in MediaStreams and media elements.
michael@0 16 *
michael@0 17 * Only one of these is supported by AudioStream, and that is determined
michael@0 18 * at compile time (roughly, FLOAT32 on desktops, S16 on mobile). Media decoders
michael@0 19 * produce that format only; queued AudioData always uses that format.
michael@0 20 */
michael@0 21 enum AudioSampleFormat
michael@0 22 {
michael@0 23 // Native-endian signed 16-bit audio samples
michael@0 24 AUDIO_FORMAT_S16,
michael@0 25 // Signed 32-bit float samples
michael@0 26 AUDIO_FORMAT_FLOAT32,
michael@0 27 // Silence: format will be chosen later
michael@0 28 AUDIO_FORMAT_SILENCE,
michael@0 29 // The format used for output by AudioStream.
michael@0 30 #ifdef MOZ_SAMPLE_TYPE_S16
michael@0 31 AUDIO_OUTPUT_FORMAT = AUDIO_FORMAT_S16
michael@0 32 #else
michael@0 33 AUDIO_OUTPUT_FORMAT = AUDIO_FORMAT_FLOAT32
michael@0 34 #endif
michael@0 35 };
michael@0 36
michael@0 37 enum {
michael@0 38 MAX_AUDIO_SAMPLE_SIZE = sizeof(float)
michael@0 39 };
michael@0 40
michael@0 41 template <AudioSampleFormat Format> class AudioSampleTraits;
michael@0 42
michael@0 43 template <> class AudioSampleTraits<AUDIO_FORMAT_FLOAT32> {
michael@0 44 public:
michael@0 45 typedef float Type;
michael@0 46 };
michael@0 47 template <> class AudioSampleTraits<AUDIO_FORMAT_S16> {
michael@0 48 public:
michael@0 49 typedef int16_t Type;
michael@0 50 };
michael@0 51
michael@0 52 typedef AudioSampleTraits<AUDIO_OUTPUT_FORMAT>::Type AudioDataValue;
michael@0 53
michael@0 54 template<typename T> class AudioSampleTypeToFormat;
michael@0 55
michael@0 56 template <> class AudioSampleTypeToFormat<float> {
michael@0 57 public:
michael@0 58 static const AudioSampleFormat Format = AUDIO_FORMAT_FLOAT32;
michael@0 59 };
michael@0 60
michael@0 61 template <> class AudioSampleTypeToFormat<short> {
michael@0 62 public:
michael@0 63 static const AudioSampleFormat Format = AUDIO_FORMAT_S16;
michael@0 64 };
michael@0 65
michael@0 66 // Single-sample conversion
michael@0 67 /*
michael@0 68 * Use "2^N" conversion since it's simple, fast, "bit transparent", used by
michael@0 69 * many other libraries and apparently behaves reasonably.
michael@0 70 * http://blog.bjornroche.com/2009/12/int-float-int-its-jungle-out-there.html
michael@0 71 * http://blog.bjornroche.com/2009/12/linearity-and-dynamic-range-in-int.html
michael@0 72 */
michael@0 73 inline float
michael@0 74 AudioSampleToFloat(float aValue)
michael@0 75 {
michael@0 76 return aValue;
michael@0 77 }
michael@0 78 inline float
michael@0 79 AudioSampleToFloat(int16_t aValue)
michael@0 80 {
michael@0 81 return aValue/32768.0f;
michael@0 82 }
michael@0 83
michael@0 84 template <typename T> T FloatToAudioSample(float aValue);
michael@0 85
michael@0 86 template <> inline float
michael@0 87 FloatToAudioSample<float>(float aValue)
michael@0 88 {
michael@0 89 return aValue;
michael@0 90 }
michael@0 91 template <> inline int16_t
michael@0 92 FloatToAudioSample<int16_t>(float aValue)
michael@0 93 {
michael@0 94 float v = aValue*32768.0f;
michael@0 95 float clamped = std::max(-32768.0f, std::min(32767.0f, v));
michael@0 96 return int16_t(clamped);
michael@0 97 }
michael@0 98
michael@0 99 // Sample buffer conversion
michael@0 100
michael@0 101 template <typename From, typename To> inline void
michael@0 102 ConvertAudioSamples(const From* aFrom, To* aTo, int aCount)
michael@0 103 {
michael@0 104 for (int i = 0; i < aCount; ++i) {
michael@0 105 aTo[i] = FloatToAudioSample<To>(AudioSampleToFloat(aFrom[i]));
michael@0 106 }
michael@0 107 }
michael@0 108 inline void
michael@0 109 ConvertAudioSamples(const int16_t* aFrom, int16_t* aTo, int aCount)
michael@0 110 {
michael@0 111 memcpy(aTo, aFrom, sizeof(*aTo)*aCount);
michael@0 112 }
michael@0 113 inline void
michael@0 114 ConvertAudioSamples(const float* aFrom, float* aTo, int aCount)
michael@0 115 {
michael@0 116 memcpy(aTo, aFrom, sizeof(*aTo)*aCount);
michael@0 117 }
michael@0 118
michael@0 119 // Sample buffer conversion with scale
michael@0 120
michael@0 121 template <typename From, typename To> inline void
michael@0 122 ConvertAudioSamplesWithScale(const From* aFrom, To* aTo, int aCount, float aScale)
michael@0 123 {
michael@0 124 if (aScale == 1.0f) {
michael@0 125 ConvertAudioSamples(aFrom, aTo, aCount);
michael@0 126 return;
michael@0 127 }
michael@0 128 for (int i = 0; i < aCount; ++i) {
michael@0 129 aTo[i] = FloatToAudioSample<To>(AudioSampleToFloat(aFrom[i])*aScale);
michael@0 130 }
michael@0 131 }
michael@0 132 inline void
michael@0 133 ConvertAudioSamplesWithScale(const int16_t* aFrom, int16_t* aTo, int aCount, float aScale)
michael@0 134 {
michael@0 135 if (aScale == 1.0f) {
michael@0 136 ConvertAudioSamples(aFrom, aTo, aCount);
michael@0 137 return;
michael@0 138 }
michael@0 139 if (0.0f <= aScale && aScale < 1.0f) {
michael@0 140 int32_t scale = int32_t((1 << 16) * aScale);
michael@0 141 for (int i = 0; i < aCount; ++i) {
michael@0 142 aTo[i] = int16_t((int32_t(aFrom[i]) * scale) >> 16);
michael@0 143 }
michael@0 144 return;
michael@0 145 }
michael@0 146 for (int i = 0; i < aCount; ++i) {
michael@0 147 aTo[i] = FloatToAudioSample<int16_t>(AudioSampleToFloat(aFrom[i])*aScale);
michael@0 148 }
michael@0 149 }
michael@0 150
michael@0 151 // In place audio sample scaling.
michael@0 152 inline void
michael@0 153 ScaleAudioSamples(float* aBuffer, int aCount, float aScale)
michael@0 154 {
michael@0 155 for (int32_t i = 0; i < aCount; ++i) {
michael@0 156 aBuffer[i] *= aScale;
michael@0 157 }
michael@0 158 }
michael@0 159
michael@0 160 inline void
michael@0 161 ScaleAudioSamples(short* aBuffer, int aCount, float aScale)
michael@0 162 {
michael@0 163 int32_t volume = int32_t((1 << 16) * aScale);
michael@0 164 for (int32_t i = 0; i < aCount; ++i) {
michael@0 165 aBuffer[i] = short((int32_t(aBuffer[i]) * volume) >> 16);
michael@0 166 }
michael@0 167 }
michael@0 168
michael@0 169 inline const void*
michael@0 170 AddAudioSampleOffset(const void* aBase, AudioSampleFormat aFormat,
michael@0 171 int32_t aOffset)
michael@0 172 {
michael@0 173 static_assert(AUDIO_FORMAT_S16 == 0, "Bad constant");
michael@0 174 static_assert(AUDIO_FORMAT_FLOAT32 == 1, "Bad constant");
michael@0 175 NS_ASSERTION(aFormat == AUDIO_FORMAT_S16 || aFormat == AUDIO_FORMAT_FLOAT32,
michael@0 176 "Unknown format");
michael@0 177
michael@0 178 return static_cast<const uint8_t*>(aBase) + (aFormat + 1)*2*aOffset;
michael@0 179 }
michael@0 180
michael@0 181 } // namespace mozilla
michael@0 182
michael@0 183 #endif /* MOZILLA_AUDIOSAMPLEFORMAT_H_ */

mercurial