1 /*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #ifndef __JFXMedia__AVFAudioSpectrumUnit__
27 #define __JFXMedia__AVFAudioSpectrumUnit__
28
29 #include <AudioUnit/AudioUnit.h>
30 #include "PipelineManagement/AudioSpectrum.h"
31 #include "AVFKernelProcessor.h"
32 #include "CASpectralProcessor.h"
33 #include "CAStreamBasicDescription.h"
34 #include "CAAutoDisposer.h"
35
36 // Defaults, these match the current defaults in JavaFX which get set anyways
37 // but we can optimize things a bit here...
38 #define kDefaultAudioSpectrumUpdateInterval 0.1 // every 1/10 second
39 #define kDefaultAudioSpectrumThreshold -60.0 // -60 dB
40
41 /*
42 * Callback proc invoked by the audio spectrum unit. This call is made periodically
43 * depending on the requested update interval. The band data is updated out-of-line.
44 *
45 * callbackContext: user specified context pointer
46 * timeStamp: the beginning time in seconds of the sample period (from beginning of stream)
47 * duration: the length of time in seconds of the sample period
48 */
49 typedef void (*AVFSpectrumUnitCallbackProc)(void *callbackContext, double duration);
50
51 class AVFAudioSpectrumUnit : public AVFKernelProcessor, public CAudioSpectrum {
52 public:
53 AVFAudioSpectrumUnit();
54 virtual ~AVFAudioSpectrumUnit();
55
76 virtual void SetThreshold(int threshold);
77
78 virtual void UpdateBands(int size, const float* magnitudes, const float* phases);
79
80 void Reset();
81 void SetSampleRate(Float32 rate);
82 void SetChannelCount(int count);
83
84 void SetSpectrumCallbackProc(AVFSpectrumUnitCallbackProc proc, void *context) {
85 mSpectrumCallbackProc = proc;
86 mSpectrumCallbackContext = context;
87 }
88
89 // Called by the spectrum processor, do not call
90 void SpectralFunction(SpectralBufferList* inSpectra);
91
92 private:
93 AVFSpectrumUnitCallbackProc mSpectrumCallbackProc;
94 void *mSpectrumCallbackContext;
95 bool mEnabled;
96 int mBandCount;
97 CBandsHolder *mBands;
98 double mUpdateInterval;
99 Float32 mThreshold;
100 CASpectralProcessor *mProcessor;
101
102 AudioBufferList mMixBuffer;
103 int mMixBufferFrameCapacity; // number of frames that can currently be stored in mix buffer
104
105 UInt32 mSamplesPerInterval;
106 UInt32 mFFTSize; // number of samples per FFT
107 UInt32 mFFTsPerInterval; // integral number of FFTs per update interval
108 UInt32 mFFTCount; // number of FFTs performed since last update
109
110 CAAutoFree<Float32> mWorkBuffer; // temp vectors for calculations
111 CAAutoFree<Float32> mMagnitudes; // magnitude accumulator
112 CAAutoFree<Float32> mPhases; // phase accumulator
113
114 bool mRebuildCrunch; // atomic lock avoidance...
115 CASpectralProcessor *mSpectralCrunch;
116
117 void SetupSpectralProcessor();
118 };
119
120 #endif /* defined(__JFXMedia__AVFAudioSpectrumUnit__) */
|
1 /*
2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #ifndef __JFXMedia__AVFAudioSpectrumUnit__
27 #define __JFXMedia__AVFAudioSpectrumUnit__
28
29 #include <AudioUnit/AudioUnit.h>
30
31 #include <pthread.h>
32 #include <memory>
33
34 #include "PipelineManagement/AudioSpectrum.h"
35 #include "AVFKernelProcessor.h"
36 #include "CASpectralProcessor.h"
37 #include "CAStreamBasicDescription.h"
38 #include "CAAutoDisposer.h"
39
40
41 // Defaults, these match the current defaults in JavaFX which get set anyways
42 // but we can optimize things a bit here...
43 #define kDefaultAudioSpectrumUpdateInterval 0.1 // every 1/10 second
44 #define kDefaultAudioSpectrumThreshold -60.0 // -60 dB
45
46 /*
47 * Callback proc invoked by the audio spectrum unit. This call is made periodically
48 * depending on the requested update interval. The band data is updated out-of-line.
49 *
50 * callbackContext: user specified context pointer
51 * timeStamp: the beginning time in seconds of the sample period (from beginning of stream)
52 * duration: the length of time in seconds of the sample period
53 */
54 typedef void (*AVFSpectrumUnitCallbackProc)(void *callbackContext, double duration);
55
56 class AVFAudioSpectrumUnit : public AVFKernelProcessor, public CAudioSpectrum {
57 public:
58 AVFAudioSpectrumUnit();
59 virtual ~AVFAudioSpectrumUnit();
60
81 virtual void SetThreshold(int threshold);
82
83 virtual void UpdateBands(int size, const float* magnitudes, const float* phases);
84
85 void Reset();
86 void SetSampleRate(Float32 rate);
87 void SetChannelCount(int count);
88
89 void SetSpectrumCallbackProc(AVFSpectrumUnitCallbackProc proc, void *context) {
90 mSpectrumCallbackProc = proc;
91 mSpectrumCallbackContext = context;
92 }
93
94 // Called by the spectrum processor, do not call
95 void SpectralFunction(SpectralBufferList* inSpectra);
96
97 private:
98 AVFSpectrumUnitCallbackProc mSpectrumCallbackProc;
99 void *mSpectrumCallbackContext;
100 bool mEnabled;
101
102 pthread_mutex_t mBandLock; // prevent bands from disappearing while we're processing
103 int mBandCount;
104 CBandsHolder *mBands;
105 double mUpdateInterval;
106 Float32 mThreshold;
107 CASpectralProcessor *mProcessor;
108
109 AudioBufferList mMixBuffer;
110 int mMixBufferFrameCapacity; // number of frames that can currently be stored in mix buffer
111
112 UInt32 mSamplesPerInterval;
113 UInt32 mFFTSize; // number of samples per FFT
114 UInt32 mFFTsPerInterval; // integral number of FFTs per update interval
115 UInt32 mFFTCount; // number of FFTs performed since last update
116
117 CAAutoFree<Float32> mWorkBuffer; // temp vectors for calculations
118 CAAutoFree<Float32> mMagnitudes; // magnitude accumulator
119 CAAutoFree<Float32> mPhases; // phase accumulator
120
121
122 bool mRebuildCrunch;
123 CASpectralProcessor *mSpectralCrunch;
124
125 void lockBands() {
126 pthread_mutex_lock(&mBandLock);
127 }
128
129 void unlockBands() {
130 pthread_mutex_unlock(&mBandLock);
131 }
132
133 void SetupSpectralProcessor();
134 };
135
136 typedef std::shared_ptr<AVFAudioSpectrumUnit> AVFAudioSpectrumUnitPtr;
137
138 #endif /* defined(__JFXMedia__AVFAudioSpectrumUnit__) */
|