[xiph-commits] r12823 - in trunk/speex: include/speex libspeex

jm at svn.xiph.org jm at svn.xiph.org
Sun Apr 1 03:40:58 PDT 2007


Author: jm
Date: 2007-04-01 03:40:54 -0700 (Sun, 01 Apr 2007)
New Revision: 12823

Modified:
   trunk/speex/include/speex/speex_resampler.h
   trunk/speex/libspeex/resample.c
   trunk/speex/libspeex/testresample.c
Log:
Adding error codes.


Modified: trunk/speex/include/speex/speex_resampler.h
===================================================================
--- trunk/speex/include/speex/speex_resampler.h	2007-03-30 11:53:02 UTC (rev 12822)
+++ trunk/speex/include/speex/speex_resampler.h	2007-04-01 10:40:54 UTC (rev 12823)
@@ -94,7 +94,17 @@
 #define SPEEX_RESAMPLER_QUALITY_DEFAULT 4
 #define SPEEX_RESAMPLER_QUALITY_VOIP 3
 #define SPEEX_RESAMPLER_QUALITY_DESKTOP 5
+
+enum {
+   RESAMPLER_ERR_SUCCESS         = 0,
+   RESAMPLER_ERR_ALLOC_FAILED    = 1,
+   RESAMPLER_ERR_BAD_STATE       = 2,
+   RESAMPLER_ERR_INVALID_ARG     = 3,
+   RESAMPLER_ERR_PTR_OVERLAP     = 4,
    
+   RESAMPLER_ERR_MAX_ERROR
+};
+
 struct SpeexResamplerState_;
 typedef struct SpeexResamplerState_ SpeexResamplerState;
 
@@ -110,7 +120,8 @@
 SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, 
                                           spx_uint32_t in_rate, 
                                           spx_uint32_t out_rate, 
-                                          int quality);
+                                          int quality,
+                                          int *err);
 
 /** Create a new resampler with fractional input/output rates. The sampling 
  * rate ratio is an arbitrary rational number with both the numerator and 
@@ -130,7 +141,8 @@
                                                spx_uint32_t ratio_den, 
                                                spx_uint32_t in_rate, 
                                                spx_uint32_t out_rate, 
-                                               int quality);
+                                               int quality,
+                                               int *err);
 
 /** Destroy a resampler state.
  * @param st Resampler state
@@ -147,7 +159,7 @@
  * @param out Output buffer
  * @param out_len Size of the output buffer. Returns the number of samples written
  */
-void speex_resampler_process_float(SpeexResamplerState *st, 
+int speex_resampler_process_float(SpeexResamplerState *st, 
                                    spx_uint32_t channel_index, 
                                    const float *in, 
                                    spx_uint32_t *in_len, 
@@ -164,7 +176,7 @@
  * @param out Output buffer
  * @param out_len Size of the output buffer. Returns the number of samples written
  */
-void speex_resampler_process_int(SpeexResamplerState *st, 
+int speex_resampler_process_int(SpeexResamplerState *st, 
                                  spx_uint32_t channel_index, 
                                  const spx_int16_t *in, 
                                  spx_uint32_t *in_len, 
@@ -180,7 +192,7 @@
  * @param out_len Size of the output buffer. Returns the number of samples written.
  * This is all per-channel.
  */
-void speex_resampler_process_interleaved_float(SpeexResamplerState *st, 
+int speex_resampler_process_interleaved_float(SpeexResamplerState *st, 
                                                const float *in, 
                                                spx_uint32_t *in_len, 
                                                float *out, 
@@ -195,7 +207,7 @@
  * @param out_len Size of the output buffer. Returns the number of samples written.
  * This is all per-channel.
  */
-void speex_resampler_process_interleaved_int(SpeexResamplerState *st, 
+int speex_resampler_process_interleaved_int(SpeexResamplerState *st, 
                                              const spx_int16_t *in, 
                                              spx_uint32_t *in_len, 
                                              spx_int16_t *out, 
@@ -206,7 +218,7 @@
  * @param in_rate Input sampling rate (integer number of Hz).
  * @param out_rate Output sampling rate (integer number of Hz).
  */
-void speex_resampler_set_rate(SpeexResamplerState *st, 
+int speex_resampler_set_rate(SpeexResamplerState *st, 
                               spx_uint32_t in_rate, 
                               spx_uint32_t out_rate);
 
@@ -227,7 +239,7 @@
  * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
  * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
  */
-void speex_resampler_set_rate_frac(SpeexResamplerState *st, 
+int speex_resampler_set_rate_frac(SpeexResamplerState *st, 
                                    spx_uint32_t ratio_num, 
                                    spx_uint32_t ratio_den, 
                                    spx_uint32_t in_rate, 
@@ -248,7 +260,7 @@
  * @param quality Resampling quality between 0 and 10, where 0 has poor 
  * quality and 10 has very high quality.
  */
-void speex_resampler_set_quality(SpeexResamplerState *st, 
+int speex_resampler_set_quality(SpeexResamplerState *st, 
                                  int quality);
 
 /** Get the conversion quality.
@@ -295,12 +307,12 @@
  * is the same for the first frame).
  * @param st Resampler state
  */
-void speex_resampler_skip_zeros(SpeexResamplerState *st);
+int speex_resampler_skip_zeros(SpeexResamplerState *st);
 
 /** Reset a resampler so a new (unrelated) stream can be processed.
  * @param st Resampler state
  */
-void speex_resampler_reset_mem(SpeexResamplerState *st);
+int speex_resampler_reset_mem(SpeexResamplerState *st);
 
 #ifdef __cplusplus
 }

Modified: trunk/speex/libspeex/resample.c
===================================================================
--- trunk/speex/libspeex/resample.c	2007-03-30 11:53:02 UTC (rev 12822)
+++ trunk/speex/libspeex/resample.c	2007-04-01 10:40:54 UTC (rev 12823)
@@ -85,6 +85,9 @@
 
 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
 
+#ifndef NULL
+#define NULL 0
+#endif
 
 typedef int (*resampler_basic_func)(SpeexResamplerState *, spx_uint32_t , const spx_word16_t *, spx_uint32_t *, spx_word16_t *, spx_uint32_t *);
 
@@ -660,15 +663,22 @@
 
 }
 
-SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality)
+SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err)
 {
-   return speex_resampler_init_frac(nb_channels, in_rate, out_rate, in_rate, out_rate, quality);
+   return speex_resampler_init_frac(nb_channels, in_rate, out_rate, in_rate, out_rate, quality, err);
 }
 
-SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels, spx_uint32_t ratio_num, spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality)
+SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels, spx_uint32_t ratio_num, spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err)
 {
    spx_uint32_t i;
-   SpeexResamplerState *st = (SpeexResamplerState *)speex_alloc(sizeof(SpeexResamplerState));
+   SpeexResamplerState *st;
+   if (quality > 10 || quality < 0)
+   {
+      if (err)
+         *err = RESAMPLER_ERR_INVALID_ARG;
+      return NULL;
+   }
+   st = (SpeexResamplerState *)speex_alloc(sizeof(SpeexResamplerState));
    st->initialised = 0;
    st->started = 0;
    st->in_rate = 0;
@@ -705,6 +715,9 @@
    update_filter(st);
    
    st->initialised = 1;
+   if (err)
+      *err = RESAMPLER_ERR_SUCCESS;
+
    return st;
 }
 
@@ -720,7 +733,7 @@
 
 
 
-static void speex_resampler_process_native(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_word16_t *in, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
+static int speex_resampler_process_native(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_word16_t *in, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
 {
    int j=0;
    int N = st->filt_len;
@@ -767,12 +780,13 @@
    for (;j<N-1;j++)
       mem[j] = in[st->in_stride*(j+*in_len-N+1)];
    
+   return RESAMPLER_ERR_SUCCESS;
 }
 
 #define FIXED_STACK_ALLOC 1024
 
 #ifdef FIXED_POINT
-void speex_resampler_process_float(SpeexResamplerState *st, spx_uint32_t channel_index, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
+int speex_resampler_process_float(SpeexResamplerState *st, spx_uint32_t channel_index, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
 {
    spx_uint32_t i;
    int istride_save, ostride_save;
@@ -824,17 +838,18 @@
    *in_len -= ilen;
    *out_len -= olen;   
 #endif
+   return RESAMPLER_ERR_SUCCESS;
 }
-void speex_resampler_process_int(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
+int speex_resampler_process_int(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
 {
-   speex_resampler_process_native(st, channel_index, in, in_len, out, out_len);
+   return speex_resampler_process_native(st, channel_index, in, in_len, out, out_len);
 }
 #else
-void speex_resampler_process_float(SpeexResamplerState *st, spx_uint32_t channel_index, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
+int speex_resampler_process_float(SpeexResamplerState *st, spx_uint32_t channel_index, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
 {
-   speex_resampler_process_native(st, channel_index, in, in_len, out, out_len);
+   return speex_resampler_process_native(st, channel_index, in, in_len, out, out_len);
 }
-void speex_resampler_process_int(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
+int speex_resampler_process_int(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
 {
    spx_uint32_t i;
    int istride_save, ostride_save;
@@ -886,10 +901,11 @@
    *in_len -= ilen;
    *out_len -= olen;   
 #endif
+   return RESAMPLER_ERR_SUCCESS;
 }
 #endif
 
-void speex_resampler_process_interleaved_float(SpeexResamplerState *st, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
+int speex_resampler_process_interleaved_float(SpeexResamplerState *st, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
 {
    spx_uint32_t i;
    int istride_save, ostride_save;
@@ -902,9 +918,10 @@
    }
    st->in_stride = istride_save;
    st->out_stride = ostride_save;
+   return RESAMPLER_ERR_SUCCESS;
 }
 
-void speex_resampler_process_interleaved_int(SpeexResamplerState *st, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
+int speex_resampler_process_interleaved_int(SpeexResamplerState *st, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
 {
    spx_uint32_t i;
    int istride_save, ostride_save;
@@ -917,11 +934,12 @@
    }
    st->in_stride = istride_save;
    st->out_stride = ostride_save;
+   return RESAMPLER_ERR_SUCCESS;
 }
 
-void speex_resampler_set_rate(SpeexResamplerState *st, spx_uint32_t in_rate, spx_uint32_t out_rate)
+int speex_resampler_set_rate(SpeexResamplerState *st, spx_uint32_t in_rate, spx_uint32_t out_rate)
 {
-   speex_resampler_set_rate_frac(st, in_rate, out_rate, in_rate, out_rate);
+   return speex_resampler_set_rate_frac(st, in_rate, out_rate, in_rate, out_rate);
 }
 
 void speex_resampler_get_rate(SpeexResamplerState *st, spx_uint32_t *in_rate, spx_uint32_t *out_rate)
@@ -930,11 +948,11 @@
    *out_rate = st->out_rate;
 }
 
-void speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t ratio_num, spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate)
+int speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t ratio_num, spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate)
 {
    int fact;
    if (st->in_rate == in_rate && st->out_rate == out_rate && st->num_rate == ratio_num && st->den_rate == ratio_den)
-      return;
+      return RESAMPLER_ERR_SUCCESS;
    
    st->in_rate = in_rate;
    st->out_rate = out_rate;
@@ -952,6 +970,7 @@
       
    if (st->initialised)
       update_filter(st);
+   return RESAMPLER_ERR_SUCCESS;
 }
 
 void speex_resampler_get_ratio(SpeexResamplerState *st, spx_uint32_t *ratio_num, spx_uint32_t *ratio_den)
@@ -960,17 +979,16 @@
    *ratio_den = st->den_rate;
 }
 
-void speex_resampler_set_quality(SpeexResamplerState *st, int quality)
+int speex_resampler_set_quality(SpeexResamplerState *st, int quality)
 {
-   if (quality < 0)
-      quality = 0;
-   if (quality > 10)
-      quality = 10;
+   if (quality > 10 || quality < 0)
+      return RESAMPLER_ERR_INVALID_ARG;
    if (st->quality == quality)
-      return;
+      return RESAMPLER_ERR_SUCCESS;
    st->quality = quality;
    if (st->initialised)
       update_filter(st);
+   return RESAMPLER_ERR_SUCCESS;
 }
 
 void speex_resampler_get_quality(SpeexResamplerState *st, int *quality)
@@ -998,17 +1016,19 @@
    *stride = st->out_stride;
 }
 
-void speex_resampler_skip_zeros(SpeexResamplerState *st)
+int speex_resampler_skip_zeros(SpeexResamplerState *st)
 {
    spx_uint32_t i;
    for (i=0;i<st->nb_channels;i++)
       st->last_sample[i] = st->filt_len/2;
+   return RESAMPLER_ERR_SUCCESS;
 }
 
-void speex_resampler_reset_mem(SpeexResamplerState *st)
+int speex_resampler_reset_mem(SpeexResamplerState *st)
 {
    spx_uint32_t i;
    for (i=0;i<st->nb_channels*(st->filt_len-1);i++)
       st->mem[i] = 0;
+   return RESAMPLER_ERR_SUCCESS;
 }
 

Modified: trunk/speex/libspeex/testresample.c
===================================================================
--- trunk/speex/libspeex/testresample.c	2007-03-30 11:53:02 UTC (rev 12822)
+++ trunk/speex/libspeex/testresample.c	2007-04-01 10:40:54 UTC (rev 12823)
@@ -41,15 +41,15 @@
 
 #define NN 256
 
-int main(int argc, char **argv)
+int main()
 {
-   int i;
+   spx_uint32_t i;
    short *in;
    short *out;
    float *fin, *fout;
    int count = 0;
-   SpeexResamplerState *st = speex_resampler_init(1, 8000, 12000, 10);
-   speex_resampler_set_rate(st, 8000, 15999);
+   SpeexResamplerState *st = speex_resampler_init(1, 8000, 12000, 10, NULL);
+   speex_resampler_set_rate(st, 96000, 44100);
    speex_resampler_skip_zeros(st);
    
    in = malloc(NN*sizeof(short));
@@ -58,8 +58,8 @@
    fout = malloc(2*NN*sizeof(float));
    while (1)
    {
-      int in_len;
-      int out_len;
+      spx_uint32_t in_len;
+      spx_uint32_t out_len;
       fread(in, sizeof(short), NN, stdin);
       if (feof(stdin))
          break;



More information about the commits mailing list