home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / Reich-o-Matic / FluteIns.m < prev    next >
Encoding:
Text File  |  1992-07-31  |  11.3 KB  |  328 lines

  1. /* The following files must be imported. */
  2. #import <musickit/musickit.h>    
  3. #import <musickit/unitgenerators/unitgenerators.h> 
  4.  
  5. #import "FluteIns.h"
  6.  
  7. @implementation FluteIns;
  8.  
  9. /* Statically declare the synthElement indices. */
  10. static int        ampAsymp,   /* amplitude envelope UG */
  11.             inputNoise,
  12.             inputMult,
  13.             inputAdd,      /* adder for envelope and noise */
  14.             inputAdd2,      /* adder to get exitation */
  15.             jetSquarer,      /* multiplier to fake reed table */
  16.             jetCuber,      /* multiplier to fake reed table */
  17.             sigAdder,     /* adder to approximate sigmoid */
  18.             jetAdder,      /* adder to get net jet pressure */
  19.             reflFilter,      /* reflection filter */
  20.             delayLine,      /* delay UG */
  21.             delayLine2,      /* delay UG */
  22.             gainElement,
  23.             stereoOut,   /* output UG */
  24.                 
  25.             inputNoisePp,
  26.             ampPp,             /* amplitude patchpoint */
  27.             inputMultPp,
  28.             inputAddPp,
  29.             inputAdd2Pp,
  30.                 jetSquarerPp,
  31.                 jetCuberPp,
  32.                 sigAddPp,
  33.             jetAddPp,
  34.                 filterPp,
  35.                 delayPp,
  36.                 delay2Pp,
  37.             gainPp; /* output patchpoint */
  38.  
  39. +patchTemplateFor:aNote
  40. {
  41.     /* Step 1: Create (or return) the PatchTemplate. */
  42.     static id theTemplate = nil;
  43.     if (theTemplate)
  44.       return theTemplate;
  45.     theTemplate = [PatchTemplate new];
  46.  
  47.     /* Step 2:  Add the SynthElement specifications. */    
  48.     ampAsymp = [theTemplate addUnitGenerator:[AsympUGx class]];
  49.     inputNoise = [theTemplate addUnitGenerator:[UnoiseUGy class]];
  50.     inputMult = [theTemplate addUnitGenerator:[Mul2UGxyx class]];
  51.     inputAdd = [theTemplate addUnitGenerator:[Scl2add2UGyxx class]];
  52.     inputAdd2 = [theTemplate addUnitGenerator:[Scl2add2UGxyx class]];
  53.     jetSquarer = [theTemplate addUnitGenerator:[Mul2UGxyy class]];
  54.     jetCuber = [theTemplate addUnitGenerator:[Mul2UGxyx class]];
  55.     sigAdder = [theTemplate addUnitGenerator:[Scl2add2UGxyx class]];
  56.     jetAdder = [theTemplate addUnitGenerator:[Scl2add2UGyxx class]];
  57.     reflFilter = [theTemplate addUnitGenerator:[OnepoleUGyy class]];
  58.     delayLine = [theTemplate addUnitGenerator:[DelayUGxyy class]];
  59.     delayLine2 = [theTemplate addUnitGenerator:[DelayUGyxy class]];
  60.     gainElement = [theTemplate addUnitGenerator:[ScaleUGyy class]];
  61.  
  62.     stereoOut = [theTemplate addUnitGenerator:[Out2sumUGy class]];
  63.  
  64.     ampPp = [theTemplate addPatchpoint:MK_xPatch];
  65.     inputNoisePp = [theTemplate addPatchpoint:MK_yPatch];
  66.     inputMultPp = [theTemplate addPatchpoint:MK_xPatch];
  67.     inputAddPp = [theTemplate addPatchpoint:MK_yPatch];
  68.     inputAdd2Pp = [theTemplate addPatchpoint:MK_xPatch];
  69.     jetSquarerPp = [theTemplate addPatchpoint:MK_xPatch];
  70.     jetCuberPp = [theTemplate addPatchpoint:MK_xPatch];
  71.     sigAddPp = [theTemplate addPatchpoint:MK_xPatch];
  72.     jetAddPp = [theTemplate addPatchpoint:MK_yPatch];
  73.     filterPp = [theTemplate addPatchpoint:MK_yPatch];
  74.     delayPp = [theTemplate addPatchpoint:MK_xPatch];
  75.     delay2Pp = [theTemplate addPatchpoint:MK_yPatch];
  76.     gainPp = [theTemplate addPatchpoint:MK_yPatch];
  77.  
  78.     /* Step 3:  Specify the connections. */
  79.     [theTemplate to:ampAsymp sel:@selector(setOutput:) arg:ampPp];
  80.  
  81.     [theTemplate to:inputNoise sel:@selector(setOutput:) arg:inputNoisePp];
  82.     
  83.     [theTemplate to:inputMult sel:@selector(setOutput:) arg:inputMultPp];
  84.     [theTemplate to:inputMult sel:@selector(setInput1:) arg:inputNoisePp];
  85.     [theTemplate to:inputMult sel:@selector(setInput2:) arg:ampPp];
  86.  
  87.     [theTemplate to:inputAdd sel:@selector(setOutput:) arg:inputAddPp];
  88.     [theTemplate to:inputAdd sel:@selector(setInput1:) arg:inputMultPp];
  89.     [theTemplate to:inputAdd sel:@selector(setInput2:) arg:ampPp];
  90.  
  91.     [theTemplate to:inputAdd2 sel:@selector(setOutput:) arg:inputAdd2Pp];
  92.     [theTemplate to:inputAdd2 sel:@selector(setInput1:) arg:inputAddPp];
  93.     [theTemplate to:inputAdd2 sel:@selector(setInput2:) arg:delayPp];
  94.  
  95.     [theTemplate to:jetSquarer sel:@selector(setOutput:) arg:jetSquarerPp];
  96.     [theTemplate to:jetSquarer sel:@selector(setInput1:) arg:delay2Pp];
  97.     [theTemplate to:jetSquarer sel:@selector(setInput2:) arg:delay2Pp];
  98.  
  99.     [theTemplate to:jetCuber sel:@selector(setOutput:) arg:jetCuberPp];
  100.     [theTemplate to:jetCuber sel:@selector(setInput1:) arg:delay2Pp];
  101.     [theTemplate to:jetCuber sel:@selector(setInput2:) arg:jetSquarerPp];
  102.  
  103.     [theTemplate to:sigAdder sel:@selector(setOutput:) arg:sigAddPp];
  104.     [theTemplate to:sigAdder sel:@selector(setInput1:) arg:delay2Pp];
  105.     [theTemplate to:sigAdder sel:@selector(setInput2:) arg:jetCuberPp];
  106.  
  107.     [theTemplate to:jetAdder sel:@selector(setOutput:) arg:jetAddPp];
  108.     [theTemplate to:jetAdder sel:@selector(setInput1:) arg:sigAddPp];
  109.     [theTemplate to:jetAdder sel:@selector(setInput2:) arg:delayPp];
  110.  
  111.     [theTemplate to:reflFilter sel:@selector(setOutput:) arg:filterPp];
  112.     [theTemplate to:reflFilter sel:@selector(setInput:) arg:jetAddPp];
  113.  
  114.     [theTemplate to:delayLine sel:@selector(setOutput:) arg:delayPp];
  115.     [theTemplate to:delayLine sel:@selector(setInput:) arg:filterPp];
  116.  
  117.     [theTemplate to:delayLine2 sel:@selector(setOutput:) arg:delay2Pp];
  118.     [theTemplate to:delayLine2 sel:@selector(setInput:) arg:inputAdd2Pp];
  119.  
  120.     [theTemplate to:gainElement sel:@selector(setOutput:) arg:gainPp];
  121.     [theTemplate to:gainElement sel:@selector(setInput:) arg: jetAddPp];
  122.  
  123.     /* Return the PatchTemplate. */    
  124.     return theTemplate;
  125. }
  126.  
  127. - init
  128. {
  129.     /* Sent once when the patch is created. */
  130.     return self;
  131. }
  132.  
  133. - setDefaults
  134. {
  135.     ampEnv  = nil;    
  136.     amp0    = 0.0;
  137.     amp1    = MK_DEFAULTAMP;  /* 0.1 */
  138.     ampAtt  = MK_NODVAL;      /* parameter not present */
  139.     ampRel  = MK_NODVAL;      /* parameter not present */
  140.  
  141.     portamento = MK_DEFAULTPORTAMENTO;     /* 0.1 */
  142.     bearing = MK_DEFAULTBEARING;        /* 0.0 (centered) */
  143.     
  144.     return self;
  145. }
  146.  
  147. - preemptFor:aNote
  148. {
  149.     [[self synthElementAt:ampAsymp] preemptEnvelope]; 
  150.     [self setDefaults];
  151.     return self;
  152. }
  153.  
  154. - applyParameters:aNote
  155.   /* This is a private method to the InsTwoWaves class. It is used internally only.
  156.      */
  157. {
  158.     /* Retrieve and store the parameters. */
  159.     id        myAmpEnv = [aNote parAsEnvelope:MK_ampEnv];
  160.     double    myAmp0   = [aNote parAsDouble:MK_amp0];
  161.     double    myAmp1   = [aNote parAsDouble:MK_amp1];
  162.     double    myAmpAtt = [aNote parAsDouble:MK_ampAtt];
  163.     double    myAmpRel = [aNote parAsDouble:MK_ampAtt];
  164.  
  165.  
  166.     int        MY_envelopeSlew = [[Note class] parName: "MY_envelopeSlew"];
  167.     double    myEnvelopeSlew   = [aNote parAsDouble:MY_envelopeSlew];
  168.  
  169.     int        MY_noiseVolume = [[Note class] parName: "MY_noiseVolume"];
  170.     double    myNoiseVolume   = [aNote parAsDouble:MY_noiseVolume];
  171.  
  172.     int        MY_delay2Length = [[Note class] parName: "MY_delay2Length"];
  173.     double    myDelay2Length   = [aNote parAsDouble:MY_delay2Length];
  174.  
  175.     int        MY_outAmp = [[Note class] parName: "MY_outAmp"];
  176.     int        MY_dLineLength = [[Note class] parName: "MY_dLineLength"];
  177.     double    myOutAmp   = [aNote parAsDouble:MY_outAmp];
  178.     double    myDLineLength   = [aNote parAsDouble:MY_dLineLength];
  179.  
  180.     double    myPortamento = [aNote parAsDouble:MK_portamento];
  181.     double    myBearing    = [aNote parAsDouble:MK_bearing];
  182.  
  183.     /* Store the phrase status. */    
  184.     MKPhraseStatus phraseStatus = [self phraseStatus];
  185.  
  186.     /* Is aNote a noteOn? */
  187. //    BOOL isNoteOn = [aNote noteType] == MK_noteOn;
  188.  
  189.     /* Is aNote the beginning of a new phrase? */
  190.     BOOL isNewPhrase = (phraseStatus == MK_phraseOn) || 
  191.                        (phraseStatus == MK_phraseOnPreempt);
  192.  
  193.     /* Used in the parameter checks. */
  194.     BOOL shouldApplyAmp = NO;
  195.     BOOL shouldApplyBearing = NO;    
  196.  
  197.     /* The same portamento is used in both frequency and amplitude. */
  198.     if (!MKIsNoDVal(myPortamento)) {
  199.         portamento = myPortamento;
  200.         shouldApplyAmp = YES;}    
  201.     
  202.     /* Check the amplitude parameters and set the instance variables. */
  203.     if (myAmpEnv != nil) {
  204.        ampEnv = myAmpEnv;
  205.         shouldApplyAmp = YES; }
  206.     if (!MKIsNoDVal(myAmp0)) {
  207.         amp0 = myAmp0;
  208.         shouldApplyAmp = YES; }
  209.     if (!MKIsNoDVal(myAmp1)) {
  210.         amp1 = myAmp1;
  211. //    [[self synthElementAt:ampAsymp] abortEnvelope];
  212. //    [[self synthElementAt:ampAsymp] setT60: 0.01];
  213.     [[self synthElementAt:ampAsymp] setTargetVal:amp1];
  214.     }
  215.     if (!MKIsNoDVal(myEnvelopeSlew)) {
  216.              [[self synthElementAt:ampAsymp] setT60: myEnvelopeSlew];
  217.                  // Hack this for slew on breath pressure
  218.     }    
  219.     if (!MKIsNoDVal(myAmpAtt)) {
  220.         ampAtt = myAmpAtt;
  221.         shouldApplyAmp = YES; }
  222.     if (!MKIsNoDVal(myAmpRel)) {
  223.         ampRel = myAmpRel;
  224.         shouldApplyAmp = YES; }
  225.     /* Apply the amplitude parameters. */
  226. //    if (shouldApplyAmp || isNoteOn)    {
  227. //      MKUpdateAsymp([self synthElementAt:ampAsymp], 
  228. //                    ampEnv, amp0, amp1, ampAtt, ampRel, 
  229. //                    portamento, phraseStatus);
  230. //    }
  231.  
  232.    /* Check and set the bearing. */
  233.    if (!MKIsNoDVal(myBearing)) {
  234.         bearing = myBearing;
  235.         shouldApplyBearing = YES; }
  236.    if (shouldApplyBearing || isNewPhrase)
  237.      [[self synthElementAt:stereoOut] setBearing:bearing];
  238.  
  239. [[self synthElementAt:inputAdd2] setScale1: 1.0];
  240. [[self synthElementAt:inputAdd2] setScale2: -0.35];
  241. [[self synthElementAt:sigAdder] setScale1: -1.0];
  242. [[self synthElementAt:sigAdder] setScale2:  1.0];
  243. [[self synthElementAt:jetAdder] setScale1: 0.4];
  244. [[self synthElementAt:jetAdder] setScale2: 0.90];
  245.  
  246. [[self synthElementAt:reflFilter] setB0: 0.7];
  247. [[self synthElementAt:reflFilter] setA1: -0.3];
  248.  
  249.    /* Check and set the embouchure. */
  250.    if (!MKIsNoDVal(myDelay2Length)) {
  251.         delay2Length = myDelay2Length;
  252.              [[self synthElementAt:delayLine2] adjustLength:delay2Length];
  253.    }
  254.  
  255.    /* Check and set the embouchure. */
  256.    if (!MKIsNoDVal(myNoiseVolume)) {
  257.         noiseVolume = myNoiseVolume;
  258.              [[self synthElementAt:inputAdd] setScale1:myNoiseVolume];
  259.              [[self synthElementAt:inputAdd] setScale2: 1.0];
  260.    }
  261.  
  262.    /* Check and set the fader level. */
  263.    if (!MKIsNoDVal(myOutAmp)) {
  264.         outAmp = myOutAmp;
  265.         [[self synthElementAt:gainElement] setScale:outAmp];
  266.    }
  267.  
  268.    if (!MKIsNoDVal(myDLineLength)) {
  269.         dLineLength = myDLineLength;
  270.      [[self synthElementAt:delayLine] adjustLength:(int) dLineLength];
  271.    }
  272.  
  273.   return self;
  274. }    
  275.  
  276. - noteOnSelf:aNote
  277. {
  278.     delayMemory = [Orchestra allocSynthData:MK_yData length:100];
  279.     [[self synthElementAt: delayLine] setDelayMemory: delayMemory];
  280.     [delayMemory setToConstant:0 length:100 offset:0];  
  281.     delayMemory2 = [Orchestra allocSynthData:MK_yData length:40];
  282.     [[self synthElementAt: delayLine2] setDelayMemory: delayMemory2];
  283.     [delayMemory2 setToConstant:0 length:40 offset:0];  
  284.     /* Apply the parameters to the patch. */    
  285.     [self applyParameters:aNote];
  286.  
  287.     /* Make the final connection to the output sample stream. */    
  288.     [[self synthElementAt:stereoOut] setInput:[self synthElementAt:gainPp]];
  289.  
  290.     /* Tell the UnitGenerators to begin running. */    
  291.     [synthElements makeObjectsPerform:@selector(run)];
  292.  
  293.     return self;
  294. }
  295.  
  296. - noteUpdateSelf:aNote
  297. {
  298.     /* Apply the parameters to the patch. */    
  299.     [self applyParameters: aNote];
  300.     
  301.     return self;    
  302. }
  303.  
  304. - (double)noteOffSelf:aNote
  305. {   
  306.     /* Apply the parameters. */
  307.     [self applyParameters: aNote];
  308.  
  309.     /* Same for amplitude, but also return the release duration. */
  310.     return [[self synthElementAt:ampAsymp] finish];
  311. }
  312.  
  313. - noteEndSelf
  314. {
  315.     /* Remove the patch's Out2sum from the output sample stream. */
  316.     [[self synthElementAt:stereoOut] idle]; 
  317.  
  318.     /* Abort the frequency Envelope. */
  319.     [[self synthElementAt:ampAsymp] abortEnvelope];
  320.  
  321.     /* Set the instance variables to their default values. */ 
  322.     [self setDefaults];
  323.  
  324.     return self;
  325. }
  326.  
  327. @end
  328.