1/***
2  This file is part of PulseAudio.
3
4  Copyright 2004-2006 Lennart Poettering
5
6  PulseAudio 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.1 of the License,
9  or (at your option) any later version.
10
11  PulseAudio 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.
15
16  You should have received a copy of the GNU Lesser General Public License
17  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18***/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <samplerate.h>
25
26#include <pulsecore/resampler.h>
27
28static unsigned libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
29    SRC_DATA data;
30    SRC_STATE *state;
31
32    pa_assert(r);
33    pa_assert(input);
34    pa_assert(output);
35    pa_assert(out_n_frames);
36
37    state = r->impl.data;
38    memset(&data, 0, sizeof(data));
39
40    data.data_in = pa_memblock_acquire_chunk(input);
41    data.input_frames = (long int) in_n_frames;
42
43    data.data_out = pa_memblock_acquire_chunk(output);
44    data.output_frames = (long int) *out_n_frames;
45
46    data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
47    data.end_of_input = 0;
48
49    pa_assert_se(src_process(state, &data) == 0);
50
51    pa_memblock_release(input->memblock);
52    pa_memblock_release(output->memblock);
53
54    *out_n_frames = (unsigned) data.output_frames_gen;
55
56    return in_n_frames - data.input_frames_used;
57}
58
59static void libsamplerate_update_rates(pa_resampler *r) {
60    SRC_STATE *state;
61    pa_assert(r);
62
63    state = r->impl.data;
64    pa_assert_se(src_set_ratio(state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
65}
66
67static void libsamplerate_reset(pa_resampler *r) {
68    SRC_STATE *state;
69    pa_assert(r);
70
71    state = r->impl.data;
72    pa_assert_se(src_reset(state) == 0);
73}
74
75static void libsamplerate_free(pa_resampler *r) {
76    SRC_STATE *state;
77    pa_assert(r);
78
79    state = r->impl.data;
80    if (state)
81        src_delete(state);
82}
83
84int pa_resampler_libsamplerate_init(pa_resampler *r) {
85    int err;
86    SRC_STATE *state;
87
88    pa_assert(r);
89
90    if (!(state = src_new(r->method, r->work_channels, &err)))
91        return -1;
92
93    r->impl.free = libsamplerate_free;
94    r->impl.update_rates = libsamplerate_update_rates;
95    r->impl.resample = libsamplerate_resample;
96    r->impl.reset = libsamplerate_reset;
97    r->impl.data = state;
98
99    return 0;
100}
101