home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / SoundApps / Patchmix / Source / Arith.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  4.2 KB  |  205 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Arith.h"
  5. #import "Instrum.h"
  6. #import <appkit/graphics.h>
  7. #import "Statement.h"
  8.  
  9. @implementation Arith
  10.  
  11. + initialize
  12. {
  13.        addimage = [NXImage findImageNamed:"Add"];
  14.        subimage = [NXImage findImageNamed:"Sub"];
  15.        multimage = [NXImage findImageNamed:"Mult"];
  16.        divimage = [NXImage findImageNamed:"Div"];
  17.     in0Offset.x = 20;
  18.     in0Offset.y = 47;
  19.     in1Offset.x = 30;
  20.     in1Offset.y = 47;
  21.     outOffset.x = 28;
  22.     outOffset.y = 17;
  23.     
  24.     return self;
  25. }
  26.  
  27. - init
  28. {
  29.     written = NO;
  30.     size.width = 80.;
  31.     size.height = 80.;
  32.     center.x = size.width/2.;
  33.     center.y = size.height/2.;
  34.     strcpy(type,"Add");
  35.     strcpy(name,"add");
  36.     
  37.     in0 = [[Param alloc] init:self :&in0Offset];
  38.     in1 = [[Param alloc] init:self :&in1Offset];
  39.     out = [[Param alloc] init:self :&outOffset];
  40.  
  41.     [in0 setTitle:"Inp1:"];
  42.     [in1 setTitle:"Inp2:"];
  43.     [out setTitle:"Out:"];
  44.  
  45.     paramList = [[List alloc] initCount:4];
  46.     [paramList addObject:in0];
  47.     [paramList addObject:in1];
  48.     [paramList addObject:out];
  49.     
  50.     [Inst putUgenInList:self];
  51.     
  52.     return self;
  53. }
  54.  
  55. - remove
  56. {
  57.     id cp,param;
  58.     int i;
  59.                             // make sure not connected first 
  60.     for(i = 0; i < ([paramList count]); i++) {    
  61.         param = [paramList objectAt:i];
  62.         if(cp = [param getConnectedParam]) 
  63.             return 0;
  64.     }
  65.                             // remove ugen from list
  66.     [Inst removeUgenFromList:self];
  67.     return self;
  68. }
  69.  
  70. - (NXImage *)getImage
  71. {                        // get the right image
  72.     return image;
  73. }
  74.  
  75. - move:(NXPoint *)newloc
  76. {
  77.     location = *newloc;
  78.     [in0 move:&location];
  79.     [in1 move:&location];
  80.     [out move:&location];
  81.     return self;
  82. }
  83.  
  84. - setAtype:(char)op
  85. {
  86.     operator = op;
  87.     switch(operator) {
  88.         case '+': {
  89.             image = addimage;
  90.             break;
  91.         }
  92.         case '-': {
  93.             image = subimage;
  94.             break;
  95.         }
  96.         case '*': {
  97.             image = multimage;
  98.             break;
  99.         }
  100.         case '/': {
  101.             image = divimage;
  102.             break;
  103.         }
  104.     }
  105.     return self;
  106. }
  107.  
  108. - findParamAtPoint:(NXPoint *)point
  109. {
  110.     NXRect *rect;
  111.     int i;
  112.     id param;
  113.     
  114.     for(i = 0; i < [paramList count]; i++) {
  115.         param = [paramList objectAt:i];
  116.         rect = [param getRect];
  117.         if(NXMouseInRect(point,rect,NO))
  118.             return param;
  119.     }
  120.     //printf("no param found\n");
  121.     return nil;
  122. }
  123.  
  124. - writeUgen
  125. {
  126.     /*     for each input param:
  127.             see if there is input ugen
  128.                 if so, grab the output (ug?) and multiply it by this
  129.                     param value
  130.                 if not just use the param value
  131.          after all params done, write relevant code into lists
  132.             (declarations, assignments and loop statements)
  133.             i.e. ug? = oscil(,,,,);
  134.         go to output param connected ugen and call its "writeUgen"
  135.     */
  136.     int i;
  137.     int parent[3];
  138.     id param;
  139.     id ug;
  140.     id nupar;
  141.     char str[50];
  142.     char par[20];
  143.     
  144.     //printf("writing ugen %d\n",index);
  145.     sprintf(str,"\tfloat ug%d;\n",index);
  146.     [Inst putVarInList:str];
  147.     sprintf(str,"\tint in0%d;\n",index);
  148.     [Inst putVarInList:str];
  149.     sprintf(str,"\tint in1%d;\n",index);
  150.     [Inst putVarInList:str];
  151.     sprintf(str,"\tfloat out%d;\n",index);
  152.     [Inst putVarInList:str];
  153.  
  154.  
  155.     for(i = 0; i < ([paramList count]-1); i++) {    
  156.         param = [paramList objectAt:i];
  157.         if(nupar = [param getConnectedParam]) {
  158.             ug = [nupar getUgen];
  159.             if([ug getWritten] == NO) { 
  160.                 parent[i] = [[ug writeUgen] getIndex];
  161.             }
  162.             else  {
  163.                 parent[i] = 0;
  164.             }
  165.             //printf("parent[%d] = %d\n",i,parent[i]);
  166.         }
  167.         else 
  168.             parent[i] = 0;
  169.         
  170.     }
  171.     
  172.     sprintf(str,"\tin0%d = %.2f;\n",index, 
  173.         atof([[paramList objectAt:0] getValue]));
  174.     [Inst putAssignInList:str];
  175.     sprintf(str,"\tin1%d = %.2f;\n",index, 
  176.         atof([[paramList objectAt:1] getValue]));
  177.     [Inst putAssignInList:str];
  178.  
  179.                     // output amplitude multiplier:
  180.     strcpy(par,[[paramList objectAt:2] getValue]);
  181.     if(par[0] == 'p' && par[1] == '[')
  182.         sprintf(str,"\tout%d = %s;\n",index,par);
  183.     else
  184.         sprintf(str,"\tout%d = %.2f;\n", index, atof([[paramList objectAt:2] getValue]));
  185.     [Inst putAssignInList:str];
  186.  
  187.     if(parent[0] && parent[1]) 
  188.         sprintf(str,"\t\tug%d = (ug%d * in0%d %c ug%d * in1%d)*out%d;\n", index, parent[0], index, operator, parent[1], index, index);
  189.     
  190.     else if(parent[0])
  191.         sprintf(str,"\t\tug%d = (ug%d * in0%d %c in1%d)*out%d;\n", index, parent[0], index, operator, index, index);
  192.  
  193.     else if(parent[1])
  194.         sprintf(str,"\t\tug%d = (in0%d %c  ug%d * in1%d)*out%d;\n", index, index, operator, parent[1], index, index);
  195.     
  196.     else 
  197.         sprintf(str,"\t\tug%d = (in0%d %c in1%d)*out%d;\n", index, index, operator,  index, index);
  198.  
  199.     [Inst putLoopInList:str];
  200.     written = YES;
  201.     return self;    
  202. }
  203.  
  204. @end
  205.