home *** CD-ROM | disk | FTP | other *** search
/ FreeWare Collection 2 / FreeSoftwareCollection2pd199x-jp.img / look / src / loader.c < prev    next >
Text File  |  1990-06-14  |  6KB  |  313 lines

  1. /*                                                */
  2. /*    Gear Box Music Loader v0.2                    */
  3. /*                                                */
  4. /*        1988/11/01                                */
  5. /*        Copyright (C) FUJITSU LIMITED             */
  6. /*        Programed by Joe Mizuno (FPS)             */
  7. /*        Small Cheng By Ken                        */
  8. /*                                                */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <snd.h>
  13.  
  14. static int buff_size;
  15. static int buff_sw;
  16. static char *buff_top[2];
  17. static char *buff_btm[2];
  18. static char status_save;
  19.  
  20. static char *bios_work;
  21. static int tempo,signature;
  22. static int eof;
  23. static FILE *fp;
  24.  
  25. /*------------------------------------------------*/
  26. int ML_Start(buffer,size,work)
  27. char *buffer;
  28. int size;
  29. char *work;
  30. {
  31.     bios_work = work;
  32.     if((buff_size = (size/12)*6) < 64)
  33.         return(2);
  34.  
  35.     buff_top[0] = buffer;
  36.     buff_btm[0] = buff_top[0] + buff_size -6;
  37.     buff_top[1] = buff_top[0] + buff_size;
  38.     buff_btm[1] = buff_top[1] + buff_size -6;
  39.  
  40.     SND_eup_init(bios_work);
  41.     return(0);
  42. }
  43. /*------------------------------------------------*/
  44. int ML_End()
  45. {
  46.     SND_eup_end();
  47.     return(0);
  48. }
  49.  
  50. /*------------------------------------------------*/
  51. int ML_Set(path)
  52. char *path;
  53. {
  54.     int i,j,err,size;
  55.     char buffer[128];
  56.     char bankpath[128];
  57.     char fm_bankname[8];
  58.     char pcm_bankname[8];
  59.  
  60.     eof = 1;
  61.     if((fp = fopen(path,"rb")) == NULL)
  62.         return(1);
  63.  
  64.     fseek(fp,852,SEEK_SET);
  65.  
  66.     if(fread(buffer,1,32,fp) < 32)
  67.         goto ERROR;
  68.     for(i=0 ; i<32 ;i++)
  69.         err=SND_eup_mute_set(i,(int)buffer[i]);
  70.  
  71.     if(fread(buffer,1,32,fp) < 32)
  72.         goto ERROR;
  73.     for(i=0 ; i<32 ;i++)
  74.         err=SND_eup_port_set(i,(int)buffer[i]);
  75.  
  76.     if(fread(buffer,1,32,fp) < 32)
  77.         goto ERROR;
  78.     for(i=0 ; i<32 ;i++)
  79.         err=SND_eup_midi_ch_set(i,(int)buffer[i]);
  80.  
  81.     if(fread(buffer,1,32,fp) < 32)
  82.         goto ERROR;
  83.     for(i=0 ; i<32 ;i++)
  84.         err=SND_eup_bias_set(i,(int)buffer[i]);
  85.  
  86.     if(fread(buffer,1,32,fp) < 32)
  87.         goto ERROR;
  88.     for(i=0 ; i<32 ;i++)
  89.         err=SND_eup_transpose_set(i,(int)buffer[i]);
  90. /*
  91. channel assign
  92. */
  93.     fseek(fp,1748,SEEK_SET);
  94.     if(fread(buffer,1,6,fp) < 6)
  95.         goto ERROR;
  96.     for(i = 0;i < 6;i++)
  97.         err=SND_midi_ch_assign(i,(int)buffer[i]);
  98.  
  99.     if(fread(buffer,1,8,fp) < 8)
  100.         goto ERROR;
  101.     for(i = 0;i < 8;i++)
  102.         err=SND_midi_ch_assign(i+64,(int)buffer[i]);
  103. /*
  104. bank load
  105. */
  106.     SND_pcm_mode_set(0);
  107.     
  108.     for(i = 0;path[i] != '\0';i++)
  109.         bankpath[i] = path[i];
  110.     while((path[i] != '\\') && (i >= 0))i--;
  111.     i++;
  112.  
  113.     if(fread(&bankpath[i],1,8,fp) < 8)
  114.         goto ERROR;
  115.     if(bankpath[i] != '\0'){
  116.         for(j= 0;bankpath[i+j] != '\0';j++);
  117.         bankpath[i+j] = '.';
  118.         bankpath[i+j+1] = 'F';
  119.         bankpath[i+j+2] = 'M';
  120.         bankpath[i+j+3] = 'B';
  121.         bankpath[i+j+4] = '\0';
  122.         if ( SND_fm_bank_load(bankpath,fm_bankname) != 0 )
  123.             DSP_error("Can't open/read '%s'",bankpath);
  124.     }
  125.  
  126.     if(fread(&bankpath[i],1,8,fp) < 8)
  127.         goto ERROR;
  128.     if(bankpath[i] != '\0'){
  129.         for(j= 0;bankpath[i+j] != '\0';j++);
  130.         bankpath[i+j] = '.';
  131.         bankpath[i+j+1] = 'P';
  132.         bankpath[i+j+2] = 'M';
  133.         bankpath[i+j+3] = 'B';
  134.         bankpath[i+j+4] = '\0';
  135.         if ( SND_pcm_bank_load(bankpath,pcm_bankname) != 0 )
  136.             DSP_error("Can't open/read '%s'",bankpath);
  137.     }
  138.  
  139.     fseek(fp,2048,SEEK_SET);
  140.  
  141.     if(fread(&size,4,1,fp) < 1)
  142.         goto ERROR;
  143.     if(fread(buffer,1,2,fp) < 2)
  144.         goto ERROR;
  145.     signature = (int)buffer[0];
  146.     tempo = (int)buffer[1];
  147.  
  148.     eof = 0;
  149.     status_save = 0xfe;
  150.     buff_sw = 1;
  151.     load_sub();
  152.     load_sub();
  153.     SND_eup_tempo_set(tempo);
  154.     return(0);
  155. ERROR:
  156.     fclose(fp);
  157.     return(2);
  158. }
  159.  
  160. /*------------------------------------------------*/
  161. int ML_Play(mode)
  162. int mode;
  163. {
  164.     SND_eup_loop_set(mode);
  165.     SND_eup_play_start(buff_top[0],buff_size*2,signature);
  166.     return(0);
  167. }
  168.  
  169. /*------------------------------------------------*/
  170. int ML_Stop()
  171. {
  172.     SND_eup_play_stop();
  173.     if(eof == 0)
  174.         fclose(fp);
  175.     return(0);
  176. }
  177.  
  178. /*------------------------------------------------*/
  179. int ML_Check()
  180. {
  181.     char *ptr;
  182.  
  183.         if(eof != 0)
  184.             return(0);
  185.         ptr = SND_eup_stat_ptr();
  186.  
  187.         if(((buff_sw == 0) && (ptr <= buff_btm[0]))
  188.                 ||((buff_sw == 1) && (ptr >= buff_top[1])))
  189.             return(load_sub());
  190.         else
  191.             return(0);
  192.             
  193. }    
  194. /*------------------------------------------------*/
  195. int SL_Start()
  196. {
  197.     SND_pcm_sound_delete(-1);
  198.     SND_pcm_mode_set(1);
  199.     return(0);
  200. }
  201.  
  202. /*------------------------------------------------*/
  203. int SL_Load(path,buffer,size)
  204. char *path;
  205. char buffer[];
  206. int size;
  207. {
  208.  
  209.     FILE *fp;
  210.     int length;
  211.  
  212.     if((fp = fopen(path,"rb")) == NULL)
  213.         return(1);
  214.     if(size < 32)
  215.         goto ERROR;
  216.     if(fread(buffer,1,32,fp) < 32)
  217.         goto ERROR;
  218.     length = *(int *)&buffer[12];
  219.     if(size < length+32)
  220.         goto ERROR;
  221.     if(fread(buffer+32,1,length,fp) < length)
  222.         goto ERROR;
  223.     fclose(fp);
  224.     return(0);
  225. ERROR:
  226.     fclose(fp);
  227.     return(2);
  228. }
  229.  
  230. /*------------------------------------------------*/
  231. int SL_Play(mode,rate,vol,pan,buff)
  232. int mode;
  233. int rate;
  234. int vol;
  235. int pan;
  236. char buff[];
  237. {
  238.     int org_note;
  239.     int loop;
  240.  
  241.     if(mode > 127)
  242.         return(3);
  243.     SND_pan_set(71,pan);
  244.     if(rate){
  245.         rate = rate * 0x62 / 1000;
  246.         *(short int*)&buff[24] = rate;
  247.     }
  248.     if(mode==1)
  249.         *(int *)&buff[20] = 0;
  250.     else if(mode == 2){
  251.         loop = *(int *)&buff[12];
  252.         *(int *)&buff[16] = 0;
  253.         *(int *)&buff[20] = loop;
  254.     }
  255.  
  256.     org_note = buff[28];
  257.     return(SND_pcm_play(71,org_note,vol,buff));
  258.     
  259. }
  260.  
  261. /*------------------------------------------------*/
  262. int SL_Status()
  263. {
  264.     return(SND_pcm_status(71));
  265. }
  266.  
  267. /*------------------------------------------------*/
  268. int SL_Stop()
  269. {
  270.  
  271.     SND_pcm_play_stop(71);
  272.     SND_pcm_rec_stop();
  273.     return(0);
  274. }
  275.  
  276. /*------------------------------------------------*/
  277. int load_sub()
  278. {
  279.     char *ptr;
  280.     char status;
  281.     int sw;
  282.  
  283.     if(!buff_sw){
  284.         buff_sw = 1;
  285.         sw = 0;
  286.     }
  287.     else{
  288.         buff_sw = 0;
  289.         sw = 1;
  290.     }
  291.     ptr = buff_top[buff_sw];
  292.     while((!eof) && (ptr <= buff_btm[buff_sw])){
  293.         if(fread(ptr,1,6,fp) < 6){
  294.             fclose(fp);
  295.             return(2);
  296.         }
  297.         else if(*ptr == 0xfe){
  298.             eof = 1;
  299.             fclose(fp);
  300.         }
  301.         ptr = ptr + 6;
  302.     }
  303.  
  304.     status = status_save;
  305.     if(!eof){
  306.         status_save = *buff_btm[buff_sw];
  307.         *buff_btm[buff_sw] = 0xfd;
  308.     }
  309.     *buff_btm[sw] =  status;
  310.     SND_eup_play_restart();
  311.     return(0);
  312. }
  313.