diff --git a/OpenAudio_ArduinoLibrary.h b/OpenAudio_ArduinoLibrary.h index c64791a..23e9a3b 100644 --- a/OpenAudio_ArduinoLibrary.h +++ b/OpenAudio_ArduinoLibrary.h @@ -22,6 +22,7 @@ #include "play_queue_f32.h" #include "record_queue_f32.h" #include "filter_moog_f32.h" +#include "filter_moog.h" #include "synth_pinknoise_f32.h" #include "synth_sine_f32.h" #include "synth_waveform_F32.h" diff --git a/filter_moog.cpp b/filter_moog.cpp new file mode 100644 index 0000000..b4ebace --- /dev/null +++ b/filter_moog.cpp @@ -0,0 +1,105 @@ +/* Audio Library for Teensy 3.X + * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com + * + * Development of this audio library was funded by PJRC.COM, LLC by sales of + * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop + * open source software by purchasing Teensy or other PJRC products. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice, development funding notice, and this permission + * notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "filter_moog.h" + +#if defined(KINETISK) + +void AudioFilterMoog::update_fixed(const int16_t *in,int16_t *lp) +{ + const float MAX_INT = 32678.0; + for (int i = 0; i < AUDIO_BLOCK_SAMPLES; i++) { + float cs = (float)in[i]; + cs=cs/MAX_INT; + cs = tanhf(cs * driv); + y_a = y_a + g * (tanhf(cs - q * ((y_d_1 + y_d)/2) - tanhf(y_a))); + y_b = y_b + g * (tanhf(y_a) - tanhf(y_b)); + y_c = y_c + g * (tanhf(y_b) - tanhf(y_c)); + y_d_1 = y_d; + y_d = y_d + g * (tanhf(y_c) - tanhf(y_d)); + cs=y_d*MAX_INT; + lp[i] = (int16_t)cs; + } +} + + +void AudioFilterMoog::update_variable(const int16_t *in,const int16_t *ctl, int16_t *lp) +{ + const float MAX_INT = 32678.0; + int over=1; + float cs = (float)ctl[0]; + cs=cs/MAX_INT; + float nf=basef*(exp2f(cs*oct)); +// Serial.println(nf); + frequency(nf,false); + update_fixed(in,lp) ; +} + + +void AudioFilterMoog::update(void) +{ + audio_block_t *input_block=NULL, *control_block=NULL; + audio_block_t *lowpass_block=NULL; + + input_block = receiveReadOnly(0); + control_block = receiveReadOnly(1); + if (!input_block) { + if (control_block) release(control_block); + return; + } + lowpass_block = allocate(); + if (!lowpass_block) { + release(input_block); + if (control_block) release(control_block); + return; + } + + if (control_block) { + update_variable(input_block->data, control_block->data, lowpass_block->data); + release(control_block); + } else { + update_fixed(input_block->data, lowpass_block->data); + } + release(input_block); + transmit(lowpass_block, 0); + release(lowpass_block); + return; +} + +#elif defined(KINETISL) + +void AudioFilterMoog::update(void) +{ + audio_block_t *block; + + block = receiveReadOnly(0); + if (block) release(block); + block = receiveReadOnly(1); + if (block) release(block); +} + +#endif + diff --git a/filter_moog.h b/filter_moog.h new file mode 100644 index 0000000..831c475 --- /dev/null +++ b/filter_moog.h @@ -0,0 +1,65 @@ + + +#ifndef filter_moog_h_ +#define filter_moog_h_ + +#include "Arduino.h" +#include "AudioStream.h" +#include "arm_math.h" + +class AudioFilterMoog: public AudioStream +{ + float g; + float q; + float driv; + float oct; + float basef; + + float y_a; + float y_b; + float y_c; + float y_d; + float y_d_1; +public: + AudioFilterMoog() : AudioStream(2, inputQueueArray) { + frequency(1000); + resonance(1); + drive(1); + octave(1); + y_a = 0; + y_b = 0; + y_c = 0; + y_d = 0; + y_d_1 = 0; + } + void resonance(float qi) { + if (qi < 0.7) qi = 0.7; + else if (qi > 5.0) qi = 5.0; + q=qi; + } + void drive(float d) { + if (d > 10.0f) d = 10.0f; + if (d < 0.1f) d = 0.1f; + driv = d; + } + void octave(float n) { + if (n < 0.0) n = 0.0; + else if (n > 6.9999) n = 6.9999; + oct=n; + } + void frequency(float freq,bool setf=true) { + if (freq < 20.0) freq = 20.0; + else if (freq > AUDIO_SAMPLE_RATE_EXACT/2.5) freq = AUDIO_SAMPLE_RATE_EXACT/2.5; + g = 1 - expf(-2 * tanf(2 * M_PI * freq/(2 * AUDIO_SAMPLE_RATE_EXACT))); + if(setf) + basef=freq; + } + + virtual void update(void); +private: + void update_fixed(const int16_t *in,int16_t *lp); + void update_variable(const int16_t *in, const int16_t *ctl, int16_t *lp); + audio_block_t *inputQueueArray[2]; +}; + +#endif