src/syntrax/resampler.cView |
---|
61 | 61 | } |
62 | 62 | } |
63 | 63 | |
64 | 64 | enum { width = 32 }; |
65 | | -enum { stereo = 2 }; |
66 | 65 | enum { max_res = 512 }; |
67 | 66 | enum { min_width = (width < 4 ? 4 : width) }; |
68 | 67 | enum { adj_width = min_width / 4 * 4 + 2 }; |
69 | | -enum { write_offset = adj_width * stereo }; |
| 68 | +enum { write_offset = adj_width }; |
70 | 69 | |
71 | 70 | enum { buffer_size = 128 }; |
72 | 71 | |
73 | 72 | typedef struct _resampler |
82 | 81 | int latency; |
83 | 82 | |
84 | 83 | imp_t const* imp; |
85 | 84 | imp_t impulses [max_res * (adj_width + 2 * (sizeof(imp_off_t) / sizeof(imp_t)))]; |
86 | | - sample_t buffer_in[buffer_size * stereo * 2]; |
87 | | - sample_t buffer_out[buffer_size * stereo]; |
| 85 | + sample_t buffer_in[buffer_size * 2]; |
| 86 | + sample_t buffer_out[buffer_size]; |
88 | 87 | } resampler; |
89 | 88 | |
90 | 89 | void * resampler_create() |
91 | 90 | { |
171 | 170 | } |
172 | 171 | rs->rate_ = ratio_; |
173 | 172 | |
174 | 173 | |
175 | | - step = stereo * (int) floor( ratio_ ); |
| 174 | + step = (int) floor( ratio_ ); |
176 | 175 | fraction = fmod( ratio_, 1.0 ); |
177 | 176 | |
178 | 177 | filter = (ratio_ < 1.0) ? 1.0 : 1.0 / ratio_; |
179 | 178 | pos = 0.0; |
192 | 191 | pos += fraction; |
193 | 192 | if ( pos >= 0.9999999 ) |
194 | 193 | { |
195 | 194 | pos -= 1.0; |
196 | | - cur_step += stereo; |
| 195 | + cur_step += 1; |
197 | 196 | } |
198 | 197 | |
199 | | - ((imp_off_t*)out)[0] = (cur_step - rs->width_ * 2 + 4) * sizeof (sample_t); |
| 198 | + ((imp_off_t*)out)[0] = (cur_step - rs->width_ + 2) * sizeof (sample_t); |
200 | 199 | ((imp_off_t*)out)[1] = 2 * sizeof (imp_t) + 2 * sizeof (imp_off_t); |
201 | 200 | out += 2 * (sizeof(imp_off_t) / sizeof(imp_t)); |
202 | 201 | |
203 | 202 | } |
209 | 208 | |
210 | 209 | int resampler_get_free(void *_r) |
211 | 210 | { |
212 | 211 | resampler *r = (resampler *)_r; |
213 | | - return buffer_size * stereo - r->infilled; |
| 212 | + return buffer_size - r->infilled; |
214 | 213 | } |
215 | 214 | |
216 | 215 | int resampler_get_min_fill(void *_r) |
217 | 216 | { |
218 | 217 | resampler *r = (resampler *)_r; |
219 | | - const int min_needed = write_offset + stereo; |
| 218 | + const int min_needed = write_offset + 1; |
220 | 219 | const int latency = r->latency ? 0 : adj_width; |
221 | 220 | int min_free = min_needed - r->infilled - latency; |
222 | 221 | return min_free < 0 ? 0 : min_free; |
223 | 222 | } |
224 | 223 | |
225 | | -void resampler_write_pair(void *_r, sample_t ls, sample_t rs) |
| 224 | +void resampler_write_sample(void *_r, sample_t s) |
226 | 225 | { |
227 | 226 | resampler *r = (resampler *)_r; |
228 | 227 | |
229 | 228 | if (!r->latency) |
230 | 229 | { |
231 | 230 | int i; |
232 | 231 | for ( i = 0; i < adj_width / 2; ++i) |
233 | 232 | { |
234 | | - r->buffer_in[r->inptr + 0] = 0; |
235 | | - r->buffer_in[r->inptr + 1] = 0; |
236 | | - r->buffer_in[buffer_size * stereo + r->inptr + 0] = 0; |
237 | | - r->buffer_in[buffer_size * stereo + r->inptr + 1] = 0; |
238 | | - r->inptr = (r->inptr + stereo) % (buffer_size * stereo); |
239 | | - r->infilled += stereo; |
| 233 | + r->buffer_in[r->inptr] = 0; |
| 234 | + r->buffer_in[buffer_size + r->inptr] = 0; |
| 235 | + r->inptr = (r->inptr + 1) % (buffer_size); |
| 236 | + r->infilled += 1; |
240 | 237 | } |
241 | 238 | r->latency = 1; |
242 | 239 | } |
243 | 240 | |
244 | | - if (r->infilled < buffer_size * stereo) |
| 241 | + if (r->infilled < buffer_size) |
245 | 242 | { |
246 | | - r->buffer_in[r->inptr + 0] = ls; |
247 | | - r->buffer_in[r->inptr + 1] = rs; |
248 | | - r->buffer_in[buffer_size * stereo + r->inptr + 0] = ls; |
249 | | - r->buffer_in[buffer_size * stereo + r->inptr + 1] = rs; |
250 | | - r->inptr = (r->inptr + stereo) % (buffer_size * stereo); |
251 | | - r->infilled += stereo; |
| 243 | + r->buffer_in[r->inptr] = s; |
| 244 | + r->buffer_in[buffer_size + r->inptr + 0] = s; |
| 245 | + r->inptr = (r->inptr + 1) % (buffer_size); |
| 246 | + r->infilled += 1; |
252 | 247 | } |
253 | 248 | } |
254 | 249 | |
255 | 250 | #if defined(_MSC_VER) || defined(__GNUC__) |
270 | 265 | { |
271 | 266 | int n; |
272 | 267 | |
273 | 268 | int pt = imp [0]; |
274 | | - intermediate_t l = (intermediate_t)pt * (intermediate_t)(in [0]); |
275 | | - intermediate_t r = (intermediate_t)pt * (intermediate_t)(in [1]); |
| 269 | + intermediate_t s = (intermediate_t)pt * (intermediate_t)(in [0]); |
276 | 270 | if ( out >= out_end ) |
277 | 271 | break; |
278 | 272 | for ( n = (adj_width - 2) / 2; n; --n ) |
279 | 273 | { |
280 | 274 | pt = imp [1]; |
281 | | - l += (intermediate_t)pt * (intermediate_t)(in [2]); |
282 | | - r += (intermediate_t)pt * (intermediate_t)(in [3]); |
| 275 | + s += (intermediate_t)pt * (intermediate_t)(in [1]); |
283 | 276 | |
284 | 277 | |
285 | 278 | imp += 2; |
286 | 279 | pt = imp [0]; |
287 | | - r += (intermediate_t)pt * (intermediate_t)(in [5]); |
288 | | - in += 4; |
289 | | - l += (intermediate_t)pt * (intermediate_t)(in [0]); |
| 280 | + s += (intermediate_t)pt * (intermediate_t)(in [2]); |
| 281 | + in += 2; |
290 | 282 | } |
291 | 283 | pt = imp [1]; |
292 | | - l += (intermediate_t)pt * (intermediate_t)(in [2]); |
293 | | - r += (intermediate_t)pt * (intermediate_t)(in [3]); |
| 284 | + s += (intermediate_t)pt * (intermediate_t)(in [1]); |
294 | 285 | |
295 | 286 | |
296 | 287 | * proper offsets to the next input sample and next impulse */ |
297 | 288 | in = (sample_t const*) ((char const*) in + ((imp_off_t*)(&imp [2]))[0]); |
298 | 289 | imp = (imp_t const*) ((char const*) imp + ((imp_off_t*)(&imp [2]))[1]); |
299 | 290 | |
300 | | - out [0] = (sample_t) (l >> 15); |
301 | | - out [1] = (sample_t) (r >> 15); |
302 | | - out += 2; |
| 291 | + out [0] = (sample_t) (s >> 15); |
| 292 | + out += 1; |
303 | 293 | } |
304 | 294 | while ( in < in_end ); |
305 | 295 | |
306 | 296 | r->imp = imp; |
326 | 316 | while (!r->outfilled && r->infilled) |
327 | 317 | { |
328 | 318 | int inread; |
329 | 319 | |
330 | | - int writepos = ( r->outptr + r->outfilled ) % (buffer_size * stereo); |
331 | | - int writesize = (buffer_size * stereo) - writepos; |
332 | | - if ( writesize > ( buffer_size * stereo - r->outfilled ) ) |
333 | | - writesize = buffer_size * stereo - r->outfilled; |
334 | | - inread = resampler_wrapper(r, &r->buffer_out[writepos], &writesize, &r->buffer_in[buffer_size * stereo + r->inptr - r->infilled], r->infilled); |
| 320 | + int writepos = ( r->outptr + r->outfilled ) % (buffer_size); |
| 321 | + int writesize = (buffer_size) - writepos; |
| 322 | + if ( writesize > ( buffer_size - r->outfilled ) ) |
| 323 | + writesize = buffer_size - r->outfilled; |
| 324 | + inread = resampler_wrapper(r, &r->buffer_out[writepos], &writesize, &r->buffer_in[buffer_size + r->inptr - r->infilled], r->infilled); |
335 | 325 | r->infilled -= inread; |
336 | 326 | r->outfilled += writesize; |
337 | 327 | if (!inread) |
338 | 328 | break; |
341 | 331 | |
342 | 332 | int resampler_get_avail(void *_r) |
343 | 333 | { |
344 | 334 | resampler *r = (resampler *)_r; |
345 | | - if (r->outfilled < stereo && r->infilled >= r->width_) |
| 335 | + if (r->outfilled < 1 && r->infilled >= r->width_) |
346 | 336 | resampler_fill( r ); |
347 | 337 | return r->outfilled; |
348 | 338 | } |
349 | 339 | |
350 | | -static void resampler_read_pair_internal( resampler *r, sample_t *ls, sample_t *rs, int advance ) |
| 340 | +static void resampler_read_sample_internal( resampler *r, sample_t *s, int advance ) |
351 | 341 | { |
352 | | - if (r->outfilled < stereo) |
| 342 | + if (r->outfilled < 1) |
353 | 343 | resampler_fill( r ); |
354 | | - if (r->outfilled < stereo) |
| 344 | + if (r->outfilled < 1) |
355 | 345 | { |
356 | | - *ls = 0; |
357 | | - *rs = 0; |
| 346 | + *s = 0; |
358 | 347 | return; |
359 | 348 | } |
360 | | - *ls = r->buffer_out[r->outptr + 0]; |
361 | | - *rs = r->buffer_out[r->outptr + 1]; |
| 349 | + *s = r->buffer_out[r->outptr]; |
362 | 350 | if (advance) |
363 | 351 | { |
364 | | - r->outptr = (r->outptr + 2) % (buffer_size * stereo); |
365 | | - r->outfilled -= stereo; |
| 352 | + r->outptr = (r->outptr + 1) % (buffer_size); |
| 353 | + r->outfilled -= 1; |
366 | 354 | } |
367 | 355 | } |
368 | 356 | |
369 | | -void resampler_read_pair( void *_r, sample_t *ls, sample_t *rs ) |
| 357 | +void resampler_read_sample( void *_r, sample_t *s ) |
370 | 358 | { |
371 | 359 | resampler *r = (resampler *)_r; |
372 | | - resampler_read_pair_internal(r, ls, rs, 1); |
| 360 | + resampler_read_sample_internal(r, s, 1); |
373 | 361 | } |
374 | 362 | |
375 | | -void resampler_peek_pair( void *_r, sample_t *ls, sample_t *rs ) |
| 363 | +void resampler_peek_sample( void *_r, sample_t *s ) |
376 | 364 | { |
377 | 365 | resampler *r = (resampler *)_r; |
378 | | - resampler_read_pair_internal(r, ls, rs, 0); |
| 366 | + resampler_read_sample_internal(r, s, 0); |
379 | 367 | } |