summaryrefslogtreecommitdiffstats
path: root/media/libeffects/lvm/lib/Reverb/src/LVREV_Process.c
blob: 81521bad89cbe2f486bbe9d91e71e392bbc3ed65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
 * Copyright (C) 2004-2010 NXP Software
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/****************************************************************************************/
/*                                                                                      */
/* Includes                                                                             */
/*                                                                                      */
/****************************************************************************************/
#include "LVREV_Private.h"
#include "VectorArithmetic.h"


/****************************************************************************************/
/*                                                                                      */
/* FUNCTION:                LVREV_Process                                               */
/*                                                                                      */
/* DESCRIPTION:                                                                         */
/*  Process function for the LVREV module.                                              */
/*                                                                                      */
/* PARAMETERS:                                                                          */
/*  hInstance               Instance handle                                             */
/*  pInData                 Pointer to the input data                                   */
/*  pOutData                Pointer to the output data                                  */
/*  NumSamples              Number of samples in the input buffer                       */
/*                                                                                      */
/* RETURNS:                                                                             */
/*  LVREV_Success           Succeeded                                                   */
/*  LVREV_INVALIDNUMSAMPLES NumSamples was larger than the maximum block size           */
/*  LVREV_NULLADDRESS       When one of hInstance, pInData or pOutData is NULL          */
/*                                                                                      */
/* NOTES:                                                                               */
/*  1. The input and output buffers must be 32-bit aligned                              */
/*                                                                                      */
/****************************************************************************************/
LVREV_ReturnStatus_en LVREV_Process(LVREV_Handle_t      hInstance,
                                    const LVM_INT32     *pInData,
                                    LVM_INT32           *pOutData,
                                    const LVM_UINT16    NumSamples)
{
   LVREV_Instance_st     *pLVREV_Private = (LVREV_Instance_st *)hInstance;
   LVM_INT32             *pInput  = (LVM_INT32 *)pInData;
   LVM_INT32             *pOutput = pOutData;
   LVM_INT32             SamplesToProcess, RemainingSamples, format;

    /*
     * Check for error conditions
     */

    /* Check for NULL pointers */
    if((hInstance == LVM_NULL) || (pInData == LVM_NULL) || (pOutData == LVM_NULL))
    {
        return LVREV_NULLADDRESS;
    }


    /*
     * Apply the new controls settings if required
     */
    if(pLVREV_Private->bControlPending == LVM_TRUE)
    {
        LVREV_ReturnStatus_en   errorCode;

        /*
         * Clear the pending flag and update the control settings
         */
        pLVREV_Private->bControlPending = LVM_FALSE;

        errorCode = LVREV_ApplyNewSettings (pLVREV_Private);

        if(errorCode != LVREV_SUCCESS)
        {
            return errorCode;
        }
    }

    /*
     * Trap the case where the number of samples is zero.
     */
    if (NumSamples == 0)
    {
        return LVREV_SUCCESS;
    }

    RemainingSamples = (LVM_INT32)NumSamples;

    format = 1;
    if (pLVREV_Private->CurrentParams.SourceFormat != LVM_MONO)
    {
        format = 2;
    }

    while (RemainingSamples!=0)
    {
        /*
         * If OFF copy and reformat the data as necessary
         */
        if (pLVREV_Private->CurrentParams.OperatingMode == LVM_MODE_OFF)
        {
            if((pInput != pOutput) || (pLVREV_Private->CurrentParams.SourceFormat == LVM_MONO))
            {
                /*
                 * Copy the data to the output buffer
                 */

                if (pLVREV_Private->CurrentParams.SourceFormat != LVM_MONO)
                {
                    RemainingSamples = (RemainingSamples << 1);           /* Stereo data */
                }

                Copy_16((LVM_INT16 *)pInput,
                        (LVM_INT16 *)pOutput,
                        (LVM_INT16)(RemainingSamples << 1));
            }

            RemainingSamples = 0;
        }

        /*
         * Process the data
         */
        else
        {

            if(RemainingSamples >  pLVREV_Private->MaxBlkLen)
            {
                SamplesToProcess =  pLVREV_Private->MaxBlkLen;
                RemainingSamples = (LVM_INT16)(RemainingSamples - SamplesToProcess);
            }
            else
            {
                SamplesToProcess = RemainingSamples;
                RemainingSamples = 0;
            }

            ReverbBlock(pInput, pOutput, pLVREV_Private, (LVM_UINT16)SamplesToProcess);

            pInput  = (LVM_INT32 *)(pInput +(SamplesToProcess*format));
            pOutput = (LVM_INT32 *)(pOutput+(SamplesToProcess*format));
        }
    }

    return LVREV_SUCCESS;
}



/****************************************************************************************/
/*                                                                                      */
/* FUNCTION:                ReverbBlock                                                 */
/*                                                                                      */
/* DESCRIPTION:                                                                         */
/*  Process function for the LVREV module.                                              */
/*                                                                                      */
/* PARAMETERS:                                                                          */
/*  hInstance               Instance handle                                             */
/*  pInData                 Pointer to the input data                                   */
/*  pOutData                Pointer to the output data                                  */
/*  NumSamples              Number of samples in the input buffer                       */
/*                                                                                      */
/* RETURNS:                                                                             */
/*  LVREV_Success           Succeeded                                                   */
/*  LVREV_INVALIDNUMSAMPLES NumSamples was larger than the maximum block size           */
/*  LVREV_NULLADDRESS       When one of hInstance, pInData or pOutData is NULL          */
/*                                                                                      */
/* NOTES:                                                                               */
/*  1. The input and output buffers must be 32-bit aligned                              */
/*                                                                                      */
/****************************************************************************************/

void ReverbBlock(LVM_INT32 *pInput, LVM_INT32 *pOutput, LVREV_Instance_st *pPrivate, LVM_UINT16 NumSamples)
{
    LVM_INT16   j, size;
    LVM_INT32   *pDelayLine;
    LVM_INT32   *pDelayLineInput = pPrivate->pScratch;
    LVM_INT32   *pScratch = pPrivate->pScratch;
    LVM_INT32   *pIn;
    LVM_INT32   *pTemp = pPrivate->pInputSave;
    LVM_INT32   NumberOfDelayLines;

    /******************************************************************************
     * All calculations will go into the buffer pointed to by pTemp, this will    *
     * then be mixed with the original input to create the final output.          *
     *                                                                            *
     * When INPLACE processing is selected this must be a temporary buffer and    *
     * hence this is the worst case, so for simplicity this will ALWAYS be so     *
     *                                                                            *
     * The input buffer will remain untouched until the output of the mixer if    *
     * INPLACE processing is selected.                                            *
     *                                                                            *
     * The temp buffer will always be NumSamples in size regardless of MONO or    *
     * STEREO input. In the case of stereo input all processing is done in MONO   *
     * and the final output is converted to STEREO after the mixer                *
     ******************************************************************************/

    if(pPrivate->InstanceParams.NumDelays == LVREV_DELAYLINES_4 )
    {
        NumberOfDelayLines = 4;
    }
    else if(pPrivate->InstanceParams.NumDelays == LVREV_DELAYLINES_2 )
    {
        NumberOfDelayLines = 2;
    }
    else
    {
        NumberOfDelayLines = 1;
    }

    if(pPrivate->CurrentParams.SourceFormat == LVM_MONO)
    {
        pIn = pInput;
    }
    else
    {
        /*
         *  Stereo to mono conversion
         */

        From2iToMono_32( pInput,
                         pTemp,
                         (LVM_INT16)NumSamples);

        pIn = pTemp;
    }

    Mult3s_32x16(pIn,
                 (LVM_INT16)LVREV_HEADROOM,
                 pTemp,
                 (LVM_INT16)NumSamples);

    /*
     *  High pass filter
     */
    FO_1I_D32F32C31_TRC_WRA_01( &pPrivate->pFastCoef->HPCoefs,
                                pTemp,
                                pTemp,
                                (LVM_INT16)NumSamples);
    /*
     *  Low pass filter
     */
    FO_1I_D32F32C31_TRC_WRA_01( &pPrivate->pFastCoef->LPCoefs,
                                pTemp,
                                pTemp,
                                (LVM_INT16)NumSamples);

    /*
     *  Process all delay lines
     */

    for(j = 0; j < NumberOfDelayLines; j++)
    {
        pDelayLine = pPrivate->pScratchDelayLine[j];

        /*
         * All-pass filter with pop and click suppression
         */
        /* Get the smoothed, delayed output. Put it in the output buffer */
        MixSoft_2St_D32C31_SAT(&pPrivate->Mixer_APTaps[j],
                               pPrivate->pOffsetA[j],
                               pPrivate->pOffsetB[j],
                               pDelayLine,
                               (LVM_INT16)NumSamples);
        /* Re-align the all pass filter delay buffer and copying the fixed delay data to the AP delay in the process */
        Copy_16((LVM_INT16 *)&pPrivate->pDelay_T[j][NumSamples],
                (LVM_INT16 *)pPrivate->pDelay_T[j],
                (LVM_INT16)((pPrivate->T[j]-NumSamples) << 1));         /* 32-bit data */
        /* Apply the smoothed feedback and save to fixed delay input (currently empty) */
        MixSoft_1St_D32C31_WRA(&pPrivate->Mixer_SGFeedback[j],
                               pDelayLine,
                               &pPrivate->pDelay_T[j][pPrivate->T[j]-NumSamples],
                               (LVM_INT16)NumSamples);
        /* Sum into the AP delay line */
        Mac3s_Sat_32x16(&pPrivate->pDelay_T[j][pPrivate->T[j]-NumSamples],
                        -0x7fff,                                        /* Invert since the feedback coefficient is negative */
                        &pPrivate->pDelay_T[j][pPrivate->Delay_AP[j]-NumSamples],
                        (LVM_INT16)NumSamples);
        /* Apply smoothed feedforward sand save to fixed delay input (currently empty) */
        MixSoft_1St_D32C31_WRA(&pPrivate->Mixer_SGFeedforward[j],
                               &pPrivate->pDelay_T[j][pPrivate->Delay_AP[j]-NumSamples],
                               &pPrivate->pDelay_T[j][pPrivate->T[j]-NumSamples],
                               (LVM_INT16)NumSamples);
        /* Sum into the AP output */
        Mac3s_Sat_32x16(&pPrivate->pDelay_T[j][pPrivate->T[j]-NumSamples],
                        0x7fff,
                        pDelayLine,
                        (LVM_INT16)NumSamples);

        /*
         *  Feedback gain
         */
        MixSoft_1St_D32C31_WRA(&pPrivate->FeedbackMixer[j], pDelayLine, pDelayLine, NumSamples);

        /*
         *  Low pass filter
         */
        FO_1I_D32F32C31_TRC_WRA_01( &pPrivate->pFastCoef->RevLPCoefs[j],
                                    pDelayLine,
                                    pDelayLine,
                                    (LVM_INT16)NumSamples);
    }

    /*
     *  Apply rotation matrix and delay samples
     */
    for(j = 0; j < NumberOfDelayLines; j++)
    {

        Copy_16( (LVM_INT16*)(pTemp),
                 (LVM_INT16*)(pDelayLineInput),
                 (LVM_INT16)(NumSamples << 1));

        /*
         *  Rotation matrix mix
         */
        switch(j)
        {
            case 3:
                /*
                 *  Add delay line 1 and 2 contribution
                 */
                 Mac3s_Sat_32x16(pPrivate->pScratchDelayLine[1], -0x8000, pDelayLineInput, (LVM_INT16)NumSamples);
                 Mac3s_Sat_32x16(pPrivate->pScratchDelayLine[2], -0x8000, pDelayLineInput, (LVM_INT16)NumSamples);

                break;
            case 2:

                /*
                 *  Add delay line 0 and 3 contribution
                 */
                 Mac3s_Sat_32x16(pPrivate->pScratchDelayLine[0], -0x8000, pDelayLineInput, (LVM_INT16)NumSamples);
                 Mac3s_Sat_32x16(pPrivate->pScratchDelayLine[3], -0x8000, pDelayLineInput, (LVM_INT16)NumSamples);

                break;
            case 1:
                if(pPrivate->InstanceParams.NumDelays == LVREV_DELAYLINES_4)
                {
                    /*
                     *  Add delay line 0 and 3 contribution
                     */
                    Mac3s_Sat_32x16(pPrivate->pScratchDelayLine[0], -0x8000, pDelayLineInput, (LVM_INT16)NumSamples);
                    Add2_Sat_32x32(pPrivate->pScratchDelayLine[3], pDelayLineInput, (LVM_INT16)NumSamples);

                }
                else
                {
                    /*
                     *  Add delay line 0 and 1 contribution
                     */
                     Mac3s_Sat_32x16(pPrivate->pScratchDelayLine[0], -0x8000, pDelayLineInput, (LVM_INT16)NumSamples);
                     Mac3s_Sat_32x16(pPrivate->pScratchDelayLine[1], -0x8000, pDelayLineInput, (LVM_INT16)NumSamples);

                }
                break;
            case 0:
                if(pPrivate->InstanceParams.NumDelays == LVREV_DELAYLINES_4)
                {
                    /*
                     *  Add delay line 1 and 2 contribution
                     */
                    Mac3s_Sat_32x16(pPrivate->pScratchDelayLine[1], -0x8000, pDelayLineInput, (LVM_INT16)NumSamples);
                    Add2_Sat_32x32(pPrivate->pScratchDelayLine[2], pDelayLineInput, (LVM_INT16)NumSamples);

                }
                else if(pPrivate->InstanceParams.NumDelays == LVREV_DELAYLINES_2)
                {
                    /*
                     *  Add delay line 0 and 1 contribution
                     */
                    Add2_Sat_32x32(pPrivate->pScratchDelayLine[0], pDelayLineInput, (LVM_INT16)NumSamples);
                    Mac3s_Sat_32x16(pPrivate->pScratchDelayLine[1], -0x8000, pDelayLineInput, (LVM_INT16)NumSamples);

                }
                else
                {
                    /*
                     *  Add delay line 0 contribution
                     */

                    /*             SOURCE                          DESTINATION*/
                    Add2_Sat_32x32(pPrivate->pScratchDelayLine[0], pDelayLineInput, (LVM_INT16)NumSamples);
                }
                break;
            default:
                break;
        }

        /*
         *  Delay samples
         */
        Copy_16((LVM_INT16 *)pDelayLineInput,
                (LVM_INT16 *)&pPrivate->pDelay_T[j][pPrivate->T[j]-NumSamples],
                (LVM_INT16)(NumSamples << 1));              /* 32-bit data */

    }


    /*
     *  Create stereo output
     */
    switch(pPrivate->InstanceParams.NumDelays)
    {
        case LVREV_DELAYLINES_4:
             Add2_Sat_32x32(pPrivate->pScratchDelayLine[3],
                            pPrivate->pScratchDelayLine[0],
                            (LVM_INT16)NumSamples);
             Add2_Sat_32x32(pPrivate->pScratchDelayLine[2],
                            pPrivate->pScratchDelayLine[1],
                            (LVM_INT16)NumSamples);

             if(pPrivate->CurrentParams.SourceFormat != LVM_MONO)
             {
                JoinTo2i_32x32(pPrivate->pScratchDelayLine[0],
                               pPrivate->pScratchDelayLine[1],
                               pTemp,
                               (LVM_INT16)NumSamples);

             }
             else
             {
                 Add2_Sat_32x32(pPrivate->pScratchDelayLine[1],
                                pPrivate->pScratchDelayLine[0],
                                (LVM_INT16)NumSamples);

                /*Apply 3-dB gain in-order to compensate for the gain change in stereo mode*/
                Mult3s_32x16(pPrivate->pScratchDelayLine[0],
                             LVREV_MIN3DB,
                             pTemp,
                             (LVM_INT16)NumSamples);
             }
            break;
        case LVREV_DELAYLINES_2:

             Copy_16( (LVM_INT16*)pPrivate->pScratchDelayLine[1],
                      (LVM_INT16*)pScratch,
                      (LVM_INT16)(NumSamples << 1));



             if(pPrivate->CurrentParams.SourceFormat != LVM_MONO)
             {

                Mac3s_Sat_32x16(pPrivate->pScratchDelayLine[0],
                                -0x8000,
                                pScratch,
                                (LVM_INT16)NumSamples);
             }

             Add2_Sat_32x32(pPrivate->pScratchDelayLine[1],
                            pPrivate->pScratchDelayLine[0],
                            (LVM_INT16)NumSamples);


             if(pPrivate->CurrentParams.SourceFormat != LVM_MONO)
             {
                 JoinTo2i_32x32(pPrivate->pScratchDelayLine[0],
                                pScratch,
                                pTemp,
                                (LVM_INT16)NumSamples);
             }
             else
             {
                Copy_16(    (LVM_INT16*)pPrivate->pScratchDelayLine[0],
                            (LVM_INT16*)pTemp,
                            (LVM_INT16)(NumSamples << 1));

             }
            break;
        case LVREV_DELAYLINES_1:
            if(pPrivate->CurrentParams.SourceFormat != LVM_MONO)
            {

                MonoTo2I_32(pPrivate->pScratchDelayLine[0],
                            pTemp,
                            (LVM_INT16)NumSamples);
            }
            else
            {
                pTemp = pPrivate->pScratchDelayLine[0];
            }
            break;
        default:
            break;
    }


    /*
     *  Dry/wet mixer
     */
    if(pPrivate->CurrentParams.SourceFormat != LVM_MONO)
    {
        size = (LVM_INT16)(NumSamples << 1);
    }
    else
    {
        size = (LVM_INT16)NumSamples;
    }

    MixSoft_2St_D32C31_SAT(&pPrivate->BypassMixer,
                           pInput,
                           pTemp,
                           pOutput,
                           size);

    /* Apply Gain*/
    if(pPrivate->CurrentParams.SourceFormat != LVM_MONO)
    {
        size = (LVM_INT16)(NumSamples << 1);
    }
    else
    {
        size = (LVM_INT16)NumSamples;
    }

    Shift_Sat_v32xv32 (LVREV_OUTPUTGAIN_SHIFT,
                       pOutput,
                       pOutput,
                       size);

    MixSoft_1St_D32C31_WRA(&pPrivate->GainMixer,
                           pOutput,
                           pOutput,
                           size);
    return;
}


/* End of file */