~ubuntu-branches/ubuntu/lucid/openal-soft/lucid-updates

« back to all changes in this revision

Viewing changes to Alc/alcReverb.c

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2009-11-25 14:59:51 UTC
  • Revision ID: james.westby@ubuntu.com-20091125145951-0dl33wc4qj3x3zt4
Tags: 1:1.10.622-1
* New upstream release.
* Issue with using PulseAudio backend fixed.
  (Closes: #548373)
  (Closes: #551018)
* Switch to Debian source format 3.0 (quilt).

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
    // All delay lines are allocated as a single buffer to reduce memory
47
47
    // fragmentation and management code.
48
48
    ALfloat  *SampleBuffer;
 
49
    ALuint    TotalLength;
49
50
    // Master effect low-pass filter (2 chained 1-pole filters).
50
51
    FILTER    LpFilter;
51
52
    ALfloat   LpHistory[2];
143
144
    return powerOf2;
144
145
}
145
146
 
 
147
static ALuint CalcLengths(ALuint length[13], ALuint frequency)
 
148
{
 
149
    ALuint samples, totalLength, index;
 
150
 
 
151
    // All line lengths are powers of 2, calculated from their lengths, with
 
152
    // an additional sample in case of rounding errors.
 
153
 
 
154
    // See VerbUpdate() for an explanation of the additional calculation
 
155
    // added to the master line length.
 
156
    samples = (ALuint)
 
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)))) *
 
161
               frequency) + 1;
 
162
    length[0] = NextPowerOf2(samples);
 
163
    totalLength = length[0];
 
164
    for(index = 0;index < 4;index++)
 
165
    {
 
166
        samples = (ALuint)(EARLY_LINE_LENGTH[index] * frequency) + 1;
 
167
        length[1 + index] = NextPowerOf2(samples);
 
168
        totalLength += length[1 + index];
 
169
    }
 
170
    for(index = 0;index < 4;index++)
 
171
    {
 
172
        samples = (ALuint)(ALLPASS_LINE_LENGTH[index] * frequency) + 1;
 
173
        length[5 + index] = NextPowerOf2(samples);
 
174
        totalLength += length[5 + index];
 
175
    }
 
176
    for(index = 0;index < 4;index++)
 
177
    {
 
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];
 
182
    }
 
183
 
 
184
    return totalLength;
 
185
}
 
186
 
146
187
// Basic delay line input/output routines.
147
188
static __inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
148
189
{
367
408
    return pos%LUT_NUM;
368
409
}
369
410
 
 
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)
 
415
{
 
416
    ALverbState *State = (ALverbState*)effect;
 
417
    ALuint length[13], totalLength;
 
418
    ALuint index;
 
419
 
 
420
    totalLength = CalcLengths(length, Device->Frequency);
 
421
    if(totalLength != State->TotalLength)
 
422
    {
 
423
        void *temp;
 
424
 
 
425
        temp = realloc(State->SampleBuffer, totalLength * sizeof(ALfloat));
 
426
        if(!temp)
 
427
        {
 
428
            alSetError(AL_OUT_OF_MEMORY);
 
429
            return AL_FALSE;
 
430
        }
 
431
        State->TotalLength = totalLength;
 
432
        State->SampleBuffer = temp;
 
433
 
 
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++)
 
439
        {
 
440
            State->Early.Delay[index].Mask = length[1 + index] - 1;
 
441
            State->Early.Delay[index].Line = &State->SampleBuffer[totalLength];
 
442
            totalLength += length[1 + index];
 
443
        }
 
444
        for(index = 0;index < 4;index++)
 
445
        {
 
446
            State->Late.ApDelay[index].Mask = length[5 + index] - 1;
 
447
            State->Late.ApDelay[index].Line = &State->SampleBuffer[totalLength];
 
448
            totalLength += length[5 + index];
 
449
        }
 
450
        for(index = 0;index < 4;index++)
 
451
        {
 
452
            State->Late.Delay[index].Mask = length[9 + index] - 1;
 
453
            State->Late.Delay[index].Line = &State->SampleBuffer[totalLength];
 
454
            totalLength += length[9 + index];
 
455
        }
 
456
    }
 
457
 
 
458
    for(index = 0;index < 4;index++)
 
459
    {
 
460
        State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
 
461
                                              Device->Frequency);
 
462
        State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
 
463
                                               Device->Frequency);
 
464
    }
 
465
 
 
466
    for(index = 0;index < State->TotalLength;index++)
 
467
        State->SampleBuffer[index] = 0.0f;
 
468
 
 
469
    return AL_TRUE;
 
470
}
 
471
 
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)
373
475
{
374
476
    ALverbState *State = (ALverbState*)effect;
 
477
    ALuint frequency = Context->Device->Frequency;
375
478
    ALuint index;
376
479
    ALfloat length, mixCoeff, cw, g, coeff;
377
480
    ALfloat hfRatio = Effect->Reverb.DecayHFRatio;
378
481
 
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
385
488
 
386
489
    // Calculate the initial delay taps.
387
490
    length = Effect->Reverb.ReflectionsDelay;
388
 
    State->Tap[0] = (ALuint)(length * Context->Frequency);
 
491
    State->Tap[0] = (ALuint)(length * frequency);
389
492
 
390
493
    length += Effect->Reverb.LateReverbDelay;
391
494
 
402
505
        length += LATE_LINE_LENGTH[0] *
403
506
            (1.0f + (Effect->Reverb.Density * LATE_LINE_MULTIPLIER)) *
404
507
            (DECO_FRACTION * (pow(DECO_MULTIPLIER, (ALfloat)index) - 1.0f));
405
 
        State->Tap[1 + index] = (ALuint)(length * Context->Frequency);
 
508
        State->Tap[1 + index] = (ALuint)(length * frequency);
406
509
    }
407
510
 
408
511
    // Calculate the early reflections gain (from the master effect gain, and
494
597
    }
495
598
 
496
599
    // Calculate the low-pass filter frequency.
497
 
    cw = cos(2.0f * M_PI * Effect->Reverb.HFReference / Context->Frequency);
 
600
    cw = cos(2.0*M_PI * Effect->Reverb.HFReference / frequency);
498
601
 
499
602
    for(index = 0;index < 4;index++)
500
603
    {
502
605
        length = LATE_LINE_LENGTH[index] * (1.0f + (Effect->Reverb.Density *
503
606
                                                    LATE_LINE_MULTIPLIER));
504
607
        // Calculate the delay offset for the cyclical delay lines.
505
 
        State->Late.Offset[index] = (ALuint)(length * Context->Frequency);
 
608
        State->Late.Offset[index] = (ALuint)(length * frequency);
506
609
 
507
610
        // Calculate the gain (coefficient) for each cyclical line.
508
611
        State->Late.Coeff[index] = pow(10.0f, length / Effect->Reverb.DecayTime *
659
762
 
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)
663
766
{
664
767
    ALverbState *State = NULL;
665
 
    ALuint samples, length[13], totalLength, index;
 
768
    ALuint index;
666
769
 
667
770
    State = malloc(sizeof(ALverbState));
668
771
    if(!State)
672
775
    }
673
776
 
674
777
    State->state.Destroy = VerbDestroy;
 
778
    State->state.DeviceUpdate = VerbDeviceUpdate;
675
779
    State->state.Update = VerbUpdate;
676
780
    State->state.Process = VerbProcess;
677
781
 
678
 
    // All line lengths are powers of 2, calculated from their lengths, with
679
 
    // an additional sample in case of rounding errors.
680
 
 
681
 
    // See VerbUpdate() for an explanation of the additional calculation
682
 
    // added to the master line length.
683
 
    samples = (ALuint)
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++)
692
 
    {
693
 
        samples = (ALuint)(EARLY_LINE_LENGTH[index] * Context->Frequency) + 1;
694
 
        length[1 + index] = NextPowerOf2(samples);
695
 
        totalLength += length[1 + index];
696
 
    }
697
 
    for(index = 0;index < 4;index++)
698
 
    {
699
 
        samples = (ALuint)(ALLPASS_LINE_LENGTH[index] * Context->Frequency) + 1;
700
 
        length[5 + index] = NextPowerOf2(samples);
701
 
        totalLength += length[5 + index];
702
 
    }
703
 
    for(index = 0;index < 4;index++)
704
 
    {
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];
709
 
    }
710
 
 
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)
715
 
    {
716
 
        free(State);
717
 
        alSetError(AL_OUT_OF_MEMORY);
718
 
        return NULL;
719
 
    }
720
 
    for(index = 0; index < totalLength;index++)
721
 
        State->SampleBuffer[index] = 0.0f;
 
782
    State->TotalLength = 0;
 
783
    State->SampleBuffer = NULL;
722
784
 
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;
729
790
 
730
791
    State->Tap[0] = 0;
731
792
    State->Tap[1] = 0;
737
798
    for(index = 0;index < 4;index++)
738
799
    {
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];
743
 
 
744
 
        // The early delay lines have their read offsets calculated once.
745
 
        State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
746
 
                                              Context->Frequency);
 
801
        State->Early.Delay[index].Mask = 0;
 
802
        State->Early.Delay[index].Line = NULL;
 
803
        State->Early.Offset[index] = 0;
747
804
    }
748
805
 
749
806
    State->Late.Gain = 0.0f;
754
811
    for(index = 0;index < 4;index++)
755
812
    {
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];
760
 
 
761
 
        // The late all-pass lines have their read offsets calculated once.
762
 
        State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
763
 
                                               Context->Frequency);
764
 
    }
765
 
 
766
 
    for(index = 0;index < 4;index++)
767
 
    {
 
814
        State->Late.ApDelay[index].Mask = 0;
 
815
        State->Late.ApDelay[index].Line = NULL;
 
816
        State->Late.ApOffset[index] = 0;
 
817
 
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];
772
 
 
 
819
        State->Late.Delay[index].Mask = 0;
 
820
        State->Late.Delay[index].Line = NULL;
773
821
        State->Late.Offset[index] = 0;
774
822
 
775
823
        State->Late.LpCoeff[index] = 0.0f;
787
835
    return &State->state;
788
836
}
789
837
 
790
 
ALeffectState *EAXVerbCreate(ALCcontext *Context)
 
838
ALeffectState *EAXVerbCreate(void)
791
839
{
792
 
    ALeffectState *State = VerbCreate(Context);
 
840
    ALeffectState *State = VerbCreate();
793
841
    if(State) State->Process = EAXVerbProcess;
794
842
    return State;
795
843
}