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__AVFAudioEqualizer__
  27 #define __JFXMedia__AVFAudioEqualizer__
  28 
  29 #include <PipelineManagement/AudioEqualizer.h>
  30 #include <map>
  31 #include "AVFKernelProcessor.h"
  32 #include "CAAutoDisposer.h"
  33 
  34 class AVFAudioEqualizer;
  35 
  36 struct AVFEQBandHistory {
  37 public:
  38     double x1, x2;  // input history
  39     double y1, y2;  // output history
  40 
  41     AVFEQBandHistory() :
  42         x1(0.0),
  43         x2(0.0),
  44         y1(0.0),
  45         y2(0.0)
  46     {}
  47 };
  48 
  49 class AVFEqualizerBand : public CEqualizerBand {
  50 public:
  51     enum AVFEqualizerFilterType {
  52         Peak,       // Use for middle bands
  53         LowShelf,   // Use for lowest freq band
  54         HighShelf   // Use for highest freq band
  55     };
  56     AVFEqualizerBand(AVFAudioEqualizer *eq, double frequency, double bandwidth, double gain);
  57 
  58     virtual ~AVFEqualizerBand();
  59 
  60     virtual double GetCenterFrequency() {
  61         return mFrequency;
  62     }
  63 
  64     virtual void SetCenterFrequency(double centerFrequency);
  65     
  66     virtual double GetBandwidth() {
  67         return m_Bandwidth;
  68     }
  69 
  70     virtual void SetBandwidth(double bandwidth) {
  71         m_Bandwidth = bandwidth;
  72         RecalculateParams();
  73     }
  74 
  75     virtual double GetGain() {
  76         return m_Gain;
  77     }
  78 
  79     virtual void SetGain(double gain) {
  80         m_Gain = gain;
  81         RecalculateParams();
  82     }
  83 
  84     void ApplyFilter(double *inSource, double *inDest, int frameCount, int channel);
  85     void SetChannelCount(int newCount);
  86     void RecalculateParams();
  87     void SetFilterType(AVFEqualizerFilterType type);
  88 
  89 private:
  90     AVFAudioEqualizer *mEQ;
  91     bool mBypass;
  92     int mChannels;          // number of channels to process
  93     AVFEQBandHistory *mHistory; // one per channel
  94     double mFrequency;
  95     AVFEqualizerFilterType mFilterType;
  96 
  97     // We're implementing a simple biquadratic peak/notch filter (depending on gain)
  98     // We need: center frequency (Hz), sample rate (Hz), Q and gain (dB)
  99     // We are provided: center frequency (Hz), bandwidth (Hz) and gain (dB)
 100     // Sample rate is fetched from the associated audio unit
 101     double mCoefficients[5];
 102 
 103     void SetupPeakFilter(double omega, double bw, double absGain);
 104     void SetupLowShelfFilter(double omega, double bw, double absGain);
 105     void SetupHighShelfFilter(double omega, double bw, double absGain);
 106 };
 107 
 108 typedef std::map<double,AVFEqualizerBand*> AVFEQBandMap;
 109 typedef AVFEQBandMap::iterator AVFEQBandIterator;
 110 
 111 // Simple bridge class that forwards messages to it's AVFMediaPlayer
 112 class AVFAudioEqualizer : public AVFKernelProcessor, public CAudioEqualizer {
 113 public:
 114     AVFAudioEqualizer() :
 115         AVFKernelProcessor(),
 116         CAudioEqualizer(),
 117         mEnabled(false),
 118         mEQBands(),
 119         mEQBufferSize(0),
 120         mEQBufferA(),
 121         mEQBufferB()
 122     {}
 123     
 124     virtual ~AVFAudioEqualizer();
 125 
 126     virtual AUKernelBase *NewKernel();
 127 
 128     virtual bool IsEnabled();
 129     virtual void SetEnabled(bool isEnabled);
 130     virtual int GetNumBands();
 131     virtual CEqualizerBand *AddBand(double frequency, double bandwidth, double gain);
 132     virtual bool RemoveBand(double frequency);
 133 
 134     void MoveBand(double oldFrequency, double newFrequency);
 135 
 136     virtual void SetAudioUnit(AUEffectBase *unit);
 137 
 138     double GetSampleRate() {
 139         if (mAudioUnit) {
 140             return (double)mAudioUnit->GetSampleRate();
 141         }
 142         return 0.0;
 143     }
 144 
 145     int GetChannelCount() {
 146         if (mAudioUnit) {
 147             return mAudioUnit->GetNumberOfChannels();
 148         }
 149         return 0;
 150     }
 151 
 152     void RunFilter(const Float32 *inSourceP, Float32 *inDestP, UInt32 inFramesToProcess, UInt32 channel);
 153 
 154 private:
 155     bool mEnabled;
 156     AVFEQBandMap mEQBands;
 157     int mEQBufferSize;
 158     CAAutoFree<double> mEQBufferA; // temp storage since we have to process out of line
 159     CAAutoFree<double> mEQBufferB;
 160 
 161     // Call this after adding, removing or reordering bands
 162     void ResetBandParameters();
 163 };
 164 
 165 #endif /* defined(__JFXMedia__AVFAudioEqualizer__) */