// ****** Start Of Included File:theMixer.h ****** /* 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. */ #ifndef themixer_h_ #define themixer_h_ #include #include //#define AudioMixer4 AudioMixer<4> #if defined(__ARM_ARCH_7EM__) #define MULTI_UNITYGAIN 65536 #define MULTI_UNITYGAIN_F 65536.0f #define MAX_GAIN 32767.0f #define MIN_GAIN -32767.0f #define MULT_DATA_TYPE int32_t #elif defined(KINETISL) #define MULTI_UNITYGAIN 256 #define MULTI_UNITYGAIN_F 256.0f #define MAX_GAIN 127.0f #define MIN_GAIN -127.0f #define MULT_DATA_TYPE int16_t #endif template class AudioMixer : public AudioStream { public: AudioMixer(void) : AudioStream(NN, inputQueueArray) { for (int i=0; i> 8); // overflow possible?? *dst++ = signed_saturate_rshift(val, 16, 0); } while (dst < end); } } #endif template void AudioMixer::gain(unsigned int channel, float gain) { if (channel >= NN) return; if (gain > MAX_GAIN) gain = MAX_GAIN; else if (gain < MIN_GAIN) gain = MIN_GAIN; multiplier[channel] = gain * MULTI_UNITYGAIN_F; // TODO: proper roundoff? } template void AudioMixer::gain(float gain) { for (int i = 0; i < NN; i++) { if (gain > MAX_GAIN) gain = MAX_GAIN; else if (gain < MIN_GAIN) gain = MIN_GAIN; multiplier[i] = gain * MULTI_UNITYGAIN_F; // TODO: proper roundoff? } } template void AudioMixer::update() { audio_block_t *in, *out=NULL; unsigned int channel; for (channel=0; channel < NN; channel++) { if (!out) { out = receiveWritable(channel); if (out) { int32_t mult = multiplier[channel]; if (mult != MULTI_UNITYGAIN) applyGain(out->data, mult); } } else { in = receiveReadOnly(channel); if (in) { applyGainThenAdd(out->data, in->data, multiplier[channel]); release(in); } } } if (out) { transmit(out); release(out); } } // this class and function forces include // of functions applyGainThenAdd and applyGain used by the template class DummyClass { public: virtual void dummyFunction(); }; void DummyClass::dummyFunction() { applyGainThenAdd(0, 0, 0); applyGain(0,0); } #endif // ****** End Of Included file:theMixer.h ******