chorus_flanger.c 14.8 KB
Newer Older
1
/*****************************************************************************
2
 * chorus_flanger: Basic chorus/flanger/delay audio filter
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2009-12 VLC authors and VideoLAN
5 6
 * $Id$
 *
7 8
 * Authors: Srikanth Raju < srikiraju at gmail dot com >
 *          Sukrit Sangwan < sukritsangwan at gmail dot com >
9
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28 29 30 31 32 33 34
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <math.h>

#include <vlc_common.h>
#include <vlc_plugin.h>

#include <vlc_aout.h>
35
#include <vlc_filter.h>
36 37 38 39 40 41 42

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

static int  Open     ( vlc_object_t * );
static void Close    ( vlc_object_t * );
43
static block_t *DoWork( filter_t *, block_t * );
44 45 46
static int paramCallback( vlc_object_t *, char const *, vlc_value_t ,
                          vlc_value_t , void * );
static int reallocate_buffer( filter_t *, filter_sys_t * );
47

48
struct filter_sys_t
49 50 51 52 53 54 55 56
{
    /* TODO: Cleanup and optimise */
    int i_cumulative;
    int i_channels, i_sampleRate;
    float f_delayTime, f_feedbackGain;  /* delayTime is in milliseconds */
    float f_wetLevel, f_dryLevel;
    float f_sweepDepth, f_sweepRate;

57 58
    float f_offset;
    int i_step;
59 60 61 62 63
    float f_temp;
    float f_sinMultiplier;

    /* This data is for the the circular queue which stores the samples. */
    int i_bufferLength;
64 65
    float * p_delayLineStart, * p_delayLineEnd;
    float * p_write;
66 67 68 69 70 71 72 73 74
};

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/


vlc_module_begin ()
    set_description( N_("Sound Delay") )
75 76
    set_shortname( N_("Delay") )
    set_help( N_("Add a delay effect to the sound") )
77 78 79
    set_category( CAT_AUDIO )
    set_subcategory( SUBCAT_AUDIO_AFILTER )
    add_shortcut( "delay" )
80
    add_float( "delay-time", 20, N_("Delay time"),
81
        N_("Time in milliseconds of the average delay. Note average"), true )
82
    add_float( "sweep-depth", 6, N_("Sweep Depth"),
83 84
        N_("Time in milliseconds of the maximum sweep depth. Thus, the sweep "
            "range will be delay-time +/- sweep-depth."), true )
85
    add_float( "sweep-rate", 6, N_("Sweep Rate"),
86 87
        N_("Rate of change of sweep depth in milliseconds shift per second "
           "of play"), true )
88
    add_float_with_range( "feedback-gain", 0.5, -0.9, 0.9,
Christoph Miebach's avatar
Christoph Miebach committed
89
        N_("Feedback gain"), N_("Gain on Feedback loop"), true )
90
    add_float_with_range( "wet-mix", 0.4, -0.999, 0.999,
91
        N_("Wet mix"), N_("Level of delayed signal"), true )
92
    add_float_with_range( "dry-mix", 0.4, -0.999, 0.999,
93
        N_("Dry Mix"), N_("Level of input signal"), true )
94
    set_capability( "audio filter", 0 )
95 96 97 98 99 100 101
    set_callbacks( Open, Close )
vlc_module_end ()

/**
 * small_value: Helper function
 * return high pass cutoff
 */
102
static inline float small_value(void)
103 104
{
    /* allows for 2^-24, should be enough for 24-bit DACs at least */
105
    return 1.f / 16777216.f;
106 107 108 109 110 111 112 113
}

/**
 * Open: initialize and create stuff
 * @param p_this
 */
static int Open( vlc_object_t *p_this )
{
114
    filter_t *p_filter = (filter_t*)p_this;
115
    filter_sys_t *p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
116 117 118
    if( !p_sys )
        return VLC_ENOMEM;

119
    p_sys->i_channels       = aout_FormatNbChannels( &p_filter->fmt_in.audio );
120 121 122 123 124 125
    p_sys->f_delayTime      = var_CreateGetFloat( p_this, "delay-time" );
    p_sys->f_sweepDepth     = var_CreateGetFloat( p_this, "sweep-depth" );
    p_sys->f_sweepRate      = var_CreateGetFloat( p_this, "sweep-rate" );
    p_sys->f_feedbackGain   = var_CreateGetFloat( p_this, "feedback-gain" );
    p_sys->f_dryLevel       = var_CreateGetFloat( p_this, "dry-mix" );
    p_sys->f_wetLevel       = var_CreateGetFloat( p_this, "wet-mix" );
126 127 128 129 130 131
    var_AddCallback( p_this, "delay-time", paramCallback, p_sys );
    var_AddCallback( p_this, "sweep-depth", paramCallback, p_sys );
    var_AddCallback( p_this, "sweep-rate", paramCallback, p_sys );
    var_AddCallback( p_this, "feedback-gain", paramCallback, p_sys );
    var_AddCallback( p_this, "dry-mix", paramCallback, p_sys );
    var_AddCallback( p_this, "wet-mix", paramCallback, p_sys );
132

133
    if( p_sys->f_delayTime < 0.f )
134 135 136 137 138 139
    {
        msg_Err( p_filter, "Delay Time is invalid" );
        free(p_sys);
        return VLC_EGENERIC;
    }

140
    if( p_sys->f_sweepDepth > p_sys->f_delayTime || p_sys->f_sweepDepth < 0.f )
141 142 143 144 145 146
    {
        msg_Err( p_filter, "Sweep Depth is invalid" );
        free( p_sys );
        return VLC_EGENERIC;
    }

147
    if( p_sys->f_sweepRate < 0.f )
148 149 150 151 152 153 154 155
    {
        msg_Err( p_filter, "Sweep Rate is invalid" );
        free( p_sys );
        return VLC_EGENERIC;
    }

    /* Max delay = delay + depth. Min = delay - depth */
    p_sys->i_bufferLength = p_sys->i_channels * ( (int)( ( p_sys->f_delayTime
156
                + p_sys->f_sweepDepth ) * p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
157 158

    msg_Dbg( p_filter , "Buffer length:%d, Channels:%d, Sweep Depth:%f, Delay "
159 160 161 162
             "time:%f, Sweep Rate:%f, Sample Rate: %d", p_sys->i_bufferLength,
             p_sys->i_channels, (double) p_sys->f_sweepDepth,
             (double) p_sys->f_delayTime, (double) p_sys->f_sweepRate,
             p_filter->fmt_in.audio.i_rate );
163 164
    if( p_sys->i_bufferLength <= 0 )
    {
165
        msg_Err( p_filter, "Delay-time, Sample rate or Channels was incorrect" );
166 167 168 169
        free(p_sys);
        return VLC_EGENERIC;
    }

170 171
    p_sys->p_delayLineStart = calloc( p_sys->i_bufferLength, sizeof( float ) );
    if( !p_sys->p_delayLineStart )
172 173 174 175 176 177 178 179 180 181
    {
        free( p_sys );
        return VLC_ENOMEM;
    }

    p_sys->i_cumulative = 0;
    p_sys->i_step = p_sys->f_sweepRate > 0 ? 1 : 0;
    p_sys->f_offset = 0;
    p_sys->f_temp = 0;

182 183
    p_sys->p_delayLineEnd = p_sys->p_delayLineStart + p_sys->i_bufferLength;
    p_sys->p_write = p_sys->p_delayLineStart;
184 185

    if( p_sys->f_sweepDepth < small_value() ||
186
            p_filter->fmt_in.audio.i_rate < small_value() ) {
187
        p_sys->f_sinMultiplier = 0.f;
188 189 190
    }
    else {
        p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
191
            ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate ) ;
192
    }
193
    p_sys->i_sampleRate = p_filter->fmt_in.audio.i_rate;
194

195
    p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
196
    aout_FormatPrepare(&p_filter->fmt_in.audio);
197 198 199
    p_filter->fmt_out.audio = p_filter->fmt_in.audio;
    p_filter->pf_audio_filter = DoWork;

200 201 202 203 204 205 206 207 208
    return VLC_SUCCESS;
}

/**
 * sanitize: Helper function to eliminate small amplitudes
 * @param f_value pointer to value to clean
 */
static inline void sanitize( float * f_value )
{
209 210
    if ( fabsf( *f_value ) < small_value() )
        *f_value = 0.f;
211 212 213 214 215 216 217
}


/**
 * DoWork : delays and finds the value of the current frame
 * @param p_filter This filter object
 * @param p_in_buf Input buffer
218
 * @return Output buffer
219
 */
220
static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
221
{
222
    struct filter_sys_t *p_sys = p_filter->p_sys;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
223
    int i_chan;
224
    unsigned i_samples = p_in_buf->i_nb_samples; /* number of samples */
225
    /* maximum number of samples to offset in buffer */
226
    int i_maxOffset = floorf( p_sys->f_sweepDepth * p_sys->i_sampleRate / 1000 );
227
    float *p_out = (float*)p_in_buf->p_buffer;
228 229
    float *p_in =  (float*)p_in_buf->p_buffer;

230
    float *p_ptr, f_temp = 0;/* f_diff = 0, f_frac = 0;*/
231 232

    /* Process each sample */
233
    for( unsigned i = 0; i < i_samples ; i++ )
234
    {
235 236 237 238
        /* Sine function as a oscillator wave to calculate sweep */
        p_sys->i_cumulative += p_sys->i_step;
        p_sys->f_offset = sinf( (p_sys->i_cumulative) * p_sys->f_sinMultiplier )
                * floorf(p_sys->f_sweepDepth * p_sys->i_sampleRate / 1000);
239 240
        if( abs( p_sys->i_step ) > 0 )
        {
241
            if( p_sys->i_cumulative >=  floorf( p_sys->f_sweepDepth *
242 243 244 245 246
                        p_sys->i_sampleRate / p_sys->f_sweepRate ))
            {
                p_sys->f_offset = i_maxOffset;
                p_sys->i_step = -1 * ( p_sys->i_step );
            }
247
            if( p_sys->i_cumulative <= floorf( -1 * p_sys->f_sweepDepth *
248 249 250 251 252 253 254
                        p_sys->i_sampleRate / p_sys->f_sweepRate ) )
            {
                p_sys->f_offset = -i_maxOffset;
                p_sys->i_step = -1 * ( p_sys->i_step );
            }
        }
        /* Calculate position in delay */
255
        int offset = floorf( p_sys->f_offset );
256
        p_ptr = p_sys->p_write + ( i_maxOffset - offset ) * p_sys->i_channels;
257 258

        /* Handle Overflow */
259
        if( p_ptr < p_sys->p_delayLineStart )
260
        {
261
            p_ptr += p_sys->i_bufferLength - p_sys->i_channels;
262
        }
263
        if( p_ptr > p_sys->p_delayLineEnd - 2*p_sys->i_channels )
264
        {
265
            p_ptr -= p_sys->i_bufferLength - p_sys->i_channels;
266 267
        }
        /* For interpolation */
268
/*        f_frac = ( p_sys->f_offset - (int)p_sys->f_offset );*/
269 270
        for( i_chan = 0; i_chan < p_sys->i_channels; i_chan++ )
        {
271 272 273 274 275 276
/*            if( p_ptr <= p_sys->p_delayLineStart + p_sys->i_channels )
                f_diff = *(p_sys->p_delayLineEnd + i_chan) - p_ptr[i_chan];
            else
                f_diff = *( p_ptr - p_sys->i_channels + i_chan )
                            - p_ptr[i_chan];*/
            f_temp = ( *( p_ptr + i_chan ) );//+ f_diff * f_frac;
277 278 279 280
            /*Linear Interpolation. FIXME. This creates LOTS of noise */
            sanitize(&f_temp);
            p_out[i_chan] = p_sys->f_dryLevel * p_in[i_chan] +
                p_sys->f_wetLevel * f_temp;
281
            *( p_sys->p_write + i_chan ) = p_in[i_chan] +
282 283
                p_sys->f_feedbackGain * f_temp;
        }
284
        if( p_sys->p_write == p_sys->p_delayLineStart )
285
            for( i_chan = 0; i_chan < p_sys->i_channels; i_chan++ )
286 287
                *( p_sys->p_delayLineEnd - p_sys->i_channels + i_chan )
                    = *( p_sys->p_delayLineStart + i_chan );
288 289 290

        p_in += p_sys->i_channels;
        p_out += p_sys->i_channels;
291 292
        p_sys->p_write += p_sys->i_channels;
        if( p_sys->p_write == p_sys->p_delayLineEnd - p_sys->i_channels )
293
        {
294
            p_sys->p_write = p_sys->p_delayLineStart;
295 296 297
        }

    }
298
    return p_in_buf;
299 300 301 302 303 304 305 306
}

/**
 * Close: Destructor
 * @param p_this pointer to this filter object
 */
static void Close( vlc_object_t *p_this )
{
307 308
    filter_t *p_filter = ( filter_t* )p_this;
    filter_sys_t *p_sys = p_filter->p_sys;
309

310 311 312 313 314 315 316 317 318 319 320 321 322 323
    var_DelCallback( p_this, "delay-time", paramCallback, p_sys );
    var_DelCallback( p_this, "sweep-depth", paramCallback, p_sys );
    var_DelCallback( p_this, "sweep-rate", paramCallback, p_sys );
    var_DelCallback( p_this, "feedback-gain", paramCallback, p_sys );
    var_DelCallback( p_this, "wet-mix", paramCallback, p_sys );
    var_DelCallback( p_this, "dry-mix", paramCallback, p_sys );
    var_Destroy( p_this, "delay-time" );
    var_Destroy( p_this, "sweep-depth" );
    var_Destroy( p_this, "sweep-rate" );
    var_Destroy( p_this, "feedback-gain" );
    var_Destroy( p_this, "wet-mix" );
    var_Destroy( p_this, "dry-mix" );

    free( p_sys->p_delayLineStart );
324 325
    free( p_sys );
}
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

/******************************************************************************
 * Callback to update parameters on the fly
 ******************************************************************************/
static int paramCallback( vlc_object_t *p_this, char const *psz_var,
                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    filter_t *p_filter = (filter_t *)p_this;
    filter_sys_t *p_sys = (filter_sys_t *) p_data;

    if( !strncmp( psz_var, "delay-time", 10 ) )
    {
        /* if invalid value pretend everything is OK without updating value */
        if( newval.f_float < 0 )
            return VLC_SUCCESS;
        p_sys->f_delayTime = newval.f_float;
        if( !reallocate_buffer( p_filter, p_sys ) )
        {
            p_sys->f_delayTime = oldval.f_float;
            p_sys->i_bufferLength = p_sys->i_channels * ( (int)
                            ( ( p_sys->f_delayTime + p_sys->f_sweepDepth ) * 
                              p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
        }
    }
    else if( !strncmp( psz_var, "sweep-depth", 11 ) )
    {
        if( newval.f_float < 0 || newval.f_float > p_sys->f_delayTime)
            return VLC_SUCCESS;
        p_sys->f_sweepDepth = newval.f_float;
        if( !reallocate_buffer( p_filter, p_sys ) )
        {
            p_sys->f_sweepDepth = oldval.f_float;
            p_sys->i_bufferLength = p_sys->i_channels * ( (int)
                            ( ( p_sys->f_delayTime + p_sys->f_sweepDepth ) * 
                              p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
        }
    }
    else if( !strncmp( psz_var, "sweep-rate", 10 ) )
    {
        if( newval.f_float > p_sys->f_sweepDepth )
            return VLC_SUCCESS;
        p_sys->f_sweepRate = newval.f_float;
        /* Calculate new f_sinMultiplier */
        if( p_sys->f_sweepDepth < small_value() ||
                p_filter->fmt_in.audio.i_rate < small_value() ) {
            p_sys->f_sinMultiplier = 0.0;
        }
        else {
            p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
                ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate ) ;
        }
    }
    else if( !strncmp( psz_var, "feedback-gain", 13 ) )
        p_sys->f_feedbackGain = newval.f_float;
    else if( !strncmp( psz_var, "wet-mix", 7 ) )
        p_sys->f_wetLevel = newval.f_float;
    else if( !strncmp( psz_var, "dry-mix", 7 ) )
        p_sys->f_dryLevel = newval.f_float;

    return VLC_SUCCESS;
}

static int reallocate_buffer( filter_t *p_filter,  filter_sys_t *p_sys )
{
    p_sys->i_bufferLength = p_sys->i_channels * ( (int)( ( p_sys->f_delayTime
           + p_sys->f_sweepDepth ) * p_filter->fmt_in.audio.i_rate/1000 ) + 1 );

    float *temp = realloc( p_sys->p_delayLineStart, p_sys->i_bufferLength );
    if( unlikely( !temp ) )
    {
        msg_Err( p_filter, "Couldnt reallocate buffer for new delay." );
        return 0;
    }
    p_sys->p_delayLineStart = temp;
    p_sys->p_delayLineEnd = p_sys->p_delayLineStart + p_sys->i_bufferLength;
    return 1;
}