home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / m / m003_1 / sdk_dos.ddi / C / TC / VOICE / DEMOVMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-06  |  10.0 KB  |  265 lines

  1. /* ------------------------------------------------------------------------ */
  2. /*  @@ Source Documentation                            *** TC Version ***   */
  3. /*                                                                          */
  4. /*  Copyright (c) Creative Technology Pte Ltd, 1991. All rights reserved.   */
  5. /*                                                                          */
  6. /*   TITLE       : DEMOVMP.C                                                */
  7. /*                                                                          */
  8. /*   DESCRIPTION :                                                          */
  9. /*       This program demostrates how to perform voice out using the        */
  10. /*       CT-VOICE.DRV driver. The voice out is using the Conventional       */
  11. /*       memory method.                                                     */
  12. /*                                                                          */
  13. /*       The program checks BLASTER environment for the Card settings.      */
  14. /*       It also performs test base on BLASTER environment settings to      */
  15. /*       ensure they are tally with the hardware settings on the Card.      */
  16. /*                                                                          */
  17. /*       Note that the program included the module LOADDRV.C to load        */
  18. /*       the loadable CT-VOICE.DRV into memory.                             */
  19. /*                                                                          */
  20. /* ------------------------------------------------------------------------ */
  21.  
  22. #include  <io.h>
  23. #include  <dos.h>
  24. #include  <bios.h>
  25. #include  <stdio.h>
  26. #include  <fcntl.h>
  27. #include  <string.h>
  28. #include  <stdlib.h>
  29.  
  30. #include  <sbc.h>
  31. #include  <sbcvoice.h>
  32.  
  33. #define   FPSEG(fp) (*((unsigned far *)&(fp)+1))
  34. #define   FPOFF(fp) (*((unsigned far *)&(fp)))
  35.  
  36. #include  "loaddrv.c"
  37.  
  38.  
  39.  
  40. /* Function Prototypes */
  41. char far *LoadFile(char *szFilename) ;
  42.  
  43.  
  44. main()
  45. {
  46.     extern  char far * near voice_drv;
  47.     char    far *lpVoiceBuf ;
  48.  
  49.  
  50.     /* Retrieve the BLASTER environment settings */
  51.     if ( ! GetEnvSetting() )
  52.     {
  53.         if (sbc_check_card() & 4)
  54.         {
  55.             if (sbc_test_int())
  56.             {
  57.                 if (sbc_test_dma() >= 0)
  58.                 {
  59.                     if ((voice_drv = LoadDriver("CT-VOICE.DRV")) != 0)
  60.                     {
  61.                         if (!ctvm_init())
  62.                         {
  63.                             ctvm_speaker(0) ;
  64.  
  65.                             if ( (lpVoiceBuf = LoadFile("DEMO.VOC")) != 0 )
  66.                             {
  67.                                 OutputVoice(lpVoiceBuf) ;
  68.                                 freemem(FPSEG(lpVoiceBuf)) ;
  69.                             }
  70.  
  71.                             ctvm_terminate() ;
  72.                         }
  73.                     }
  74.                 }
  75.                 else
  76.                     printf("Error on DMA channel.\n");
  77.             }
  78.             else
  79.                 printf("Error on interrupt.\n");
  80.         }
  81.         else
  82.             printf("Sound Blaster Card not found or wrong I/O settings.\n") ;
  83.     }
  84.     else
  85.         printf("BLASTER environment not set or incomplete or invalid.\n");
  86. }
  87.  
  88.  
  89. /* ------------------------------------------------------------------------ */
  90. /*  @@ Usage                                                                */
  91. /*                                                                          */
  92. /*   char far *LoadFile (char *szFilename)                                  */
  93. /*                                                                          */
  94. /*   DESCRIPTION:                                                           */
  95. /*       Load file into memory.                                             */
  96. /*                                                                          */
  97. /*   ENTRY:                                                                 */
  98. /*       szFileName :- File to be loaded.                                   */
  99. /*                                                                          */
  100. /*   EXIT:                                                                  */
  101. /*       Pointer to the loaded memory buffer if successfull, else returns   */
  102. /*       NULL pointer.                                                      */
  103. /*                                                                          */
  104. /* ------------------------------------------------------------------------ */
  105.  
  106. char far *LoadFile (char *szFilename)
  107. {
  108.     char        far *lpFileBuf=0, far *lpTmpPtr ;
  109.     int         Handle ;
  110.     unsigned    wByteRead;
  111.     long        lFileSize ;
  112.  
  113.  
  114.     /* open file */
  115.     if ((Handle=_open(szFilename,O_RDONLY)) != -1)
  116.     {
  117.         lFileSize = filelength(Handle) ;
  118.  
  119.         if (allocmem((unsigned)((lFileSize+15) >> 4),&wByteRead) == -1)
  120.         {
  121.             FPSEG(lpFileBuf) = wByteRead ;
  122.             FPOFF(lpFileBuf) = 0 ;
  123.  
  124.             lpTmpPtr = lpFileBuf ;
  125.  
  126.             do
  127.             {
  128.                 if (DosRead(Handle,lpTmpPtr,0x8000,&wByteRead) != 0)
  129.                 {
  130.                     if ( !(FPOFF(lpTmpPtr) += wByteRead) )
  131.                         FPSEG(lpTmpPtr) += 0x1000 ;
  132.                 }
  133.                 else
  134.                 {
  135.                     printf("Load file error.\n");
  136.                     wByteRead = 0 ;
  137.                     lpFileBuf = 0 ;
  138.                     freemem(FPSEG(lpFileBuf)) ;
  139.                 }
  140.  
  141.             } while (wByteRead == 0x8000) ;
  142.         }
  143.         else
  144.             printf("Memory allocation error.\n");
  145.  
  146.         _close(Handle) ;
  147.     }
  148.     else
  149.         printf("Open %s fails.\n",szFilename) ;
  150.  
  151.  
  152.     return(lpFileBuf) ;
  153. }
  154.  
  155.  
  156. /* ------------------------------------------------------------------------ */
  157. /*  @@ Usage                                                                */
  158. /*                                                                          */
  159. /*   int OutputVoice (char far *lpBuf)                                      */
  160. /*                                                                          */
  161. /*   DESCRIPTION:                                                           */
  162. /*       Output voice from a memory buffer. The user is allowed to control  */
  163. /*       the voice output from the keyboard.                                */
  164. /*                                                                          */
  165. /*   ENTRY:                                                                 */
  166. /*       lpBuf :- Memory buffer to be output.                               */
  167. /*                                                                          */
  168. /*   EXIT:                                                                  */
  169. /*       Non-zero if successful, else returns 0                             */
  170. /*                                                                          */
  171. /* ------------------------------------------------------------------------ */
  172.  
  173. #pragma loop_opt (off)
  174. OutputVoice (char far *lpBuf)
  175. {
  176.     unsigned    wKey ;
  177.     int         done = 0;
  178.  
  179.  
  180.     lpBuf += ((VOCHDR far *)lpBuf)->voice_offset ;
  181.  
  182.     /* turn on speaker */
  183.     ctvm_speaker(1) ;
  184.  
  185.     if (ctvm_output(lpBuf) == NO_ERROR)
  186.     {
  187.         done = 1;
  188.  
  189.         /* loop until voice stop */
  190.         while (ct_voice_status)
  191.         {
  192.             if (bioskey(1))
  193.             {
  194.                 if ( (wKey=bioskey(0)) & 0xff )
  195.                 {
  196.                     switch(toupper(wKey & 0xff))
  197.                     {
  198.                         case 0x1b:
  199.                         case 'S' : ctvm_stop() ;
  200.                             break;
  201.                         case 'P' : ctvm_pause() ;
  202.                             break;
  203.                         case 'C' : ctvm_continue() ;
  204.                             break;
  205.                         case 'B' : ctvm_break_loop(1) ;
  206.                             break;
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.     }
  212.  
  213.     /* turn off speaker */
  214.     ctvm_speaker(0) ;
  215.  
  216.  
  217.     return (done);
  218. }
  219. #pragma loop_opt()
  220.  
  221.  
  222.  
  223. /* ------------------------------------------------------------------------ */
  224. /*  @@ Usage                                                                */
  225. /*                                                                          */
  226. /*   DosRead (int Handle, char far *Buffer,                                 */
  227. /*            unsigned wLen, unsigned *lpByteRead)                          */
  228. /*                                                                          */
  229. /*   DESCRIPTION:                                                           */
  230. /*       DOS read function using DOS interrupt 0x21 function 0x3F.          */
  231. /*                                                                          */
  232. /*   ENTRY:                                                                 */
  233. /*       Handle :- File handle to read.                                     */
  234. /*       Buffer :- Buffer to write to.                                      */
  235. /*       wLen   :- Number of byte to read.                                  */
  236. /*       lpByteRead :- pointer to number of byte actually read.             */
  237. /*                                                                          */
  238. /*   EXIT:                                                                  */
  239. /*       Byte read if successful, else returns 0.                           */
  240. /*                                                                          */
  241. /* ------------------------------------------------------------------------ */
  242.  
  243. DosRead (int Handle, char far *Buffer, unsigned wLen, unsigned *wByteRead)
  244. {
  245.     union REGS regs;
  246.     struct SREGS segregs;
  247.  
  248.  
  249.     regs.h.ah = 0x3f ;
  250.     regs.x.bx = Handle;
  251.     regs.x.dx = FPOFF(Buffer);
  252.     regs.x.cx = wLen;
  253.     segregs.ds = FPSEG(Buffer);
  254.  
  255.     intdosx(®s, ®s, &segregs);
  256.  
  257.     if(regs.x.cflag)    /* error */
  258.         *wByteRead = 0;
  259.     else
  260.         *wByteRead = regs.x.ax ;
  261.  
  262.  
  263.     return(*wByteRead);
  264. }
  265.