OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 #include "platform/audio/AudioChannel.h" | 32 #include "platform/audio/AudioChannel.h" |
33 #include "wtf/Noncopyable.h" | 33 #include "wtf/Noncopyable.h" |
34 #include "wtf/ThreadSafeRefCounted.h" | 34 #include "wtf/ThreadSafeRefCounted.h" |
35 #include "wtf/Vector.h" | 35 #include "wtf/Vector.h" |
36 #include <memory> | 36 #include <memory> |
37 | 37 |
38 namespace blink { | 38 namespace blink { |
39 | 39 |
40 // An AudioBus represents a collection of one or more AudioChannels. | 40 // An AudioBus represents a collection of one or more AudioChannels. |
41 // The data layout is "planar" as opposed to "interleaved". | 41 // The data layout is "planar" as opposed to "interleaved". An AudioBus with |
42 // An AudioBus with one channel is mono, an AudioBus with two channels is stereo
, etc. | 42 // one channel is mono, an AudioBus with two channels is stereo, etc. |
43 class PLATFORM_EXPORT AudioBus : public ThreadSafeRefCounted<AudioBus> { | 43 class PLATFORM_EXPORT AudioBus : public ThreadSafeRefCounted<AudioBus> { |
44 WTF_MAKE_NONCOPYABLE(AudioBus); | 44 WTF_MAKE_NONCOPYABLE(AudioBus); |
45 | 45 |
46 public: | 46 public: |
47 enum { | 47 enum { |
48 ChannelLeft = 0, | 48 ChannelLeft = 0, |
49 ChannelRight = 1, | 49 ChannelRight = 1, |
50 ChannelCenter = 2, // center and mono are the same | 50 ChannelCenter = 2, // center and mono are the same |
51 ChannelMono = 2, | 51 ChannelMono = 2, |
52 ChannelLFE = 3, | 52 ChannelLFE = 3, |
53 ChannelSurroundLeft = 4, | 53 ChannelSurroundLeft = 4, |
54 ChannelSurroundRight = 5, | 54 ChannelSurroundRight = 5, |
55 }; | 55 }; |
56 | 56 |
57 enum { | 57 enum { |
58 LayoutCanonical = 0 | 58 LayoutCanonical = 0 |
59 // Can define non-standard layouts here | 59 // Can define non-standard layouts here |
60 }; | 60 }; |
61 | 61 |
62 enum ChannelInterpretation { | 62 enum ChannelInterpretation { |
63 Speakers, | 63 Speakers, |
64 Discrete, | 64 Discrete, |
65 }; | 65 }; |
66 | 66 |
67 // allocate indicates whether or not to initially have the AudioChannels creat
ed with managed storage. | 67 // allocate indicates whether or not to initially have the AudioChannels |
68 // Normal usage is to pass true here, in which case the AudioChannels will mem
ory-manage their own storage. | 68 // created with managed storage. Normal usage is to pass true here, in which |
69 // If allocate is false then setChannelMemory() has to be called later on for
each channel before the AudioBus is useable... | 69 // case the AudioChannels will memory-manage their own storage. If allocate |
| 70 // is false then setChannelMemory() has to be called later on for each |
| 71 // channel before the AudioBus is useable... |
70 static PassRefPtr<AudioBus> create(unsigned numberOfChannels, | 72 static PassRefPtr<AudioBus> create(unsigned numberOfChannels, |
71 size_t length, | 73 size_t length, |
72 bool allocate = true); | 74 bool allocate = true); |
73 | 75 |
74 // Tells the given channel to use an externally allocated buffer. | 76 // Tells the given channel to use an externally allocated buffer. |
75 void setChannelMemory(unsigned channelIndex, float* storage, size_t length); | 77 void setChannelMemory(unsigned channelIndex, float* storage, size_t length); |
76 | 78 |
77 // Channels | 79 // Channels |
78 unsigned numberOfChannels() const { return m_channels.size(); } | 80 unsigned numberOfChannels() const { return m_channels.size(); } |
79 | 81 |
(...skipping 27 matching lines...) Expand all Loading... |
107 // Returns true if the channel count and frame-size match. | 109 // Returns true if the channel count and frame-size match. |
108 bool topologyMatches(const AudioBus& sourceBus) const; | 110 bool topologyMatches(const AudioBus& sourceBus) const; |
109 | 111 |
110 // Creates a new buffer from a range in the source buffer. | 112 // Creates a new buffer from a range in the source buffer. |
111 // 0 may be returned if the range does not fit in the sourceBuffer | 113 // 0 may be returned if the range does not fit in the sourceBuffer |
112 static PassRefPtr<AudioBus> createBufferFromRange( | 114 static PassRefPtr<AudioBus> createBufferFromRange( |
113 const AudioBus* sourceBuffer, | 115 const AudioBus* sourceBuffer, |
114 unsigned startFrame, | 116 unsigned startFrame, |
115 unsigned endFrame); | 117 unsigned endFrame); |
116 | 118 |
117 // Creates a new AudioBus by sample-rate converting sourceBus to the newSample
Rate. | 119 // Creates a new AudioBus by sample-rate converting sourceBus to the |
| 120 // newSampleRate. |
118 // setSampleRate() must have been previously called on sourceBus. | 121 // setSampleRate() must have been previously called on sourceBus. |
119 // Note: sample-rate conversion is already handled in the file-reading code fo
r the mac port, so we don't need this. | 122 // Note: sample-rate conversion is already handled in the file-reading code |
| 123 // for the mac port, so we don't need this. |
120 static PassRefPtr<AudioBus> createBySampleRateConverting( | 124 static PassRefPtr<AudioBus> createBySampleRateConverting( |
121 const AudioBus* sourceBus, | 125 const AudioBus* sourceBus, |
122 bool mixToMono, | 126 bool mixToMono, |
123 double newSampleRate); | 127 double newSampleRate); |
124 | 128 |
125 // Creates a new AudioBus by mixing all the channels down to mono. | 129 // Creates a new AudioBus by mixing all the channels down to mono. |
126 // If sourceBus is already mono, then the returned AudioBus will simply be a c
opy. | 130 // If sourceBus is already mono, then the returned AudioBus will simply be a |
| 131 // copy. |
127 static PassRefPtr<AudioBus> createByMixingToMono(const AudioBus* sourceBus); | 132 static PassRefPtr<AudioBus> createByMixingToMono(const AudioBus* sourceBus); |
128 | 133 |
129 // Scales all samples by the same amount. | 134 // Scales all samples by the same amount. |
130 void scale(float scale); | 135 void scale(float scale); |
131 | 136 |
132 void reset() { m_isFirstTime = true; } // for de-zippering | 137 void reset() { m_isFirstTime = true; } // for de-zippering |
133 | 138 |
134 // Copies the samples from the source bus to this one. | 139 // Copies the samples from the source bus to this one. |
135 // This is just a simple per-channel copy if the number of channels match, oth
erwise an up-mix or down-mix is done. | 140 // This is just a simple per-channel copy if the number of channels match, |
| 141 // otherwise an up-mix or down-mix is done. |
136 void copyFrom(const AudioBus& sourceBus, ChannelInterpretation = Speakers); | 142 void copyFrom(const AudioBus& sourceBus, ChannelInterpretation = Speakers); |
137 | 143 |
138 // Sums the samples from the source bus to this one. | 144 // Sums the samples from the source bus to this one. |
139 // This is just a simple per-channel summing if the number of channels match,
otherwise an up-mix or down-mix is done. | 145 // This is just a simple per-channel summing if the number of channels match, |
| 146 // otherwise an up-mix or down-mix is done. |
140 void sumFrom(const AudioBus& sourceBus, ChannelInterpretation = Speakers); | 147 void sumFrom(const AudioBus& sourceBus, ChannelInterpretation = Speakers); |
141 | 148 |
142 // Copy each channel from sourceBus into our corresponding channel. | 149 // Copy each channel from sourceBus into our corresponding channel. |
143 // We scale by targetGain (and our own internal gain m_busGain), performing "d
e-zippering" to smoothly change from *lastMixGain to (targetGain*m_busGain). | 150 // We scale by targetGain (and our own internal gain m_busGain), performing |
144 // The caller is responsible for setting up lastMixGain to point to storage wh
ich is unique for every "stream" which will be applied to this bus. | 151 // "de-zippering" to smoothly change from *lastMixGain to |
| 152 // (targetGain*m_busGain). The caller is responsible for setting up |
| 153 // lastMixGain to point to storage which is unique for every "stream" which |
| 154 // will be applied to this bus. |
145 // This represents the dezippering memory. | 155 // This represents the dezippering memory. |
146 void copyWithGainFrom(const AudioBus& sourceBus, | 156 void copyWithGainFrom(const AudioBus& sourceBus, |
147 float* lastMixGain, | 157 float* lastMixGain, |
148 float targetGain); | 158 float targetGain); |
149 | 159 |
150 // Copies the sourceBus by scaling with sample-accurate gain values. | 160 // Copies the sourceBus by scaling with sample-accurate gain values. |
151 void copyWithSampleAccurateGainValuesFrom(const AudioBus& sourceBus, | 161 void copyWithSampleAccurateGainValuesFrom(const AudioBus& sourceBus, |
152 float* gainValues, | 162 float* gainValues, |
153 unsigned numberOfGainValues); | 163 unsigned numberOfGainValues); |
154 | 164 |
155 // Returns maximum absolute value across all channels (useful for normalizatio
n). | 165 // Returns maximum absolute value across all channels (useful for |
| 166 // normalization). |
156 float maxAbsValue() const; | 167 float maxAbsValue() const; |
157 | 168 |
158 // Makes maximum absolute value == 1.0 (if possible). | 169 // Makes maximum absolute value == 1.0 (if possible). |
159 void normalize(); | 170 void normalize(); |
160 | 171 |
161 static PassRefPtr<AudioBus> loadPlatformResource(const char* name, | 172 static PassRefPtr<AudioBus> loadPlatformResource(const char* name, |
162 float sampleRate); | 173 float sampleRate); |
163 | 174 |
164 protected: | 175 protected: |
165 AudioBus() {} | 176 AudioBus() {} |
(...skipping 12 matching lines...) Expand all Loading... |
178 int m_layout; | 189 int m_layout; |
179 float m_busGain; | 190 float m_busGain; |
180 std::unique_ptr<AudioFloatArray> m_dezipperGainValues; | 191 std::unique_ptr<AudioFloatArray> m_dezipperGainValues; |
181 bool m_isFirstTime; | 192 bool m_isFirstTime; |
182 float m_sampleRate; // 0.0 if unknown or N/A | 193 float m_sampleRate; // 0.0 if unknown or N/A |
183 }; | 194 }; |
184 | 195 |
185 } // namespace blink | 196 } // namespace blink |
186 | 197 |
187 #endif // AudioBus_h | 198 #endif // AudioBus_h |
OLD | NEW |