[xiph-commits] r13456 - trunk/ghost/libghost

jm at svn.xiph.org jm at svn.xiph.org
Mon Aug 6 16:42:26 PDT 2007


Author: jm
Date: 2007-08-06 16:42:26 -0700 (Mon, 06 Aug 2007)
New Revision: 13456

Added:
   trunk/ghost/libghost/adpcm.h
Modified:
   trunk/ghost/libghost/Makefile.am
   trunk/ghost/libghost/adpcm.c
   trunk/ghost/libghost/ghost.c
   trunk/ghost/libghost/ghost.h
   trunk/ghost/libghost/vorbis_psy.c
Log:
Crappy (but sort of vaguely working) adpcm-like code


Modified: trunk/ghost/libghost/Makefile.am
===================================================================
--- trunk/ghost/libghost/Makefile.am	2007-08-06 19:20:25 UTC (rev 13455)
+++ trunk/ghost/libghost/Makefile.am	2007-08-06 23:42:26 UTC (rev 13456)
@@ -16,8 +16,8 @@
 
 libghost_la_LDFLAGS = -version-info @GHOST_LT_CURRENT@:@GHOST_LT_REVISION@:@GHOST_LT_AGE@
 
-noinst_HEADERS = ghost.h pitch.h lifting.h smallft.h fftwrap.h \
-	arch.h misc.h vorbis_psy.h lpc.h
+noinst_HEADERS = adpcm.h arch.h fftwrap.h ghost.h lifting.h lpc.h misc.h \
+	pitch.h smallft.h vorbis_psy.h
 
 noinst_PROGRAMS = testghost
 testghost_SOURCES = testghost.c

Modified: trunk/ghost/libghost/adpcm.c
===================================================================
--- trunk/ghost/libghost/adpcm.c	2007-08-06 19:20:25 UTC (rev 13455)
+++ trunk/ghost/libghost/adpcm.c	2007-08-06 23:42:26 UTC (rev 13456)
@@ -21,12 +21,17 @@
 */
 
 #include <math.h>
+#include <assert.h>
+#include <stdlib.h>
+#include "adpcm.h"
 
-typedef struct {
+struct ADPCMState_ {
    int N;
    float *coef;
    float *mem;
-} ADPCMState;
+   float E;
+   float alpha;
+};
 
 ADPCMState *adpcm_init(int N)
 {
@@ -40,26 +45,49 @@
       st->coef[i] = 0;
       st->mem[i] = 0;
    }
+   st->E = 1;
+   st->alpha = .3/N;
+   return st;
 }
 
+#define MIN(a,b) ((a) < (b) ? (a) : (b)) 
+
 void adpcm_quant(ADPCMState *st, float *x, int *q, int len)
 {
    int i,j;
    int N=st->N;
    float *a = st->coef;
-
+   float *mem = st->mem;
+   float mu=.05;
+   
    for (i=0;i<len;i++)
    {
       float p = 0;
+      float e;
       /* Prediction: conceptual code, this will segfault (or worse) */
-      for (j=0;j<N;j++)
+      for (j=i;j<N;j++)
+         p += a[j]*mem[j-i];
+      for (j=0;j<MIN(i,N);j++)
          p += a[j]*x[i-j-1];
+      
       /* Difference */
-      q[i] = rint(x[i]-p);
+      e = x[i]-p;
+      q[i] = rint(e);
       x[i] = q[i]+p;
+      
+      /* Energy update */
+      st->E = (1-st->alpha)*st->E + st->alpha*x[i]*x[i];
+      if (st->E < 1)
+         st->E = 1;
+      
       /* Adaptation: conceptual code, this will segfault (or worse) */
-      for (j=0;j<N;j++)
-         a[j] += q[i]*q[i-j-1]/(q[i]*q[i]); 
+      for (j=i;j<N;j++)
+         a[j] += mu*e*mem[j-i]/st->E;
+      for (j=0;j<MIN(i,N);j++)
+         a[j] += mu*e*x[i-j-1]/st->E;
    }
+   assert(len >= N);
+   for (i=0;i<N;i++)
+      mem[i] = x[len-i-1];
 }
 

Added: trunk/ghost/libghost/adpcm.h
===================================================================
--- trunk/ghost/libghost/adpcm.h	                        (rev 0)
+++ trunk/ghost/libghost/adpcm.h	2007-08-06 23:42:26 UTC (rev 13456)
@@ -0,0 +1,29 @@
+/**
+   @file adpcm.h
+   @brief ADPCM-like code
+ */
+
+/* Copyright (C) 2007
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+struct ADPCMState_;
+
+typedef struct ADPCMState_ ADPCMState;
+
+ADPCMState *adpcm_init(int N);
+
+void adpcm_quant(ADPCMState *st, float *x, int *q, int len);

Modified: trunk/ghost/libghost/ghost.c
===================================================================
--- trunk/ghost/libghost/ghost.c	2007-08-06 19:20:25 UTC (rev 13455)
+++ trunk/ghost/libghost/ghost.c	2007-08-06 23:42:26 UTC (rev 13456)
@@ -33,7 +33,7 @@
 #define PCM_BUF_SIZE 2048
 
 #define SINUSOIDS 30
-#define MASK_LPC_ORDER 12
+#define MASK_LPC_ORDER 10
 
 void fir_mem2(const spx_sig_t *x, const spx_coef_t *num, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
 {
@@ -139,6 +139,8 @@
    st->lpc_fft = spx_fft_init(st->lpc_length);
    for (i=0;i<PCM_BUF_SIZE;i++)
       st->big_window[i] = .5-.5*cos(2*M_PI*(i+1)/PCM_BUF_SIZE);
+   
+   st->adpcm = adpcm_init(8);
    return st;
 }
 
@@ -154,6 +156,7 @@
    float curve[PCM_BUF_SIZE>>1];
    float awk1[MASK_LPC_ORDER], awk2[MASK_LPC_ORDER];
    float mask_gain;
+   int q[st->advance];
    
    for (i=0;i<PCM_BUF_SIZE-st->advance;i++)
       st->pcm_buf[i] = st->pcm_buf[i+st->advance];
@@ -310,8 +313,13 @@
          printf ("%f\n", noise[i]);
       printf ("\n");*/
       for (i=0;i<st->advance;i++)
-         noise[i] = 16*floor(.5+.0625*noise[i]);
+         noise[i] = noise[i]/16;
+      adpcm_quant(st->adpcm, noise, q, st->advance);
+      //for (i=0;i<st->advance;i++)
+      //   printf ("%f %d\n", noise[i], q[i]);
       for (i=0;i<st->advance;i++)
+         noise[i] = 16*noise[i];
+      for (i=0;i<st->advance;i++)
          noise[i] *= mask_gain;
       iir_mem2(noise, awk1, noise, st->advance, MASK_LPC_ORDER, st->noise_mem2);
       
@@ -319,7 +327,7 @@
       pcm[i] = st->current_frame[i]-st->new_noise[i];*/
       
       for (i=0;i<st->advance;i++)
-         pcm[i] = /*st->current_frame[i]-*/st->new_noise[i] /*+ noise[i]*/;
+         pcm[i] = st->current_frame[i]-st->new_noise[i] /*+ noise[i]*/;
       
    }
    

Modified: trunk/ghost/libghost/ghost.h
===================================================================
--- trunk/ghost/libghost/ghost.h	2007-08-06 19:20:25 UTC (rev 13455)
+++ trunk/ghost/libghost/ghost.h	2007-08-06 23:42:26 UTC (rev 13456)
@@ -24,6 +24,7 @@
 #define _GHOST_H
 
 #include "vorbis_psy.h"
+#include "adpcm.h"
 
 typedef struct {
    float *pcm_buf;
@@ -53,6 +54,7 @@
    
    void *big_fft;
    void *lpc_fft;
+   ADPCMState *adpcm;
 } GhostEncState;
 
 GhostEncState *ghost_encoder_state_new(int sampling_rate);

Modified: trunk/ghost/libghost/vorbis_psy.c
===================================================================
--- trunk/ghost/libghost/vorbis_psy.c	2007-08-06 19:20:25 UTC (rev 13455)
+++ trunk/ghost/libghost/vorbis_psy.c	2007-08-06 23:42:26 UTC (rev 13456)
@@ -465,6 +465,7 @@
    ac[2*len-1] = curve[len-1];
    
    spx_drft_backward(&psy->lookup, ac);
+   ac[0] *= 1.0003;
    _spx_lpc(awk1, ac, ord);
    tmp = 1.;
    for (i=0;i<ord;i++)



More information about the commits mailing list