| 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 |
#include "tables.h" |
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Reset Packet Loss Concealment state |
| 25 |
*/ |
| 26 |
void lc3_plc_reset(struct lc3_plc_state *plc) |
| 27 |
{ |
| 28 |
plc->seed = 24607; |
| 29 |
lc3_plc_suspend(plc); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Suspend PLC execution (Good frame received) |
| 34 |
*/ |
| 35 |
void lc3_plc_suspend(struct lc3_plc_state *plc) |
| 36 |
{ |
| 37 |
plc->count = 1; |
| 38 |
plc->alpha = 1.0f; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Synthesis of a PLC frame |
| 43 |
*/ |
| 44 |
void lc3_plc_synthesize(enum lc3_dt dt, enum lc3_srate sr, |
| 45 |
struct lc3_plc_state *plc, const float *x, float *y) |
| 46 |
{ |
| 47 |
uint16_t seed = plc->seed; |
| 48 |
float alpha = plc->alpha; |
| 49 |
int ne = lc3_ne(dt, sr); |
| 50 |
|
| 51 |
alpha *= (plc->count < 4 ? 1.0f : |
| 52 |
plc->count < 8 ? 0.9f : 0.85f); |
| 53 |
|
| 54 |
for (int i = 0; i < ne; i++) { |
| 55 |
seed = (16831 + seed * 12821) & 0xffff; |
| 56 |
y[i] = alpha * (seed & 0x8000 ? -x[i] : x[i]); |
| 57 |
} |
| 58 |
|
| 59 |
plc->seed = seed; |
| 60 |
plc->alpha = alpha; |
| 61 |
plc->count++; |
| 62 |
} |