annotate 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
rev   line source
rlm@0 1 /**
rlm@0 2 * OpenAL cross platform audio library
rlm@0 3 * Copyright (C) 2009 by Chris Robinson.
rlm@0 4 * This library is free software; you can redistribute it and/or
rlm@0 5 * modify it under the terms of the GNU Library General Public
rlm@0 6 * License as published by the Free Software Foundation; either
rlm@0 7 * version 2 of the License, or (at your option) any later version.
rlm@0 8 *
rlm@0 9 * This library is distributed in the hope that it will be useful,
rlm@0 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rlm@0 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
rlm@0 12 * Library General Public License for more details.
rlm@0 13 *
rlm@0 14 * You should have received a copy of the GNU Library General Public
rlm@0 15 * License along with this library; if not, write to the
rlm@0 16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rlm@0 17 * Boston, MA 02111-1307, USA.
rlm@0 18 * Or go to http://www.gnu.org/copyleft/lgpl.html
rlm@0 19 */
rlm@0 20
rlm@0 21 #include "config.h"
rlm@0 22
rlm@0 23 #include <math.h>
rlm@0 24 #include <stdlib.h>
rlm@0 25
rlm@0 26 #include "alMain.h"
rlm@0 27 #include "alFilter.h"
rlm@0 28 #include "alAuxEffectSlot.h"
rlm@0 29 #include "alError.h"
rlm@0 30 #include "alu.h"
rlm@0 31
rlm@0 32
rlm@0 33 typedef struct ALechoState {
rlm@0 34 // Must be first in all effects!
rlm@0 35 ALeffectState state;
rlm@0 36
rlm@0 37 ALfloat *SampleBuffer;
rlm@0 38 ALuint BufferLength;
rlm@0 39
rlm@0 40 // The echo is two tap. The delay is the number of samples from before the
rlm@0 41 // current offset
rlm@0 42 struct {
rlm@0 43 ALuint delay;
rlm@0 44 } Tap[2];
rlm@0 45 ALuint Offset;
rlm@0 46 // The LR gains for the first tap. The second tap uses the reverse
rlm@0 47 ALfloat GainL;
rlm@0 48 ALfloat GainR;
rlm@0 49
rlm@0 50 ALfloat FeedGain;
rlm@0 51
rlm@0 52 ALfloat Gain[MAXCHANNELS];
rlm@0 53
rlm@0 54 FILTER iirFilter;
rlm@0 55 ALfloat history[2];
rlm@0 56 } ALechoState;
rlm@0 57
rlm@0 58 static ALvoid EchoDestroy(ALeffectState *effect)
rlm@0 59 {
rlm@0 60 ALechoState *state = (ALechoState*)effect;
rlm@0 61 if(state)
rlm@0 62 {
rlm@0 63 free(state->SampleBuffer);
rlm@0 64 state->SampleBuffer = NULL;
rlm@0 65 free(state);
rlm@0 66 }
rlm@0 67 }
rlm@0 68
rlm@0 69 static ALboolean EchoDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
rlm@0 70 {
rlm@0 71 ALechoState *state = (ALechoState*)effect;
rlm@0 72 ALuint maxlen, i;
rlm@0 73
rlm@0 74 // Use the next power of 2 for the buffer length, so the tap offsets can be
rlm@0 75 // wrapped using a mask instead of a modulo
rlm@0 76 maxlen = (ALuint)(AL_ECHO_MAX_DELAY * Device->Frequency) + 1;
rlm@0 77 maxlen += (ALuint)(AL_ECHO_MAX_LRDELAY * Device->Frequency) + 1;
rlm@0 78 maxlen = NextPowerOf2(maxlen);
rlm@0 79
rlm@0 80 if(maxlen != state->BufferLength)
rlm@0 81 {
rlm@0 82 void *temp;
rlm@0 83
rlm@0 84 temp = realloc(state->SampleBuffer, maxlen * sizeof(ALfloat));
rlm@0 85 if(!temp)
rlm@0 86 return AL_FALSE;
rlm@0 87 state->SampleBuffer = temp;
rlm@0 88 state->BufferLength = maxlen;
rlm@0 89 }
rlm@0 90 for(i = 0;i < state->BufferLength;i++)
rlm@0 91 state->SampleBuffer[i] = 0.0f;
rlm@0 92
rlm@0 93 return AL_TRUE;
rlm@0 94 }
rlm@0 95
rlm@0 96 static ALvoid EchoUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffectslot *Slot)
rlm@0 97 {
rlm@0 98 ALechoState *state = (ALechoState*)effect;
rlm@0 99 ALCdevice *Device = Context->Device;
rlm@0 100 ALuint frequency = Device->Frequency;
rlm@0 101 ALfloat lrpan, cw, g, gain;
rlm@0 102 ALuint i;
rlm@0 103
rlm@0 104 state->Tap[0].delay = (ALuint)(Slot->effect.Params.Echo.Delay * frequency) + 1;
rlm@0 105 state->Tap[1].delay = (ALuint)(Slot->effect.Params.Echo.LRDelay * frequency);
rlm@0 106 state->Tap[1].delay += state->Tap[0].delay;
rlm@0 107
rlm@0 108 lrpan = Slot->effect.Params.Echo.Spread*0.5f + 0.5f;
rlm@0 109 state->GainL = aluSqrt( lrpan);
rlm@0 110 state->GainR = aluSqrt(1.0f-lrpan);
rlm@0 111
rlm@0 112 state->FeedGain = Slot->effect.Params.Echo.Feedback;
rlm@0 113
rlm@0 114 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / frequency);
rlm@0 115 g = 1.0f - Slot->effect.Params.Echo.Damping;
rlm@0 116 state->iirFilter.coeff = lpCoeffCalc(g, cw);
rlm@0 117
rlm@0 118 gain = Slot->Gain;
rlm@0 119 for(i = 0;i < MAXCHANNELS;i++)
rlm@0 120 state->Gain[i] = 0.0f;
rlm@0 121 for(i = 0;i < Device->NumChan;i++)
rlm@0 122 {
rlm@0 123 enum Channel chan = Device->Speaker2Chan[i];
rlm@0 124 state->Gain[chan] = gain;
rlm@0 125 }
rlm@0 126 }
rlm@0 127
rlm@0 128 static ALvoid EchoProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
rlm@0 129 {
rlm@0 130 ALechoState *state = (ALechoState*)effect;
rlm@0 131 const ALuint mask = state->BufferLength-1;
rlm@0 132 const ALuint tap1 = state->Tap[0].delay;
rlm@0 133 const ALuint tap2 = state->Tap[1].delay;
rlm@0 134 ALuint offset = state->Offset;
rlm@0 135 ALfloat samp[2], smp;
rlm@0 136 ALuint i;
rlm@0 137 (void)Slot;
rlm@0 138
rlm@0 139 for(i = 0;i < SamplesToDo;i++,offset++)
rlm@0 140 {
rlm@0 141 // Sample first tap
rlm@0 142 smp = state->SampleBuffer[(offset-tap1) & mask];
rlm@0 143 samp[0] = smp * state->GainL;
rlm@0 144 samp[1] = smp * state->GainR;
rlm@0 145 // Sample second tap. Reverse LR panning
rlm@0 146 smp = state->SampleBuffer[(offset-tap2) & mask];
rlm@0 147 samp[0] += smp * state->GainR;
rlm@0 148 samp[1] += smp * state->GainL;
rlm@0 149
rlm@0 150 // Apply damping and feedback gain to the second tap, and mix in the
rlm@0 151 // new sample
rlm@0 152 smp = lpFilter2P(&state->iirFilter, 0, smp+SamplesIn[i]);
rlm@0 153 state->SampleBuffer[offset&mask] = smp * state->FeedGain;
rlm@0 154
rlm@0 155 SamplesOut[i][FRONT_LEFT] += state->Gain[FRONT_LEFT] * samp[0];
rlm@0 156 SamplesOut[i][FRONT_RIGHT] += state->Gain[FRONT_RIGHT] * samp[1];
rlm@0 157 SamplesOut[i][SIDE_LEFT] += state->Gain[SIDE_LEFT] * samp[0];
rlm@0 158 SamplesOut[i][SIDE_RIGHT] += state->Gain[SIDE_RIGHT] * samp[1];
rlm@0 159 SamplesOut[i][BACK_LEFT] += state->Gain[BACK_LEFT] * samp[0];
rlm@0 160 SamplesOut[i][BACK_RIGHT] += state->Gain[BACK_RIGHT] * samp[1];
rlm@0 161 }
rlm@0 162 state->Offset = offset;
rlm@0 163 }
rlm@0 164
rlm@0 165 ALeffectState *EchoCreate(void)
rlm@0 166 {
rlm@0 167 ALechoState *state;
rlm@0 168
rlm@0 169 state = malloc(sizeof(*state));
rlm@0 170 if(!state)
rlm@0 171 return NULL;
rlm@0 172
rlm@0 173 state->state.Destroy = EchoDestroy;
rlm@0 174 state->state.DeviceUpdate = EchoDeviceUpdate;
rlm@0 175 state->state.Update = EchoUpdate;
rlm@0 176 state->state.Process = EchoProcess;
rlm@0 177
rlm@0 178 state->BufferLength = 0;
rlm@0 179 state->SampleBuffer = NULL;
rlm@0 180
rlm@0 181 state->Tap[0].delay = 0;
rlm@0 182 state->Tap[1].delay = 0;
rlm@0 183 state->Offset = 0;
rlm@0 184 state->GainL = 0.0f;
rlm@0 185 state->GainR = 0.0f;
rlm@0 186
rlm@0 187 state->iirFilter.coeff = 0.0f;
rlm@0 188 state->iirFilter.history[0] = 0.0f;
rlm@0 189 state->iirFilter.history[1] = 0.0f;
rlm@0 190
rlm@0 191 return &state->state;
rlm@0 192 }