View Single Post
Old 02-12-2018, 10:57 PM   #106
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,793
Default

Here you are: Midi Volume Control V 2.0
-Michael

Code:
// Author: Michael Schnell, based on a work of Time Waster (M. Smith)
// License: LGPL - http://www.gnu.org/licenses/lgpl.html
//
// The midi CC values 0 ... 127 are mapped to an "amplifier" curve that consists of a linear and an exponential part
// in a way, that with CC = 0 the amplification is 0 (-inf dB) and with CC = 127 the amplification is 1 (0db)
// A slider defines the amount (in dB), the amplification is reduced with each CC step.
// According to that, the breakpoint between the exponential and the linear part is set so that 
// at this point the value and the slope of the curves match.
// Below the breakpoint, a linear curve is used so that with CC = 0 the amplification is Zero (-infinity dB).
// Another slider defines the maximum speed the amplification is modulated. This is set in dB per modulation step.
// (Moreover the maximum speed used is reaching a new defined level in as many steps a samples in a block)
// A graph shows as well the curve (Amplification vs CC steps), as the dynamic movement of the amplification level.



desc:Midi Volume Control
version: 2.0

slider1:0<0,15,1{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}>MIDI Input Channel
slider2:1<0,127,1{0 Bank Sel M,1 Mod Wheel M,2 Breath M,3,4 Foot P M,5 Porta M,6 Data Entry M,7 Vol M,8 Balance M,9,10 Pan M,11 Expression M,12 Ctrl 1 M,13 Ctrl 2 M,14,15,16 GP Slider 1,17 GP Slider 2,18 GP Slider 3,19 GP Slider 4,20,21,22,23,24,25,26,27,28,29,30,31,32 Bank Sel L,33 Mod Wheel L,34 Breath L,35,36 Foot P L,37 Porta L,38 Data Entry L,39 Vol L,40 Balance L,41,42 Pan L,43 Expression L,44 Ctrl 1 L,45 Ctrl 2 L,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64 Hold P sw,65 Porta sw,66 Sustenuto sw,67 Soft P sw,68 Legato P sw,69 Hold 2 P sw,70 S.Variation,71 S.Timbre,72 S.Release,73 S.Attack,74 S.Brightness,75 S.Ctrl 6,76 S.Ctrl 7,77 S.Ctrl 8,78 S.Ctrl 9,79 S.Ctrl 10,80 GP B.1 sw,81 GP B.2 sw,82 GP B.3 sw,83 GP B.4 sw,84,85,86,87,88,89,90,91 Effects Lv,92 Trem Lv,93 Chorus Lv,94 Celeste Lv,95 Phaser Lv,96 Data B. Inc,97 Data B. Dec,98 NRP L,99 NRP M,100 RP L,101 RP M,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127}>CC Input
slider3:-0.3<-1,-0.1,0.01>Attenuation per CC step
slider4:0.003<0.0005, 0.01, 0.0001>max step(dB)
//slider5:0<0,1,0.01>Factor (Test)
slider6:0<0,127,1{None,0 Bank Sel M,1 Mod Wheel M,2 Breath M,3,4 Foot P M,5 Porta M,6 Data Entry M,7 Vol M,8 Balance M,9,10 Pan M,11 Expression M,12 Ctrl 1 M,13 Ctrl 2 M,14,15,16 GP Slider 1,17 GP Slider 2,18 GP Slider 3,19 GP Slider 4,20,21,22,23,24,25,26,27,28,29,30,31,32 Bank Sel L,33 Mod Wheel L,34 Breath L,35,36 Foot P L,37 Porta L,38 Data Entry L,39 Vol L,40 Balance L,41,42 Pan L,43 Expression L,44 Ctrl 1 L,45 Ctrl 2 L,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64 Hold P sw,65 Porta sw,66 Sustenuto sw,67 Soft P sw,68 Legato P sw,69 Hold 2 P sw,70 S.Variation,71 S.Timbre,72 S.Release,73 S.Attack,74 S.Brightness,75 S.Ctrl 6,76 S.Ctrl 7,77 S.Ctrl 8,78 S.Ctrl 9,79 S.Ctrl 10,80 GP B.1 sw,81 GP B.2 sw,82 GP B.3 sw,83 GP B.4 sw,84,85,86,87,88,89,90,91 Effects Lv,92 Trem Lv,93 Chorus Lv,94 Celeste Lv,95 Phaser Lv,96 Data B. Inc,97 Data B. Dec,98 NRP L,99 NRP M,100 RP L,101 RP M,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127}>Muting Notofication CC

@init
  modval      = 0;
//  prelevel    = 0;
  maxCCvalue  = 127;
  r4          = log(10)/20;
  outval      = 0;
  
  function f (x) (
    x < limit  ? (
      x*r3;
     ) : (
      exp((maxCCvalue-x) * r5);
    );
  )

@slider
  inChannel   = slider1;
  modcc       = slider2; 
  dbperstep   = slider3;
  db20perstep = dbperstep/20;
  p1          = 1 / log(10) / db20perstep;
  p2          = 1 / maxCCvalue;
  p3          = p1 + p2;
  limit       = (-p3+0.5) |0; 
  r1          = (maxCCvalue-limit) * dbperstep;
  r2          = exp(log(10)*r1/20);
  r3          = r2 / limit;  
  r5          = dbperstep*r4;
  s1          = exp(log(10)*slider4/20);
  s2          = 1 / s1;
  f1          = f(1);
  outcc       = slider6-1;
  
@block
  while (midirecv(offset, msg1, msg2, msg3)) (
    status = msg1 & $xF0;      // Extract message type
    channel = msg1 & $x0F;
    channel == inChannel ? (   // Is it on our channel?
      status == $xB0 ? (       // Is it a controller event?
        msg2 == modcc ? (      // Is it the right CC?
          modval = msg3;
          modval ? (
            outlevel < f1 ? outlevel = f1;
          );  
          modlevel = f(modval);
//        slider5 = modlevel;                        // test
        );
      );
    );
    midisend(offset, msg1, msg2, msg3); // pass through
  );    
  
  modval ? (
    modstep  = exp( log(modlevel / outlevel) / samplesblock);
    modstep == 1 ? (
      stepping = 0;
     ) : (
      modstep > s1 ? (
        modstep = s1;
       ) : modstep < s2 ? ( 
        modstep = s2;
      );  
      stepping = 1;
    );  
   ) : ( 
    modstep = s2;
  );    
 
  outcc >= 0 ? (
    outlevel <= f1 ? (
      outval ? (
        outval = 0;
        midisend(offset, inChannel+$xB0, outcc, outval); // pass through
      );  
     ) : ( 
      !outval ? (
        outval = 127;
        midisend(offset, inChannel+$xB0, outcc, outval); // pass through
      );  
    );  
  ); 
  
@sample
  outlevel *= modstep;
  spl0*=outlevel;
  spl1*=outlevel;  
  
  
@gfx 640 400

gfx_r=gfx_g=gfx_b=0; gfx_a=1;
gfx_x=gfx_y=0;
gfx_rectto(gfx_w,gfx_h);

outcc >= 0 ? (
  outval ? (
    gfx_r = 0; gfx_g = 1; gfx_b = 0;
    gfx_x = 0;
    gfx_y = 0;
    gfx_rectto(gfx_w, gfx_h/2);  
  );
);

q1 = gfx_w / maxCCvalue;
q2 = gfx_h;


//gfx_line();
gfx_r=gfx_g=gfx_b=1;
gfx_y = 0;
gfx_x = 0;
x = 0;
while (x<=maxCCvalue) (
  a = x*q1;
  b = gfx_h - f(x)*q2;
  gfx_lineto(a, b, 1);
  x = x+1;
);
gfx_y = 0;
gfx_x = modval*q1;
gfx_lineto(gfx_x, gfx_h);
gfx_x = 0;
gfx_y = gfx_h-outlevel*q2;   
gfx_lineto(gfx_w, gfx_y);
mschnell is online now   Reply With Quote