335
337
static void alloc_frames(gavl_audio_converter_t* cnv,
338
int in_samples, double new_ratio)
338
340
gavl_audio_convert_context_t * ctx;
340
if(cnv->input_format.samples_per_frame >= in_samples)
341
int out_samples_needed;
342
if((cnv->input_format.samples_per_frame >= in_samples) && (new_ratio < 0.0))
343
345
cnv->input_format.samples_per_frame = in_samples;
345
347
/* Set the samples_per_frame member of all intermediate formats */
347
349
ctx = cnv->contexts;
350
out_samples_needed = in_samples;
351
ctx->input_format.samples_per_frame = in_samples;
354
ctx->input_format.samples_per_frame = out_samples_needed;
353
if(ctx->input_format.samplerate != ctx->output_format.samplerate)
356
if(ctx->samplerate_converter)
356
(in_samples * ctx->output_format.samplerate) /
357
ctx->input_format.samplerate + 10;
362
(int)(0.5 * (ctx->samplerate_converter->ratio + new_ratio) * out_samples_needed) + 10;
368
(out_samples_needed * ctx->output_format.samplerate) /
369
ctx->input_format.samplerate + 10;
359
ctx->output_format.samples_per_frame = in_samples;
363
/* Create temporary buffers */
368
// dump_context(ctx);
372
if(ctx->output_format.samples_per_frame < out_samples_needed)
374
ctx->output_format.samples_per_frame = out_samples_needed + 1024;
371
375
if(ctx->output_frame)
372
376
gavl_audio_frame_destroy(ctx->output_frame);
373
377
ctx->output_frame = gavl_audio_frame_create(&(ctx->output_format));
440
443
return &(cnv->opt);
447
int gavl_audio_converter_init_resample(gavl_audio_converter_t * cnv,
448
const gavl_audio_format_t * format)
450
gavl_audio_format_t tmp_format;
451
gavl_audio_convert_context_t * ctx;
453
gavl_audio_format_copy(&(cnv->input_format), format);
454
gavl_audio_format_copy(&(cnv->output_format), format);
455
gavl_audio_format_copy(&(tmp_format), format);
457
adjust_format(&(cnv->input_format));
458
adjust_format(&(cnv->output_format));
460
// Delete previous conversions
461
audio_converter_cleanup(cnv);
463
cnv->current_format = &(cnv->input_format);
465
put_samplerate_context(cnv, &tmp_format, cnv->output_format.samplerate);
467
/* put_samplerate will automatically convert sample format and interleave format
468
* we need to check to see if it did or not and add contexts to convert back */
469
if(cnv->current_format->sample_format != cnv->output_format.sample_format)
471
if(cnv->current_format->interleave_mode == GAVL_INTERLEAVE_2)
473
tmp_format.interleave_mode = GAVL_INTERLEAVE_NONE;
474
ctx = gavl_interleave_context_create(&(cnv->opt),
477
add_context(cnv, ctx);
480
tmp_format.sample_format = cnv->output_format.sample_format;
481
ctx = gavl_sampleformat_context_create(&(cnv->opt),
484
add_context(cnv, ctx);
487
/* Final interleaving */
489
if(cnv->current_format->interleave_mode != cnv->output_format.interleave_mode)
491
tmp_format.interleave_mode = cnv->output_format.interleave_mode;
492
ctx = gavl_interleave_context_create(&(cnv->opt),
495
add_context(cnv, ctx);
498
cnv->input_format.samples_per_frame = 0;
500
return cnv->num_conversions;
503
int gavl_audio_converter_set_resample_ratio(gavl_audio_converter_t * cnv,
507
gavl_audio_convert_context_t * ctx;
511
if (ratio > SRC_MAX_RATIO || ratio < 1/SRC_MAX_RATIO)
516
if (ctx->samplerate_converter != NULL)
518
for (j=0; j < ctx->samplerate_converter->num_resamplers; j++)
519
gavl_src_set_ratio( ctx->samplerate_converter->resamplers[j], ratio);
521
ctx->samplerate_converter->ratio = ratio;
527
void gavl_audio_converter_resample(gavl_audio_converter_t * cnv,
528
gavl_audio_frame_t * input_frame,
529
gavl_audio_frame_t * output_frame,
532
gavl_audio_convert_context_t * ctx;
534
cnv->contexts->input_frame = input_frame;
535
cnv->last_context->output_frame = output_frame;
537
alloc_frames(cnv, input_frame->valid_samples, ratio);
543
ctx->output_frame->valid_samples = 0;
544
if (ctx->samplerate_converter != NULL)
546
if (ctx->samplerate_converter->ratio != ratio )
548
//ctx->output_format.samplerate = ctx->input_format.samplerate * ratio;
549
ctx->samplerate_converter->ratio = ratio;
550
ctx->samplerate_converter->data.src_ratio = ratio;
551
//for (j=0; j < ctx->samplerate_converter->num_resamplers; j++)
552
// gavl_src_set_ratio( ctx->samplerate_converter->resamplers[j], ratio);
559
// DO WE NEED THIS HERE???
560
if(!ctx->output_frame->valid_samples)
561
ctx->output_frame->valid_samples = ctx->input_frame->valid_samples;
563
ctx->output_frame->timestamp = ctx->input_frame->timestamp;