home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / m / m003_1 / sdk_dos.ddi / C / TC / MUSIC / DEMOCMF.C next >
Encoding:
C/C++ Source or Header  |  1991-11-06  |  11.7 KB  |  287 lines

  1. /* ------------------------------------------------------------------------ */
  2. /*  @@ Source Documentation                            *** TC Version ***   */
  3. /*                                                                          */
  4. /*  Copyright (c) Creative Technology Pte Ltd, 1991. All rights reserved.   */
  5. /*                                                                          */
  6. /*   TITLE       : DEMOCMF.C                                                */
  7. /*                                                                          */
  8. /*   DESCRIPTION :                                                          */
  9. /*       This program demonstrates how to use the SBFM high level functions */
  10. /*       to play back the music file FFARES.CMF. The user is allowed to     */
  11. /*       control the music output from the keyboard.                        */
  12. /*                                                                          */
  13. /*       Note that the BLASTER environment has to be set and SBFMDRV.COM    */
  14. /*       has to be installed before executing this program.                 */
  15. /*                                                                          */
  16. /* ------------------------------------------------------------------------ */
  17.  
  18. #include  <io.h>
  19. #include  <dos.h>
  20. #include  <bios.h>
  21. #include  <fcntl.h>
  22. #include  <sbcmusic.h>
  23. #include  <sbc.h>
  24.  
  25. #define   FPSEG(fp) (*((unsigned far *)&(fp)+1))
  26. #define   FPOFF(fp) (*((unsigned far *)&(fp)))
  27.  
  28.  
  29. char    far  *lpMusicBuf = 0 ;
  30. char    cTranspose = 0 ;
  31.  
  32. main()
  33. {
  34.     unsigned    wVersion;
  35.  
  36.  
  37.     if ( sbfm_init() )
  38.     {
  39.         wVersion = sbfm_version() ;
  40.  
  41.         printf("Driver version %d.%02d\n",wVersion >> 8,wVersion & 255) ;
  42.  
  43.         PlayCmfFile("FFARES.CMF") ;
  44.  
  45.         sbfm_terminate() ;
  46.     }
  47.     else
  48.         printf("SBFMDRV not installed or FM Driver initialization error.\n") ;
  49. }
  50.  
  51.  
  52. /* ------------------------------------------------------------------------ */
  53. /*  @@ Usage                                                                */
  54. /*                                                                          */
  55. /*   PlayCmfFile (char *szFilename)                                         */
  56. /*                                                                          */
  57. /*   DESCRIPTION:                                                           */
  58. /*       Play a CMF file and wait for music to end.                         */
  59. /*                                                                          */
  60. /*   ENTRY:                                                                 */
  61. /*       szFileName :- Music file to be played.                             */
  62. /*                                                                          */
  63. /*   EXIT:                                                                  */
  64. /*       None.                                                              */
  65. /*                                                                          */
  66. /* ------------------------------------------------------------------------ */
  67.  
  68. PlayCmfFile (char *szFilename)
  69. {
  70.     if ( LoadFile(szFilename) )
  71.     {
  72.         StartMusic() ;
  73.         WaitMusicEnd() ;
  74.     }
  75. }
  76.  
  77.  
  78. /* ------------------------------------------------------------------------ */
  79. /*  @@ Usage                                                                */
  80. /*                                                                          */
  81. /*   LoadFile (char *szFilename)                                            */
  82. /*                                                                          */
  83. /*   DESCRIPTION:                                                           */
  84. /*       Load file into memory. The Global variable lpMusicBuf is used to   */
  85. /*       point to the loaded buffer.                                        */
  86. /*                                                                          */
  87. /*   ENTRY:                                                                 */
  88. /*       szFileName :- File to be loaded.                                   */
  89. /*                                                                          */
  90. /*   EXIT:                                                                  */
  91. /*       Non-Zero if successful, else returns 0.                            */
  92. /*                                                                          */
  93. /* ------------------------------------------------------------------------ */
  94.  
  95. LoadFile (char *szFilename)
  96. {
  97.     char        far *lpTmpPtr ;
  98.     int         Handle ;
  99.     unsigned    wByteRead, RetVal = 0 ;
  100.     long        lFileSize ;
  101.  
  102.  
  103.     /* set the default file mode to binary mode */
  104.     _fmode = O_BINARY;
  105.  
  106.     /* open file */
  107.     if ( (Handle=_open(szFilename,O_RDONLY)) != -1 )
  108.     {
  109.         lFileSize = filelength(Handle) ;
  110.  
  111.         if ( allocmem((unsigned)((lFileSize+15) >> 4),&wByteRead) == -1 )
  112.         {
  113.             FPSEG(lpMusicBuf) = wByteRead ;
  114.             FPOFF(lpMusicBuf) = 0 ;
  115.  
  116.             lpTmpPtr = lpMusicBuf ;
  117.  
  118.             RetVal = 1;
  119.  
  120.             do
  121.             {
  122.                 if ( DosRead(Handle,lpTmpPtr,0x8000,&wByteRead))
  123.                 {
  124.                     if ( !(FPOFF(lpTmpPtr) += wByteRead) )
  125.                         FPSEG(lpTmpPtr) += 0x1000 ;
  126.                 }
  127.                 else
  128.                 {
  129.                     printf("Load file error.\n");
  130.                     wByteRead = 0 ;
  131.                     RetVal = 0 ;
  132.                     freemem(FPSEG(lpMusicBuf)) ;
  133.                 }
  134.  
  135.             } while (wByteRead == 0x8000) ;
  136.         }
  137.         else
  138.             printf("Memory allocation error.\n");
  139.  
  140.         _close(Handle) ;
  141.     }
  142.     else
  143.         printf("Open %s fails.\n",szFilename) ;
  144.  
  145.  
  146.     return(RetVal) ;
  147. }
  148.  
  149.  
  150. /* ------------------------------------------------------------------------ */
  151. /*  @@ Usage                                                                */
  152. /*                                                                          */
  153. /*   StartMusic (void)                                                      */
  154. /*                                                                          */
  155. /*   DESCRIPTION:                                                           */
  156. /*       Retrieves music information from the CMF music file header and     */
  157. /*       starts playing music.                                              */
  158. /*                                                                          */
  159. /*   ENTRY:                                                                 */
  160. /*       None.                                                              */
  161. /*                                                                          */
  162. /*   EXIT:                                                                  */
  163. /*       None.                                                              */
  164. /*                                                                          */
  165. /* ------------------------------------------------------------------------ */
  166.  
  167. StartMusic (void)
  168. {
  169.     char    far *lpInstPtr ;
  170.     char    far *lpMusicPtr ;
  171.     int     Timer0Freq ;
  172.  
  173.  
  174.     /* get instrument block and music block address */
  175.     lpInstPtr = lpMusicBuf + *(int far*)(lpMusicBuf + 6) ;
  176.     lpMusicPtr = lpMusicBuf + *(int far*)(lpMusicBuf + 8) ;
  177.  
  178.     sbfm_reset() ;
  179.  
  180.     /* set song speed */
  181.     Timer0Freq = (int)(1193180L / (*(int far*)(lpMusicBuf + 12))) ;
  182.     sbfm_song_speed(Timer0Freq) ;
  183.  
  184.     /* set instrument table */
  185.     if (FPOFF(lpInstPtr))
  186.         sbfm_instrument(lpInstPtr,*(lpMusicBuf+0x24)) ;
  187.  
  188.     sbfm_play_music(lpMusicPtr) ;
  189. }
  190.  
  191.  
  192. /* ------------------------------------------------------------------------ */
  193. /*  @@ Usage                                                                */
  194. /*                                                                          */
  195. /*   WaitMusicEnd (void)                                                    */
  196. /*                                                                          */
  197. /*   DESCRIPTION:                                                           */
  198. /*       Control the music output from keyboard.                            */
  199. /*                                                                          */
  200. /*   ENTRY:                                                                 */
  201. /*       None.                                                              */
  202. /*                                                                          */
  203. /*   EXIT:                                                                  */
  204. /*       None.                                                              */
  205. /*                                                                          */
  206. /* ------------------------------------------------------------------------ */
  207.  
  208. #pragma loop_opt(off)
  209. WaitMusicEnd (void)
  210. {
  211.     unsigned     wKey, wStatus ;
  212.  
  213.  
  214.     while( (wStatus = sbfm_read_status()) != 0 )
  215.     {
  216.         if (wStatus != 255)
  217.             printf("Status: %c - %d\n",wStatus,wStatus) ;
  218.  
  219.         if (bioskey(1))
  220.         {
  221.             if ((wKey=bioskey(0)) & 0xff)
  222.                 wKey = toupper(wKey & 255) ;
  223.  
  224.             switch(wKey)
  225.             {
  226.                 case 0x1b   : sbfm_stop_music() ;
  227.                               break ;
  228.                 case 0x4b00 : sbfm_transpose(--cTranspose) ;
  229.                               printf("Transpose: %d\n",cTranspose) ;
  230.                               break ;
  231.                 case 0x4d00 : sbfm_transpose(++cTranspose) ;
  232.                               printf("Transpose: %d\n",cTranspose) ;
  233.                               break ;
  234.                 case 'P'    : sbfm_pause_music() ;
  235.                               break ;
  236.                 case 'C'    : sbfm_resume_music() ;
  237.                               break ;
  238.             }
  239.         }
  240.     }
  241. }
  242. #pragma loop_opt()
  243.  
  244.  
  245. /* ------------------------------------------------------------------------ */
  246. /*  @@ Usage                                                                */
  247. /*                                                                          */
  248. /*   DosRead (int Handle, char far *Buffer,                                 */
  249. /*            unsigned wLen, unsigned *lpByteRead)                          */
  250. /*                                                                          */
  251. /*   DESCRIPTION:                                                           */
  252. /*       DOS read function using DOS interrupt 0x21 function 0x3F.          */
  253. /*                                                                          */
  254. /*   ENTRY:                                                                 */
  255. /*       Handle :- File handle to read.                                     */
  256. /*       Buffer :- Buffer to write to.                                      */
  257. /*       wLen   :- Number of byte to read.                                  */
  258. /*       lpByteRead :- pointer to number of byte actually read.             */
  259. /*                                                                          */
  260. /*   EXIT:                                                                  */
  261. /*       Byte read if successful, else returns 0.                           */
  262. /*                                                                          */
  263. /* ------------------------------------------------------------------------ */
  264.  
  265. DosRead (int Handle, char far *Buffer, unsigned wLen, unsigned *wByteRead)
  266. {
  267.     union REGS regs;
  268.     struct SREGS segregs;
  269.  
  270.  
  271.     regs.h.ah = 0x3f ;
  272.     regs.x.bx = Handle;
  273.     regs.x.dx = FPOFF(Buffer);
  274.     regs.x.cx = wLen;
  275.     segregs.ds = FPSEG(Buffer);
  276.  
  277.     intdosx(®s, ®s, &segregs);
  278.  
  279.     if(regs.x.cflag)    /* error */
  280.         *wByteRead = 0;
  281.     else
  282.         *wByteRead = regs.x.ax ;
  283.  
  284.  
  285.     return(*wByteRead);
  286. }
  287.