24 */
25
26 #include "AVFAudioSpectrumUnit.h"
27
28 #include <iostream>
29 #include <Accelerate/Accelerate.h>
30
31 AVFAudioSpectrumUnit::AVFAudioSpectrumUnit() : mSpectrumCallbackProc(NULL),
32 mSpectrumCallbackContext(NULL),
33 mEnabled(true),
34 mBandCount(128),
35 mBands(NULL),
36 mUpdateInterval(kDefaultAudioSpectrumUpdateInterval),
37 mThreshold(kDefaultAudioSpectrumThreshold),
38 mMixBufferFrameCapacity(0),
39 mSampleRate(0),
40 mChannels(0),
41 mMaxFrames(0),
42 mSamplesPerInterval(0),
43 mRebuildCrunch(true),
44 mSpectrumElement(NULL),
45 mSpectrum(NULL) {
46 mMixBuffer.mNumberBuffers = 1;
47 mMixBuffer.mBuffers[0].mData = NULL;
48
49 pthread_mutex_init(&mBandLock, NULL);
50
51 gst_init_check(NULL, NULL, NULL);
52 }
53
54 AVFAudioSpectrumUnit::~AVFAudioSpectrumUnit() {
55 if (mMixBuffer.mBuffers[0].mData) {
56 free(mMixBuffer.mBuffers[0].mData);
57 mMixBuffer.mBuffers[0].mData = NULL;
58 }
59
60 ReleaseSpectralProcessor();
61 }
62
63 bool AVFAudioSpectrumUnit::ProcessBufferLists(const AudioBufferList& inBuffer,
174 if (mThreshold != (Float32) threshold) {
175 mThreshold = (Float32) threshold;
176 mRebuildCrunch = true;
177 }
178 }
179
180 void AVFAudioSpectrumUnit::UpdateBands(int size, const float* magnitudes, const float* phases) {
181 // lock now otherwise the bands could change while we're processing
182 lockBands();
183 if (!mBands || size <= 0 || !mEnabled) {
184 unlockBands();
185 return;
186 }
187
188 // Update band data
189 mBands->UpdateBands(size, magnitudes, magnitudes);
190
191 // Call our listener to dispatch the spectrum event
192 if (mSpectrumCallbackProc) {
193 double duration = (double) mSamplesPerInterval / (double) 44100;
194 mSpectrumCallbackProc(mSpectrumCallbackContext, duration);
195 }
196
197 unlockBands();
198 }
199
200 void AVFAudioSpectrumUnit::SetSampleRate(UInt32 rate) {
201 mSampleRate = rate;
202 }
203
204 void AVFAudioSpectrumUnit::SetChannels(UInt32 count) {
205 mChannels = count;
206 }
207
208 void AVFAudioSpectrumUnit::SetMaxFrames(UInt32 maxFrames) {
209 mMaxFrames = maxFrames;
210 }
211
212 void AVFAudioSpectrumUnit::SetSpectrumCallbackProc(AVFSpectrumUnitCallbackProc proc, void *context) {
213 mSpectrumCallbackProc = proc;
214 mSpectrumCallbackContext = context;
215 }
216
217 static gboolean PostMessageCallback(GstElement * element, GstMessage * message) {
218 if (message == NULL) {
219 return FALSE;
220 }
221
222 GstSpectrum *pSpectrum = GST_SPECTRUM(element);
223 if (pSpectrum == NULL || pSpectrum->user_data == NULL) {
224 return FALSE;
225 }
226
227 AVFAudioSpectrumUnit *pSpectrumUnit = (AVFAudioSpectrumUnit*)pSpectrum->user_data;
228
229 const GstStructure *pStr = gst_message_get_structure(message);
230 if (gst_structure_has_name(pStr, "spectrum")) {
231 GstClockTime timestamp, duration;
232
233 if (!gst_structure_get_clock_time(pStr, "timestamp", ×tamp))
234 timestamp = GST_CLOCK_TIME_NONE;
235
236 if (!gst_structure_get_clock_time(pStr, "duration", &duration))
|
24 */
25
26 #include "AVFAudioSpectrumUnit.h"
27
28 #include <iostream>
29 #include <Accelerate/Accelerate.h>
30
31 AVFAudioSpectrumUnit::AVFAudioSpectrumUnit() : mSpectrumCallbackProc(NULL),
32 mSpectrumCallbackContext(NULL),
33 mEnabled(true),
34 mBandCount(128),
35 mBands(NULL),
36 mUpdateInterval(kDefaultAudioSpectrumUpdateInterval),
37 mThreshold(kDefaultAudioSpectrumThreshold),
38 mMixBufferFrameCapacity(0),
39 mSampleRate(0),
40 mChannels(0),
41 mMaxFrames(0),
42 mSamplesPerInterval(0),
43 mRebuildCrunch(true),
44 mFirstBufferDelivered(false),
45 mSpectrumElement(NULL),
46 mSpectrum(NULL) {
47 mMixBuffer.mNumberBuffers = 1;
48 mMixBuffer.mBuffers[0].mData = NULL;
49
50 pthread_mutex_init(&mBandLock, NULL);
51
52 gst_init_check(NULL, NULL, NULL);
53 }
54
55 AVFAudioSpectrumUnit::~AVFAudioSpectrumUnit() {
56 if (mMixBuffer.mBuffers[0].mData) {
57 free(mMixBuffer.mBuffers[0].mData);
58 mMixBuffer.mBuffers[0].mData = NULL;
59 }
60
61 ReleaseSpectralProcessor();
62 }
63
64 bool AVFAudioSpectrumUnit::ProcessBufferLists(const AudioBufferList& inBuffer,
175 if (mThreshold != (Float32) threshold) {
176 mThreshold = (Float32) threshold;
177 mRebuildCrunch = true;
178 }
179 }
180
181 void AVFAudioSpectrumUnit::UpdateBands(int size, const float* magnitudes, const float* phases) {
182 // lock now otherwise the bands could change while we're processing
183 lockBands();
184 if (!mBands || size <= 0 || !mEnabled) {
185 unlockBands();
186 return;
187 }
188
189 // Update band data
190 mBands->UpdateBands(size, magnitudes, magnitudes);
191
192 // Call our listener to dispatch the spectrum event
193 if (mSpectrumCallbackProc) {
194 double duration = (double) mSamplesPerInterval / (double) 44100;
195 double timestamp = mFirstBufferDelivered ? -1.0 : 0.0;
196 mSpectrumCallbackProc(mSpectrumCallbackContext, duration, timestamp);
197 }
198
199 unlockBands();
200 }
201
202 void AVFAudioSpectrumUnit::SetSampleRate(UInt32 rate) {
203 mSampleRate = rate;
204 }
205
206 void AVFAudioSpectrumUnit::SetChannels(UInt32 count) {
207 mChannels = count;
208 }
209
210 void AVFAudioSpectrumUnit::SetMaxFrames(UInt32 maxFrames) {
211 mMaxFrames = maxFrames;
212 }
213
214 void AVFAudioSpectrumUnit::SetSpectrumCallbackProc(AVFSpectrumUnitCallbackProc proc, void *context) {
215 mSpectrumCallbackProc = proc;
216 mSpectrumCallbackContext = context;
217 }
218
219 void AVFAudioSpectrumUnit::SetFirstBufferDelivered(bool isFirstBufferDelivered) {
220 mFirstBufferDelivered = isFirstBufferDelivered;
221 }
222
223 static gboolean PostMessageCallback(GstElement * element, GstMessage * message) {
224 if (message == NULL) {
225 return FALSE;
226 }
227
228 GstSpectrum *pSpectrum = GST_SPECTRUM(element);
229 if (pSpectrum == NULL || pSpectrum->user_data == NULL) {
230 return FALSE;
231 }
232
233 AVFAudioSpectrumUnit *pSpectrumUnit = (AVFAudioSpectrumUnit*)pSpectrum->user_data;
234
235 const GstStructure *pStr = gst_message_get_structure(message);
236 if (gst_structure_has_name(pStr, "spectrum")) {
237 GstClockTime timestamp, duration;
238
239 if (!gst_structure_get_clock_time(pStr, "timestamp", ×tamp))
240 timestamp = GST_CLOCK_TIME_NONE;
241
242 if (!gst_structure_get_clock_time(pStr, "duration", &duration))
|