Mercurial > audio-send
view Alc/alcEcho.c @ 32:b8bc24918d63
moved ear.org into cortex
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 01:43:33 -0700 |
parents | f9476ff7637e |
children |
line wrap: on
line source
1 /**2 * OpenAL cross platform audio library3 * Copyright (C) 2009 by Chris Robinson.4 * This library is free software; you can redistribute it and/or5 * modify it under the terms of the GNU Library General Public6 * License as published by the Free Software Foundation; either7 * version 2 of the License, or (at your option) any later version.8 *9 * This library is distributed in the hope that it will be useful,10 * but WITHOUT ANY WARRANTY; without even the implied warranty of11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12 * Library General Public License for more details.13 *14 * You should have received a copy of the GNU Library General Public15 * License along with this library; if not, write to the16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,17 * Boston, MA 02111-1307, USA.18 * Or go to http://www.gnu.org/copyleft/lgpl.html19 */21 #include "config.h"23 #include <math.h>24 #include <stdlib.h>26 #include "alMain.h"27 #include "alFilter.h"28 #include "alAuxEffectSlot.h"29 #include "alError.h"30 #include "alu.h"33 typedef struct ALechoState {34 // Must be first in all effects!35 ALeffectState state;37 ALfloat *SampleBuffer;38 ALuint BufferLength;40 // The echo is two tap. The delay is the number of samples from before the41 // current offset42 struct {43 ALuint delay;44 } Tap[2];45 ALuint Offset;46 // The LR gains for the first tap. The second tap uses the reverse47 ALfloat GainL;48 ALfloat GainR;50 ALfloat FeedGain;52 ALfloat Gain[MAXCHANNELS];54 FILTER iirFilter;55 ALfloat history[2];56 } ALechoState;58 static ALvoid EchoDestroy(ALeffectState *effect)59 {60 ALechoState *state = (ALechoState*)effect;61 if(state)62 {63 free(state->SampleBuffer);64 state->SampleBuffer = NULL;65 free(state);66 }67 }69 static ALboolean EchoDeviceUpdate(ALeffectState *effect, ALCdevice *Device)70 {71 ALechoState *state = (ALechoState*)effect;72 ALuint maxlen, i;74 // Use the next power of 2 for the buffer length, so the tap offsets can be75 // wrapped using a mask instead of a modulo76 maxlen = (ALuint)(AL_ECHO_MAX_DELAY * Device->Frequency) + 1;77 maxlen += (ALuint)(AL_ECHO_MAX_LRDELAY * Device->Frequency) + 1;78 maxlen = NextPowerOf2(maxlen);80 if(maxlen != state->BufferLength)81 {82 void *temp;84 temp = realloc(state->SampleBuffer, maxlen * sizeof(ALfloat));85 if(!temp)86 return AL_FALSE;87 state->SampleBuffer = temp;88 state->BufferLength = maxlen;89 }90 for(i = 0;i < state->BufferLength;i++)91 state->SampleBuffer[i] = 0.0f;93 return AL_TRUE;94 }96 static ALvoid EchoUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffectslot *Slot)97 {98 ALechoState *state = (ALechoState*)effect;99 ALCdevice *Device = Context->Device;100 ALuint frequency = Device->Frequency;101 ALfloat lrpan, cw, g, gain;102 ALuint i;104 state->Tap[0].delay = (ALuint)(Slot->effect.Params.Echo.Delay * frequency) + 1;105 state->Tap[1].delay = (ALuint)(Slot->effect.Params.Echo.LRDelay * frequency);106 state->Tap[1].delay += state->Tap[0].delay;108 lrpan = Slot->effect.Params.Echo.Spread*0.5f + 0.5f;109 state->GainL = aluSqrt( lrpan);110 state->GainR = aluSqrt(1.0f-lrpan);112 state->FeedGain = Slot->effect.Params.Echo.Feedback;114 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / frequency);115 g = 1.0f - Slot->effect.Params.Echo.Damping;116 state->iirFilter.coeff = lpCoeffCalc(g, cw);118 gain = Slot->Gain;119 for(i = 0;i < MAXCHANNELS;i++)120 state->Gain[i] = 0.0f;121 for(i = 0;i < Device->NumChan;i++)122 {123 enum Channel chan = Device->Speaker2Chan[i];124 state->Gain[chan] = gain;125 }126 }128 static ALvoid EchoProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])129 {130 ALechoState *state = (ALechoState*)effect;131 const ALuint mask = state->BufferLength-1;132 const ALuint tap1 = state->Tap[0].delay;133 const ALuint tap2 = state->Tap[1].delay;134 ALuint offset = state->Offset;135 ALfloat samp[2], smp;136 ALuint i;137 (void)Slot;139 for(i = 0;i < SamplesToDo;i++,offset++)140 {141 // Sample first tap142 smp = state->SampleBuffer[(offset-tap1) & mask];143 samp[0] = smp * state->GainL;144 samp[1] = smp * state->GainR;145 // Sample second tap. Reverse LR panning146 smp = state->SampleBuffer[(offset-tap2) & mask];147 samp[0] += smp * state->GainR;148 samp[1] += smp * state->GainL;150 // Apply damping and feedback gain to the second tap, and mix in the151 // new sample152 smp = lpFilter2P(&state->iirFilter, 0, smp+SamplesIn[i]);153 state->SampleBuffer[offset&mask] = smp * state->FeedGain;155 SamplesOut[i][FRONT_LEFT] += state->Gain[FRONT_LEFT] * samp[0];156 SamplesOut[i][FRONT_RIGHT] += state->Gain[FRONT_RIGHT] * samp[1];157 SamplesOut[i][SIDE_LEFT] += state->Gain[SIDE_LEFT] * samp[0];158 SamplesOut[i][SIDE_RIGHT] += state->Gain[SIDE_RIGHT] * samp[1];159 SamplesOut[i][BACK_LEFT] += state->Gain[BACK_LEFT] * samp[0];160 SamplesOut[i][BACK_RIGHT] += state->Gain[BACK_RIGHT] * samp[1];161 }162 state->Offset = offset;163 }165 ALeffectState *EchoCreate(void)166 {167 ALechoState *state;169 state = malloc(sizeof(*state));170 if(!state)171 return NULL;173 state->state.Destroy = EchoDestroy;174 state->state.DeviceUpdate = EchoDeviceUpdate;175 state->state.Update = EchoUpdate;176 state->state.Process = EchoProcess;178 state->BufferLength = 0;179 state->SampleBuffer = NULL;181 state->Tap[0].delay = 0;182 state->Tap[1].delay = 0;183 state->Offset = 0;184 state->GainL = 0.0f;185 state->GainR = 0.0f;187 state->iirFilter.coeff = 0.0f;188 state->iirFilter.history[0] = 0.0f;189 state->iirFilter.history[1] = 0.0f;191 return &state->state;192 }