[xiph-commits] r12372 - in trunk/speex: include/speex libspeex
jm at svn.xiph.org
jm at svn.xiph.org
Sun Jan 21 20:58:10 PST 2007
Author: jm
Date: 2007-01-21 20:58:07 -0800 (Sun, 21 Jan 2007)
New Revision: 12372
Added:
trunk/speex/include/speex/speex_resampler.h
trunk/speex/libspeex/testresample.c
Modified:
trunk/speex/libspeex/resample.c
Log:
separated into header, source and test program.
Added: trunk/speex/include/speex/speex_resampler.h
===================================================================
--- trunk/speex/include/speex/speex_resampler.h 2007-01-22 03:49:56 UTC (rev 12371)
+++ trunk/speex/include/speex/speex_resampler.h 2007-01-22 04:58:07 UTC (rev 12372)
@@ -0,0 +1,77 @@
+/* Copyright (C) 2007 Jean-Marc Valin
+
+ File: speex_resampler.h
+ Resampling code
+
+ The design goals of this code are:
+ - Very fast algorithm
+ - Low memory requirement
+ - Good *perceptual* quality (and not best SNR)
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are
+ met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ 3. The name of the author may not be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#ifndef SPEEX_RESAMPLER_H
+#define SPEEX_RESAMPLER_H
+
+#ifdef OUTSIDE_SPEEX
+#else
+#include "speex/speex_types.h"
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct SpeexResamplerState_;
+typedef struct SpeexResamplerState_ SpeexResamplerState;
+//typedef SpeexResamplerState;
+
+SpeexResamplerState *speex_resampler_init(int nb_channels, int in_rate, int out_rate, int in_rate_den, int out_rate_den);
+
+void speex_resampler_destroy(SpeexResamplerState *st);
+
+void speex_resampler_process_float(SpeexResamplerState *st, int channel_index, const float *in, int *in_len, float *out, int *out_len);
+
+void speex_resampler_process_interleaved_float(SpeexResamplerState *st, const float *in, int *in_len, float *out, int *out_len);
+
+void speex_resample_set_rate(SpeexResamplerState *st, int in_rate, int out_rate, int in_rate_den, int out_rate_den);
+
+void speex_resample_set_input_stride(SpeexResamplerState *st, int stride);
+
+void speex_resample_set_output_stride(SpeexResamplerState *st, int stride);
+
+void speex_resample_skip_zeros(SpeexResamplerState *st);
+
+void speex_resample_reset_mem(SpeexResamplerState *st);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
Modified: trunk/speex/libspeex/resample.c
===================================================================
--- trunk/speex/libspeex/resample.c 2007-01-22 03:49:56 UTC (rev 12371)
+++ trunk/speex/libspeex/resample.c 2007-01-22 04:58:07 UTC (rev 12372)
@@ -35,17 +35,32 @@
POSSIBILITY OF SUCH DAMAGE.
*/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef OUTSIDE_SPEEX
+#include <stdlib.h>
+void *speex_alloc (int size) {return calloc(size,1);}
+void *speex_realloc (void *ptr, int size) {return realloc(ptr, size);}
+void speex_free (void *ptr) {free(ptr);}
+#else
#include "misc.h"
+#endif
+
+
#include <math.h>
-#include <stdio.h>
-
+#include "speex/speex_resampler.h"
+
/*#define float double*/
#define FILTER_SIZE 64
#define OVERSAMPLE 8
+#define IMAX(a,b) ((a) > (b) ? (a) : (b))
+
typedef enum {SPEEX_RESAMPLER_DIRECT=0, SPEEX_RESAMPLER_INTERPOLATE=1} SpeexSincType;
-typedef struct {
+struct SpeexResamplerState_ {
int in_rate;
int out_rate;
int num_rate;
@@ -62,7 +77,7 @@
int in_stride;
int out_stride;
SpeexSincType type;
-} SpeexResamplerState;
+} ;
/* The slow way of computing a sinc for the table. Should improve that some day */
@@ -77,8 +92,6 @@
return sin(M_PI*x)/(M_PI*x) * (.5+.5*cos(2*x*M_PI/N));
}
-void speex_resample_set_rate(SpeexResamplerState *st, int in_rate, int out_rate, int in_rate_den, int out_rate_den);
-
SpeexResamplerState *speex_resampler_init(int nb_channels, int in_rate, int out_rate, int in_rate_den, int out_rate_den)
{
int i;
@@ -228,7 +241,7 @@
st->num_rate = in_rate;
st->den_rate = out_rate;
/* FIXME: This is terribly inefficient, but who cares (at least for now)? */
- for (fact=2;fact<=sqrt(MAX32(in_rate, out_rate));fact++)
+ for (fact=2;fact<=sqrt(IMAX(in_rate, out_rate));fact++)
{
while ((st->num_rate % fact == 0) && (st->den_rate % fact == 0))
{
@@ -266,7 +279,7 @@
}
}
st->type = SPEEX_RESAMPLER_DIRECT;
- fprintf (stderr, "resampler uses direct sinc table and normalised cutoff %f\n", cutoff);
+ /*fprintf (stderr, "resampler uses direct sinc table and normalised cutoff %f\n", cutoff);*/
} else {
if (!st->sinc_table)
st->sinc_table = (float *)speex_alloc((st->filt_len*OVERSAMPLE+8)*sizeof(float));
@@ -278,7 +291,7 @@
for (i=-4;i<OVERSAMPLE*st->filt_len+4;i++)
st->sinc_table[i+4] = sinc(cutoff*(i/(float)OVERSAMPLE - st->filt_len/2), st->filt_len);
st->type = SPEEX_RESAMPLER_INTERPOLATE;
- fprintf (stderr, "resampler uses interpolated sinc table and normalised cutoff %f\n", cutoff);
+ /*fprintf (stderr, "resampler uses interpolated sinc table and normalised cutoff %f\n", cutoff);*/
}
}
@@ -305,43 +318,3 @@
st->mem[i] = 0;
}
-#define NN 256
-
-int main(int argc, char **argv)
-{
- int i;
- short *in;
- short *out;
- float *fin, *fout;
- SpeexResamplerState *st = speex_resampler_init(1, 8000, 12000, 1, 1);
- speex_resample_set_rate(st, 8000, 16000, 1, 1);
- speex_resample_skip_zeros(st);
-
- in = speex_alloc(NN*sizeof(short));
- out = speex_alloc(2*NN*sizeof(short));
- fin = speex_alloc(NN*sizeof(float));
- fout = speex_alloc(2*NN*sizeof(float));
- while (1)
- {
- int in_len;
- int out_len;
- fread(in, sizeof(short), NN, stdin);
- if (feof(stdin))
- break;
- for (i=0;i<NN;i++)
- fin[i]=in[i];
- in_len = NN;
- out_len = 2*NN;
- speex_resampler_process_float(st, 0, fin, &in_len, fout, &out_len);
- for (i=0;i<out_len;i++)
- out[i]=floor(.5+fout[i]);
- fwrite(out, sizeof(short), out_len, stdout);
- }
- speex_resampler_destroy(st);
- speex_free(in);
- speex_free(out);
- speex_free(fin);
- speex_free(fout);
- return 0;
-}
-
Added: trunk/speex/libspeex/testresample.c
===================================================================
--- trunk/speex/libspeex/testresample.c 2007-01-22 03:49:56 UTC (rev 12371)
+++ trunk/speex/libspeex/testresample.c 2007-01-22 04:58:07 UTC (rev 12372)
@@ -0,0 +1,81 @@
+/* Copyright (C) 2007 Jean-Marc Valin
+
+ File: testresample.c
+ Testing the resampling code
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are
+ met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ 3. The name of the author may not be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include "speex/speex_resampler.h"
+#include <math.h>
+#include <stdlib.h>
+
+#define NN 256
+
+int main(int argc, char **argv)
+{
+ int i;
+ short *in;
+ short *out;
+ float *fin, *fout;
+ SpeexResamplerState *st = speex_resampler_init(1, 8000, 12000, 1, 1);
+ speex_resample_set_rate(st, 8000, 16000, 1, 1);
+ speex_resample_skip_zeros(st);
+
+ in = malloc(NN*sizeof(short));
+ out = malloc(2*NN*sizeof(short));
+ fin = malloc(NN*sizeof(float));
+ fout = malloc(2*NN*sizeof(float));
+ while (1)
+ {
+ int in_len;
+ int out_len;
+ fread(in, sizeof(short), NN, stdin);
+ if (feof(stdin))
+ break;
+ for (i=0;i<NN;i++)
+ fin[i]=in[i];
+ in_len = NN;
+ out_len = 2*NN;
+ speex_resampler_process_float(st, 0, fin, &in_len, fout, &out_len);
+ for (i=0;i<out_len;i++)
+ out[i]=floor(.5+fout[i]);
+ fwrite(out, sizeof(short), out_len, stdout);
+ }
+ speex_resampler_destroy(st);
+ free(in);
+ free(out);
+ free(fin);
+ free(fout);
+ return 0;
+}
+
More information about the commits
mailing list