diff --git a/_audio_effect_8h_source.html b/_audio_effect_8h_source.html index 7cf86aa224..a220fe6c07 100644 --- a/_audio_effect_8h_source.html +++ b/_audio_effect_8h_source.html @@ -449,144 +449,144 @@
458 
466 class Compressor : public AudioEffect {
467 
-
468  Compressor(const Compressor &copy) = default;
-
469 
-
470  Compressor(uint32_t sampleRate = 44100, int32_t attackMs=30, int32_t releaseMs=20, int32_t holdMs=10, uint8_t thresholdPercent=10, float compressionRatio=0.5){
-
471  //assuming 1 sample = 1/96kHz = ~10us
-
472  //Attack -> 30 ms -> 3000
-
473  //Release -> 20 ms -> 2000
-
474  //Hold -> 10ms -> 1000
-
475  sample_rate = sample_rate * attackMs / 1000;
-
476  attack_count = sample_rate * attackMs / 1000;
-
477  release_count = sample_rate * releaseMs / 1000;
-
478  hold_count = sample_rate * holdMs / 1000;
-
479 
-
480  //threshold -20dB below limit -> 0.1 * 2^31
-
481  threshold = 0.01f * thresholdPercent * NumberConverter::maxValueT<effect_t>();
-
482  //compression ratio: 6:1 -> -6dB = 0.5
-
483  gainreduce = compressionRatio;
-
484  //initial gain = 1.0 -> no compression
-
485  gain = 1.0f;
-
486  recalculate();
-
487  }
-
488 
-
489  Compressor *clone() { return new Compressor(*this); }
+
469  Compressor(const Compressor &copy) = default;
+
470 
+
472  Compressor(uint32_t sampleRate = 44100, int32_t attackMs=30, int32_t releaseMs=20, int32_t holdMs=10, uint8_t thresholdPercent=10, float compressionRatio=0.5){
+
473  //assuming 1 sample = 1/96kHz = ~10us
+
474  //Attack -> 30 ms -> 3000
+
475  //Release -> 20 ms -> 2000
+
476  //Hold -> 10ms -> 1000
+
477  sample_rate = sample_rate * attackMs / 1000;
+
478  attack_count = sample_rate * attackMs / 1000;
+
479  release_count = sample_rate * releaseMs / 1000;
+
480  hold_count = sample_rate * holdMs / 1000;
+
481 
+
482  //threshold -20dB below limit -> 0.1 * 2^31
+
483  threshold = 0.01f * thresholdPercent * NumberConverter::maxValueT<effect_t>();
+
484  //compression ratio: 6:1 -> -6dB = 0.5
+
485  gainreduce = compressionRatio;
+
486  //initial gain = 1.0 -> no compression
+
487  gain = 1.0f;
+
488  recalculate();
+
489  }
490 
-
491  void setAttack(int32_t attackMs){
-
492  attack_count = sample_rate * attackMs / 1000;
-
493  recalculate();
-
494  }
-
495 
-
496  void setRelease(int32_t releaseMs){
-
497  release_count = sample_rate * releaseMs / 1000;
-
498  recalculate();
-
499  }
-
500 
-
501  void setHold(int32_t holdMs){
-
502  hold_count = sample_rate * holdMs / 1000;
-
503  recalculate();
-
504  }
-
505 
-
506  void setThresholdPercent(uint8_t thresholdPercent){
-
507  threshold = 0.01f * thresholdPercent * NumberConverter::maxValueT<effect_t>();
-
508  }
-
509 
-
510  void setCompressionRatio(float compressionRatio){
-
511  if (compressionRatio<1.0){
-
512  gainreduce = compressionRatio;
-
513  }
-
514  recalculate();
-
515  }
-
516 
-
517  effect_t process(effect_t inSample) {
-
518  float inSampleF = (float)inSample;
-
519 
-
520  if (fabs(inSampleF) > threshold) {
-
521  if (gain >= gainreduce) {
-
522  if (State==S_NoOperation) {
-
523  State=S_Attack;
-
524  timeout = attack_count;
-
525  }
-
526  else if (State==S_Release) {
-
527  State=S_Attack;
-
528  timeout = attack_count;
-
529  }
-
530  }
-
531  if (State==S_GainReduction) timeout = hold_count;
-
532 
-
533  }
-
534 
-
535  if (fabs(inSampleF) < threshold && gain <= 1.0f) {
-
536  if ( timeout==0 && State==S_GainReduction) {
-
537  State=S_Release;
-
538  timeout = release_count;
-
539  }
-
540  }
-
541 
-
542  switch (State) {
-
543  case S_Attack:
-
544  if ( timeout>0 && gain > gainreduce) {
-
545  gain -= gain_step_attack;
-
546  timeout--;
-
547  }
-
548  else {
-
549  State=S_GainReduction;
-
550  timeout = hold_count;
-
551  }
-
552  break;
-
553 
-
554 
-
555  case S_GainReduction:
-
556  if ( timeout>0) timeout--;
-
557  else {
-
558  State=S_Release;
-
559  timeout = release_count;
-
560  }
-
561  break;
-
562 
-
563 
-
564  case S_Release:
-
565  if ( timeout>0 && gain<1.0f) {
-
566  timeout--;
-
567  gain += gain_step_release;
-
568  }
-
569  else {
-
570  State=S_NoOperation;
-
571  }
-
572  break;
-
573 
-
574  case S_NoOperation:
-
575  if (gain < 1.0f) gain = 1.0F;
-
576  break;
-
577 
-
578  default:
+
492  void setAttack(int32_t attackMs){
+
493  attack_count = sample_rate * attackMs / 1000;
+
494  recalculate();
+
495  }
+
496 
+
498  void setRelease(int32_t releaseMs){
+
499  release_count = sample_rate * releaseMs / 1000;
+
500  recalculate();
+
501  }
+
502 
+
504  void setHold(int32_t holdMs){
+
505  hold_count = sample_rate * holdMs / 1000;
+
506  recalculate();
+
507  }
+
508 
+
510  void setThresholdPercent(uint8_t thresholdPercent){
+
511  threshold = 0.01f * thresholdPercent * NumberConverter::maxValueT<effect_t>();
+
512  }
+
513 
+
515  void setCompressionRatio(float compressionRatio){
+
516  if (compressionRatio<1.0){
+
517  gainreduce = compressionRatio;
+
518  }
+
519  recalculate();
+
520  }
+
521 
+
523  effect_t process(effect_t inSample) {
+
524  float inSampleF = (float)inSample;
+
525 
+
526  if (fabs(inSampleF) > threshold) {
+
527  if (gain >= gainreduce) {
+
528  if (State==S_NoOperation) {
+
529  State=S_Attack;
+
530  timeout = attack_count;
+
531  }
+
532  else if (State==S_Release) {
+
533  State=S_Attack;
+
534  timeout = attack_count;
+
535  }
+
536  }
+
537  if (State==S_GainReduction) timeout = hold_count;
+
538 
+
539  }
+
540 
+
541  if (fabs(inSampleF) < threshold && gain <= 1.0f) {
+
542  if ( timeout==0 && State==S_GainReduction) {
+
543  State=S_Release;
+
544  timeout = release_count;
+
545  }
+
546  }
+
547 
+
548  switch (State) {
+
549  case S_Attack:
+
550  if ( timeout>0 && gain > gainreduce) {
+
551  gain -= gain_step_attack;
+
552  timeout--;
+
553  }
+
554  else {
+
555  State=S_GainReduction;
+
556  timeout = hold_count;
+
557  }
+
558  break;
+
559 
+
560 
+
561  case S_GainReduction:
+
562  if ( timeout>0) timeout--;
+
563  else {
+
564  State=S_Release;
+
565  timeout = release_count;
+
566  }
+
567  break;
+
568 
+
569 
+
570  case S_Release:
+
571  if ( timeout>0 && gain<1.0f) {
+
572  timeout--;
+
573  gain += gain_step_release;
+
574  }
+
575  else {
+
576  State=S_NoOperation;
+
577  }
+
578  break;
579 
-
580  break;
-
581 
-
582  }
+
580  case S_NoOperation:
+
581  if (gain < 1.0f) gain = 1.0F;
+
582  break;
583 
-
584  float outSampleF = inSample*gain;
+
584  default:
585 
-
586  return (int) outSampleF;
-
587  }
-
588 
-
589 protected:
-
590  enum CompStates {S_NoOperation, S_Attack, S_GainReduction, S_Release };
-
591  enum CompStates State = S_NoOperation;
-
592 
-
593  int32_t attack_count, release_count, hold_count, timeout;
-
594  float gainreduce, gain_step_attack, gain_step_release, gain, threshold;
-
595  uint32_t sample_rate;
+
586  break;
+
587 
+
588  }
+
589 
+
590  float outSampleF = inSample*gain;
+
591 
+
592  return (int) outSampleF;
+
593  }
+
594 
+
595  Compressor *clone() { return new Compressor(*this); }
596 
-
597  void recalculate() {
-
598  gain_step_attack = (1.0f - gainreduce) / attack_count;
-
599  gain_step_release = (1.0f - gainreduce) / release_count;
-
600  }
-
601 
-
602 };
-
603 
+
597 protected:
+
598  enum CompStates {S_NoOperation, S_Attack, S_GainReduction, S_Release };
+
599  enum CompStates State = S_NoOperation;
+
600 
+
601  int32_t attack_count, release_count, hold_count, timeout;
+
602  float gainreduce, gain_step_attack, gain_step_release, gain, threshold;
+
603  uint32_t sample_rate;
604 
-
605 } // namespace audio_tools
+
605  void recalculate() {
+
606  gain_step_attack = (1.0f - gainreduce) / attack_count;
+
607  gain_step_release = (1.0f - gainreduce) / release_count;
+
608  }
+
609 
+
610 };
+
611 
+
612 
+
613 } // namespace audio_tools
audio_tools::ADSRGain
ADSR Envelope: Attack, Decay, Sustain and Release. Attack is the time taken for initial run-up oeffec...
Definition: AudioEffect.h:362
audio_tools::ADSRGain::process
effect_t process(effect_t input)
calculates the effect output from the input
Definition: AudioEffect.h:398
audio_tools::ADSR
Generates ADSR values between 0.0 and 1.0.
Definition: AudioParameters.h:51