scaletempo.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:19k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * scaletempo.c: Scale audio tempo while maintaining pitch
  3.  *****************************************************************************
  4.  * Copyright © 2008 the VideoLAN team
  5.  * $Id: a40435d45ee500213e1434230cfa2235f2aa65d3 $
  6.  *
  7.  * Authors: Rov Juvano <rovjuvano@users.sourceforge.net>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <vlc_common.h>
  30. #include <vlc_plugin.h>
  31. #include <vlc_aout.h>
  32. #include <string.h> /* for memset */
  33. #include <limits.h> /* form INT_MIN */
  34. /*****************************************************************************
  35.  * Module descriptor
  36.  *****************************************************************************/
  37. static int  Open( vlc_object_t * );
  38. static void Close( vlc_object_t * );
  39. static void DoWork( aout_instance_t *, aout_filter_t *,
  40.                     aout_buffer_t *, aout_buffer_t * );
  41. vlc_module_begin ()
  42.     set_description( N_("Audio tempo scaler synched with rate") )
  43.     set_shortname( N_("Scaletempo") )
  44.     set_capability( "audio filter", 0 )
  45.     set_category( CAT_AUDIO )
  46.     set_subcategory( SUBCAT_AUDIO_AFILTER )
  47.     add_integer_with_range( "scaletempo-stride", 30, 1, 2000, NULL,
  48.         N_("Stride Length"), N_("Length in milliseconds to output each stride"), true )
  49.     add_float_with_range( "scaletempo-overlap", .20, 0.0, 1.0, NULL,
  50.         N_("Overlap Length"), N_("Percentage of stride to overlap"), true )
  51.     add_integer_with_range( "scaletempo-search", 14, 0, 200, NULL,
  52.         N_("Search Length"), N_("Length in milliseconds to search for best overlap position"), true )
  53.     set_callbacks( Open, Close )
  54. vlc_module_end ()
  55. /*
  56.  * Scaletempo works by producing audio in constant sized chunks (a "stride") but
  57.  * consuming chunks proportional to the playback rate.
  58.  *
  59.  * Scaletempo then smooths the output by blending the end of one stride with
  60.  * the next ("overlap").
  61.  *
  62.  * Scaletempo smooths the overlap further by searching within the input buffer
  63.  * for the best overlap position.  Scaletempo uses a statistical cross correlation
  64.  * (roughly a dot-product).  Scaletempo consumes most of its CPU cycles here.
  65.  *
  66.  * NOTE:
  67.  * sample: a single audio sample for one channel
  68.  * frame: a single set of samples, one for each channel
  69.  * VLC uses these terms differently
  70.  */
  71. struct aout_filter_sys_t
  72. {
  73.     /* Filter static config */
  74.     double    scale;
  75.     /* parameters */
  76.     unsigned  ms_stride;
  77.     double    percent_overlap;
  78.     unsigned  ms_search;
  79.     /* audio format */
  80.     unsigned  samples_per_frame;  /* AKA number of channels */
  81.     unsigned  bytes_per_sample;
  82.     unsigned  bytes_per_frame;
  83.     unsigned  sample_rate;
  84.     /* stride */
  85.     double    frames_stride_scaled;
  86.     double    frames_stride_error;
  87.     unsigned  bytes_stride;
  88.     double    bytes_stride_scaled;
  89.     unsigned  bytes_queue_max;
  90.     unsigned  bytes_queued;
  91.     unsigned  bytes_to_slide;
  92.     uint8_t  *buf_queue;
  93.     /* overlap */
  94.     unsigned  samples_overlap;
  95.     unsigned  samples_standing;
  96.     unsigned  bytes_overlap;
  97.     unsigned  bytes_standing;
  98.     void     *buf_overlap;
  99.     void     *table_blend;
  100.     void    (*output_overlap)( aout_filter_t *p_filter, void *p_out_buf, unsigned bytes_off );
  101.     /* best overlap */
  102.     unsigned  frames_search;
  103.     void     *buf_pre_corr;
  104.     void     *table_window;
  105.     unsigned(*best_overlap_offset)( aout_filter_t *p_filter );
  106.     /* for "audio filter" only, manage own buffers */
  107.     int       i_buf;
  108.     uint8_t  *p_buffers[2];
  109. };
  110. /*****************************************************************************
  111.  * best_overlap_offset: calculate best offset for overlap
  112.  *****************************************************************************/
  113. static unsigned best_overlap_offset_float( aout_filter_t *p_filter )
  114. {
  115.     aout_filter_sys_t *p = p_filter->p_sys;
  116.     float *pw, *po, *ppc, *search_start;
  117.     float best_corr = INT_MIN;
  118.     unsigned best_off = 0;
  119.     unsigned i, off;
  120.     pw  = p->table_window;
  121.     po  = p->buf_overlap;
  122.     po += p->samples_per_frame;
  123.     ppc = p->buf_pre_corr;
  124.     for( i = p->samples_per_frame; i < p->samples_overlap; i++ ) {
  125.       *ppc++ = *pw++ * *po++;
  126.     }
  127.     search_start = (float *)p->buf_queue + p->samples_per_frame;
  128.     for( off = 0; off < p->frames_search; off++ ) {
  129.       float corr = 0;
  130.       float *ps = search_start;
  131.       ppc = p->buf_pre_corr;
  132.       for( i = p->samples_per_frame; i < p->samples_overlap; i++ ) {
  133.         corr += *ppc++ * *ps++;
  134.       }
  135.       if( corr > best_corr ) {
  136.         best_corr = corr;
  137.         best_off  = off;
  138.       }
  139.       search_start += p->samples_per_frame;
  140.     }
  141.     return best_off * p->bytes_per_frame;
  142. }
  143. /*****************************************************************************
  144.  * output_overlap: blend end of previous stride with beginning of current stride
  145.  *****************************************************************************/
  146. static void output_overlap_float( aout_filter_t   *p_filter,
  147.                                   void            *buf_out,
  148.                                   unsigned         bytes_off )
  149. {
  150.     aout_filter_sys_t *p = p_filter->p_sys;
  151.     float *pout = buf_out;
  152.     float *pb   = p->table_blend;
  153.     float *po   = p->buf_overlap;
  154.     float *pin  = (float *)( p->buf_queue + bytes_off );
  155.     unsigned i;
  156.     for( i = 0; i < p->samples_overlap; i++ ) {
  157.         *pout++ = *po - *pb++ * ( *po - *pin++ ); po++;
  158.     }
  159. }
  160. /*****************************************************************************
  161.  * fill_queue: fill p_sys->buf_queue as much possible, skipping samples as needed
  162.  *****************************************************************************/
  163. static size_t fill_queue( aout_filter_t *p_filter,
  164.                           uint8_t       *p_buffer,
  165.                           size_t         i_buffer,
  166.                           size_t         offset )
  167. {
  168.     aout_filter_sys_t *p = p_filter->p_sys;
  169.     unsigned bytes_in = i_buffer - offset;
  170.     size_t offset_unchanged = offset;
  171.     if( p->bytes_to_slide > 0 ) {
  172.         if( p->bytes_to_slide < p->bytes_queued ) {
  173.             unsigned bytes_in_move = p->bytes_queued - p->bytes_to_slide;
  174.             memmove( p->buf_queue,
  175.                      p->buf_queue + p->bytes_to_slide,
  176.                      bytes_in_move );
  177.             p->bytes_to_slide = 0;
  178.             p->bytes_queued   = bytes_in_move;
  179.         } else {
  180.             unsigned bytes_in_skip;
  181.             p->bytes_to_slide -= p->bytes_queued;
  182.             bytes_in_skip      = __MIN( p->bytes_to_slide, bytes_in );
  183.             p->bytes_queued    = 0;
  184.             p->bytes_to_slide -= bytes_in_skip;
  185.             offset            += bytes_in_skip;
  186.             bytes_in          -= bytes_in_skip;
  187.         }
  188.     }
  189.     if( bytes_in > 0 ) {
  190.         unsigned bytes_in_copy = __MIN( p->bytes_queue_max - p->bytes_queued, bytes_in );
  191.         memcpy( p->buf_queue + p->bytes_queued,
  192.                 p_buffer + offset,
  193.                 bytes_in_copy );
  194.         p->bytes_queued += bytes_in_copy;
  195.         offset          += bytes_in_copy;
  196.     }
  197.     return offset - offset_unchanged;
  198. }
  199. /*****************************************************************************
  200.  * transform_buffer: main filter loop
  201.  *****************************************************************************/
  202. static size_t transform_buffer( aout_filter_t   *p_filter,
  203.                                 uint8_t         *p_buffer,
  204.                                 size_t           i_buffer,
  205.                                 uint8_t         *pout )
  206. {
  207.     aout_filter_sys_t *p = p_filter->p_sys;
  208.     size_t offset_in = fill_queue( p_filter, p_buffer, i_buffer, 0 );
  209.     unsigned bytes_out = 0;
  210.     while( p->bytes_queued >= p->bytes_queue_max ) {
  211.         unsigned bytes_off = 0;
  212.         // output stride
  213.         if( p->output_overlap ) {
  214.             if( p->best_overlap_offset ) {
  215.                 bytes_off = p->best_overlap_offset( p_filter );
  216.             }
  217.             p->output_overlap( p_filter, pout, bytes_off );
  218.         }
  219.         memcpy( pout + p->bytes_overlap,
  220.                 p->buf_queue + bytes_off + p->bytes_overlap,
  221.                 p->bytes_standing );
  222.         pout += p->bytes_stride;
  223.         bytes_out += p->bytes_stride;
  224.         // input stride
  225.         memcpy( p->buf_overlap,
  226.                 p->buf_queue + bytes_off + p->bytes_stride,
  227.                 p->bytes_overlap );
  228.         double frames_to_slide = p->frames_stride_scaled + p->frames_stride_error;
  229.         unsigned frames_to_stride_whole = (int)frames_to_slide;
  230.         p->bytes_to_slide       = frames_to_stride_whole * p->bytes_per_frame;
  231.         p->frames_stride_error  = frames_to_slide - frames_to_stride_whole;
  232.         offset_in += fill_queue( p_filter, p_buffer, i_buffer, offset_in );
  233.     }
  234.     return bytes_out;
  235. }
  236. /*****************************************************************************
  237.  * calculate_output_buffer_size
  238.  *****************************************************************************/
  239. static size_t calculate_output_buffer_size( aout_filter_t   *p_filter,
  240.                                             size_t           bytes_in )
  241. {
  242.     aout_filter_sys_t *p = p_filter->p_sys;
  243.     size_t bytes_out = 0;
  244.     int bytes_to_out = bytes_in + p->bytes_queued - p->bytes_to_slide;
  245.     if( bytes_to_out >= (int)p->bytes_queue_max ) {
  246.       /* while (total_buffered - stride_length * n >= queue_max) n++ */
  247.       bytes_out = p->bytes_stride * ( (unsigned)(
  248.           ( bytes_to_out - p->bytes_queue_max + /* rounding protection */ p->bytes_per_frame )
  249.           / p->bytes_stride_scaled ) + 1 );
  250.     }
  251.     return bytes_out;
  252. }
  253. /*****************************************************************************
  254.  * reinit_buffers: reinitializes buffers in p_filter->p_sys
  255.  *****************************************************************************/
  256. static int reinit_buffers( aout_filter_t *p_filter )
  257. {
  258.     aout_filter_sys_t *p = p_filter->p_sys;
  259.     unsigned i,j;
  260.     unsigned frames_stride = p->ms_stride * p->sample_rate / 1000.0;
  261.     p->bytes_stride = frames_stride * p->bytes_per_frame;
  262.     /* overlap */
  263.     unsigned frames_overlap = frames_stride * p->percent_overlap;
  264.     if( frames_overlap < 1 )
  265.     { /* if no overlap */
  266.         p->bytes_overlap    = 0;
  267.         p->bytes_standing   = p->bytes_stride;
  268.         p->samples_standing = p->bytes_standing / p->bytes_per_sample;
  269.         p->output_overlap   = NULL;
  270.     }
  271.     else
  272.     {
  273.         unsigned prev_overlap   = p->bytes_overlap;
  274.         p->bytes_overlap    = frames_overlap * p->bytes_per_frame;
  275.         p->samples_overlap  = frames_overlap * p->samples_per_frame;
  276.         p->bytes_standing   = p->bytes_stride - p->bytes_overlap;
  277.         p->samples_standing = p->bytes_standing / p->bytes_per_sample;
  278.         p->buf_overlap      = malloc( p->bytes_overlap );
  279.         p->table_blend      = malloc( p->samples_overlap * 4 ); /* sizeof (int32|float) */
  280.         if( !p->buf_overlap || !p->table_blend )
  281.             return VLC_ENOMEM;
  282.         if( p->bytes_overlap > prev_overlap )
  283.             memset( (uint8_t *)p->buf_overlap + prev_overlap, 0, p->bytes_overlap - prev_overlap );
  284.         float *pb = p->table_blend;
  285.         float t = (float)frames_overlap;
  286.         for( i = 0; i<frames_overlap; i++ )
  287.         {
  288.             float v = i / t;
  289.             for( j = 0; j < p->samples_per_frame; j++ )
  290.                 *pb++ = v;
  291.         }
  292.         p->output_overlap = output_overlap_float;
  293.     }
  294.     /* best overlap */
  295.     p->frames_search = ( frames_overlap <= 1 ) ? 0 : p->ms_search * p->sample_rate / 1000.0;
  296.     if( p->frames_search < 1 )
  297.     { /* if no search */
  298.         p->best_overlap_offset = NULL;
  299.     }
  300.     else
  301.     {
  302.         unsigned bytes_pre_corr = ( p->samples_overlap - p->samples_per_frame ) * 4; /* sizeof (int32|float) */
  303.         p->buf_pre_corr = malloc( bytes_pre_corr );
  304.         p->table_window = malloc( bytes_pre_corr );
  305.         if( ! p->buf_pre_corr || ! p->table_window )
  306.             return VLC_ENOMEM;
  307.         float *pw = p->table_window;
  308.         for( i = 1; i<frames_overlap; i++ )
  309.         {
  310.             float v = i * ( frames_overlap - i );
  311.             for( j = 0; j < p->samples_per_frame; j++ )
  312.                 *pw++ = v;
  313.         }
  314.         p->best_overlap_offset = best_overlap_offset_float;
  315.     }
  316.     unsigned new_size = ( p->frames_search + frames_stride + frames_overlap ) * p->bytes_per_frame;
  317.     if( p->bytes_queued > new_size )
  318.     {
  319.         if( p->bytes_to_slide > p->bytes_queued )
  320.         {
  321.           p->bytes_to_slide -= p->bytes_queued;
  322.           p->bytes_queued    = 0;
  323.         }
  324.         else
  325.         {
  326.             unsigned new_queued = __MIN( p->bytes_queued - p->bytes_to_slide, new_size );
  327.             memmove( p->buf_queue,
  328.                      p->buf_queue + p->bytes_queued - new_queued,
  329.                      new_queued );
  330.             p->bytes_to_slide = 0;
  331.             p->bytes_queued   = new_queued;
  332.         }
  333.     }
  334.     p->bytes_queue_max = new_size;
  335.     p->buf_queue = malloc( p->bytes_queue_max );
  336.     if( ! p->buf_queue )
  337.         return VLC_ENOMEM;
  338.     p->bytes_stride_scaled  = p->bytes_stride * p->scale;
  339.     p->frames_stride_scaled = p->bytes_stride_scaled / p->bytes_per_frame;
  340.     msg_Dbg( VLC_OBJECT(p_filter),
  341.              "%.3f scale, %.3f stride_in, %i stride_out, %i standing, %i overlap, %i search, %i queue, %s mode",
  342.              p->scale,
  343.              p->frames_stride_scaled,
  344.              (int)( p->bytes_stride / p->bytes_per_frame ),
  345.              (int)( p->bytes_standing / p->bytes_per_frame ),
  346.              (int)( p->bytes_overlap / p->bytes_per_frame ),
  347.              p->frames_search,
  348.              (int)( p->bytes_queue_max / p->bytes_per_frame ),
  349.              "fl32");
  350.     return VLC_SUCCESS;
  351. }
  352. /*****************************************************************************
  353.  * Open: initialize as "audio filter"
  354.  *****************************************************************************/
  355. static int Open( vlc_object_t *p_this )
  356. {
  357.     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
  358.     aout_filter_sys_t *p_sys;
  359.     bool b_fit = true;
  360.     if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' ) ||
  361.         p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
  362.     {
  363.         b_fit = false;
  364.         p_filter->input.i_format = p_filter->output.i_format = VLC_FOURCC('f','l','3','2');
  365.         msg_Warn( p_filter, "bad input or output format" );
  366.     }
  367.     if( ! AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
  368.     {
  369.         b_fit = false;
  370.         memcpy( &p_filter->output, &p_filter->input, sizeof(audio_sample_format_t) );
  371.         msg_Warn( p_filter, "input and output formats are not similar" );
  372.     }
  373.     if( ! b_fit )
  374.         return VLC_EGENERIC;
  375.     p_filter->pf_do_work = DoWork;
  376.     p_filter->b_in_place = false;
  377.     /* Allocate structure */
  378.     p_sys = p_filter->p_sys = malloc( sizeof(aout_filter_sys_t) );
  379.     if( ! p_sys )
  380.         return VLC_ENOMEM;
  381.     p_sys->scale             = 1.0;
  382.     p_sys->sample_rate       = p_filter->input.i_rate;
  383.     p_sys->samples_per_frame = aout_FormatNbChannels( &p_filter->input );
  384.     p_sys->bytes_per_sample  = 4;
  385.     p_sys->bytes_per_frame   = p_sys->samples_per_frame * p_sys->bytes_per_sample;
  386.     msg_Dbg( p_this, "format: %5i rate, %i nch, %i bps, %s",
  387.              p_sys->sample_rate,
  388.              p_sys->samples_per_frame,
  389.              p_sys->bytes_per_sample,
  390.              "fl32" );
  391.     p_sys->ms_stride       = config_GetInt(   p_this, "scaletempo-stride" );
  392.     p_sys->percent_overlap = config_GetFloat( p_this, "scaletempo-overlap" );
  393.     p_sys->ms_search       = config_GetInt(   p_this, "scaletempo-search" );
  394.     msg_Dbg( p_this, "params: %i stride, %.3f overlap, %i search",
  395.              p_sys->ms_stride, p_sys->percent_overlap, p_sys->ms_search );
  396.     p_sys->i_buf = 0;
  397.     p_sys->p_buffers[0] = NULL;
  398.     p_sys->p_buffers[1] = NULL;
  399.     p_sys->buf_queue      = NULL;
  400.     p_sys->buf_overlap    = NULL;
  401.     p_sys->table_blend    = NULL;
  402.     p_sys->buf_pre_corr   = NULL;
  403.     p_sys->table_window   = NULL;
  404.     p_sys->bytes_overlap  = 0;
  405.     p_sys->bytes_queued   = 0;
  406.     p_sys->bytes_to_slide = 0;
  407.     p_sys->frames_stride_error = 0;
  408.     if( reinit_buffers( p_filter ) != VLC_SUCCESS )
  409.     {
  410.         Close( p_this );
  411.         return VLC_EGENERIC;
  412.     }
  413.     return VLC_SUCCESS;
  414. }
  415. static void Close( vlc_object_t *p_this )
  416. {
  417.     aout_filter_t *p_filter = (aout_filter_t *)p_this;
  418.     aout_filter_sys_t *p_sys = p_filter->p_sys;
  419.     free( p_sys->buf_queue );
  420.     free( p_sys->buf_overlap );
  421.     free( p_sys->table_blend );
  422.     free( p_sys->buf_pre_corr );
  423.     free( p_sys->table_window );
  424.     free( p_sys->p_buffers[0] );
  425.     free( p_sys->p_buffers[1] );
  426.     free( p_filter->p_sys );
  427. }
  428. /*****************************************************************************
  429.  * DoWork: aout_filter wrapper for transform_buffer
  430.  *****************************************************************************/
  431. static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
  432.                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
  433. {
  434.     VLC_UNUSED(p_aout);
  435.     aout_filter_sys_t *p = p_filter->p_sys;
  436.     if( p_filter->input.i_rate == p->sample_rate ) {
  437.       memcpy( p_out_buf->p_buffer, p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
  438.       p_out_buf->i_nb_bytes   = p_in_buf->i_nb_bytes;
  439.       p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
  440.       return;
  441.     }
  442.     double scale = p_filter->input.i_rate / (double)p->sample_rate;
  443.     if( scale != p->scale ) {
  444.       p->scale = scale;
  445.       p->bytes_stride_scaled  = p->bytes_stride * p->scale;
  446.       p->frames_stride_scaled = p->bytes_stride_scaled / p->bytes_per_frame;
  447.       p->bytes_to_slide = 0;
  448.       msg_Dbg( p_filter, "%.3f scale, %.3f stride_in, %i stride_out",
  449.                p->scale,
  450.                p->frames_stride_scaled,
  451.                (int)( p->bytes_stride / p->bytes_per_frame ) );
  452.     }
  453.     size_t i_outsize = calculate_output_buffer_size ( p_filter, p_in_buf->i_nb_bytes );
  454.     if( i_outsize > p_out_buf->i_size ) {
  455.         void *temp = realloc( p->p_buffers[ p->i_buf ], i_outsize );
  456.         if( temp == NULL )
  457.         {
  458.             return;
  459.         }
  460.         p->p_buffers[ p->i_buf ] = temp;
  461.         p_out_buf->p_buffer = p->p_buffers[ p->i_buf ];
  462.         p->i_buf = ! p->i_buf;
  463.     }
  464.     size_t bytes_out = transform_buffer( p_filter,
  465.         p_in_buf->p_buffer, p_in_buf->i_nb_bytes,
  466.         p_out_buf->p_buffer );
  467.     p_out_buf->i_nb_bytes   = bytes_out;
  468.     p_out_buf->i_nb_samples = bytes_out / p->bytes_per_frame;
  469. }