alexbrandmeyer@609: // alexbrandmeyer@609: // ear.h alexbrandmeyer@609: // CARFAC Open Source C++ Library alexbrandmeyer@609: // alexbrandmeyer@609: // Created by Alex Brandmeyer on 5/10/13. alexbrandmeyer@609: // alexbrandmeyer@609: // This C++ file is part of an implementation of Lyon's cochlear model: alexbrandmeyer@609: // "Cascade of Asymmetric Resonators with Fast-Acting Compression" alexbrandmeyer@609: // to supplement Lyon's upcoming book "Human and Machine Hearing" alexbrandmeyer@609: // alexbrandmeyer@609: // Licensed under the Apache License, Version 2.0 (the "License"); alexbrandmeyer@609: // you may not use this file except in compliance with the License. alexbrandmeyer@609: // You may obtain a copy of the License at alexbrandmeyer@609: // alexbrandmeyer@609: // http://www.apache.org/licenses/LICENSE-2.0 alexbrandmeyer@609: // alexbrandmeyer@609: // Unless required by applicable law or agreed to in writing, software alexbrandmeyer@609: // distributed under the License is distributed on an "AS IS" BASIS, alexbrandmeyer@609: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. alexbrandmeyer@609: // See the License for the specific language governing permissions and alexbrandmeyer@609: // limitations under the License. alexbrandmeyer@609: alexbrandmeyer@609: #ifndef CARFAC_Open_Source_C__Library_Ear_h alexbrandmeyer@609: #define CARFAC_Open_Source_C__Library_Ear_h alexbrandmeyer@609: alexbrandmeyer@609: #include "car_state.h" alexbrandmeyer@609: #include "ihc_state.h" alexbrandmeyer@609: #include "agc_state.h" alexbrandmeyer@609: alexbrandmeyer@609: class Ear { alexbrandmeyer@610: public: alexbrandmeyer@610: // This is the primary initialization function that is called for each alexbrandmeyer@610: // Ear object in the CARFAC 'Design' method. alexbrandmeyer@610: void InitEar(int n_ch, long fs, FloatArray pole_freqs, CARParams car_p, alexbrandmeyer@610: IHCParams ihc_p, AGCParams agc_p); alexbrandmeyer@610: // These three methods apply the different stages of the model in sequence alexbrandmeyer@610: // to individual audio samples. alexbrandmeyer@610: FloatArray CARStep(FPType input); alexbrandmeyer@610: FloatArray IHCStep(FloatArray car_out); alexbrandmeyer@610: bool AGCStep(FloatArray ihc_out); alexbrandmeyer@610: // These helper functions return portions of the CAR state for storage in the alexbrandmeyer@610: // CAROutput structures. alexbrandmeyer@610: FloatArray ReturnZAMemory(); alexbrandmeyer@610: FloatArray ReturnZBMemory(); alexbrandmeyer@610: FloatArray ReturnGMemory(); alexbrandmeyer@610: FloatArray ReturnZRCoeffs(); alexbrandmeyer@610: // These helper functions return portions of the AGC state during the cross alexbrandmeyer@610: // coupling of the ears. alexbrandmeyer@610: int ReturnAGCNStages(); alexbrandmeyer@610: int ReturnAGCStateDecimPhase(int stage); alexbrandmeyer@610: FPType ReturnAGCMixCoeff(int stage); alexbrandmeyer@610: int ReturnAGCDecimation(int stage); alexbrandmeyer@610: FloatArray ReturnAGCStateMemory(int stage); alexbrandmeyer@610: // This returns the stage G value during the closing of the AGC loop. alexbrandmeyer@610: FloatArray StageGValue(FloatArray undamping); alexbrandmeyer@610: // This function sets the AGC memory during the cross coupling stage. alexbrandmeyer@610: void SetAGCStateMemory(int stage, FloatArray new_values); alexbrandmeyer@610: // These are two functions to set the CARState dzB and dG memories when alexbrandmeyer@610: // closing the AGC loop alexbrandmeyer@610: void SetCARStateDZBMemory(FloatArray new_values); alexbrandmeyer@610: void SetCARStateDGMemory(FloatArray new_values); alexbrandmeyer@610: private: alexbrandmeyer@610: // These methods carry out the design of the coefficient sets for each of the alexbrandmeyer@610: // three model stages. alexbrandmeyer@610: void DesignFilters(CARParams car_params, long fs, FloatArray pole_freqs); alexbrandmeyer@610: void DesignIHC(IHCParams ihc_params, long fs); alexbrandmeyer@610: void DesignAGC(AGCParams agc_params, long fs); alexbrandmeyer@610: // These are the corresponding methods that initialize the model state alexbrandmeyer@610: // variables before runtime using the model coefficients. alexbrandmeyer@610: void InitIHCState(); alexbrandmeyer@610: void InitAGCState(); alexbrandmeyer@610: void InitCARState(); alexbrandmeyer@610: // These are the various helper functions called during the model runtime. alexbrandmeyer@610: FloatArray OHC_NLF(FloatArray velocities); alexbrandmeyer@610: bool AGCRecurse(int stage, FloatArray agc_in); alexbrandmeyer@610: FloatArray AGCSpatialSmooth(int stage, FloatArray stage_state); alexbrandmeyer@610: FloatArray AGCSmoothDoubleExponential(FloatArray stage_state, FPType pole_z1, alexbrandmeyer@610: FPType pole_z2); alexbrandmeyer@610: // These are the private data members that store the state and coefficient alexbrandmeyer@610: // information. alexbrandmeyer@609: CARCoeffs car_coeffs_; alexbrandmeyer@609: IHCCoeffs ihc_coeffs_; alexbrandmeyer@609: AGCCoeffs agc_coeffs_; alexbrandmeyer@609: CARState car_state_; alexbrandmeyer@609: IHCState ihc_state_; alexbrandmeyer@609: AGCState agc_state_; alexbrandmeyer@610: int n_ch_; alexbrandmeyer@609: }; alexbrandmeyer@609: alexbrandmeyer@610: #endif