19a19cd78SMatthias Ringwald /******************************************************************************
29a19cd78SMatthias Ringwald *
34930cef6SMatthias Ringwald * Copyright 2022 Google LLC
49a19cd78SMatthias Ringwald *
59a19cd78SMatthias Ringwald * Licensed under the Apache License, Version 2.0 (the "License");
69a19cd78SMatthias Ringwald * you may not use this file except in compliance with the License.
79a19cd78SMatthias Ringwald * You may obtain a copy of the License at:
89a19cd78SMatthias Ringwald *
99a19cd78SMatthias Ringwald * http://www.apache.org/licenses/LICENSE-2.0
109a19cd78SMatthias Ringwald *
119a19cd78SMatthias Ringwald * Unless required by applicable law or agreed to in writing, software
129a19cd78SMatthias Ringwald * distributed under the License is distributed on an "AS IS" BASIS,
139a19cd78SMatthias Ringwald * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149a19cd78SMatthias Ringwald * See the License for the specific language governing permissions and
159a19cd78SMatthias Ringwald * limitations under the License.
169a19cd78SMatthias Ringwald *
179a19cd78SMatthias Ringwald ******************************************************************************/
189a19cd78SMatthias Ringwald
199a19cd78SMatthias Ringwald #include "plc.h"
20*6897da5cSDirk Helbig #include "tables.h"
219a19cd78SMatthias Ringwald
229a19cd78SMatthias Ringwald
239a19cd78SMatthias Ringwald /**
249a19cd78SMatthias Ringwald * Reset Packet Loss Concealment state
259a19cd78SMatthias Ringwald */
lc3_plc_reset(struct lc3_plc_state * plc)269a19cd78SMatthias Ringwald void lc3_plc_reset(struct lc3_plc_state *plc)
279a19cd78SMatthias Ringwald {
289a19cd78SMatthias Ringwald plc->seed = 24607;
299a19cd78SMatthias Ringwald lc3_plc_suspend(plc);
309a19cd78SMatthias Ringwald }
319a19cd78SMatthias Ringwald
329a19cd78SMatthias Ringwald /**
339a19cd78SMatthias Ringwald * Suspend PLC execution (Good frame received)
349a19cd78SMatthias Ringwald */
lc3_plc_suspend(struct lc3_plc_state * plc)359a19cd78SMatthias Ringwald void lc3_plc_suspend(struct lc3_plc_state *plc)
369a19cd78SMatthias Ringwald {
379a19cd78SMatthias Ringwald plc->count = 1;
389a19cd78SMatthias Ringwald plc->alpha = 1.0f;
399a19cd78SMatthias Ringwald }
409a19cd78SMatthias Ringwald
419a19cd78SMatthias Ringwald /**
429a19cd78SMatthias Ringwald * Synthesis of a PLC frame
439a19cd78SMatthias Ringwald */
lc3_plc_synthesize(enum lc3_dt dt,enum lc3_srate sr,struct lc3_plc_state * plc,const float * x,float * y)449a19cd78SMatthias Ringwald void lc3_plc_synthesize(enum lc3_dt dt, enum lc3_srate sr,
459a19cd78SMatthias Ringwald struct lc3_plc_state *plc, const float *x, float *y)
469a19cd78SMatthias Ringwald {
479a19cd78SMatthias Ringwald uint16_t seed = plc->seed;
489a19cd78SMatthias Ringwald float alpha = plc->alpha;
49*6897da5cSDirk Helbig int ne = lc3_ne(dt, sr);
509a19cd78SMatthias Ringwald
519a19cd78SMatthias Ringwald alpha *= (plc->count < 4 ? 1.0f :
529a19cd78SMatthias Ringwald plc->count < 8 ? 0.9f : 0.85f);
539a19cd78SMatthias Ringwald
549a19cd78SMatthias Ringwald for (int i = 0; i < ne; i++) {
559a19cd78SMatthias Ringwald seed = (16831 + seed * 12821) & 0xffff;
569a19cd78SMatthias Ringwald y[i] = alpha * (seed & 0x8000 ? -x[i] : x[i]);
579a19cd78SMatthias Ringwald }
589a19cd78SMatthias Ringwald
599a19cd78SMatthias Ringwald plc->seed = seed;
609a19cd78SMatthias Ringwald plc->alpha = alpha;
619a19cd78SMatthias Ringwald plc->count++;
629a19cd78SMatthias Ringwald }
63