1 /******************************************************************************
2  *
3  *  Copyright 2022 Google LLC
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include "plc.h"
20 
21 
22 /**
23  * Reset Packet Loss Concealment state
24  */
lc3_plc_reset(struct lc3_plc_state * plc)25 void lc3_plc_reset(struct lc3_plc_state *plc)
26 {
27     plc->seed = 24607;
28     lc3_plc_suspend(plc);
29 }
30 
31 /**
32  * Suspend PLC execution (Good frame received)
33  */
lc3_plc_suspend(struct lc3_plc_state * plc)34 void lc3_plc_suspend(struct lc3_plc_state *plc)
35 {
36     plc->count = 1;
37     plc->alpha = 1.0f;
38 }
39 
40 /**
41  * Synthesis of a PLC frame
42  */
lc3_plc_synthesize(enum lc3_dt dt,enum lc3_srate sr,struct lc3_plc_state * plc,const float * x,float * y)43 void lc3_plc_synthesize(enum lc3_dt dt, enum lc3_srate sr,
44     struct lc3_plc_state *plc, const float *x, float *y)
45 {
46     uint16_t seed = plc->seed;
47     float alpha = plc->alpha;
48     int ne = LC3_NE(dt, sr);
49 
50     alpha *= (plc->count < 4 ? 1.0f :
51               plc->count < 8 ? 0.9f : 0.85f);
52 
53     for (int i = 0; i < ne; i++) {
54         seed = (16831 + seed * 12821) & 0xffff;
55         y[i] = alpha * (seed & 0x8000 ? -x[i] : x[i]);
56     }
57 
58     plc->seed = seed;
59     plc->alpha = alpha;
60     plc->count++;
61 }
62