next SuperCollider: Reverberación
up Ejemplos en SuperCollider
previous SuperCollider: Filtros bi-cuadráticos
  Índice General   Índice de Materias


SuperCollider: Compresión


(
        // Compresión de amplitud
        
        var w;
        var caption1,caption2,caption3,captionfbSlider,captionddSlider;
        var in1Box,in2Box,in3Box,captionSource;
        var applyBox;
        var captiongainSlider,gainSlider,gainNum;
        var captionratioSlider,ratioSlider,ratioNum;
        var captionthrSlider,thrSlider,thrNum;
        var captionattackSlider,attackSlider,attackNum;
        var captionreleaseSlider,releaseSlider,releaseNum;
        var captiondelaySlider,delaySlider,delayNum;
        var ampview,ampdbview,captionampview,captionampdbview;
        var originalview, compressedview, captionoriginalview, captioncompressedview;
        var est_amp_box,est_amp_label;  
        var attack_coef_label,release_coef_label,attack_coef_box,release_coef_box;
        var rect_db_label,rect_db_box,rect_label,rect_box;
        var factor_label,factor_box,output_label,output_box;
        var bias_label,bias_box,input_db_label,input_db_box;

        var filename, sound, signal;
        var fore_color;
        
        var attack_coef;
        var release_coef;
        
        var xoff1,xoff2;
        var ftoW;
        
        // convert dB to amplitude
        
        var a_to_db;
        var dBtoA,dB_to_A;
        
        
        var show_value_in_box;  
        
        dBtoA = { arg decibels;
                r = 0.5011872336;
                r*10**(decibels/20.0);};
                
     // 96 decibels scale
        dB_to_A = { arg decibels;
             r = 0.008229747050;        
                r*10**(decibels/20.0);};
                
        // 96 decibels scale
        a_to_db = { arg amp;
            r = 0.008229747050;
            20*log(amp/r);};
                        
        // Hertz to radian frequency
        ftoW = { arg hertz;
                2pi*(hertz/Synth.sampleRate);};
        
        
        // Show any signal value on a number box                
        show_value_in_box = {arg input, box, period;
                Sequencer.kr({ var val;
                        val = input.poll;
                        box.value = val;
                        val
                }, Impulse.kr(period));};
        
        
        
        // GUI Window
        w = GUIWindow.new("Compressor >>> Rodrigo F. Cadiz", Rect.newBy(500, 70, 
550, 650));
        w.backColor = Color.new(99,130,160);
        
        
        // Offsets
        xoff1 = 10;
        xoff2 = 250;
        
        // Colors
        fore_color = Color.new(255,255,0);
        
        // Various labels
        caption1 = StringView.new( w, Rect.newBy(xoff1, 10, 228, 20), "Advanced
Audio Signal Processing");
        caption1.labelColor = fore_color;
        caption2 = StringView.new( w, Rect.newBy(xoff1, 30, 328, 20),
"Assignment 3: Compressor");
        caption2.labelColor = fore_color;
        caption3 = StringView.new( w, Rect.newBy(xoff1, 50, 128, 20), "Rodrigo
F. Cadiz");
        caption3.labelColor = fore_color;
                
        // Input Gain
        captiongainSlider = StringView.new( w, Rect.newBy(xoff2, 10, 235, 20),
"Input gain [dB] "); 
     gainSlider = SliderView.new( w, Rect.newBy( xoff2, 30, 230, 20 ),
"Mix", 0, -30 , 6 , 1, 'linear');
     gainNum = NumericalView.new( w, Rect.newBy( xoff2+235, 30, 50, 20 ), 
"NumericalView", 0, -30, 6, 1, 'linear');
     gainNum.backColor = fore_color;
     gainSlider.action = { gainNum.value = gainSlider.value};
     
     
     // Compression ratio
        captionratioSlider = StringView.new( w, Rect.newBy(xoff2, 60, 235, 20),
"Compression ratio"); 
     ratioSlider = SliderView.new( w, Rect.newBy( xoff2, 80, 230, 20 ),
"Mix", 1.0, 1.0, 10.0 , 0.1, 'linear');
     ratioNum = NumericalView.new( w, Rect.newBy( xoff2+235, 80, 50, 20
), "NumericalView", 1.0, 1.0, 10.0, 0.1, 'linear');
     ratioNum.backColor = fore_color;
     ratioSlider.action = { ratioNum.value = ratioSlider.value};
     
          
     // Threshold
        captionthrSlider = StringView.new( w, Rect.newBy(xoff2, 110, 235, 20),
"Threshold [dB]"); 
     thrSlider = SliderView.new( w, Rect.newBy( xoff2, 130, 230, 20 ),
"Mix", 36.0, 0.0, 96.0 , 1, 'linear');
     thrNum = NumericalView.new( w, Rect.newBy( xoff2+235, 130, 50, 20 ), 
"NumericalView", 36.0, 0.0, 96.0, 1, 'linear');
     thrNum.backColor = fore_color;
     thrSlider.action = { thrNum.value = thrSlider.value};
     
     
     // Attack time
        captionattackSlider = StringView.new( w, Rect.newBy(xoff2, 160, 235,
20), "Attack time [msec]"); 
     attackSlider = SliderView.new( w, Rect.newBy( xoff2, 180, 230, 20 ), 
"Mix", 0.1, 0.001 , 40.0 , 0.0001, 'exponential');
     attackNum = NumericalView.new( w, Rect.newBy( xoff2+235, 180, 50, 20 
), "NumericalView", 10.0, 0.001, 40.0, 0.0001, 'exponential');
     attackNum.backColor = fore_color;
     attackSlider.action = { attackNum.value = attackSlider.value};
        
        // Release time
        captionreleaseSlider = StringView.new( w, Rect.newBy(xoff2, 210, 235,
20), "Release time [msec] "); 
     releaseSlider = SliderView.new( w, Rect.newBy( xoff2, 230, 230, 20
), "Mix", 0.1, 0.001 , 500.0 , 0.0001, 'exponential');
     releaseNum = NumericalView.new( w, Rect.newBy( xoff2+235, 230, 50,
20 ), "NumericalView", 50.0, 0.001, 500.0, 0.0001, 'exponential');
     releaseNum.backColor = fore_color;
     releaseSlider.action = { releaseNum.value = releaseSlider.value};
                
        // Delay time
        captiondelaySlider = StringView.new( w, Rect.newBy(xoff2, 260, 235, 20), 
"Delay time [msec] "); 
     delaySlider = SliderView.new( w, Rect.newBy( xoff2, 280, 230, 20 ),
"Mix", 50.0, 0.0 , 500.0 , 0.01, 'linear');
     delayNum = NumericalView.new( w, Rect.newBy( xoff2+235, 280, 50, 20
), "NumericalView", 50.0, 0.0 ,500.0, 0.01, 'linear');
     delayNum.backColor = fore_color;
     delaySlider.action = { delayNum.value = delaySlider.value};
                
        
        // Number boxes for variables

        est_amp_label = StringView.new( w, Rect.newBy(xoff1, 207, 150, 20),
"Estimated amplitude [dB] "); 
        est_amp_box = NumericalView.new( w, Rect.newBy( xoff1+160,207,50,20 ),
"amp", 0.0, 0.0, 100.0, 0.0001, 'linear');      
        attack_coef_label = StringView.new( w, Rect.newBy(xoff1, 230, 150, 20),
"Attack time coefficient"); 
        attack_coef_box = NumericalView.new( w, Rect.newBy( xoff1+160,230,50,20
), "amp", 0.0, 0.0, 1.5, 0.00001, 'linear');            
        release_coef_label = StringView.new( w, Rect.newBy(xoff1, 253, 150, 20), 
"Release time coefficient [dB] "); 
        release_coef_box = NumericalView.new( w, Rect.newBy( xoff1+160,253,50,20 
), "amp", 0.0, 0.0, 1.5, 0.00001, 'linear');    
        rect_label = StringView.new( w, Rect.newBy(xoff1, 276, 150, 20),
"Rectified signal [lin]"); 
        rect_box = NumericalView.new( w, Rect.newBy( xoff1+160,276,50,20 ),
"amp", 0.0, 0.0, 1.5, 0.0001, 'linear');
        rect_db_label = StringView.new( w, Rect.newBy(xoff1, 300, 150, 20),
"Rectified signal [dB] "); 
        rect_db_box = NumericalView.new( w, Rect.newBy( xoff1+160,300,50,20 ),
"amp", 0.0, 0.0, 96.0, 0.0001, 'linear');
        factor_label = StringView.new( w, Rect.newBy(xoff1, 324, 150, 20),
"Amplitude correction"); 
        factor_box = NumericalView.new( w, Rect.newBy( xoff1+160,324,50,20 ),
"amp", 0.0, 0.0, 500, 0.0001, 'linear');
        output_label = StringView.new( w, Rect.newBy(xoff1, 347, 150, 20),
"Output signal [lin] "); 
        output_box = NumericalView.new( w, Rect.newBy( xoff1+160,347,50,20 ),
"amp", 0.0, 0.0, 30.0, 0.0001, 'linear');               
        
        input_db_label = StringView.new( w, Rect.newBy(xoff2, 324, 125, 20),
"Input signal [dB] "); 
        input_db_box = NumericalView.new( w, Rect.newBy( xoff2+160,324,50,20 ),
"bias", 0.0, 0.0, 100.0, 0.001, 'linear');
        bias_label = StringView.new( w, Rect.newBy(xoff2, 347, 125, 20), "Bias "); 
        bias_box = NumericalView.new( w, Rect.newBy( xoff2+160,347,50,20 ),
"bias", 0.0, 0.0, 100.0, 0.001, 'linear');
        
                
        // Apply compressor
     applyBox = CheckBoxView.new( w, Rect.newBy( xoff1, 77, 230, 20 ),
"Apply compressor ", 1, 0, 1, 0, 'linear');
        
     // Select source
     captionSource = StringView.new( w, Rect.newBy(xoff1, 100, 235, 20),
"Select source"); 
     captionSource.labelColor = fore_color;     
     in1Box = CheckBoxView.new( w, Rect.newBy( xoff1, 120, 230, 20 ),
"Sound file ", 1, 0, 1, 0, 'linear');
     in2Box = CheckBoxView.new( w, Rect.newBy( xoff1, 140, 230, 20 ),
"Sinusoid", 0, 0, 1, 0, 'linear');
     in3Box = CheckBoxView.new( w, Rect.newBy( xoff1, 160, 230, 20 ),
"BrownNoise ", 0, 0, 1, 0, 'linear');


        // Scopes

        captionampview = StringView.new( w, Rect.newBy(xoff1, 370, 235, 20),
"RMS estimator"); 
        ampview = ScopeView.new( w, Rect.newBy(xoff1, 395, 200, 100), 4410, 0, 1);

        captionampdbview = StringView.new( w, Rect.newBy(xoff1, 500, 235, 20),
"RMS estimator [dB]"); 
        ampdbview = ScopeView.new( w, Rect.newBy(xoff1, 525, 200, 100), 4410, 0, 96);

        captionoriginalview = StringView.new( w, Rect.newBy(xoff2, 370, 235,
20), "Original signal"); 
        originalview = ScopeView.new( w, Rect.newBy(xoff2, 395, 200, 100), 4410, 
-1, 1);

        captioncompressedview = StringView.new( w, Rect.newBy(xoff2, 500, 235,
20), "Compressed signal"); 
        compressedview = ScopeView.new( w, Rect.newBy(xoff2, 525, 200, 100),
4410, -1, 1);
        
        filename = ":Sounds:redobles.aiff";
        sound = SoundFile.new;
        if (sound.read(filename), {
        
                Synth.play({
                        var signal;   // for audiofile stream
                        var input,input1,input2,input3;    // for effects processor
                        var rect,rect_db,derivative,est_amp,est_amp_db;
                        var bias,delayed_input;
                        
                        signal = sound.data.at(0);
                        
                        // Different sources
                        input1 = PlayBuf.ar(signal, sound.sampleRate, 1, 0, 0,
signal.size-2)*dBtoA.value(gainSlider.kr); 
                        input2 = SinOsc.ar(200, 0.1)*dBtoA.value(gainSlider.kr);
                        input3 = BrownNoise.ar(0.1)*dBtoA.value(gainSlider.kr);
                        
                        // Source mix
                        input = Mix.ar([input1 * in1Box.kr,
                                        input2 * in2Box.kr,
                                        input3 * in3Box.kr]);
                                        
                        
                        // Filter coefficients                
                        attack_coef = 10**(-2.0/((attackSlider.kr*0.001)*Synth.sampleRate));
                        release_coef = 10**(-2.0/((releaseSlider.kr*0.001)*Synth.sampleRate));
                        
                                        
                        // Rectify the signal
                        rect = abs(input);
                        rect_db = a_to_db.value(rect);  
                        
                        // Take derivative
                        //derivative = HPZ1.ar(rect_db);
                        derivative = HPZ1.ar(rect);
                        
                        
                        // Filtered signal
                        //est_amp = OnePole.ar(rect,if (derivative > 0, attack_coef, release_coef),
                        //                                   if (derivative > 0, (1.0 - attack_coef), (1.0 - release_coef))); 
                          
                         
                         //est_amp = OnePole.ar(rect_db,if (derivative > 0, attack_coef,
release_coef)); 
                         
                         est_amp = OnePole.ar(rect,if (derivative > 0, attack_coef, release_coef)); 
                         
                         //est_amp = OnePole.ar(rect_db,attack_coef);
                          
                        // est_amp_db = ampdb(est_amp);
                        est_amp_db = a_to_db.value(est_amp);
                        // est_amp_db = 20*log(est_amp);
                        
                        
                        bias = (96 - (thrSlider.kr+((96.0 - thrSlider.kr)/ratioSlider.kr)));
                        
                        //Peep.kr(bias,"bias");
                        //Peep.kr(derivative,"derivative",10);
                        //Peep.kr(rect_dB),"rect in dB",10);
                        //Peep.kr(est_amp,"estimated amplitude in dB",10);
                        
                        //Peep.kr(attack_coef,"attack",1);
                        //Peep.kr(release_coef,"release",1);
                        
                        //bias = 0;
        
                        
                        
                        //ampestNum.value = est_amp;
                        
                        a = max(est_amp_db,thrSlider.kr);
                        b = a - thrSlider.kr;
                        c = b * (1 - (1/ratioSlider.kr));
                        d = bias - c;
                        //d = c;
                        //e = dB_to_A.value(d);
                        //e = dbamp(d);
                        e = 10**(d/20.0);
                        
                        
                        delayed_input = DelayN.ar(input,500*0.001,delaySlider.kr);      
                        
                        show_value_in_box.value(gainSlider.kr+90,input_db_box,7);
                        show_value_in_box.value(bias,bias_box,7);
                        show_value_in_box.value(est_amp_db,est_amp_box,7);
                        show_value_in_box.value(attack_coef,attack_coef_box,5);
                        show_value_in_box.value(release_coef,release_coef_box,5);
                        show_value_in_box.value(rect,rect_box,7);
                        show_value_in_box.value(rect_db,rect_db_box,7);
                        show_value_in_box.value(e,factor_box,7);
                        show_value_in_box.value(delayed_input*e*applyBox.kr,output_box,7);      
                        
                        
                        Scope.ar(ampview,est_amp);
                        Scope.ar(ampdbview,est_amp_db);
                        Scope.ar(originalview,delayed_input);
                        Scope.ar(compressedview,delayed_input*e*applyBox.kr);
                        
                        Mix.ar([delayed_input*e*applyBox.kr,delayed_input*(1.0 - applyBox.kr)]);
                        
                
                        });
                
                                
        },{ (filename ++ " not found.\n").post });
        
        w.close;



)


next SuperCollider: Reverberación
up Ejemplos en SuperCollider
previous SuperCollider: Filtros bi-cuadráticos
  Índice General   Índice de Materias

Copyright © 2008-06-05
Rodrigo F. Cádiz   - Centro de Investigación en Tecnologías de Audio, Instituto de Música, Pontificia Universidad Católica de Chile