4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <samplerate.h>
30 #include <liboil/liboilfuncs.h>
31 #include <liboil/liboil.h>
33 #include <polyp/xmalloc.h>
35 #include <polypcore/sconv.h>
36 #include <polypcore/log.h>
38 #include "resampler.h"
41 pa_resample_method_t resample_method;
42 pa_sample_spec i_ss, o_ss;
43 pa_channel_map i_cm, o_cm;
45 pa_memblock_stat *memblock_stat;
47 void (*impl_free)(pa_resampler *r);
48 void (*impl_update_input_rate)(pa_resampler *r, uint32_t rate);
49 void (*impl_run)(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out);
53 struct impl_libsamplerate {
54 float* buf1, *buf2, *buf3, *buf4;
55 unsigned buf1_samples, buf2_samples, buf3_samples, buf4_samples;
57 pa_convert_to_float32ne_func_t to_float32ne_func;
58 pa_convert_from_float32ne_func_t from_float32ne_func;
61 int map_table[PA_CHANNELS_MAX][PA_CHANNELS_MAX];
70 static int libsamplerate_init(pa_resampler*r);
71 static int trivial_init(pa_resampler*r);
73 pa_resampler* pa_resampler_new(
74 const pa_sample_spec *a,
75 const pa_channel_map *am,
76 const pa_sample_spec *b,
77 const pa_channel_map *bm,
79 pa_resample_method_t resample_method) {
81 pa_resampler *r = NULL;
85 assert(pa_sample_spec_valid(a));
86 assert(pa_sample_spec_valid(b));
87 assert(resample_method != PA_RESAMPLER_INVALID);
89 r = pa_xnew(pa_resampler, 1);
92 r->resample_method = resample_method;
95 r->impl_update_input_rate = NULL;
98 /* Fill sample specs */
105 pa_channel_map_init_auto(&r->i_cm, r->i_ss.channels, PA_CHANNEL_MAP_DEFAULT);
110 pa_channel_map_init_auto(&r->o_cm, r->o_ss.channels, PA_CHANNEL_MAP_DEFAULT);
112 r->i_fz = pa_frame_size(a);
113 r->o_fz = pa_frame_size(b);
115 /* Choose implementation */
116 if (a->channels != b->channels ||
117 a->format != b->format ||
118 !pa_channel_map_equal(&r->i_cm, &r->o_cm) ||
119 resample_method != PA_RESAMPLER_TRIVIAL) {
121 /* Use the libsamplerate based resampler for the complicated cases */
122 if (resample_method == PA_RESAMPLER_TRIVIAL)
123 r->resample_method = PA_RESAMPLER_SRC_ZERO_ORDER_HOLD;
125 if (libsamplerate_init(r) < 0)
129 /* Use our own simple non-fp resampler for the trivial cases and when the user selects it */
130 if (trivial_init(r) < 0)
143 void pa_resampler_free(pa_resampler *r) {
152 void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
156 if (r->i_ss.rate == rate)
161 if (r->impl_update_input_rate)
162 r->impl_update_input_rate(r, rate);
165 void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
166 assert(r && in && out && r->impl_run);
168 r->impl_run(r, in, out);
171 size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
174 return (((out_length / r->o_fz)*r->i_ss.rate)/r->o_ss.rate) * r->i_fz;
177 pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
179 return r->resample_method;
182 static const char * const resample_methods[] = {
183 "src-sinc-best-quality",
184 "src-sinc-medium-quality",
186 "src-zero-order-hold",
191 const char *pa_resample_method_to_string(pa_resample_method_t m) {
193 if (m < 0 || m >= PA_RESAMPLER_MAX)
196 return resample_methods[m];
199 pa_resample_method_t pa_parse_resample_method(const char *string) {
200 pa_resample_method_t m;
204 for (m = 0; m < PA_RESAMPLER_MAX; m++)
205 if (!strcmp(string, resample_methods[m]))
208 return PA_RESAMPLER_INVALID;
212 /*** libsamplerate based implementation ***/
214 static void libsamplerate_free(pa_resampler *r) {
215 struct impl_libsamplerate *u;
218 assert(r->impl_data);
223 src_delete(u->src_state);
232 static void calc_map_table(pa_resampler *r) {
233 struct impl_libsamplerate *u;
236 assert(r->impl_data);
240 if (!(u->map_required = (!pa_channel_map_equal(&r->i_cm, &r->o_cm) || r->i_ss.channels != r->o_ss.channels)))
243 for (oc = 0; oc < r->o_ss.channels; oc++) {
246 for (ic = 0; ic < r->i_ss.channels; ic++) {
247 pa_channel_position_t a, b;
253 (a == PA_CHANNEL_POSITION_MONO && b == PA_CHANNEL_POSITION_LEFT) ||
254 (a == PA_CHANNEL_POSITION_MONO && b == PA_CHANNEL_POSITION_RIGHT) ||
255 (a == PA_CHANNEL_POSITION_LEFT && b == PA_CHANNEL_POSITION_MONO) ||
256 (a == PA_CHANNEL_POSITION_RIGHT && b == PA_CHANNEL_POSITION_MONO))
258 u->map_table[oc][i++] = ic;
261 /* Add an end marker */
262 if (i < PA_CHANNELS_MAX)
263 u->map_table[oc][i] = -1;
267 static float * convert_to_float(pa_resampler *r, void *input, unsigned n_frames) {
268 struct impl_libsamplerate *u;
273 assert(r->impl_data);
276 /* Convert the incoming sample into floats and place them in buf1 */
278 if (!u->to_float32ne_func)
281 n_samples = n_frames * r->i_ss.channels;
283 if (u->buf1_samples < n_samples)
284 u->buf1 = pa_xrealloc(u->buf1, sizeof(float) * (u->buf1_samples = n_samples));
286 u->to_float32ne_func(n_samples, input, u->buf1);
291 static float *remap_channels(pa_resampler *r, float *input, unsigned n_frames) {
292 struct impl_libsamplerate *u;
299 assert(r->impl_data);
302 /* Remap channels and place the result int buf2 */
304 if (!u->map_required)
307 n_samples = n_frames * r->o_ss.channels;
309 if (u->buf2_samples < n_samples)
310 u->buf2 = pa_xrealloc(u->buf2, sizeof(float) * (u->buf2_samples = n_samples));
312 memset(u->buf2, 0, n_samples * sizeof(float));
314 o_skip = sizeof(float) * r->o_ss.channels;
315 i_skip = sizeof(float) * r->i_ss.channels;
317 for (oc = 0; oc < r->o_ss.channels; oc++) {
319 static const float one = 1.0;
321 for (i = 0; i < PA_CHANNELS_MAX && u->map_table[oc][i] >= 0; i++)
323 u->buf2 + oc, o_skip,
324 u->buf2 + oc, o_skip,
325 input + u->map_table[oc][i], i_skip,
333 static float *resample(pa_resampler *r, float *input, unsigned *n_frames) {
334 struct impl_libsamplerate *u;
336 unsigned out_n_frames, out_n_samples;
342 assert(r->impl_data);
345 /* Resample the data and place the result in buf3 */
350 out_n_frames = (*n_frames*r->o_ss.rate/r->i_ss.rate)+1024;
351 out_n_samples = out_n_frames * r->o_ss.channels;
353 if (u->buf3_samples < out_n_samples)
354 u->buf3 = pa_xrealloc(u->buf3, sizeof(float) * (u->buf3_samples = out_n_samples));
356 data.data_in = input;
357 data.input_frames = *n_frames;
359 data.data_out = u->buf3;
360 data.output_frames = out_n_frames;
362 data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
363 data.end_of_input = 0;
365 ret = src_process(u->src_state, &data);
367 assert((unsigned) data.input_frames_used == *n_frames);
369 *n_frames = data.output_frames_gen;
374 static void *convert_from_float(pa_resampler *r, float *input, unsigned n_frames) {
375 struct impl_libsamplerate *u;
380 assert(r->impl_data);
383 /* Convert the data into the correct sample type and place the result in buf4 */
385 if (!u->from_float32ne_func)
388 n_samples = n_frames * r->o_ss.channels;
390 if (u->buf4_samples < n_samples)
391 u->buf4 = pa_xrealloc(u->buf4, sizeof(float) * (u->buf4_samples = n_samples));
393 u->from_float32ne_func(n_samples, input, u->buf4);
398 static void libsamplerate_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
399 struct impl_libsamplerate *u;
401 void *input, *output;
408 assert(in->memblock);
409 assert(in->length % r->i_fz == 0);
410 assert(r->impl_data);
414 input = ((uint8_t*) in->memblock->data + in->index);
415 n_frames = in->length / r->i_fz;
416 assert(n_frames > 0);
418 buf = convert_to_float(r, input, n_frames);
419 buf = remap_channels(r, buf, n_frames);
420 buf = resample(r, buf, &n_frames);
423 output = convert_from_float(r, buf, n_frames);
425 if (output == input) {
426 /* Mm, no adjustment has been necessary, so let's return the original block */
427 out->memblock = pa_memblock_ref(in->memblock);
428 out->index = in->index;
429 out->length = in->length;
433 out->length = n_frames * r->o_fz;
436 if (output == u->buf1) {
439 } else if (output == u->buf2) {
442 } else if (output == u->buf3) {
445 } else if (output == u->buf4) {
452 /* Take the existing buffer and make it a memblock */
453 out->memblock = pa_memblock_new_dynamic(*p, out->length, r->memblock_stat);
457 out->memblock = NULL;
458 out->index = out->length = 0;
462 static void libsamplerate_update_input_rate(pa_resampler *r, uint32_t rate) {
463 struct impl_libsamplerate *u;
467 assert(r->impl_data);
472 u->src_state = src_new(r->resample_method, r->o_ss.channels, &err);
473 assert(u->src_state);
475 int ret = src_set_ratio(u->src_state, (double) r->o_ss.rate / rate);
480 static int libsamplerate_init(pa_resampler *r) {
481 struct impl_libsamplerate *u = NULL;
484 r->impl_data = u = pa_xnew(struct impl_libsamplerate, 1);
486 u->buf1 = u->buf2 = u->buf3 = u->buf4 = NULL;
487 u->buf1_samples = u->buf2_samples = u->buf3_samples = u->buf4_samples = 0;
489 if (r->i_ss.format == PA_SAMPLE_FLOAT32NE)
490 u->to_float32ne_func = NULL;
491 else if (!(u->to_float32ne_func = pa_get_convert_to_float32ne_function(r->i_ss.format)))
494 if (r->o_ss.format == PA_SAMPLE_FLOAT32NE)
495 u->from_float32ne_func = NULL;
496 else if (!(u->from_float32ne_func = pa_get_convert_from_float32ne_function(r->o_ss.format)))
499 if (r->o_ss.rate == r->i_ss.rate)
501 else if (!(u->src_state = src_new(r->resample_method, r->o_ss.channels, &err)))
504 r->impl_free = libsamplerate_free;
505 r->impl_update_input_rate = libsamplerate_update_input_rate;
506 r->impl_run = libsamplerate_run;
517 /* Trivial implementation */
519 static void trivial_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
522 struct impl_trivial *u;
527 assert(r->impl_data);
532 assert(fz == r->o_fz);
534 n_frames = in->length/fz;
536 if (r->i_ss.rate == r->o_ss.rate) {
538 /* In case there's no diefference in sample types, do nothing */
540 pa_memblock_ref(out->memblock);
542 u->o_counter += n_frames;
544 /* Do real resampling */
548 /* The length of the new memory block rounded up */
549 l = ((((n_frames+1) * r->o_ss.rate) / r->i_ss.rate) + 1) * fz;
552 out->memblock = pa_memblock_new(l, r->memblock_stat);
554 for (o_index = 0;; o_index++, u->o_counter++) {
557 j = (u->o_counter * r->i_ss.rate / r->o_ss.rate);
558 j = j > u->i_counter ? j - u->i_counter : 0;
563 assert(o_index*fz < out->memblock->length);
565 memcpy((uint8_t*) out->memblock->data + fz*o_index,
566 (uint8_t*) in->memblock->data + in->index + fz*j, fz);
570 out->length = o_index*fz;
573 u->i_counter += n_frames;
575 /* Normalize counters */
576 while (u->i_counter >= r->i_ss.rate) {
577 u->i_counter -= r->i_ss.rate;
578 assert(u->o_counter >= r->o_ss.rate);
579 u->o_counter -= r->o_ss.rate;
583 static void trivial_free(pa_resampler *r) {
586 pa_xfree(r->impl_data);
589 static void trivial_update_input_rate(pa_resampler *r, uint32_t rate) {
590 struct impl_trivial *u;
594 assert(r->impl_data);
601 static int trivial_init(pa_resampler*r) {
602 struct impl_trivial *u;
605 assert(r->i_ss.format == r->o_ss.format);
606 assert(r->i_ss.channels == r->o_ss.channels);
608 r->impl_data = u = pa_xnew(struct impl_trivial, 1);
609 u->o_counter = u->i_counter = 0;
611 r->impl_run = trivial_run;
612 r->impl_free = trivial_free;
613 r->impl_update_input_rate = trivial_update_input_rate;