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@626: void InitEar(const int n_ch, const FPType fs, const FloatArray& pole_freqs, alexbrandmeyer@626: const CARParams& car_params, const IHCParams& ihc_params, alexbrandmeyer@626: const AGCParams& agc_params); alexbrandmeyer@610: // These three methods apply the different stages of the model in sequence alexbrandmeyer@610: // to individual audio samples. alexbrandmeyer@626: void CARStep(const FPType input, FloatArray* car_out); alexbrandmeyer@626: void IHCStep(const FloatArray& car_out, FloatArray* ihc_out); alexbrandmeyer@626: bool AGCStep(const FloatArray& ihc_out); alexbrandmeyer@626: // These accessor functions return portions of the CAR state for storage in alexbrandmeyer@626: // the CAROutput structures. alexbrandmeyer@626: const FloatArray& za_memory() { return car_state_.za_memory_; } alexbrandmeyer@626: const FloatArray& zb_memory() { return car_state_.zb_memory_; } alexbrandmeyer@626: const FloatArray& g_memory() { return car_state_.g_memory_; } alexbrandmeyer@626: const FloatArray& dzb_memory() { return car_state_.dzb_memory_; } alexbrandmeyer@626: // These accessor functions return CAR coefficients. alexbrandmeyer@626: const FloatArray& zr_coeffs() { return car_coeffs_.zr_coeffs_; } alexbrandmeyer@626: // These accessor functions return portions of the AGC state during the cross alexbrandmeyer@610: // coupling of the ears. alexbrandmeyer@626: const int agc_nstages() { return agc_coeffs_.size(); } alexbrandmeyer@626: const int agc_decim_phase(const int stage) { alexbrandmeyer@626: return agc_state_[stage].decim_phase_; } alexbrandmeyer@626: const FPType agc_mix_coeff(const int stage) { alexbrandmeyer@626: return agc_coeffs_[stage].agc_mix_coeffs_; } alexbrandmeyer@626: const FloatArray& agc_memory(const int stage) { alexbrandmeyer@626: return agc_state_[stage].agc_memory_; } alexbrandmeyer@626: const int agc_decimation(const int stage) { alexbrandmeyer@626: return agc_coeffs_[stage].decimation_; } alexbrandmeyer@610: // This returns the stage G value during the closing of the AGC loop. alexbrandmeyer@626: FloatArray StageGValue(const FloatArray& undamping); alexbrandmeyer@610: // This function sets the AGC memory during the cross coupling stage. alexbrandmeyer@626: void set_agc_memory(const int stage, const FloatArray& new_values) { alexbrandmeyer@626: agc_state_[stage].agc_memory_ = new_values; } alexbrandmeyer@626: // These are the setter functions for the CAR memory states. alexbrandmeyer@626: void set_dzb_memory(const FloatArray& new_values) { alexbrandmeyer@626: car_state_.dzb_memory_ = new_values; } alexbrandmeyer@626: void set_dg_memory(const FloatArray& new_values) { alexbrandmeyer@626: car_state_.dg_memory_ = new_values; } alexbrandmeyer@626: alexbrandmeyer@610: private: 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@626: void OHCNonlinearFunction(const FloatArray& velocities, alexbrandmeyer@626: FloatArray* nonlinear_fun); alexbrandmeyer@626: bool AGCRecurse(const int stage, FloatArray agc_in); alexbrandmeyer@626: FloatArray AGCSpatialSmooth(const int stage, FloatArray stage_state); alexbrandmeyer@626: FloatArray AGCSmoothDoubleExponential(FloatArray stage_state, alexbrandmeyer@626: const FPType pole_z1, alexbrandmeyer@626: const 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@626: std::vector agc_coeffs_; alexbrandmeyer@609: CARState car_state_; alexbrandmeyer@609: IHCState ihc_state_; alexbrandmeyer@626: std::vector agc_state_; alexbrandmeyer@610: int n_ch_; alexbrandmeyer@609: }; alexbrandmeyer@609: alexbrandmeyer@610: #endif