~ubuntu-branches/ubuntu/raring/lmms/raring-proposed

« back to all changes in this revision

Viewing changes to plugins/ladspa_effect/calf/src/audio_fx.cpp

  • Committer: Charlie Smotherman
  • Date: 2012-12-05 22:08:38 UTC
  • mfrom: (33.1.7 lmms_0.4.13)
  • Revision ID: cjsmo@cableone.net-20121205220838-09pjfzew9m5023hr
* New  Upstream release.
  - Minor tweaking to ZynAddSubFX, CALF, SWH plugins  and Stefan Fendt's RC
    filters.
  - Added UI fixes: Magnentic effect of knobs and Piano-roll fixes
  - Updated German localization and copyright year
* debian/lmms-common.install:
  - added /usr/share/applications so the lmms.desktop file will correctly
    install (LP: #863366)
  - This should also fix the Software Center not displaying lmms in sound
    and video (LP: #824231)

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 *
16
16
 * You should have received a copy of the GNU Lesser General
17
17
 * Public License along with this program; if not, write to the
18
 
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 
18
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
19
 * Boston, MA  02110-1301  USA
20
20
 */
21
21
 
22
22
#include <calf/audio_fx.h>
23
23
#include <calf/giface.h>
 
24
#include <stdlib.h>
 
25
#include <time.h>
 
26
#include <math.h>
24
27
 
25
28
using namespace calf_plugins;
26
29
using namespace dsp;
37
40
    state = 0;
38
41
    cnt = 0;
39
42
    stages = 0;
40
 
    set_stages(_max_stages);    
 
43
    set_stages(_max_stages);
41
44
}
42
45
 
43
46
void simple_phaser::set_stages(int _stages)
74
77
    v ^= sign;
75
78
    // triangle wave, range from 0 to INT_MAX
76
79
    double vf = (double)((v >> 16) * (1.0 / 16384.0) - 1);
77
 
    
 
80
 
78
81
    float freq = base_frq * pow(2.0, vf * mod_depth / 1200.0);
79
82
    freq = dsp::clip<float>(freq, 10.0, 0.49 * sample_rate);
80
83
    stage1.set_ap_w(freq * (M_PI / 2.0) * odsr);
98
101
        for (int j = 0; j < stages; j++)
99
102
            fd = stage1.process_ap(fd, x1[j], y1[j]);
100
103
        state = fd;
101
 
        
 
104
 
102
105
        float sdry = in * gs_dry.get();
103
106
        float swet = fd * gs_wet.get();
104
107
        *buf_out++ = sdry + swet;
110
113
    typedef std::complex<double> cfloat;
111
114
    freq *= 2.0 * M_PI / sr;
112
115
    cfloat z = 1.0 / exp(cfloat(0.0, freq)); // z^-1
113
 
    
 
116
 
114
117
    cfloat p = cfloat(1.0);
115
118
    cfloat stg = stage1.h_z(z);
116
 
    
 
119
 
117
120
    for (int i = 0; i < stages; i++)
118
121
        p = p * stg;
119
 
    
120
 
    p = p / (cfloat(1.0) - cfloat(fb) * p);        
 
122
 
 
123
    p = p / (cfloat(1.0) - cfloat(fb) * p);
121
124
    return std::abs(cfloat(gs_dry.get_last()) + cfloat(gs_wet.get_last()) * p);
122
125
}
123
126
 
138
141
        order = mode - mode_6db_br + 1;
139
142
        left[0].set_br_rbj(freq, order * 0.1 * q, srate, gain);
140
143
    }
141
 
    
 
144
 
142
145
    right[0].copy_coeffs(left[0]);
143
146
    for (int i = 1; i < order; i++) {
144
147
        left[i].copy_coeffs(left[0]);
168
171
    case 0:
169
172
        filter = left;
170
173
        break;
171
 
        
 
174
 
172
175
    case 1:
173
176
        filter = right;
174
177
        break;
175
 
    
 
178
 
176
179
    default:
177
180
        assert(false);
178
181
        return 0;
179
182
    }
180
 
    
 
183
 
181
184
    if (inmask) {
182
185
        switch(order) {
183
186
            case 1:
288
291
        tl[5] = 1577 << 16, tr[5] = 1881 << 16;
289
292
        break;
290
293
    }
291
 
    
 
294
 
292
295
    float fDec=1000 + 2400.f * diffusion;
293
296
    for (int i = 0 ; i < 6; i++) {
294
 
        ldec[i]=exp(-float(tl[i] >> 16) / fDec), 
 
297
        ldec[i]=exp(-float(tl[i] >> 16) / fDec),
295
298
        rdec[i]=exp(-float(tr[i] >> 16) / fDec);
296
299
    }
297
300
}
311
314
void reverb::process(float &left, float &right)
312
315
{
313
316
    unsigned int ipart = phase.ipart();
314
 
    
 
317
 
315
318
    // the interpolated LFO might be an overkill here
316
319
    int lfo = phase.lerp_by_fract_int<int, 14, int>(sine.data[ipart], sine.data[ipart+1]) >> 2;
317
320
    phase += dphase;
318
 
    
 
321
 
319
322
    left += old_right;
320
323
    left = apL1.process_allpass_comb_lerp16(left, tl[0] - 45*lfo, ldec[0]);
321
324
    left = apL2.process_allpass_comb_lerp16(left, tl[1] + 47*lfo, ldec[1]);
337
340
    right = apR6.process_allpass_comb_lerp16(right, tr[5] - 46*lfo, rdec[5]);
338
341
    old_right = lp_right.process(right * fb);
339
342
    sanitize(old_right);
340
 
    
 
343
 
341
344
    left = out_left, right = out_right;
342
345
}
343
346
 
351
354
    is_active = false;
352
355
    srate = 0;
353
356
    meter = 0.f;
 
357
    prev_med = prev_out = 0.f;
 
358
    drive_old = blend_old = -1.f;
354
359
}
355
360
 
356
361
void tap_distortion::activate()
381
386
        an = rbdr*rbdr / sq;
382
387
        imr = 2.0f * knb + D(2.0f * kna + 4.0f * an - 1.0f);
383
388
        pwrq = 2.0f / (imr + 1.0f);
384
 
        
 
389
 
385
390
        drive_old = drive;
386
391
        blend_old = blend;
387
392
    }
530
535
    return true;
531
536
}
532
537
 
 
538
 
 
539
/// Lookahead Limiter by Christian Holschuh and Markus Schmidt
 
540
 
 
541
lookahead_limiter::lookahead_limiter() {
 
542
    is_active = false;
 
543
    channels = 2;
 
544
    id = 0;
 
545
    buffer_size = 0;
 
546
    overall_buffer_size = 0;
 
547
    att = 1.f;
 
548
    att_max = 1.0;
 
549
    pos = 0;
 
550
    delta = 0.f;
 
551
    _delta = 0.f;
 
552
    peak = 0.f;
 
553
    over_s = 0;
 
554
    over_c = 1.f;
 
555
    attack = 0.005;
 
556
    use_multi = false;
 
557
    weight = 1.f;
 
558
    _sanitize = false;
 
559
    auto_release = false;
 
560
    asc_active = false;
 
561
    nextiter = 0;
 
562
    nextlen = 0;
 
563
    asc = 0.f;
 
564
    asc_c = 0;
 
565
    asc_pos = -1;
 
566
    asc_changed = false;
 
567
    asc_coeff = 1.f;
 
568
}
 
569
 
 
570
void lookahead_limiter::activate()
 
571
{
 
572
    is_active = true;
 
573
    pos = 0;
 
574
 
 
575
}
 
576
 
 
577
void lookahead_limiter::set_multi(bool set) { use_multi = set; }
 
578
 
 
579
void lookahead_limiter::deactivate()
 
580
{
 
581
    is_active = false;
 
582
}
 
583
 
 
584
float lookahead_limiter::get_attenuation()
 
585
{
 
586
    float a = att_max;
 
587
    att_max = 1.0;
 
588
    return a;
 
589
}
 
590
 
 
591
void lookahead_limiter::set_sample_rate(uint32_t sr)
 
592
{
 
593
    srate = sr;
 
594
    // rebuild buffer
 
595
    overall_buffer_size = (int)(srate * (100.f / 1000.f) * channels) + channels; // buffer size attack rate multiplied by 2 channels
 
596
    buffer = (float*) calloc(overall_buffer_size, sizeof(float));
 
597
    memset(buffer, 0, overall_buffer_size * sizeof(float)); // reset buffer to zero
 
598
    pos = 0;
 
599
 
 
600
    nextpos = (int*) calloc(overall_buffer_size, sizeof(int));
 
601
    nextdelta = (float*) calloc(overall_buffer_size, sizeof(float));
 
602
    memset(nextpos, -1, overall_buffer_size * sizeof(int));
 
603
}
 
604
 
 
605
void lookahead_limiter::set_params(float l, float a, float r, float w, bool ar, float arc, bool d)
 
606
{
 
607
    limit = l;
 
608
    attack = a / 1000.f;
 
609
    release = r / 1000.f;
 
610
    auto_release = ar;
 
611
    asc_coeff = arc;
 
612
    debug = d;
 
613
    weight = w;
 
614
}
 
615
 
 
616
void lookahead_limiter::reset() {
 
617
    int bs = (int)(srate * attack * channels);
 
618
    buffer_size = bs - bs % channels; // buffer size attack rate
 
619
    _sanitize = true;
 
620
    pos = 0;
 
621
    nextpos[0] = -1;
 
622
    nextlen = 0;
 
623
    nextiter = 0;
 
624
    delta = 0.f;
 
625
    att = 1.f;
 
626
    reset_asc();
 
627
}
 
628
 
 
629
void lookahead_limiter::reset_asc() {
 
630
    asc = 0.f;
 
631
    asc_c = 0;
 
632
    asc_pos = pos;
 
633
    asc_changed = true;
 
634
}
 
635
 
 
636
void lookahead_limiter::process(float &left, float &right, float * multi_buffer)
 
637
{
 
638
    // PROTIP: harming paying customers enough to make them develop a competing
 
639
    // product may be considered an example of a less than sound business practice.
 
640
 
 
641
    // fill lookahead buffer
 
642
    if(_sanitize) {
 
643
        // if we're sanitizing (zeroing) the buffer on attack time change,
 
644
        // don't write the samples to the buffer
 
645
        buffer[pos] = 0.f;
 
646
        buffer[pos + 1] = 0.f;
 
647
    } else {
 
648
        buffer[pos] = left;
 
649
        buffer[pos + 1] = right;
 
650
    }
 
651
 
 
652
    // are we using multiband? get the multiband coefficient or use 1.f
 
653
    float multi_coeff = (use_multi) ? multi_buffer[pos] : 1.f;
 
654
 
 
655
    // input peak - impact higher in left or right channel?
 
656
    peak = fabs(left) > fabs(right) ? fabs(left) : fabs(right);
 
657
 
 
658
    // calc the real limit including weight and multi coeff
 
659
    float _limit = limit * multi_coeff * weight;
 
660
 
 
661
    // add an eventually appearing peak to the asc fake buffer if asc active
 
662
    if(auto_release and peak > _limit) {
 
663
        asc += peak;
 
664
        asc_c ++;
 
665
    }
 
666
 
 
667
    if(peak > _limit or multi_coeff < 1.0) {
 
668
        float _multi_coeff = 1.f;
 
669
        float _peak;
 
670
 
 
671
        // calc the attenuation needed to reduce incoming peak
 
672
        float _att = std::min(_limit / peak, 1.f);
 
673
 
 
674
 
 
675
        // calc a release delta from this attenuation
 
676
        float _rdelta = (1.0 - _att) / (srate * release);
 
677
        if(auto_release and asc_c > 0) {
 
678
            // check if releasing to average level of peaks is steeper than
 
679
            // releasing to 1.f
 
680
            float _delta = std::max((limit * weight) / (asc_coeff * asc) * (float)asc_c - _att, 0.000001f) / (srate * release);
 
681
            if(_delta < _rdelta) {
 
682
                asc_active = true;
 
683
                _rdelta = _delta;
 
684
            }
 
685
        }
 
686
 
 
687
        // calc the delta for walking to incoming peak attenuation
 
688
        float _delta = (_limit / peak - att) / buffer_size * channels;
 
689
 
 
690
        if(_delta < delta) {
 
691
            // is the delta more important than the actual one?
 
692
            // if so, we can forget about all stored deltas (because they can't
 
693
            // be more important - we already checked that earlier) and use this
 
694
            // delta now. and we have to create a release delta in nextpos buffer
 
695
            nextpos[0] = pos;
 
696
            nextpos[1] = -1;
 
697
            nextdelta[0] = _rdelta;
 
698
            nextlen = 1;
 
699
            nextiter = 0;
 
700
            delta = _delta;
 
701
        } else {
 
702
            // we have a peak on input its delta is less important than the
 
703
            // actual delta. But what about the stored deltas we're following?
 
704
            bool _found = false;
 
705
            int i = 0;
 
706
            for(i = nextiter; i < nextiter + nextlen; i++) {
 
707
                // walk through our nextpos buffer
 
708
                int j = i % buffer_size;
 
709
                // calculate a delta for the next stored peak
 
710
                // are we using multiband? then get the multi_coeff for the
 
711
                // stored position
 
712
                _multi_coeff = (use_multi) ? multi_buffer[nextpos[j]] : 1.f;
 
713
                // is the left or the right channel on this position more
 
714
                // important?
 
715
                _peak = fabs(buffer[nextpos[j]]) > fabs(buffer[nextpos[j] + 1]) ? fabs(buffer[nextpos[j]]) : fabs(buffer[nextpos[j] + 1]);
 
716
                // calc a delta to use to reach our incoming peak from the
 
717
                // stored position
 
718
                _delta = (_limit / peak - (limit * _multi_coeff * weight) / _peak) / (((buffer_size - nextpos[j] + pos) % buffer_size) / channels);
 
719
                if(_delta < nextdelta[j]) {
 
720
                    // if the buffered delta is more important than the delta
 
721
                    // used to reach our peak from the stored position, store
 
722
                    // the new delta at that position and stop the loop
 
723
                    nextdelta[j] = _delta;
 
724
                    _found = true;
 
725
                    break;
 
726
                }
 
727
            }
 
728
            if(_found) {
 
729
                // there was something more important in the next-buffer.
 
730
                // throw away any position and delta after the important
 
731
                // position and add a new release delta
 
732
                nextlen = i - nextiter + 1;
 
733
                nextpos[(nextiter + nextlen) % buffer_size] = pos;
 
734
                nextdelta[(nextiter + nextlen) % buffer_size] = _rdelta;
 
735
                // set the next following position value to -1 (cleaning up the
 
736
                // nextpos buffer)
 
737
                nextpos[(nextiter + nextlen + 1) % buffer_size] = -1;
 
738
                // and raise the length of our nextpos buffer for keeping the
 
739
                // release value
 
740
                nextlen ++;
 
741
            }
 
742
        }
 
743
    }
 
744
 
 
745
    // switch left and right pointers in buffer to output position
 
746
    left = buffer[(pos + channels) % buffer_size];
 
747
    right = buffer[(pos + channels + 1) % buffer_size];
 
748
 
 
749
    // if a peak leaves the buffer, remove it from asc fake buffer
 
750
    // but only if we're not sanitizing asc buffer
 
751
    float _peak = fabs(left) > fabs(right) ? fabs(left) : fabs(right);
 
752
    float _multi_coeff = (use_multi) ? multi_buffer[(pos + channels) % buffer_size] : 1.f;
 
753
    if(pos == asc_pos and !asc_changed) {
 
754
        asc_pos = -1;
 
755
    }
 
756
    if(auto_release and asc_pos == -1 and _peak > (limit * weight * _multi_coeff)) {
 
757
        asc -= _peak;
 
758
        asc_c --;
 
759
    }
 
760
 
 
761
    // change the attenuation level
 
762
    att += delta;
 
763
 
 
764
    // ...and calculate outpout from it
 
765
    left *= att;
 
766
    right *= att;
 
767
 
 
768
    if((pos + channels) % buffer_size == nextpos[nextiter]) {
 
769
        // if we reach a buffered position, change the actual delta and erase
 
770
        // this (the first) element from nextpos and nextdelta buffer
 
771
        delta = nextdelta[nextiter];
 
772
        nextlen = (nextlen - 1) % buffer_size;
 
773
        nextpos[nextiter] = -1;
 
774
        nextiter = (nextiter + 1) % buffer_size;
 
775
    }
 
776
 
 
777
    if (att > 1.0f) {
 
778
        // release time seems over, reset attenuation and delta
 
779
        att = 1.0f;
 
780
        delta = 0.0f;
 
781
    }
 
782
 
 
783
    // main limiting party is over, let's cleanup the puke
 
784
 
 
785
    if(_sanitize) {
 
786
        // we're sanitizing? then send 0.f as output
 
787
        left = 0.f;
 
788
        right = 0.f;
 
789
    }
 
790
 
 
791
    // security personnel pawing your values
 
792
    if(att <= 0.f) {
 
793
        // if this happens we're doomed!!
 
794
        // may happen on manually lowering attack
 
795
        att = 0.0000000000001;
 
796
        delta = (1.0f - att) / (srate * release);
 
797
    }
 
798
 
 
799
    if(att != 1.f and 1 - att < 0.0000000000001) {
 
800
        // denormalize att
 
801
        att = 1.f;
 
802
    }
 
803
 
 
804
    if(delta != 0.f and fabs(delta) < 0.00000000000001) {
 
805
        // denormalize delta
 
806
        delta = 0.f;
 
807
    }
 
808
 
 
809
    // post treatment (denormal, limit)
 
810
    denormal(&left);
 
811
    denormal(&right);
 
812
 
 
813
    // store max attenuation for meter output
 
814
    att_max = (att < att_max) ? att : att_max;
 
815
 
 
816
    // step forward in our sample ring buffer
 
817
    pos = (pos + channels) % buffer_size;
 
818
 
 
819
    // sanitizing is always done after a full cycle through the lookahead buffer
 
820
    if(_sanitize and pos == 0) _sanitize = false;
 
821
 
 
822
    asc_changed = false;
 
823
}
 
824
 
 
825
bool lookahead_limiter::get_asc() {
 
826
    if(!asc_active) return false;
 
827
    asc_active = false;
 
828
    return true;
 
829
}