home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / dir_nm20.zip / Dir_NM_2.0 / Dir_TTS / tts.m < prev    next >
Text File  |  1992-07-16  |  9KB  |  280 lines

  1. /* 
  2.   tts.m --- NM to Score translater
  3. */
  4.  
  5. #import <musickit/musickit.h>
  6. #import <musickit/synthpatches/synthpatches.h>
  7. #import <musickit/pitches.h>
  8. #import "tts.h"
  9.  
  10. int Speed,Percent,V_Fit[3],Fit[3][7];
  11. double T[3],T_H_tmp[3],T_L_tmp[3];
  12. id Nt_on,Nt_off,IM_score,Pt[3][4],Pt_tmp[6],ampEnvelope,freqEnvelope;
  13. id Pt_rec[8],IM_info;
  14.  
  15. int Nt_Y(Nt,At_high)
  16.   int Nt,At_high;
  17. {
  18.   int Y;
  19.  
  20.   Y=26;
  21.   if (At_high==1 || At_high==2)
  22.     switch(Nt)
  23.       {
  24.         case 48:Y=29;break;      case 50:Y=28;break;      case 52:Y=27;break;
  25.     case 53:Y=26;break;      case 55:Y=25;break;      case 57:Y=24;break;
  26.     case 59:Y=23;break;      case 60:Y=22;break;      case 62:Y=21;break;
  27.     case 64:Y=20;break;     case 65:Y=19;break;      case 67:Y=18;break; 
  28.     case 69:Y=17;break;     case 71:Y=16;break;      case 72:Y=15;break;
  29.     case 74:Y=14;break;     case 76:Y=13;break;      case 77:Y=12;break;
  30.     case 79:Y=11;break;     case 81:Y=10;break;      case 83:Y=9;break;
  31.     case 84:Y=8;break;     case 86:Y=7;break;       case 88:Y=6;break; 
  32.     case 89:Y=5;break;     case 91:Y=4;break;       case 93:Y=3;break;
  33.     case 95:Y=2;break;     case 96:Y=1;break;       case 98:Y=0;break;
  34.       }
  35.   else switch(Nt)
  36.          {
  37.            case 21:Y=53;break;      case 23:Y=52;break;      case 24:Y=51;break;
  38.        case 26:Y=50;break;      case 28:Y=49;break;      case 29:Y=48;break;
  39.        case 31:Y=47;break;      case 33:Y=46;break;      case 35:Y=45;break;
  40.        case 36:Y=44;break;      case 38:Y=43;break;      case 40:Y=42;break;
  41.        case 41:Y=41;break;      case 43:Y=40;break;      case 45:Y=39;break;
  42.        case 47:Y=38;break;      case 48:Y=37;break;      case 50:Y=36;break;
  43.        case 52:Y=35;break;      case 53:Y=34;break;      case 55:Y=33;break;
  44.        case 57:Y=32;break;      case 59:Y=31;break;      case 60:Y=30;break;
  45.        case 62:Y=29;break;      case 64:Y=28;break;      case 65:Y=27;break;
  46.        case 67:Y=26;break;      case 69:Y=25;break;      case 71:Y=24;break;
  47.        case 72:Y=23;break;
  48.      }
  49.   if (At_high<2) Y+=V_Ht/4;
  50.   return Y;
  51. }
  52.  
  53. int Fix_Note(nt,At_High)
  54.   int nt,At_High;
  55. {
  56.   int i;
  57.  
  58.   for (i=0;i<7;i++) 
  59.     if (Nt_Y(nt,At_High)==Fit[At_High][i] || Nt_Y(nt,At_High)==Fit[At_High][i]-8
  60.         || Nt_Y(nt,At_High)==Fit[At_High][i]-16
  61.         || Nt_Y(nt,At_High)==Fit[At_High][i]-24 
  62.         || Nt_Y(nt,At_High)==Fit[At_High][i]+8
  63.         || Nt_Y(nt,At_High)==Fit[At_High][i]+16
  64.         || Nt_Y(nt,At_High)==Fit[At_High][i]+24) 
  65.       return (nt+V_Fit[At_High]);
  66.   return nt;
  67. }
  68.  
  69. char *Sub_Str(string,head,length)
  70.   char *string;
  71.   int head,length;
  72. {
  73.   char *sub;
  74.   int i;
  75.  
  76.   sub=(char *)malloc(5000);*sub='\0';i=-1;
  77.   if (strlen(string)>head && string[head]!='\n')
  78.     { 
  79.       do 
  80.     { 
  81.       i++;sprintf(sub+strlen(sub),"%c",string[head+i]);
  82.     } while (i<length-1 && string[head+i]!='\n');
  83.     }
  84.   return sub;
  85. }
  86.  
  87. int Load_From_Text(filename)
  88.   char *filename;
  89. {
  90.   int i,j;
  91.   char Line[100],Word_buf[3000];
  92.  
  93.   if ((Textfile=fopen(filename,"r"))==NULL) exit(0);;
  94.   S_bf=(char *)malloc(10);*S_bf='\0';
  95.   for (j=0;j<3;j++)
  96.     {
  97.       for (i=0;i<St_Start;i++)
  98.         {    
  99.           St[i][j].nt[0]=0;St[i][j].nt[1]=0;
  100.           St[i][j].nt[2]=0;St[i][j].nt[3]=0;
  101.           St[i][j].act=0;St[i][j].vol=100;
  102.         }
  103.       for (i=St_Start;i<M_Len;i++)
  104.         {
  105.           St[i][j].nt[0]=200;St[i][j].nt[1]=0;
  106.           St[i][j].nt[2]=0;St[i][j].nt[3]=0;
  107.           St[i][j].act=0;St[i][j].vol=100;
  108.         }
  109.     }
  110.   setvbuf(Textfile,buf,_IOFBF,2048);rewind(Textfile);S_bf=(char *)malloc(10);
  111.   *S_bf='\0';i=0;fgets(Line,100,Textfile);fgets(Word_buf,3000,Textfile);
  112.   do {
  113.        fgets(Line,100,Textfile);S_bf=(char *)malloc(10);*S_bf='\0';
  114.        for (j=0;j<4;j++) St[i][0].nt[j]=atoi(strncpy(S_bf,Line+4*j,4));
  115.        St[i][0].act=atoi(strncpy(S_bf,Line+4*4,4));
  116.        St[i][0].vol=atoi(strncpy(S_bf,Line+4*5,4));
  117.        for (j=6;j<10;j++) St[i][1].nt[j-6]=atoi(strncpy(S_bf,Line+4*j,4));
  118.        St[i][1].act=atoi(strncpy(S_bf,Line+4*10,4));
  119.        St[i][1].vol=atoi(strncpy(S_bf,Line+4*11,4));
  120.        for (j=12;j<16;j++) St[i][2].nt[j-12]=atoi(strncpy(S_bf,Line+4*j,4));
  121.        St[i][2].act=atoi(strncpy(S_bf,Line+4*16,4));
  122.        St[i++][2].vol=atoi(strncpy(S_bf,Line+4*17,4));
  123.      } while ((St[i-1][0].nt[0]!=200 || St[i-1][1].nt[0]!=200 || 
  124.            St[i-1][2].nt[0]!=200) && i<M_Len-1);
  125.  
  126.   if (Textfile != NULL) fclose(Textfile);Textfile= NULL;
  127.   Speed=St[3][1].nt[0];Percent=St[4][1].nt[0];
  128.   for (j=0;j<3;j++)
  129.     {
  130.       if ((V_Fit[j]=St[6][i].nt[0])!=0)
  131.         for (i=0;i<7;i++)
  132.           switch (i)
  133.             {
  134.               case 0:Fit[j][i]=St[St_Start-3][j].nt[1];break;
  135.               case 1:Fit[j][i]=St[St_Start-2][j].nt[1];break;
  136.               case 2:Fit[j][i]=St[St_Start-2][j].nt[2];break;
  137.               case 3:Fit[j][i]=St[St_Start-2][j].nt[3];break;
  138.               case 4:Fit[j][i]=St[St_Start-1][j].nt[1];break;
  139.               case 5:Fit[j][i]=St[St_Start-1][j].nt[2];break;
  140.               case 6:Fit[j][i]=St[St_Start-1][j].nt[3];break;
  141.             }
  142.     }
  143.   return 1;
  144. }
  145.  
  146. void Add_One_Note(At_high,Num,Freq,Vol,Time_A,Time_Z)
  147.   int At_high,Num;
  148.   double Freq,Vol,Time_A,Time_Z;
  149. {
  150.   if (At_high!=-1)
  151.     {
  152.       Nt_on=[[Note alloc] initWithTimeTag:Time_A];
  153.       Nt_off=[[Note alloc] initWithTimeTag:Time_Z];
  154.       [Nt_on setNoteType:MK_noteOn];[Nt_off setNoteType:MK_noteOff];
  155.       [Nt_on setNoteTag:MKNoteTag()];[Nt_off setNoteTag:[Nt_on noteTag]];
  156.       [Nt_on setPar:MK_keyNum toDouble:Freq];[Nt_on setPar:MK_amp toDouble:Vol];
  157.       [Pt[At_high][Num] addNote:Nt_on];[Pt[At_high][Num] addNote:Nt_off];
  158.     }
  159.   else
  160.     {
  161.       Nt_on=[[Note alloc] initWithTimeTag:Time_A];
  162.       Nt_off=[[Note alloc] initWithTimeTag:Time_Z];
  163.       [Nt_on setNoteType:MK_noteOn];[Nt_off setNoteType:MK_noteOff];
  164.       [Nt_on setNoteTag:MKNoteTag()];[Nt_off setNoteTag:[Nt_on noteTag]];
  165.       [Nt_on setPar:MK_keyNum toDouble:Freq];[Nt_on setPar:MK_amp toDouble:Vol];
  166.       [Pt_tmp[Num] addNote:Nt_on];[Pt_tmp[Num] addNote:Nt_off];
  167.     }
  168. }
  169.  
  170. void Add_Note_To_Part()
  171. {
  172.   int i,j,k,nt[3],Nt,Tmp_H_pt[3],Tmp_L_pt[3];
  173.   double act[3],vol[3],act_H_tmp[3],act_L_tmp[3],P_act,P_vol;
  174.  
  175.   i=St_Start-1;T[0]=T[1]=T[2]=0.0;P_act=0.0003*(double)Percent;P_vol=0.009;
  176.   for (j=0;j<3;j++) 
  177.     { Tmp_H_pt[j]=2;Tmp_L_pt[j]=2;T_H_tmp[j]=0.0;T_L_tmp[j]=0.0; }
  178.   do {
  179.        for (k=0;k<3;k++)
  180.          {
  181.            nt[k]=St[i+1][k].nt[0];act[k]=P_act*(double)St[i][k].act;
  182.            vol[k]=P_vol*(double)St[i][k].vol;
  183.            for (j=0;j<4;j++)
  184.              {
  185.                Nt=St[i][k].nt[j];
  186.                if (Nt!=600)
  187.                  {
  188.                    if (Nt>0)
  189.                      {
  190.                        if (200<Nt && Nt<328) Nt-=200;
  191.                        else if (St[6][k].nt[0]!=0) Nt=Fix_Note(Nt,1);
  192.                    Add_One_Note(k,j,(double)Nt,vol[k],T[k],T[k]+act[k]);
  193.                      }
  194.                  }
  195.              }
  196.            T[k]+=act[k];
  197.          }
  198.        for (j=0;j<3;j++)
  199.          {
  200.        if (St[2][1].nt[j+1]!=200 && St[Tmp_H_pt[j]][1].nt[j+1]!=0)
  201.              {
  202.            act_H_tmp[j]=(double)St[Tmp_H_pt[j]][1].nt[j+1];
  203.                if (act_H_tmp[j]==200.0) 
  204.              { 
  205.                Tmp_H_pt[j]=2;
  206.                act_H_tmp[j]=(double)St[Tmp_H_pt[j]][1].nt[j+1]; 
  207.              }
  208.            if (act_H_tmp[j]>0)
  209.              {
  210.                    act_H_tmp[j]=P_act*act_H_tmp[j];
  211.                Add_One_Note(-1,j,60.0,0.8,T_H_tmp[j],
  212.                             T_H_tmp[j]+act_H_tmp[j]/3.0);
  213.          }
  214.              }
  215.        if (St[2][2].nt[j+1]!=200 && St[Tmp_L_pt[j]][2].nt[j+1]!=0)
  216.              {
  217.            act_L_tmp[j]=(double)St[Tmp_L_pt[j]][2].nt[j+1];
  218.                if (act_L_tmp[j]==200.0) 
  219.              { 
  220.                Tmp_L_pt[j]=2;
  221.                act_L_tmp[j]=(double)St[Tmp_L_pt[j]][2].nt[j+1]; 
  222.              }
  223.            if (act_L_tmp[j]>0)
  224.              {
  225.                    act_L_tmp[j]=P_act*act_L_tmp[j];
  226.                Add_One_Note(-1,j+3,-60.0,0.8,T_L_tmp[j],
  227.                                 T_L_tmp[j]+act_L_tmp[j]/3.0);
  228.          }
  229.              }
  230.            T_H_tmp[j]+=fabs(act_H_tmp[j]);T_L_tmp[j]+=fabs(act_L_tmp[j]);
  231.          }
  232.      } while (!(nt[0]==200 && nt[1]==200 && nt[2]==200) && ++i<M_Len);
  233. }
  234.  
  235. Translate_Text_To_Score(Input_name,Output_name)
  236.   char Input_name[100],Output_name[100];
  237. {
  238.   int i,j;
  239.  
  240.   /* Make envelopes */
  241.   double xAmpArray[] = {0,.1,.2,.3}; 
  242.   double yAmpArray[] = {0,1,.1,0};
  243.   double xFreqArray[] = {0,.05,.1,.2};
  244.   double yFreqArray[] = {.99,1.1,1.0,.99};
  245.   ampEnvelope = [[Envelope alloc] init];
  246.   [ampEnvelope setPointCount:4 xArray:xAmpArray yArray:yAmpArray];
  247.   [ampEnvelope setStickPoint:2];
  248.   freqEnvelope = [[Envelope alloc] init];
  249.   [freqEnvelope setPointCount:4 xArray:xFreqArray yArray:yFreqArray];
  250.   [freqEnvelope setStickPoint:1];
  251.  
  252.   Load_From_Text(Input_name);IM_score=[[Score alloc] init];
  253.  
  254.   for (j=0;j<3;j++)
  255.     for (i=0;i<4;i++)
  256.       { 
  257.         Pt[j][i]=[[Part alloc] init];Nt_on=[[Note alloc] init];
  258.         [Nt_on setPar:MK_synthPatch toString:"Pluck"];
  259.         [Nt_on setPar:MK_tempo toDouble:(double)Speed];
  260.         [Pt[j][i] setInfo:Nt_on];[IM_score addPart:Pt[j][i]];
  261.       }
  262.   for (i=0;i<6;i++)
  263.     {
  264.       Pt_tmp[i]=[[Part alloc] init];Nt_on=[[Note alloc] init];
  265.       [Nt_on setPar:MK_synthPatch toString:"Pluck"];
  266.       [Nt_on setPar:MK_tempo toDouble:(double)Speed];
  267.       [Pt_tmp[i] setInfo:Nt_on];[IM_score addPart:Pt_tmp[i]];
  268.     }
  269.  
  270.   Add_Note_To_Part();[[Conductor defaultConductor] setTempo:(double)Speed];
  271.   [IM_score writeScorefile:Output_name];
  272. }
  273.   
  274. main(ac, av)
  275.   int ac;
  276.   char * av[];
  277. {
  278.   Translate_Text_To_Score(av[1],av[1]);
  279. }
  280.