]> code.delx.au - pulseaudio/blob - polyp/resampler.c
e8dd01b9f3ff7c984ff0a7f9666b06191feb4bec
[pulseaudio] / polyp / resampler.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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.
10
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.
15
16 You should have received a copy of the GNU 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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27
28 #include <samplerate.h>
29
30 #include "resampler.h"
31 #include "sconv.h"
32 #include "xmalloc.h"
33 #include "log.h"
34
35 struct pa_resampler {
36 struct pa_sample_spec i_ss, o_ss;
37 float* i_buf, *o_buf;
38 unsigned i_alloc, o_alloc;
39 size_t i_sz, o_sz;
40
41 int channels;
42
43 pa_convert_to_float32_func_t to_float32_func;
44 pa_convert_from_float32_func_t from_float32_func;
45 SRC_STATE *src_state;
46
47 struct pa_memblock_stat *memblock_stat;
48 };
49
50 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) {
51 struct pa_resampler *r = NULL;
52 int err;
53 assert(a && b && pa_sample_spec_valid(a) && pa_sample_spec_valid(b));
54
55 if (a->channels != b->channels && a->channels != 1 && b->channels != 1)
56 goto fail;
57
58 if (a->format == PA_SAMPLE_ALAW || a->format == PA_SAMPLE_ULAW || b->format == PA_SAMPLE_ALAW || b->format == PA_SAMPLE_ULAW)
59 goto fail;
60
61 r = pa_xmalloc(sizeof(struct pa_resampler));
62
63 r->channels = a->channels;
64 if (b->channels < r->channels)
65 r->channels = b->channels;
66
67 r->i_buf = r->o_buf = NULL;
68 r->i_alloc = r->o_alloc = 0;
69
70 r->src_state = src_new(resample_method, r->channels, &err);
71 if (err != 0 || !r->src_state)
72 goto fail;
73
74 r->i_ss = *a;
75 r->o_ss = *b;
76
77 r->i_sz = pa_frame_size(a);
78 r->o_sz = pa_frame_size(b);
79
80 r->to_float32_func = pa_get_convert_to_float32_function(a->format);
81 r->from_float32_func = pa_get_convert_from_float32_function(b->format);
82
83 assert(r->to_float32_func && r->from_float32_func);
84
85 r->memblock_stat = s;
86
87 return r;
88
89 fail:
90 if (r)
91 pa_xfree(r);
92
93 return NULL;
94 }
95
96 void pa_resampler_free(struct pa_resampler *r) {
97 assert(r);
98 if (r->src_state)
99 src_delete(r->src_state);
100 pa_xfree(r->i_buf);
101 pa_xfree(r->o_buf);
102 pa_xfree(r);
103 }
104
105 size_t pa_resampler_request(struct pa_resampler *r, size_t out_length) {
106 assert(r && (out_length % r->o_sz) == 0);
107
108 return (((out_length / r->o_sz)*r->i_ss.rate)/r->o_ss.rate) * r->i_sz;
109 }
110
111
112 void pa_resampler_run(struct pa_resampler *r, const struct pa_memchunk *in, struct pa_memchunk *out) {
113 unsigned i_nchannels, o_nchannels, ins, ons, eff_ins, eff_ons;
114 float *cbuf;
115 assert(r && in && out && in->length && in->memblock && (in->length % r->i_sz) == 0);
116
117 /* How many input samples? */
118 ins = in->length/r->i_sz;
119
120 /* pa_log("%u / %u = %u\n", in->length, r->i_sz, ins); */
121
122 /* How much space for output samples? */
123 if (r->src_state)
124 ons = (ins*r->o_ss.rate/r->i_ss.rate)+1024;
125 else
126 ons = ins;
127
128 /* How many channels? */
129 if (r->i_ss.channels == r->o_ss.channels) {
130 i_nchannels = o_nchannels = 1;
131 eff_ins = ins*r->i_ss.channels; /* effective samples */
132 eff_ons = ons*r->o_ss.channels;
133 } else {
134 i_nchannels = r->i_ss.channels;
135 o_nchannels = r->o_ss.channels;
136 eff_ins = ins;
137 eff_ons = ons;
138 }
139
140 /* pa_log("eff_ins = %u \n", eff_ins); */
141
142
143 out->memblock = pa_memblock_new(out->length = (ons*r->o_sz), r->memblock_stat);
144 out->index = 0;
145 assert(out->memblock);
146
147 if (r->i_alloc < eff_ins)
148 r->i_buf = pa_xrealloc(r->i_buf, sizeof(float) * (r->i_alloc = eff_ins));
149 assert(r->i_buf);
150
151 /* pa_log("eff_ins = %u \n", eff_ins); */
152
153 r->to_float32_func(eff_ins, (uint8_t*) in->memblock->data+in->index, i_nchannels, r->i_buf);
154
155 if (r->src_state) {
156 int ret;
157 SRC_DATA data;
158
159 if (r->o_alloc < eff_ons)
160 r->o_buf = pa_xrealloc(r->o_buf, sizeof(float) * (r->o_alloc = eff_ons));
161 assert(r->o_buf);
162
163 data.data_in = r->i_buf;
164 data.input_frames = ins;
165
166 data.data_out = r->o_buf;
167 data.output_frames = ons;
168
169 data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
170 data.end_of_input = 0;
171
172 ret = src_process(r->src_state, &data);
173 assert(ret == 0);
174 assert((unsigned) data.input_frames_used == ins);
175
176 cbuf = r->o_buf;
177 ons = data.output_frames_gen;
178
179 if (r->i_ss.channels == r->o_ss.channels)
180 eff_ons = ons*r->o_ss.channels;
181 else
182 eff_ons = ons;
183 } else
184 cbuf = r->i_buf;
185
186 if (eff_ons)
187 r->from_float32_func(eff_ons, cbuf, (uint8_t*)out->memblock->data+out->index, o_nchannels);
188 out->length = ons*r->o_sz;
189
190
191 if (!out->length) {
192 pa_memblock_unref(out->memblock);
193 out->memblock = NULL;
194 }
195 }
196
197 void pa_resampler_set_input_rate(struct pa_resampler *r, uint32_t rate) {
198 int ret;
199 assert(r);
200
201 r->i_ss.rate = rate;
202 ret = src_set_ratio(r->src_state, (double) r->o_ss.rate / r->i_ss.rate);
203 assert(ret == 0);
204 }