]> code.delx.au - pulseaudio/blob - src/resampler.c
add resampler
[pulseaudio] / src / resampler.c
1 #include <stdlib.h>
2 #include <assert.h>
3
4 #include <samplerate.h>
5
6 #include "resampler.h"
7 #include "sconv.h"
8
9 struct resampler {
10 struct pa_sample_spec i_ss, o_ss;
11 float* i_buf, *o_buf;
12 unsigned i_alloc, o_alloc;
13 size_t i_sz, o_sz;
14
15 int channels;
16
17 convert_to_float32_func_t to_float32_func;
18 convert_from_float32_func_t from_float32_func;
19 SRC_STATE *src_state;
20 };
21
22 struct resampler* resampler_new(const struct pa_sample_spec *a, const struct pa_sample_spec *b) {
23 struct resampler *r;
24 int err;
25 assert(a && b && pa_sample_spec_valid(a) && pa_sample_spec_valid(b));
26
27 if (a->channels != b->channels && a->channels != 1 && b->channels != 1)
28 goto fail;
29
30 if (a->format == PA_SAMPLE_ALAW || a->format == PA_SAMPLE_ULAW || b->format == PA_SAMPLE_ALAW || b->format == PA_SAMPLE_ULAW)
31 goto fail;
32
33 r->channels = a->channels;
34 if (b->channels < r->channels)
35 r->channels = b->channels;
36
37 r = malloc(sizeof(struct resampler));
38 assert(r);
39 r->i_buf = r->o_buf = NULL;
40 r->i_alloc = r->o_alloc = 0;
41
42 if (a->rate != b->rate) {
43 r->src_state = src_new(SRC_SINC_FASTEST, r->channels, &err);
44 if (err != 0 || !r->src_state)
45 goto fail;
46 } else
47 r->src_state = NULL;
48
49 r->i_ss = *a;
50 r->o_ss = *b;
51
52 r->i_sz = pa_sample_size(a);
53 r->o_sz = pa_sample_size(b);
54
55 r->to_float32_func = get_convert_to_float32_function(a->format);
56 r->from_float32_func = get_convert_from_float32_function(b->format);
57
58 assert(r->to_float32_func && r->from_float32_func);
59
60 return r;
61
62 fail:
63 if (r)
64 free(r);
65
66 return NULL;
67 }
68
69 void resampler_free(struct resampler *r) {
70 assert(r);
71 if (r->src_state)
72 src_delete(r->src_state);
73 free(r->i_buf);
74 free(r->o_buf);
75 free(r);
76 }
77
78 size_t resampler_request(struct resampler *r, size_t out_length) {
79 assert(r && (out_length % r->o_sz) == 0);
80
81 return (((out_length / r->o_sz)*r->i_ss.rate)/r->o_ss.rate) * r->i_sz;
82 }
83
84
85 int resampler_run(struct resampler *r, struct memchunk *in, struct memchunk *out) {
86 unsigned i_nchannels, o_nchannels, ins, ons, eff_ins, eff_ons;
87 float *cbuf;
88 size_t in_bytes_used = 0;
89 assert(r && in && out && in->length && in->memblock);
90
91 /* How many input samples? */
92 ins = in->length/r->i_sz;
93
94 /* How much space for output samples? */
95 if (r->src_state)
96 ons = (ins*r->o_ss.rate/r->i_ss.rate)+1024;
97 else
98 ons = ins;
99
100 /* How many channels? */
101 if (r->i_ss.channels == r->o_ss.channels) {
102 i_nchannels = o_nchannels = 1;
103 eff_ins = ins*r->i_ss.channels; /* effective samples */
104 eff_ons = ons*r->o_ss.channels;
105 } else {
106 i_nchannels = r->i_ss.channels;
107 o_nchannels = r->o_ss.channels;
108 eff_ins = ins;
109 eff_ons = ons;
110 }
111
112 out->memblock = memblock_new(out->length = (ons*r->o_sz));
113 out->index = 0;
114 assert(out->memblock);
115
116 if (r->i_alloc < eff_ins)
117 r->i_buf = realloc(r->i_buf, sizeof(float) * (r->i_alloc = eff_ins));
118 assert(r->i_buf);
119
120 r->to_float32_func(eff_ins, in->memblock->data+in->index, i_nchannels, r->i_buf);
121
122 if (r->src_state) {
123 int ret;
124 SRC_DATA data;
125
126 if (r->o_alloc < eff_ons)
127 r->o_buf = realloc(r->o_buf, sizeof(float) * (r->o_alloc = eff_ons));
128 assert(r->o_buf);
129
130 data.data_in = r->i_buf;
131 data.input_frames = ins;
132
133 data.data_out = r->o_buf;
134 data.output_frames = ons;
135
136 data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
137 data.end_of_input = 0;
138
139 ret = src_process(r->src_state, &data);
140 assert(ret == 0);
141
142 in_bytes_used = data.input_frames_used*r->i_sz;
143 cbuf = r->o_buf;
144 ons = data.output_frames_gen;
145
146 if (r->i_ss.channels == r->o_ss.channels)
147 eff_ons = ons*r->o_ss.channels;
148 else
149 eff_ons = ons;
150 } else {
151 in_bytes_used = ins*r->i_sz;
152 cbuf = r->i_buf;
153 }
154
155 assert(in_bytes_used < in->length);
156 in->index += in_bytes_used;
157 in->length -= in_bytes_used;
158
159 r->from_float32_func(eff_ons, cbuf, out->memblock->data+out->index, o_nchannels);
160 out->length = ons*r->o_sz;
161 return 0;
162 }