28 #define VOCLIB_MAX_BANDS 96
31 #define VOCLIB_MAX_FILTERS_PER_BAND 8
66 int voclib_process (
voclib_instance* instance,
const float* carrier_buffer,
const float* modulator_buffer,
float* output_buffer,
unsigned int frames );
110 float a0, a1, a2, a3,
a4;
111 float x1, x2, y1,
y2;
148 #ifdef VOCLIB_IMPLEMENTATION
154 #define VOCLIB_INLINE __forceinline
157 #define VOCLIB_INLINE inline __attribute__((always_inline))
159 #define VOCLIB_INLINE inline
189 #define VOCLIB_M_LN2 0.69314718055994530942
193 #define VOCLIB_M_PI 3.14159265358979323846
197 static VOCLIB_INLINE
float voclib_BiQuad (
float sample,
voclib_biquad* b )
202 result = b->
a0 * sample + b->
a1 * b->
x1 + b->
a2 * b->
x2 -
229 static void voclib_BiQuad_new (
voclib_biquad* b,
int type,
float dbGain,
234 float A, omega, sn, cs, alpha, beta;
235 float a0, a1, a2, b0, b1, b2;
238 A = ( float ) pow ( 10, dbGain / 40.0f );
239 omega = ( float ) ( 2.0 * VOCLIB_M_PI * freq / srate );
240 sn = ( float ) sin ( omega );
241 cs = ( float ) cos ( omega );
242 alpha = sn * ( float ) sinh ( VOCLIB_M_LN2 / 2 * bandwidth * omega / sn );
243 beta = ( float ) sqrt ( A + A );
280 b0 = 1 + ( alpha * A );
282 b2 = 1 - ( alpha * A );
283 a0 = 1 + ( alpha / A );
285 a2 = 1 - ( alpha / A );
288 b0 = A * ( ( A + 1 ) - ( A - 1 ) * cs + beta * sn );
289 b1 = 2 * A * ( ( A - 1 ) - ( A + 1 ) * cs );
290 b2 = A * ( ( A + 1 ) - ( A - 1 ) * cs - beta * sn );
291 a0 = ( A + 1 ) + ( A - 1 ) * cs + beta * sn;
292 a1 = -2 * ( ( A - 1 ) + ( A + 1 ) * cs );
293 a2 = ( A + 1 ) + ( A - 1 ) * cs - beta * sn;
296 b0 = A * ( ( A + 1 ) + ( A - 1 ) * cs + beta * sn );
297 b1 = -2 * A * ( ( A - 1 ) + ( A + 1 ) * cs );
298 b2 = A * ( ( A + 1 ) + ( A - 1 ) * cs - beta * sn );
299 a0 = ( A + 1 ) - ( A - 1 ) * cs + beta * sn;
300 a1 = 2 * ( ( A - 1 ) - ( A + 1 ) * cs );
301 a2 = ( A + 1 ) - ( A - 1 ) * cs - beta * sn;
319 b->
x1 = b->
x2 = 0.0f;
320 b->
y1 = b->
y2 = 0.0f;
325 static void voclib_envelope_configure (
voclib_envelope* envelope,
double time_in_seconds,
double sample_rate )
327 envelope->
coef = ( float ) ( pow ( 0.01, 1.0 / ( time_in_seconds * sample_rate ) ) );
339 static VOCLIB_INLINE
float voclib_envelope_tick (
voclib_envelope* envelope,
float sample )
341 const float coef = envelope->
coef;
342 envelope->
history[0] = ( float ) ( ( 1.0f - coef ) * fabs ( sample ) ) + ( coef * envelope->
history[0] );
350 static void voclib_initialize_filterbank (
voclib_instance* instance,
int carrier_only )
354 double lastfreq = 0.0;
355 double minfreq = 80.0;
357 if ( maxfreq > 12000.0 )
361 step = pow ( ( maxfreq / minfreq ), ( 1.0 / instance->
bands ) );
363 for ( i = 0; i < instance->
bands; ++i )
366 double bandwidth, nextfreq;
367 double priorfreq = lastfreq;
368 if ( lastfreq > 0.0 )
376 nextfreq = lastfreq * step;
377 bandwidth = ( nextfreq - priorfreq ) / lastfreq;
435 for ( i = 1; i < instance->
bands; ++i )
441 int voclib_initialize (
voclib_instance* instance,
unsigned char bands,
unsigned char filters_per_band,
unsigned int sample_rate,
unsigned char carrier_channels )
455 if ( sample_rate < 8000 || sample_rate > 192000 )
459 if ( carrier_channels < 1 || carrier_channels > 2 )
467 instance->
bands = bands;
472 voclib_initialize_filterbank ( instance, 0 );
473 voclib_initialize_envelopes ( instance );
482 for ( i = 0; i < instance->
bands; ++i )
496 int voclib_process (
voclib_instance* instance,
const float* carrier_buffer,
const float* modulator_buffer,
float* output_buffer,
unsigned int frames )
499 const unsigned char bands = instance->
bands;
502 if ( !carrier_buffer )
506 if ( !modulator_buffer )
510 if ( !output_buffer )
523 for ( i = 0; i < frames * 2; i += 2, ++modulator_buffer )
526 float out_left = 0.0f;
527 float out_right = 0.0f;
530 for ( i2 = 0; i2 < bands; ++i2 )
534 float synthesis_band_left = voclib_BiQuad ( carrier_buffer[i], &instance->
synthesis_bands[i2].
filters[0] );
537 for ( i3 = 1; i3 < filters_per_band; ++i3 )
543 analysis_band = voclib_envelope_tick ( &instance->
analysis_envelopes[i2], analysis_band );
544 out_left += synthesis_band_left * analysis_band;
545 out_right += synthesis_band_right * analysis_band;
547 output_buffer[i] = out_left;
548 output_buffer[i + 1] = out_right;
556 for ( i = 0; i < frames; ++i )
562 for ( i2 = 0; i2 < bands; ++i2 )
565 float analysis_band = voclib_BiQuad ( modulator_buffer[i], &instance->
analysis_bands[i2].
filters[0] );
568 for ( i3 = 1; i3 < filters_per_band; ++i3 )
573 analysis_band = voclib_envelope_tick ( &instance->
analysis_envelopes[i2], analysis_band );
574 out += synthesis_band * analysis_band;
576 output_buffer[i] = out;
585 if ( reaction_time < 0.002f || reaction_time > 2.0f )
591 voclib_initialize_envelopes ( instance );
602 if ( formant_shift < 0.25f || formant_shift > 4.0f )
608 voclib_initialize_filterbank ( instance, 1 );