~ubuntu-branches/ubuntu/trusty/gavl/trusty

« back to all changes in this revision

Viewing changes to gavl/audioconverter.c

  • Committer: Bazaar Package Importer
  • Author(s): Romain Beauxis
  • Date: 2009-01-17 20:38:33 UTC
  • mfrom: (1.1.3 upstream) (4.1.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090117203833-t8fq1e1jdquyelmy
Tags: 1.1.0-2
Fixed debian/copyright 

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <stdio.h>
25
25
 
26
26
#include <audio.h>
 
27
#include <libsamplerate/common.h>
27
28
#include <mix.h>
28
29
#include <accel.h>
29
30
 
115
116
    cnv->last_context = ctx;
116
117
    cnv->contexts     = ctx;
117
118
    }
 
119
  ctx->output_format.samples_per_frame = 0;
118
120
  cnv->current_format = &(ctx->output_format);
119
121
  cnv->num_conversions++;
120
122
  }
333
335
  }
334
336
 
335
337
static void alloc_frames(gavl_audio_converter_t* cnv,
336
 
                         int in_samples)
 
338
                         int in_samples, double new_ratio)
337
339
  {
338
340
  gavl_audio_convert_context_t * ctx;
339
 
  
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))
341
343
    return;
342
 
  
 
344
 
343
345
  cnv->input_format.samples_per_frame = in_samples;
344
346
  
345
347
  /* Set the samples_per_frame member of all intermediate formats */
346
348
  
347
349
  ctx = cnv->contexts;
348
 
  
349
 
  while(ctx)
 
350
  out_samples_needed = in_samples;  
 
351
 
 
352
  while(ctx->next)
350
353
    {
351
 
    ctx->input_format.samples_per_frame = in_samples;
 
354
    ctx->input_format.samples_per_frame = out_samples_needed;
352
355
    
353
 
    if(ctx->input_format.samplerate != ctx->output_format.samplerate)
 
356
    if(ctx->samplerate_converter)
354
357
      {
355
 
      in_samples =
356
 
        (in_samples * ctx->output_format.samplerate) /
357
 
        ctx->input_format.samplerate + 10;
 
358
      /* Varispeed */
 
359
      if(new_ratio > 0.0)
 
360
        {
 
361
        out_samples_needed = 
 
362
          (int)(0.5 * (ctx->samplerate_converter->ratio + new_ratio) * out_samples_needed) + 10;
 
363
        }
 
364
      /* Constant ratio */
 
365
      else
 
366
        {
 
367
        out_samples_needed =
 
368
          (out_samples_needed * ctx->output_format.samplerate) /
 
369
          ctx->input_format.samplerate + 10;
 
370
        }
358
371
      }
359
 
    ctx->output_format.samples_per_frame = in_samples;
360
 
    ctx = ctx->next;
361
 
    }
362
 
  
363
 
  /* Create temporary buffers */
364
 
  ctx = cnv->contexts;
365
 
  
366
 
  while(ctx)
367
 
    {
368
 
    //    dump_context(ctx);
369
 
    if(ctx->next)
 
372
    if(ctx->output_format.samples_per_frame < out_samples_needed)
370
373
      {
 
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));
375
379
      }
376
380
    ctx = ctx->next;
377
381
    }
378
 
  
379
382
  }
380
383
 
381
384
int gavl_audio_converter_init(gavl_audio_converter_t* cnv,
402
405
  cnv->contexts->input_frame = input_frame;
403
406
  cnv->last_context->output_frame = output_frame;
404
407
  
405
 
  alloc_frames(cnv, input_frame->valid_samples);
 
408
  alloc_frames(cnv, input_frame->valid_samples, -1.0);
406
409
  
407
410
  ctx = cnv->contexts;
408
411
  
439
442
  {
440
443
  return &(cnv->opt);
441
444
  }
 
445
 
 
446
 
 
447
int gavl_audio_converter_init_resample(gavl_audio_converter_t * cnv,
 
448
                                   const gavl_audio_format_t * format)
 
449
{
 
450
  gavl_audio_format_t tmp_format;
 
451
  gavl_audio_convert_context_t * ctx;
 
452
  
 
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);
 
456
 
 
457
  adjust_format(&(cnv->input_format));
 
458
  adjust_format(&(cnv->output_format));
 
459
 
 
460
  // Delete previous conversions 
 
461
  audio_converter_cleanup(cnv);
 
462
 
 
463
  cnv->current_format = &(cnv->input_format);
 
464
 
 
465
  put_samplerate_context(cnv, &tmp_format, cnv->output_format.samplerate);
 
466
 
 
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)
 
470
  {
 
471
          if(cnv->current_format->interleave_mode == GAVL_INTERLEAVE_2)
 
472
          {
 
473
                  tmp_format.interleave_mode = GAVL_INTERLEAVE_NONE;
 
474
                  ctx = gavl_interleave_context_create(&(cnv->opt),
 
475
                                  cnv->current_format,
 
476
                                  &tmp_format);
 
477
                  add_context(cnv, ctx);
 
478
          }
 
479
 
 
480
          tmp_format.sample_format = cnv->output_format.sample_format;
 
481
          ctx = gavl_sampleformat_context_create(&(cnv->opt),
 
482
                          cnv->current_format,
 
483
                          &tmp_format);
 
484
          add_context(cnv, ctx);
 
485
  }
 
486
 
 
487
  /* Final interleaving */
 
488
 
 
489
  if(cnv->current_format->interleave_mode != cnv->output_format.interleave_mode)
 
490
  {
 
491
          tmp_format.interleave_mode = cnv->output_format.interleave_mode;
 
492
          ctx = gavl_interleave_context_create(&(cnv->opt),
 
493
                          cnv->current_format,
 
494
                          &tmp_format);
 
495
          add_context(cnv, ctx);
 
496
  }
 
497
 
 
498
  cnv->input_format.samples_per_frame = 0;
 
499
 
 
500
  return cnv->num_conversions;
 
501
}
 
502
 
 
503
int gavl_audio_converter_set_resample_ratio(gavl_audio_converter_t * cnv, 
 
504
                double ratio ) 
 
505
{
 
506
        int j;
 
507
        gavl_audio_convert_context_t * ctx;
 
508
 
 
509
        ctx = cnv->contexts;
 
510
 
 
511
        if (ratio > SRC_MAX_RATIO  || ratio < 1/SRC_MAX_RATIO)
 
512
                return 0;
 
513
        
 
514
        while(ctx) 
 
515
        {
 
516
                if (ctx->samplerate_converter != NULL)
 
517
                {
 
518
                        for (j=0; j < ctx->samplerate_converter->num_resamplers; j++)
 
519
                                gavl_src_set_ratio( ctx->samplerate_converter->resamplers[j], ratio);
 
520
                }
 
521
                ctx->samplerate_converter->ratio = ratio;
 
522
                ctx = ctx->next;
 
523
        }
 
524
        return 1;
 
525
}
 
526
 
 
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,
 
530
                double ratio)
 
531
{
 
532
        gavl_audio_convert_context_t * ctx;
 
533
 
 
534
        cnv->contexts->input_frame = input_frame;
 
535
        cnv->last_context->output_frame = output_frame;
 
536
 
 
537
        alloc_frames(cnv, input_frame->valid_samples, ratio);
 
538
 
 
539
        ctx = cnv->contexts;
 
540
 
 
541
        while(ctx) 
 
542
        {
 
543
                ctx->output_frame->valid_samples = 0;
 
544
                if (ctx->samplerate_converter != NULL)
 
545
                {
 
546
                        if (ctx->samplerate_converter->ratio != ratio )
 
547
                        {
 
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);
 
553
                        }
 
554
                }
 
555
 
 
556
                if(ctx->func)
 
557
                {
 
558
                        ctx->func(ctx);
 
559
                        // DO WE NEED THIS HERE???
 
560
                        if(!ctx->output_frame->valid_samples)
 
561
                                ctx->output_frame->valid_samples = ctx->input_frame->valid_samples;
 
562
 
 
563
                        ctx->output_frame->timestamp = ctx->input_frame->timestamp;
 
564
 
 
565
                }
 
566
                ctx = ctx->next;
 
567
        }
 
568
}