4 #include <samplerate.h>
10 struct pa_sample_spec i_ss
, o_ss
;
12 unsigned i_alloc
, o_alloc
;
17 convert_to_float32_func_t to_float32_func
;
18 convert_from_float32_func_t from_float32_func
;
22 struct resampler
* resampler_new(const struct pa_sample_spec
*a
, const struct pa_sample_spec
*b
) {
25 assert(a
&& b
&& pa_sample_spec_valid(a
) && pa_sample_spec_valid(b
));
27 if (a
->channels
!= b
->channels
&& a
->channels
!= 1 && b
->channels
!= 1)
30 if (a
->format
== PA_SAMPLE_ALAW
|| a
->format
== PA_SAMPLE_ULAW
|| b
->format
== PA_SAMPLE_ALAW
|| b
->format
== PA_SAMPLE_ULAW
)
33 r
->channels
= a
->channels
;
34 if (b
->channels
< r
->channels
)
35 r
->channels
= b
->channels
;
37 r
= malloc(sizeof(struct resampler
));
39 r
->i_buf
= r
->o_buf
= NULL
;
40 r
->i_alloc
= r
->o_alloc
= 0;
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
)
52 r
->i_sz
= pa_sample_size(a
);
53 r
->o_sz
= pa_sample_size(b
);
55 r
->to_float32_func
= get_convert_to_float32_function(a
->format
);
56 r
->from_float32_func
= get_convert_from_float32_function(b
->format
);
58 assert(r
->to_float32_func
&& r
->from_float32_func
);
69 void resampler_free(struct resampler
*r
) {
72 src_delete(r
->src_state
);
78 size_t resampler_request(struct resampler
*r
, size_t out_length
) {
79 assert(r
&& (out_length
% r
->o_sz
) == 0);
81 return (((out_length
/ r
->o_sz
)*r
->i_ss
.rate
)/r
->o_ss
.rate
) * r
->i_sz
;
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
;
88 size_t in_bytes_used
= 0;
89 assert(r
&& in
&& out
&& in
->length
&& in
->memblock
);
91 /* How many input samples? */
92 ins
= in
->length
/r
->i_sz
;
94 /* How much space for output samples? */
96 ons
= (ins
*r
->o_ss
.rate
/r
->i_ss
.rate
)+1024;
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
;
106 i_nchannels
= r
->i_ss
.channels
;
107 o_nchannels
= r
->o_ss
.channels
;
112 out
->memblock
= memblock_new(out
->length
= (ons
*r
->o_sz
));
114 assert(out
->memblock
);
116 if (r
->i_alloc
< eff_ins
)
117 r
->i_buf
= realloc(r
->i_buf
, sizeof(float) * (r
->i_alloc
= eff_ins
));
120 r
->to_float32_func(eff_ins
, in
->memblock
->data
+in
->index
, i_nchannels
, r
->i_buf
);
126 if (r
->o_alloc
< eff_ons
)
127 r
->o_buf
= realloc(r
->o_buf
, sizeof(float) * (r
->o_alloc
= eff_ons
));
130 data
.data_in
= r
->i_buf
;
131 data
.input_frames
= ins
;
133 data
.data_out
= r
->o_buf
;
134 data
.output_frames
= ons
;
136 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
137 data
.end_of_input
= 0;
139 ret
= src_process(r
->src_state
, &data
);
142 in_bytes_used
= data
.input_frames_used
*r
->i_sz
;
144 ons
= data
.output_frames_gen
;
146 if (r
->i_ss
.channels
== r
->o_ss
.channels
)
147 eff_ons
= ons
*r
->o_ss
.channels
;
151 in_bytes_used
= ins
*r
->i_sz
;
155 assert(in_bytes_used
< in
->length
);
156 in
->index
+= in_bytes_used
;
157 in
->length
-= in_bytes_used
;
159 r
->from_float32_func(eff_ons
, cbuf
, out
->memblock
->data
+out
->index
, o_nchannels
);
160 out
->length
= ons
*r
->o_sz
;