home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 3 / FREEWARE.BIN / towns_os / whisper / source / snd.c < prev    next >
Text File  |  1980-01-02  |  6KB  |  288 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <string.h>
  4. #include    <snd.h>
  5.  
  6. #define    TRUE    1
  7. #define    FALSE    0
  8. #define    ERR    (-1)
  9.  
  10. typedef struct _MP {
  11.     struct _MP    *next;
  12.     char    *file;
  13.     char    *buf;
  14. } MEM_FP;
  15.  
  16. void    macset(char *mac,char *str);
  17. char    *MEM_lzss(FILE *fp);
  18. char    *strdup(char *str);
  19.  
  20. extern char    swork[];
  21.  
  22. static char    *snd_buf=NULL;
  23. static char    *eup_buf=NULL;
  24. static int    eup_time=0;
  25. static MEM_FP    *top_mp=NULL;
  26.  
  27. char    *getins(env,file)
  28. char    *env,*file;
  29. {
  30.     static char tmp[128];
  31.     char *p;
  32.  
  33.     if ( (p = getenv(env)) == NULL )
  34.     return NULL;
  35.     sprintf(tmp,"%s\\%s",p,file);
  36.     return tmp;
  37. }
  38. char    *xopen(char *file)
  39. {
  40.     FILE    *fp;
  41.     long    fsz;
  42.     char    *buf;
  43.     register MEM_FP  *mp;
  44.  
  45.     for ( mp = top_mp ; mp != NULL ; mp = mp->next ) {
  46.     if ( strcmp(mp->file,file) == 0 )
  47.         return mp->buf;
  48.     }
  49.  
  50.     if( (fp = fopen(file,"rb")) == NULL )
  51.     return NULL;
  52.  
  53.     if ( (buf = MEM_lzss(fp)) != NULL ) {
  54.     fclose(fp);
  55.     return buf;
  56.     }
  57.  
  58.     fseek(fp,0L,SEEK_END);
  59.     fsz = ftell(fp);
  60.     rewind(fp);
  61.  
  62.     if( (buf = malloc(fsz+4)) == NULL) {
  63.     fclose(fp);
  64.     return NULL;
  65.     }
  66.     *((int *)(&buf)) = *((int *)(&buf)) & 0xFFFFFFFC;
  67.     fread(buf,fsz,1,fp);
  68.     fclose(fp);
  69.     return buf;
  70. }
  71. void    xclose(char *buf)
  72. {
  73.     register MEM_FP  *mp;
  74.  
  75.     for ( mp = top_mp ; mp != NULL ; mp = mp->next ) {
  76.     if ( buf == mp->buf )
  77.         return;
  78.     }
  79.     free(buf);
  80. }
  81. int    mem_file(char *file)
  82. {
  83.     register MEM_FP  *mp;
  84.  
  85.     for ( mp = top_mp ; mp != NULL ; mp = mp->next ) {
  86.     if ( strcmp(mp->file,file) == 0 )
  87.         return TRUE;
  88.     }
  89.  
  90.     if ( (mp = (MEM_FP *)malloc(sizeof(MEM_FP))) == NULL )
  91.     return ERR;
  92.  
  93.     if ( (mp->buf = xopen(file)) == NULL ) {
  94.     free(mp);
  95.     return ERR;
  96.     }
  97.  
  98.     mp->file = strdup(file);
  99.     mp->next = top_mp;
  100.     top_mp = mp;
  101.     return FALSE;
  102. }
  103. void    unmem_file(char *file)
  104. {
  105.     register MEM_FP  *mp;
  106.     register MEM_FP  *op;
  107.  
  108.     for ( mp = top_mp ; mp != NULL ; mp = mp->next ) {
  109.     if ( strcmp(mp->file,file) == 0 ) {
  110.         if ( mp == top_mp )
  111.         top_mp = mp->next;
  112.         else
  113.         op->next = mp->next;
  114.         free(mp->buf);
  115.         free(mp);
  116.         break;
  117.     }
  118.     op = mp;
  119.     }
  120. }
  121. void    PLAY_snd(file)
  122. char    *file;
  123. {
  124.     if ( snd_buf != NULL || eup_buf != NULL )
  125.     return;
  126.  
  127.     SND_pcm_sound_delete(-1);
  128.     SND_pcm_mode_set(1);
  129.  
  130.     if ( (snd_buf = xopen(file)) == NULL )
  131.     return;
  132.  
  133.     SND_pan_set(71,64);
  134.     *(int *)(snd_buf+20) = 0;
  135.     SND_pcm_play(71,snd_buf[28],127,snd_buf);
  136.  
  137.     macset("SND",file);
  138. }
  139.  
  140. void    CHK_snd(void)
  141. {
  142.     if ( snd_buf != NULL && !SND_pcm_status(71) ) {
  143.     SND_pcm_play_stop(71);
  144.     SND_pcm_rec_stop();
  145.     xclose(snd_buf);
  146.     snd_buf = NULL;
  147.     macset("SND","");
  148.     }
  149. }
  150.  
  151. void    PLAY_eup(file)
  152. char    *file;
  153. {
  154.     int     i;
  155.     int     err,size,signa,tempo;
  156.     char    *p;
  157.     char    tmp[16];
  158.     char    dmy[16];
  159.     char    wrk[80];
  160.  
  161.     if ( snd_buf != NULL || eup_buf != NULL )
  162.     return;
  163.  
  164.     if( (eup_buf = xopen(file)) == NULL )
  165.     return;
  166.  
  167.     SND_eup_init(swork);
  168.     SND_elevol_mute(0xB3);
  169.  
  170.     p = &eup_buf[852];        /* trk mute */
  171.     for( i = 0 ; i < 32 ; i++ )
  172.     err = SND_eup_mute_set(i,*(p++));
  173.  
  174.     p = &eup_buf[884];        /* trk port */
  175.     for( i = 0 ; i < 32 ; i++ )
  176.     err = SND_eup_port_set(i,*(p++));
  177.  
  178.     p = &eup_buf[916];        /* trk midi ch */
  179.     for( i = 0 ; i < 32 ; i++ )
  180.     err = SND_eup_midi_ch_set(i,*(p++));
  181.  
  182.     p = &eup_buf[948];        /* trk key bias */
  183.     for( i = 0 ; i < 32 ; i++ )
  184.     err = SND_eup_bias_set(i,*(p++));
  185.  
  186.     p = &eup_buf[980];        /* trk transpose */
  187.     for( i = 0 ; i < 32 ; i++ )
  188.     err = SND_eup_transpose_set(i,*(p++));
  189.  
  190. /********************
  191.     channel assign
  192. *********************/
  193.  
  194.     p = &eup_buf[1748];        /* fm midi ch */
  195.     for( i = 0 ; i < 6 ; i++ )
  196.     err = SND_midi_ch_assign(i,*(p++));
  197.  
  198.     p = &eup_buf[1754];        /* pcm midi ch */
  199.     for( i = 0 ; i < 8 ; i++ )
  200.     err = SND_midi_ch_assign(i+64,*(p++));
  201.  
  202. /****************
  203.     bank load
  204. *****************/
  205.  
  206.     SND_pcm_mode_set(0);
  207.  
  208.     strncpy(tmp,&eup_buf[1762],8);    /* fm file name */
  209.     tmp[8] = '\0';
  210.     if ( tmp[0] != '\0' ) {
  211.     strcat(tmp,".FMB");
  212.     strcpy(wrk,file);
  213.     if ( (p = strrchr(wrk,'\\')) != NULL ) {
  214.         strcpy(p+1,tmp);
  215.         p = wrk;
  216.     } else
  217.         p = tmp;
  218.  
  219.         if ( SND_fm_bank_load(p,dmy) != 0 ) {
  220.         if ( (p = getins("FMINST",tmp)) != NULL )
  221.          SND_fm_bank_load(p,dmy);
  222.     }
  223.     }
  224.  
  225.     strncpy(tmp,&eup_buf[1770],8);    /* pcm file name */
  226.     tmp[8] = '\0';
  227.     if ( tmp[0] != '\0' ) {
  228.     strcat(tmp,".PMB");
  229.     strcpy(wrk,file);
  230.     if ( (p = strrchr(wrk,'\\')) != NULL ) {
  231.         strcpy(p+1,tmp);
  232.         p = wrk;
  233.     } else
  234.         p = tmp;
  235.  
  236.         if ( SND_pcm_bank_load(p,dmy) != 0 ) {
  237.         if ( (p = getins("PCMINST",tmp)) != NULL )
  238.          SND_pcm_bank_load(p,dmy);
  239.     }
  240.     }
  241.  
  242. /*******************
  243.     play eup file
  244. ********************/
  245.  
  246.     p = &eup_buf[2048];        /* data top */
  247.     size = *((int *)p); p += 4;
  248.     signa = *(p++);
  249.     tempo = *(p++);
  250.  
  251.     SND_eup_loop_set(0);
  252.     SND_eup_tempo_set(tempo);
  253.     SND_eup_play_start(p,size,signa);
  254.  
  255.     macset("EUP",file);
  256. /*****************************
  257.     while ( SND_eup_stat_meas() < 1 );
  258. *****************************/
  259.     eup_time = 500;
  260. }
  261.  
  262. void    CHK_eup(void)
  263. {
  264.     if ( eup_buf != NULL && !SND_eup_stat_flag() ) {
  265.     if ( --eup_time > 0 )
  266.         return;
  267.     SND_eup_play_stop();
  268.     SND_eup_end();
  269.     xclose(eup_buf);
  270.     eup_buf = NULL;
  271.     if ( macval("EUP_REPLAY_FLG") == 0 )
  272.         macset("EUP","");
  273.     else
  274.         MSG_run(prcget("endof_eup"));
  275.     }
  276. }
  277.  
  278. void    END_eup(void)
  279. {
  280.     if ( eup_buf != NULL ) {
  281.     SND_eup_play_stop();
  282.     SND_eup_end();
  283.     xclose(eup_buf);
  284.     eup_buf = NULL;
  285.     macset("EUP","");
  286.     }
  287. }
  288.