home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / Looching2.0 / Looching.m < prev    next >
Encoding:
Text File  |  1992-08-03  |  7.0 KB  |  357 lines

  1. /* Generated by Interface Builder */
  2.  
  3. #include <libc.h>
  4. #include <strings.h>
  5.  
  6. #import "Looching.h"
  7. #import <appkit/Form.h>
  8. #import <appkit/Slider.h>
  9. #import <musickit/musickit.h>
  10. #import <musickit/synthpatches/synthpatches.h>
  11.  
  12. #import "LoochPerformer.h"
  13. #import "Bark.h"
  14. #import "RandomIzer.h"
  15. #import "LoochWaves.h"
  16.  
  17. #define    NWAVES    7
  18. #define FADELO    10
  19. #define FADEHI    20
  20. #define AMPLO    0.05
  21. #define AMPHI    0.1
  22. #define DURSHORT    5.0
  23. #define    DURLONG        20.0
  24. #define SLEEPLO    2
  25. #define    SLEEPHI    10
  26.  
  27. extern int Pid;
  28.  
  29. @implementation Looching
  30.  
  31. /* these are the global vars */
  32. static int IsLooching = 0;
  33. static int ToneRow = 0;
  34. static int TimbreSet = 0;
  35. static int fds[2];
  36. static int isPiped = 0;
  37. double VibWide;
  38. double VibRate;
  39. double FreqSpread;
  40.  
  41.  
  42. - setInVibRate:anObject
  43. {
  44.     inVibRate = anObject;
  45.     [inVibRate setDoubleValue:0.5];
  46.     VibRate = 0.5; /* initialize the variable */    
  47.     return self;
  48. }
  49.  
  50. - setInVibWidth:anObject
  51. {
  52.     inVibWidth = anObject;
  53.     [inVibWidth setDoubleValue:1.0];
  54.     VibWide = 1.0; /* initialize the variable */
  55.     return self;
  56. }
  57.  
  58. - setOutVibRate:anObject
  59. {
  60.     outVibRate = anObject;
  61.     [outVibRate setDoubleValue:0.5 at:0];
  62.     return self;
  63. }
  64.  
  65. - setOutVibWidth:anObject
  66. {
  67.     outVibWidth = anObject;
  68.     [outVibWidth setDoubleValue:1.0 at:0];
  69.     return self;
  70. }
  71.  
  72. - setInSpread:anObject
  73. {
  74.     inSpread = anObject;
  75.     [inSpread setDoubleValue:1.0];
  76.     FreqSpread = 1.0; /* initialize the variable */
  77.     return self;
  78. }
  79.  
  80. - setOutSpread:anObject
  81. {
  82.     outSpread = anObject;
  83.     [outSpread setDoubleValue:1.0 at:0];
  84.     return self;
  85. }
  86.  
  87. - displaySpread:sender
  88. {
  89.     double val;
  90.  
  91.     val = [inSpread doubleValue];
  92.     [outSpread setDoubleValue:val at:0];
  93.     return self;
  94. }
  95.  
  96. - setParams:sender
  97. {
  98.     FreqSpread = [inSpread doubleValue];
  99.     VibWide = [inVibWidth doubleValue];
  100.     VibRate = [inVibRate doubleValue];
  101.     
  102.     if (isPiped == 1) {
  103.         write(fds[1], (char *) &FreqSpread, sizeof(double));
  104.        write(fds[1], (char *) &VibWide, sizeof(double));
  105.        write(fds[1], (char *) &VibRate, sizeof(double));
  106.        write(fds[1], (char *) &ToneRow, sizeof(int));
  107.  
  108.     }
  109.     
  110.     return self;
  111. }
  112.  
  113. - StartAndStop:sender
  114. {
  115.     FreqSpread = [inSpread doubleValue];
  116.     VibWide = [inVibWidth doubleValue];
  117.     VibRate = [inVibRate doubleValue];
  118.     
  119.     if (IsLooching == 0) {
  120.     IsLooching = 1;
  121.  
  122.     if (!isPiped) {
  123.         pipe(fds); /* set up communications with the child process */
  124.         isPiped = 1;
  125.         }
  126.  
  127.     Pid = fork();
  128.  
  129.     if (Pid == 0) { /* child does the playing */
  130.         int i,j,n;
  131.         int sleeptime;
  132.         int nfound = 0;
  133.         double fade,coin;
  134.         int isPlaying[4] = {0, 0, 0, 0};
  135.         double durations[4] = {0.0, 0.0, 0.0, 0.0};
  136.         double val,sval,fval;
  137.             id aLoochPerformer[4],theOrch;
  138.             SynthInstrument *anIns[4];
  139.         id RanNum;
  140.         id aPartials[NWAVES];
  141.         double *LWfreqptr,*LWampptr;
  142.         fd_set rfds,wfds;
  143.         struct timeval ttt;
  144.  
  145.         RanNum = [RandomIzer new];
  146.         [RanNum setit];
  147.  
  148.         /* Create the Partials objects. */
  149.         for (i = 0; i < NWAVES; i++) { 
  150.             aPartials[i] = [Partials new];
  151.             LWfreqptr = freqs[TimbreSet][i];
  152.             LWampptr = amps[TimbreSet][i];
  153.  
  154.             /* Fill the object with data. */ 
  155.             [aPartials[i] setPartialCount:7 
  156.                     freqRatios:LWfreqptr 
  157.                     ampRatios:LWampptr 
  158.                     phases:NULL 
  159.                     orDefaultPhase:0.0];
  160.                 }
  161.             
  162.         /* Create the Orchestra which manages all DSP activity. */
  163.             theOrch = [Orchestra new];
  164.             if (![theOrch open]) {
  165.             fprintf(stderr, "Can't open the DSP!\n");
  166.             Pid = 0;
  167.             exit(-1);
  168.                 }
  169.         [theOrch setSamplingRate:22050.0];
  170.                 
  171.         for(i = 0; i < 4; i++) {    
  172.             /* make performers */
  173.             aLoochPerformer[i] = [LoochPerformer new];
  174.     
  175.             /* create instruments */                  
  176.             anIns[i] = [SynthInstrument new];      
  177.             /* Assign the class of SynthPatch. */
  178.             [anIns[i] setSynthPatchClass:[Bark class]];
  179.     
  180.             /* set up the performer--instrument connection
  181.                         and get ready to roll */
  182.             [[aLoochPerformer[i] noteSender] connect:[anIns[i]
  183.                             noteReceiver]];
  184.             [aLoochPerformer[i] activate];
  185.             [aLoochPerformer[i] pause];   /* Start paused */
  186.             }
  187.                 
  188.         [theOrch run];    /* Start the DSP. */
  189.  
  190.         sleeptime = 0.0;
  191.         while(1) {
  192.             for(j = 0; j < 4; j++) {
  193.                 if (isPlaying[j]) {
  194.                     durations[j] -= sleeptime;
  195.                     if (durations[j] <= 0) {
  196.                         /* choose the fadeout dur */
  197.                         fade = [RanNum
  198.                             GetNumberRangeHi:FADEHI
  199.                                 Lo:FADELO];
  200.                         [aLoochPerformer[j]
  201.                             setdecay:fade];
  202.                         [aLoochPerformer[j]
  203.                             changeNote];
  204.                         [theOrch flushTimedMessages];
  205.                         durations[j] += fade;
  206.                         isPlaying[j] = 0;
  207.                         }
  208.                     }
  209.                 else {
  210.                     durations[j] -= sleeptime;
  211.                     if (durations[j] > 0) {
  212.                         ;
  213.                         }
  214.                     else {
  215.                         /* play a new note? */
  216.                         coin = [RanNum GetNumber];
  217.                         if (coin < 0.5) {
  218.  
  219. /* set the waveform */
  220. n = [RanNum GetIndex:(NWAVES-1)];
  221. [aLoochPerformer[j] setwave:aPartials[n]];
  222.  
  223. /* set the frequency */
  224. n = [RanNum GetIndex:toneindex[ToneRow]];
  225. fval = pitches[ToneRow][n];
  226. sval = ( ([RanNum GetNumber:FreqSpread]) / 100.0 ) * fval;
  227. [aLoochPerformer[j] setfreq:fval spread:sval];
  228.  
  229. /* set the amplitude */
  230. val = [RanNum GetNumberRangeHi:AMPHI Lo:AMPLO];
  231. [aLoochPerformer[j] setamp:val];
  232.  
  233. /* set vibrato0 */
  234. val = [RanNum GetNumber:VibRate];
  235. [aLoochPerformer[j] setvibfreq0:val];
  236.  
  237. /* set amp0 */
  238. val = ( ([RanNum GetNumber:VibWide]) / 100.0 ) * fval;
  239. [aLoochPerformer[j] setvibamp0:val];
  240.  
  241. /* set vibrato1 */
  242. val = [RanNum GetNumber:VibRate];
  243. [aLoochPerformer[j] setvibfreq1:val];
  244.  
  245. /* set amp1 */
  246. val = ( ([RanNum GetNumber:VibWide]) / 100.0 ) *fval;
  247. [aLoochPerformer[j] setvibamp1:val];
  248.  
  249. /* set attack time */
  250. fade = [RanNum GetNumberRangeHi:FADEHI Lo:FADELO];
  251. [aLoochPerformer[j] setattack:fade];
  252.  
  253. /* set note duration */
  254. durations[j] = [RanNum GetNumberRangeHi:DURLONG Lo:DURSHORT];
  255. [theOrch flushTimedMessages];
  256.  
  257. [aLoochPerformer[j] startNote];
  258. [theOrch flushTimedMessages];
  259. isPlaying[j] = 1;
  260.  
  261.                             }
  262.                         }
  263.                     }
  264.                 }
  265.             sleeptime = [RanNum GetIndexRangeHi:SLEEPHI
  266.                                 Lo:SLEEPLO];
  267.             ttt.tv_sec = sleeptime;
  268.             ttt.tv_usec = 0;
  269.             FD_ZERO(&rfds);
  270.             FD_SET(fds[0],&rfds);
  271.  
  272.             nfound = select(5, &rfds, 0, 0, &ttt);
  273.             if ( (nfound > 0) && (FD_ISSET(fds[0], &rfds)) ) {
  274.                 read(fds[0], (char *) &FreqSpread,
  275.                             sizeof(double));
  276.                 read(fds[0], (char *) &VibWide,
  277.                             sizeof(double));
  278.                 read(fds[0], (char *) &VibRate,
  279.                             sizeof(double));
  280.                 read(fds[0], (char *) &ToneRow,
  281.                             sizeof(int));
  282.  
  283.                 }
  284.             }
  285.         }
  286.     }
  287.  
  288.     else {
  289.     char s[10] = "kill ";
  290.     char num[10];
  291.  
  292.     sprintf(num,"%d",Pid);
  293.     strcat(s,num);
  294.     system(s);
  295.     IsLooching = 0;
  296.     Pid = 0;
  297.     }
  298.  
  299.     return self;
  300. }
  301.  
  302. - trArnie:sender
  303. {
  304.     ToneRow = 3;
  305.     TimbreSet = 2;
  306.     return self;
  307. }
  308.  
  309. - trBach:sender
  310. {
  311.     ToneRow = 4;
  312.     TimbreSet = 0;
  313.     return self;
  314. }
  315.  
  316. - displayVibWidth:sender
  317. {
  318.     double val;
  319.  
  320.     val = [inVibWidth doubleValue];
  321.     [outVibWidth setDoubleValue:val at:0];
  322.     return self;
  323. }
  324.  
  325. - displayVibRate:sender
  326. {
  327.     double val;
  328.  
  329.     val = [inVibRate doubleValue];
  330.     [outVibRate setDoubleValue:val at:0];
  331.     return self;
  332. }
  333.  
  334. - trDrone:sender
  335. {
  336.     ToneRow = 2;
  337.     TimbreSet = 1;
  338.     return self;
  339. }
  340.  
  341. - trMorning:sender
  342. {
  343.     ToneRow = 1;
  344.     TimbreSet = 1;
  345.     return self;
  346. }
  347.  
  348. - trOriginal:sender
  349. {
  350.     ToneRow = 0;
  351.     TimbreSet = 0;
  352.     return self;
  353. }
  354.  
  355.  
  356. @end
  357.