]> code.delx.au - pulseaudio/blobdiff - polyp/resampler.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / resampler.c
index 241f97c490e52f4afae2405166297aaef8f127c6..4ddcfa40359ca0ba67b8d7fb31cd6a31f39b3c7b 100644 (file)
@@ -4,7 +4,7 @@
   This file is part of polypaudio.
  
   polypaudio is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published
+  it under the terms of the GNU Lesser General Public License as published
   by the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.
  
@@ -13,7 +13,7 @@
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.
  
-  You should have received a copy of the GNU General Public License
+  You should have received a copy of the GNU Lesser General Public License
   along with polypaudio; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA.
@@ -23,7 +23,6 @@
 #include <config.h>
 #endif
 
-#include <stdlib.h>
 #include <assert.h>
 
 #include <samplerate.h>
@@ -31,6 +30,7 @@
 #include "resampler.h"
 #include "sconv.h"
 #include "xmalloc.h"
+#include "log.h"
 
 struct pa_resampler {
     struct pa_sample_spec i_ss, o_ss;
@@ -43,9 +43,11 @@ struct pa_resampler {
     pa_convert_to_float32_func_t to_float32_func;
     pa_convert_from_float32_func_t from_float32_func;
     SRC_STATE *src_state;
+
+    struct pa_memblock_stat *memblock_stat;
 };
 
-struct pa_resampler* pa_resampler_new(const struct pa_sample_spec *a, const struct pa_sample_spec *b) {
+struct pa_resampler* pa_resampler_new(const struct pa_sample_spec *a, const struct pa_sample_spec *b, struct pa_memblock_stat *s, int resample_method) {
     struct pa_resampler *r = NULL;
     int err;
     assert(a && b && pa_sample_spec_valid(a) && pa_sample_spec_valid(b));
@@ -65,12 +67,9 @@ struct pa_resampler* pa_resampler_new(const struct pa_sample_spec *a, const stru
     r->i_buf = r->o_buf = NULL;
     r->i_alloc = r->o_alloc = 0;
 
-    if (a->rate != b->rate) {
-        r->src_state = src_new(SRC_SINC_FASTEST, r->channels, &err);
-        if (err != 0 || !r->src_state)
-            goto fail;
-    } else
-        r->src_state = NULL;
+    r->src_state = src_new(resample_method, r->channels, &err);
+    if (err != 0 || !r->src_state)
+        goto fail;
 
     r->i_ss = *a;
     r->o_ss = *b;
@@ -82,6 +81,8 @@ struct pa_resampler* pa_resampler_new(const struct pa_sample_spec *a, const stru
     r->from_float32_func = pa_get_convert_from_float32_function(b->format);
 
     assert(r->to_float32_func && r->from_float32_func);
+
+    r->memblock_stat = s;
     
     return r;
     
@@ -116,6 +117,8 @@ void pa_resampler_run(struct pa_resampler *r, const struct pa_memchunk *in, stru
     /* How many input samples? */
     ins = in->length/r->i_sz;
 
+/*     pa_log("%u / %u = %u\n", in->length, r->i_sz, ins); */
+
     /* How much space for output samples? */
     if (r->src_state)
         ons = (ins*r->o_ss.rate/r->i_ss.rate)+1024;
@@ -133,16 +136,21 @@ void pa_resampler_run(struct pa_resampler *r, const struct pa_memchunk *in, stru
         eff_ins = ins;
         eff_ons = ons;
     }
+
+/*     pa_log("eff_ins = %u \n", eff_ins); */
+    
     
-    out->memblock = pa_memblock_new(out->length = (ons*r->o_sz));
+    out->memblock = pa_memblock_new(out->length = (ons*r->o_sz), r->memblock_stat);
     out->index = 0;
     assert(out->memblock);
 
     if (r->i_alloc < eff_ins)
         r->i_buf = pa_xrealloc(r->i_buf, sizeof(float) * (r->i_alloc = eff_ins));
     assert(r->i_buf);
-    
-    r->to_float32_func(eff_ins, in->memblock->data+in->index, i_nchannels, r->i_buf);
+
+/*     pa_log("eff_ins = %u \n", eff_ins); */
+
+    r->to_float32_func(eff_ins, (uint8_t*) in->memblock->data+in->index, i_nchannels, r->i_buf);
 
     if (r->src_state) {
         int ret;
@@ -175,6 +183,22 @@ void pa_resampler_run(struct pa_resampler *r, const struct pa_memchunk *in, stru
     } else
         cbuf = r->i_buf;
 
-    r->from_float32_func(eff_ons, cbuf, out->memblock->data+out->index, o_nchannels);
+    if (eff_ons)
+        r->from_float32_func(eff_ons, cbuf, (uint8_t*)out->memblock->data+out->index, o_nchannels);
     out->length = ons*r->o_sz;
+
+
+    if (!out->length) {
+        pa_memblock_unref(out->memblock);
+        out->memblock = NULL;
+    }
+}
+
+void pa_resampler_set_input_rate(struct pa_resampler *r, uint32_t rate) {
+    int ret;
+    assert(r);
+
+    r->i_ss.rate = rate;
+    ret = src_set_ratio(r->src_state, (double) r->o_ss.rate / r->i_ss.rate);
+    assert(ret == 0);
 }