changeset 405:8fff74ca83e8

libtwamr: integrate ton_stab.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 06 May 2024 23:23:40 +0000
parents a37687c6ff22
children 85e9768d497f
files libtwamr/Makefile libtwamr/namespace.list libtwamr/ton_stab.c libtwamr/ton_stab.h
diffstat 4 files changed, 258 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libtwamr/Makefile	Mon May 06 23:17:09 2024 +0000
+++ b/libtwamr/Makefile	Mon May 06 23:23:40 2024 +0000
@@ -14,8 +14,8 @@
 	prmno.o pstfilt.o q_gain_c.o q_gain_p.o q_plsf.o q_plsf3_tab.o \
 	q_plsf5_tab.o q_plsf_3.o q_plsf_5.o qgain475.o qgain795.o qua_gain.o \
 	qua_gain_tab.o reorder.o residu.o s10_8pf.o set_sign.o sid_sync.o \
-	spreproc.o spstproc.o sqrt_l.o syn_filt.o tls_flags.o weight_a.o \
-	window.o
+	spreproc.o spstproc.o sqrt_l.o syn_filt.o tls_flags.o ton_stab.o \
+	weight_a.o window.o
 HDRS=	namespace.h
 LIB=	libtwamr.a
 
--- a/libtwamr/namespace.list	Mon May 06 23:17:09 2024 +0000
+++ b/libtwamr/namespace.list	Mon May 06 23:23:40 2024 +0000
@@ -49,6 +49,7 @@
 pre_big preemphasis preemphasis_reset
 q_gain_code q_gain_pitch
 sid_sync sid_sync_reset sid_sync_set_handover_debt
+ton_stab_reset check_lsp check_gp_clipping update_gp_clipping
 
 Bits2prm Prm2bits
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/ton_stab.c	Mon May 06 23:23:40 2024 +0000
@@ -0,0 +1,182 @@
+/*
+*****************************************************************************
+*
+*      GSM AMR-NB speech codec   R98   Version 7.6.0   December 12, 2001
+*                                R99   Version 3.3.0                
+*                                REL-4 Version 4.1.0                
+*
+*****************************************************************************
+*
+*      File             : ton_stab.c
+*
+*****************************************************************************
+*/
+
+/*
+*****************************************************************************
+*                         MODULE INCLUDE FILE AND VERSION ID
+*****************************************************************************
+*/
+#include "namespace.h"
+#include "ton_stab.h"
+ 
+/*
+*****************************************************************************
+*                         INCLUDE FILES
+*****************************************************************************
+*/
+#include "typedef.h"
+#include "basic_op.h"
+#include "no_count.h"
+#include "oper_32b.h"
+#include "cnst.h"
+#include "memops.h"
+
+/*
+*****************************************************************************
+*                         LOCAL VARIABLES AND TABLES
+*****************************************************************************
+*/
+
+/*
+*****************************************************************************
+*                         PUBLIC PROGRAM CODE
+*****************************************************************************
+*/
+
+/*************************************************************************
+ *
+ *  Function:   ton_stab_reset
+ *  Purpose:    Initializes state memory to zero
+ *
+ **************************************************************************
+ */
+void ton_stab_reset (tonStabState *st)
+{
+    /* initialize tone stabilizer state */ 
+    st->count = 0;
+    Set_zero(st->gp, N_FRAME);    /* Init Gp_Clipping */
+}
+
+/***************************************************************************
+ *                                                                          *
+ *  Function:  check_lsp()                                                  *
+ *  Purpose:   Check the LSP's to detect resonances                         *
+ *                                                                          *
+ ****************************************************************************
+ */
+Word16 check_lsp(tonStabState *st, /* i/o : State struct            */
+                 Word16 *lsp       /* i   : unquantized LSP's       */
+)
+{
+   Word16 i, dist, dist_min1, dist_min2, dist_th;
+ 
+   /* Check for a resonance:                             */
+   /* Find minimum distance between lsp[i] and lsp[i+1]  */
+ 
+   dist_min1 = MAX_16;                       move16 ();
+   for (i = 3; i < M-2; i++)
+   {
+      dist = sub(lsp[i], lsp[i+1]);
+
+      test ();
+      if (sub(dist, dist_min1) < 0)
+      {
+         dist_min1 = dist;                   move16 ();
+      }
+   }
+
+   dist_min2 = MAX_16;                       move16 ();
+   for (i = 1; i < 3; i++)
+   {
+      dist = sub(lsp[i], lsp[i+1]);
+
+      test ();
+      if (sub(dist, dist_min2) < 0)
+      {
+         dist_min2 = dist;                   move16 ();
+      }
+   }
+
+   if (test (), sub(lsp[1], 32000) > 0)
+   {
+      dist_th = 600;                         move16 ();
+   }
+   else if (test (), sub(lsp[1], 30500) > 0)
+   {
+      dist_th = 800;                         move16 ();
+   }
+   else
+   {
+      dist_th = 1100;                        move16 ();
+   }
+
+   test (); test ();
+   if (sub(dist_min1, 1500) < 0 ||
+       sub(dist_min2, dist_th) < 0)
+   {
+      st->count = add(st->count, 1);
+   }
+   else
+   {
+      st->count = 0;                         move16 ();
+   }
+   
+   /* Need 12 consecutive frames to set the flag */
+   test ();
+   if (sub(st->count, 12) >= 0)
+   {
+      st->count = 12;                        move16 ();
+      return 1;
+   }
+   else
+   {
+      return 0;
+   }
+}
+
+/***************************************************************************
+ *
+ *  Function:   Check_Gp_Clipping()                                          
+ *  Purpose:    Verify that the sum of the last (N_FRAME+1) pitch  
+ *              gains is under a certain threshold.              
+ *                                                                         
+ ***************************************************************************
+ */ 
+Word16 check_gp_clipping(tonStabState *st, /* i/o : State struct            */
+                         Word16 g_pitch    /* i   : pitch gain              */
+)
+{
+   Word16 i, sum;
+   
+   sum = shr(g_pitch, 3);          /* Division by 8 */
+   for (i = 0; i < N_FRAME; i++)
+   {
+      sum = add(sum, st->gp[i]);
+   }
+
+   test ();
+   if (sub(sum, GP_CLIP) > 0)
+   {
+      return 1;
+   }
+   else
+   {
+      return 0;
+   }
+}
+
+/***************************************************************************
+ *
+ *  Function:  Update_Gp_Clipping()                                          
+ *  Purpose:   Update past pitch gain memory
+ *                                                                         
+ ***************************************************************************
+ */
+void update_gp_clipping(tonStabState *st, /* i/o : State struct            */
+                        Word16 g_pitch    /* i   : pitch gain              */
+)
+{
+   Copy(&st->gp[1], &st->gp[0], N_FRAME-1);
+   st->gp[N_FRAME-1] = shr(g_pitch, 3);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/ton_stab.h	Mon May 06 23:23:40 2024 +0000
@@ -0,0 +1,73 @@
+/*
+********************************************************************************
+*
+*      GSM AMR-NB speech codec   R98   Version 7.6.0   December 12, 2001
+*                                R99   Version 3.3.0                
+*                                REL-4 Version 4.1.0                
+*
+********************************************************************************
+*
+*      File             : ton_stab.h
+*      Purpose          : Tone stabilization routines 
+*
+********************************************************************************
+*/
+#ifndef ton_stab_h
+#define ton_stab_h "$Id $"
+ 
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "tw_amr.h"
+#include "typedef.h"
+#include "cnst.h"
+
+/*
+********************************************************************************
+*                         LOCAL VARIABLES AND TABLES
+********************************************************************************
+*/
+
+/*
+********************************************************************************
+*                         DEFINITION OF DATA TYPES
+********************************************************************************
+*/
+
+/* state variable */
+typedef struct {
+
+   /* counters */
+   Word16 count;
+   
+   /* gain history Q11 */
+   Word16 gp[N_FRAME];
+   
+} tonStabState;
+
+/*
+********************************************************************************
+*                         DECLARATION OF PROTOTYPES
+********************************************************************************
+*/
+ 
+void ton_stab_reset (tonStabState *st);
+/* reset of pre processing state (i.e. set state memory to zero)
+   returns 0 on success
+ */
+
+Word16 check_lsp(tonStabState *st, /* i/o : State struct            */
+                 Word16 *lsp       /* i   : unquantized LSP's       */
+);
+
+Word16 check_gp_clipping(tonStabState *st, /* i/o : State struct            */
+                         Word16 g_pitch    /* i   : pitch gain              */
+);
+
+void update_gp_clipping(tonStabState *st, /* i/o : State struct            */
+                        Word16 g_pitch    /* i   : pitch gain              */
+);
+
+#endif