]> code.delx.au - pulseaudio/blob - polyp/resampler.c
two simple fixes
[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 <stdlib.h>
27 #include <assert.h>
28
29 #include <samplerate.h>
30
31 #include "resampler.h"
32 #include "sconv.h"
33
34 struct pa_resampler {
35 struct pa_sample_spec i_ss, o_ss;
36 float* i_buf, *o_buf;
37 unsigned i_alloc, o_alloc;
38 size_t i_sz, o_sz;
39
40 int channels;
41
42 pa_convert_to_float32_func_t to_float32_func;
43 pa_convert_from_float32_func_t from_float32_func;
44 SRC_STATE *src_state;
45 };
46
47 struct pa_resampler* pa_resampler_new(const struct pa_sample_spec *a, const struct pa_sample_spec *b) {
48 struct pa_resampler *r = NULL;
49 int err;
50 assert(a && b && pa_sample_spec_valid(a) && pa_sample_spec_valid(b));
51
52 if (a->channels != b->channels && a->channels != 1 && b->channels != 1)
53 goto fail;
54
55 if (a->format == PA_SAMPLE_ALAW || a->format == PA_SAMPLE_ULAW || b->format == PA_SAMPLE_ALAW || b->format == PA_SAMPLE_ULAW)
56 goto fail;
57
58 r = malloc(sizeof(struct pa_resampler));
59 assert(r);
60
61 r->channels = a->channels;
62 if (b->channels < r->channels)
63 r->channels = b->channels;
64
65 r->i_buf = r->o_buf = NULL;
66 r->i_alloc = r->o_alloc = 0;
67
68 if (a->rate != b->rate) {
69 r->src_state = src_new(SRC_SINC_FASTEST, r->channels, &err);
70 if (err != 0 || !r->src_state)
71 goto fail;
72 } else
73 r->src_state = NULL;
74
75 r->i_ss = *a;
76 r->o_ss = *b;
77
78 r->i_sz = pa_sample_size(a);
79 r->o_sz = pa_sample_size(b);
80
81 r->to_float32_func = pa_get_convert_to_float32_function(a->format);
82 r->from_float32_func = pa_get_convert_from_float32_function(b->format);
83
84 assert(r->to_float32_func && r->from_float32_func);
85
86 return r;
87
88 fail:
89 if (r)
90 free(r);
91
92 return NULL;
93 }
94
95 void pa_resampler_free(struct pa_resampler *r) {
96 assert(r);
97 if (r->src_state)
98 src_delete(r->src_state);
99 free(r->i_buf);
100 free(r->o_buf);
101 free(r);
102 }
103
104 size_t pa_resampler_request(struct pa_resampler *r, size_t out_length) {
105 assert(r && (out_length % r->o_sz) == 0);
106
107 return (((out_length / r->o_sz)*r->i_ss.rate)/r->o_ss.rate) * r->i_sz;
108 }
109
110
111 void pa_resampler_run(struct pa_resampler *r, const struct pa_memchunk *in, struct pa_memchunk *out) {
112 unsigned i_nchannels, o_nchannels, ins, ons, eff_ins, eff_ons;
113 float *cbuf;
114 assert(r && in && out && in->length && in->memblock && (in->length % r->i_sz) == 0);
115
116 /* How many input samples? */
117 ins = in->length/r->i_sz;
118
119 /* How much space for output samples? */
120 if (r->src_state)
121 ons = (ins*r->o_ss.rate/r->i_ss.rate)+1024;
122 else
123 ons = ins;
124
125 /* How many channels? */
126 if (r->i_ss.channels == r->o_ss.channels) {
127 i_nchannels = o_nchannels = 1;
128 eff_ins = ins*r->i_ss.channels; /* effective samples */
129 eff_ons = ons*r->o_ss.channels;
130 } else {
131 i_nchannels = r->i_ss.channels;
132 o_nchannels = r->o_ss.channels;
133 eff_ins = ins;
134 eff_ons = ons;
135 }
136
137 out->memblock = pa_memblock_new(out->length = (ons*r->o_sz));
138 out->index = 0;
139 assert(out->memblock);
140
141 if (r->i_alloc < eff_ins)
142 r->i_buf = realloc(r->i_buf, sizeof(float) * (r->i_alloc = eff_ins));
143 assert(r->i_buf);
144
145 r->to_float32_func(eff_ins, in->memblock->data+in->index, i_nchannels, r->i_buf);
146
147 if (r->src_state) {
148 int ret;
149 SRC_DATA data;
150
151 if (r->o_alloc < eff_ons)
152 r->o_buf = realloc(r->o_buf, sizeof(float) * (r->o_alloc = eff_ons));
153 assert(r->o_buf);
154
155 data.data_in = r->i_buf;
156 data.input_frames = ins;
157
158 data.data_out = r->o_buf;
159 data.output_frames = ons;
160
161 data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
162 data.end_of_input = 0;
163
164 ret = src_process(r->src_state, &data);
165 assert(ret == 0);
166 assert((unsigned) data.input_frames_used == ins);
167
168 cbuf = r->o_buf;
169 ons = data.output_frames_gen;
170
171 if (r->i_ss.channels == r->o_ss.channels)
172 eff_ons = ons*r->o_ss.channels;
173 else
174 eff_ons = ons;
175 } else
176 cbuf = r->i_buf;
177
178 r->from_float32_func(eff_ons, cbuf, out->memblock->data+out->index, o_nchannels);
179 out->length = ons*r->o_sz;
180 }