147
static ALuint CalcLengths(ALuint length[13], ALuint frequency)
149
ALuint samples, totalLength, index;
151
// All line lengths are powers of 2, calculated from their lengths, with
152
// an additional sample in case of rounding errors.
154
// See VerbUpdate() for an explanation of the additional calculation
155
// added to the master line length.
157
((MASTER_LINE_LENGTH +
158
(LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER) *
159
(DECO_FRACTION * ((DECO_MULTIPLIER * DECO_MULTIPLIER *
160
DECO_MULTIPLIER) - 1.0f)))) *
162
length[0] = NextPowerOf2(samples);
163
totalLength = length[0];
164
for(index = 0;index < 4;index++)
166
samples = (ALuint)(EARLY_LINE_LENGTH[index] * frequency) + 1;
167
length[1 + index] = NextPowerOf2(samples);
168
totalLength += length[1 + index];
170
for(index = 0;index < 4;index++)
172
samples = (ALuint)(ALLPASS_LINE_LENGTH[index] * frequency) + 1;
173
length[5 + index] = NextPowerOf2(samples);
174
totalLength += length[5 + index];
176
for(index = 0;index < 4;index++)
178
samples = (ALuint)(LATE_LINE_LENGTH[index] *
179
(1.0f + LATE_LINE_MULTIPLIER) * frequency) + 1;
180
length[9 + index] = NextPowerOf2(samples);
181
totalLength += length[9 + index];
146
187
// Basic delay line input/output routines.
147
188
static __inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
367
408
return pos%LUT_NUM;
411
// This updates the device-dependant reverb state. This is called on
412
// initialization and any time the device parameters (eg. playback frequency,
413
// format) have been changed.
414
ALboolean VerbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
416
ALverbState *State = (ALverbState*)effect;
417
ALuint length[13], totalLength;
420
totalLength = CalcLengths(length, Device->Frequency);
421
if(totalLength != State->TotalLength)
425
temp = realloc(State->SampleBuffer, totalLength * sizeof(ALfloat));
428
alSetError(AL_OUT_OF_MEMORY);
431
State->TotalLength = totalLength;
432
State->SampleBuffer = temp;
434
// All lines share a single sample buffer
435
State->Delay.Mask = length[0] - 1;
436
State->Delay.Line = &State->SampleBuffer[0];
437
totalLength = length[0];
438
for(index = 0;index < 4;index++)
440
State->Early.Delay[index].Mask = length[1 + index] - 1;
441
State->Early.Delay[index].Line = &State->SampleBuffer[totalLength];
442
totalLength += length[1 + index];
444
for(index = 0;index < 4;index++)
446
State->Late.ApDelay[index].Mask = length[5 + index] - 1;
447
State->Late.ApDelay[index].Line = &State->SampleBuffer[totalLength];
448
totalLength += length[5 + index];
450
for(index = 0;index < 4;index++)
452
State->Late.Delay[index].Mask = length[9 + index] - 1;
453
State->Late.Delay[index].Line = &State->SampleBuffer[totalLength];
454
totalLength += length[9 + index];
458
for(index = 0;index < 4;index++)
460
State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
462
State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
466
for(index = 0;index < State->TotalLength;index++)
467
State->SampleBuffer[index] = 0.0f;
370
472
// This updates the reverb state. This is called any time the reverb effect
371
473
// is loaded into a slot.
372
ALvoid VerbUpdate(ALeffectState *effect, ALCcontext *Context, ALeffect *Effect)
474
ALvoid VerbUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
374
476
ALverbState *State = (ALverbState*)effect;
477
ALuint frequency = Context->Device->Frequency;
376
479
ALfloat length, mixCoeff, cw, g, coeff;
377
480
ALfloat hfRatio = Effect->Reverb.DecayHFRatio;
379
482
// Calculate the master low-pass filter (from the master effect HF gain).
380
cw = cos(2.0 * M_PI * Effect->Reverb.HFReference / Context->Frequency);
483
cw = cos(2.0*M_PI * Effect->Reverb.HFReference / frequency);
381
484
g = __max(Effect->Reverb.GainHF, 0.0001f);
382
485
State->LpFilter.coeff = 0.0f;
383
486
if(g < 0.9999f) // 1-epsilon
660
763
// This creates the reverb state. It should be called only when the reverb
661
764
// effect is loaded into a slot that doesn't already have a reverb effect.
662
ALeffectState *VerbCreate(ALCcontext *Context)
765
ALeffectState *VerbCreate(void)
664
767
ALverbState *State = NULL;
665
ALuint samples, length[13], totalLength, index;
667
770
State = malloc(sizeof(ALverbState));
674
777
State->state.Destroy = VerbDestroy;
778
State->state.DeviceUpdate = VerbDeviceUpdate;
675
779
State->state.Update = VerbUpdate;
676
780
State->state.Process = VerbProcess;
678
// All line lengths are powers of 2, calculated from their lengths, with
679
// an additional sample in case of rounding errors.
681
// See VerbUpdate() for an explanation of the additional calculation
682
// added to the master line length.
684
((MASTER_LINE_LENGTH +
685
(LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER) *
686
(DECO_FRACTION * ((DECO_MULTIPLIER * DECO_MULTIPLIER *
687
DECO_MULTIPLIER) - 1.0f)))) *
688
Context->Frequency) + 1;
689
length[0] = NextPowerOf2(samples);
690
totalLength = length[0];
691
for(index = 0;index < 4;index++)
693
samples = (ALuint)(EARLY_LINE_LENGTH[index] * Context->Frequency) + 1;
694
length[1 + index] = NextPowerOf2(samples);
695
totalLength += length[1 + index];
697
for(index = 0;index < 4;index++)
699
samples = (ALuint)(ALLPASS_LINE_LENGTH[index] * Context->Frequency) + 1;
700
length[5 + index] = NextPowerOf2(samples);
701
totalLength += length[5 + index];
703
for(index = 0;index < 4;index++)
705
samples = (ALuint)(LATE_LINE_LENGTH[index] *
706
(1.0f + LATE_LINE_MULTIPLIER) * Context->Frequency) + 1;
707
length[9 + index] = NextPowerOf2(samples);
708
totalLength += length[9 + index];
711
// All lines share a single sample buffer and have their masks and start
712
// addresses calculated once.
713
State->SampleBuffer = malloc(totalLength * sizeof(ALfloat));
714
if(!State->SampleBuffer)
717
alSetError(AL_OUT_OF_MEMORY);
720
for(index = 0; index < totalLength;index++)
721
State->SampleBuffer[index] = 0.0f;
782
State->TotalLength = 0;
783
State->SampleBuffer = NULL;
723
785
State->LpFilter.coeff = 0.0f;
724
786
State->LpFilter.history[0] = 0.0f;
725
787
State->LpFilter.history[1] = 0.0f;
726
State->Delay.Mask = length[0] - 1;
727
State->Delay.Line = &State->SampleBuffer[0];
728
totalLength = length[0];
788
State->Delay.Mask = 0;
789
State->Delay.Line = NULL;
730
791
State->Tap[0] = 0;
731
792
State->Tap[1] = 0;
737
798
for(index = 0;index < 4;index++)
739
800
State->Early.Coeff[index] = 0.0f;
740
State->Early.Delay[index].Mask = length[1 + index] - 1;
741
State->Early.Delay[index].Line = &State->SampleBuffer[totalLength];
742
totalLength += length[1 + index];
744
// The early delay lines have their read offsets calculated once.
745
State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
801
State->Early.Delay[index].Mask = 0;
802
State->Early.Delay[index].Line = NULL;
803
State->Early.Offset[index] = 0;
749
806
State->Late.Gain = 0.0f;
754
811
for(index = 0;index < 4;index++)
756
813
State->Late.ApCoeff[index] = 0.0f;
757
State->Late.ApDelay[index].Mask = length[5 + index] - 1;
758
State->Late.ApDelay[index].Line = &State->SampleBuffer[totalLength];
759
totalLength += length[5 + index];
761
// The late all-pass lines have their read offsets calculated once.
762
State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
766
for(index = 0;index < 4;index++)
814
State->Late.ApDelay[index].Mask = 0;
815
State->Late.ApDelay[index].Line = NULL;
816
State->Late.ApOffset[index] = 0;
768
818
State->Late.Coeff[index] = 0.0f;
769
State->Late.Delay[index].Mask = length[9 + index] - 1;
770
State->Late.Delay[index].Line = &State->SampleBuffer[totalLength];
771
totalLength += length[9 + index];
819
State->Late.Delay[index].Mask = 0;
820
State->Late.Delay[index].Line = NULL;
773
821
State->Late.Offset[index] = 0;
775
823
State->Late.LpCoeff[index] = 0.0f;