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 56 // We'll use ProcessBufferLists as it sends all channels at once instead 57 // of individual channels 58 virtual OSStatus ProcessBufferLists(AudioUnitRenderActionFlags& ioActionFlags, 59 const AudioBufferList& inBuffer, 60 AudioBufferList& outBuffer, 61 UInt32 inFramesToProcess); 62 63 virtual void StreamFormatChanged(const CAStreamBasicDescription &newFormat); 64 65 // Parameter accessors 66 virtual bool IsEnabled(); 67 virtual void SetEnabled(bool isEnabled); 68 69 virtual void SetBands(int bands, CBandsHolder* holder); 70 virtual size_t GetBands(); 71 72 virtual double GetInterval(); 73 virtual void SetInterval(double interval); 74 75 virtual int GetThreshold(); 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__) */