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

  1. /* ------------------------------------------------------------------------ */
  2. /*  @@ Source Documentation                            *** TC Version ***   */
  3. /*                                                                          */
  4. /*  Copyright (c) Creative Technology Pte Ltd, 1991. All rights reserved.   */
  5. /*                                                                          */
  6. /*   TITLE       : DEMOVMR.C                                                */
  7. /*                                                                          */
  8. /*   DESCRIPTION :                                                          */
  9. /*       This program demostrates how to perform voice recording using the  */
  10. /*       CT-VOICE.DRV driver. The voice recording is using the Conventional */
  11. /*       memory method. The recording can be terminated by pressing ESC.    */
  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.  
  23. #include  <io.h>
  24. #include  <string.h>
  25. #include  <stdlib.h>
  26. #include  <dos.h>
  27. #include  <bios.h>
  28. #include  <stdio.h>
  29. #include  <fcntl.h>
  30.  
  31. #include  <sbc.h>
  32. #include  <sbcvoice.h>
  33.  
  34. #define   FPSEG(fp) (*((unsigned far *)&(fp)+1))
  35. #define   FPOFF(fp) (*((unsigned far *)&(fp)))
  36.  
  37. #include  "loaddrv.c"
  38.  
  39.  
  40. /* Function Prototypes */
  41. SaveVoiceFile(char *,char far *) ;
  42.  
  43.  
  44. main()
  45. {
  46.     extern  char far * near voice_drv;
  47.  
  48.  
  49.     /* Retrieve the BLASTER environment settings */
  50.     if ( ! GetEnvSetting() )
  51.     {
  52.         if (sbc_check_card() & 4)
  53.         {
  54.             if (sbc_test_int())
  55.             {
  56.                 if (sbc_test_dma() >= 0)
  57.                 {
  58.                     if ((voice_drv = LoadDriver("CT-VOICE.DRV")) != 0)
  59.                     {
  60.                         if (!ctvm_init())
  61.                         {
  62.                             ctvm_speaker(0) ;
  63.  
  64.                             RecordVoice("TEMP.VOC");
  65.  
  66.                             ctvm_terminate() ;
  67.                         }
  68.                     }
  69.                 }
  70.                 else
  71.                     printf("Error on DMA channel.\n");
  72.             }
  73.             else
  74.                 printf("Error on interrupt.\n");
  75.         }
  76.         else
  77.             printf("Sound Blaster Card not found or wrong I/O settings.\n") ;
  78.     }
  79.     else
  80.         printf("BLASTER environment not set or incomplete or invalid.\n");
  81. }
  82.  
  83.  
  84. /* ------------------------------------------------------------------------ */
  85. /*  @@ Usage                                                                */
  86. /*                                                                          */
  87. /*   RecordVoice (char *szFilename)                                         */
  88. /*                                                                          */
  89. /*   DESCRIPTION:                                                           */
  90. /*       Record voice into a file with filename specified.                  */
  91. /*                                                                          */
  92. /*   ENTRY:                                                                 */
  93. /*       szFileName :- File to be recorded.                                 */
  94. /*                                                                          */
  95. /*   EXIT:                                                                  */
  96. /*       None                                                               */
  97. /*                                                                          */
  98. /* ------------------------------------------------------------------------ */
  99.  
  100. RecordVoice (char *szFilename)
  101. {
  102.     unsigned  wSeg ;
  103.     char      far *lpVoiceBuf ;
  104.     long      lBufSize = 0x20000L;
  105.  
  106.  
  107.     if (allocmem((unsigned)((lBufSize+15) >> 4),&wSeg) == -1)
  108.     {
  109.         FPSEG(lpVoiceBuf) = wSeg ;
  110.         FPOFF(lpVoiceBuf) = 0 ;
  111.  
  112.         if (Recording(lpVoiceBuf,lBufSize))
  113.             SaveVoiceFile(szFilename,lpVoiceBuf) ;
  114.  
  115.         freemem(FPSEG(lpVoiceBuf)) ;
  116.      }
  117.      else
  118.         printf("Memory allocation error.\n");
  119. }
  120.  
  121.  
  122. /* ------------------------------------------------------------------------ */
  123. /*  @@ Usage                                                                */
  124. /*                                                                          */
  125. /*   Recording (char far *lpBuf, long lBufSize)                             */
  126. /*                                                                          */
  127. /*   DESCRIPTION:                                                           */
  128. /*       Start recording voice.                                             */
  129. /*                                                                          */
  130. /*   ENTRY:                                                                 */
  131. /*       lpBuf :- buffer for voice recording.                               */
  132. /*       lBufSize :- buffer size.                                           */
  133. /*                                                                          */
  134. /*   EXIT:                                                                  */
  135. /*       Non-zero if successful, else returns 0.                            */
  136. /*                                                                          */
  137. /* ------------------------------------------------------------------------ */
  138.  
  139. #pragma loop_opt(off)
  140. Recording (char far *lpBuf,long lBufSize)
  141. {
  142.     int    RetVal = 0 ;
  143.  
  144.  
  145.     ctvm_speaker(0) ;
  146.  
  147.     if (ctvm_input(lpBuf,lBufSize,8000) == NO_ERROR)
  148.     {
  149.         RetVal = 1 ;
  150.         printf("\nStart recording, press ESC key to terminate .....");
  151.  
  152.         while (ct_voice_status)
  153.         {
  154.             if (bioskey(1))
  155.             {
  156.                  /* check for escape key */
  157.                 if (bioskey(0) == 0x11b)
  158.                     ctvm_stop() ;
  159.             }
  160.         }
  161.     }
  162.  
  163.  
  164.     return(RetVal) ;
  165. }
  166. #pragma loop_opt()
  167.  
  168.  
  169. /* ------------------------------------------------------------------------ */
  170. /*  @@ Usage                                                                */
  171. /*                                                                          */
  172. /*   SaveVoiceFile (char *szFilename, char far *lpBuf)                      */
  173. /*                                                                          */
  174. /*   DESCRIPTION:                                                           */
  175. /*       Save recorded voice from memory to file.                           */
  176. /*                                                                          */
  177. /*   ENTRY:                                                                 */
  178. /*       szFilename :- file name to be saved to.                            */
  179. /*       lpBuf :- recorded voice buffer.                                    */
  180. /*                                                                          */
  181. /*   EXIT:                                                                  */
  182. /*       None                                                               */
  183. /*                                                                          */
  184. /* ------------------------------------------------------------------------ */
  185.  
  186. SaveVoiceFile (char *szFilename, char far *lpBuf)
  187. {
  188.     int     Handle ;
  189.     long    lVoiceSize ;
  190.     VOCHDR  stHeader ;
  191.  
  192.  
  193.     strcpy(stHeader.id,"Creative Voice File\x0\0x1A") ;
  194.     stHeader.voice_offset = sizeof(VOCHDR) ;
  195.     stHeader.version = 0x10a ;
  196.     stHeader.check_code = ~stHeader.version + 0x1234 ;
  197.  
  198.     if ( (Handle=_creat(szFilename,0)) == -1 )
  199.         printf("Create %s error.\n",szFilename) ;
  200.     else
  201.     {
  202.         /* write voice header */
  203.         if (WriteToFile(Handle,(char far*)&stHeader,(long)sizeof(VOCHDR)))
  204.         {
  205.             /* get the recorded data block size */
  206.             lVoiceSize = *(lpBuf+3) ;
  207.             lVoiceSize <<= 16 ;
  208.             lVoiceSize += *(unsigned far *)(lpBuf+1) ;
  209.  
  210.             /*    include the block type, block length  */
  211.             /*  and terminator block type             */
  212.  
  213.             lVoiceSize += 5 ;
  214.  
  215.             WriteToFile(Handle,lpBuf,lVoiceSize) ;
  216.         }
  217.  
  218.         _close(Handle) ;
  219.     }
  220. }
  221.  
  222.  
  223. /* ------------------------------------------------------------------------ */
  224. /*  @@ Usage                                                                */
  225. /*                                                                          */
  226. /*   WriteToFile (int Handle, char far *lpBuf, long lSize)                  */
  227. /*                                                                          */
  228. /*   DESCRIPTION:                                                           */
  229. /*       Write data from buffer to file.                                    */
  230. /*                                                                          */
  231. /*   ENTRY:                                                                 */
  232. /*       Handle :- File handle where data to be written to.                 */
  233. /*       lpBuf :- buffer to be written to file.                             */
  234. /*       lSize :- Size to be written to file.                               */
  235. /*                                                                          */
  236. /*   EXIT:                                                                  */
  237. /*       Non-Zero if successful, else returns 0.                            */
  238. /*                                                                          */
  239. /* ------------------------------------------------------------------------ */
  240.  
  241. WriteToFile (int Handle,char far *lpBuf,long lSize)
  242. {
  243.     int         RetVal = 1 ;
  244.     unsigned    wByteToWrite, wByteWritten ;
  245.  
  246.  
  247.     while (lSize)
  248.     {
  249.         wByteToWrite = 0x8000 ;
  250.  
  251.         if (lSize < 0x8000)
  252.             wByteToWrite = (int)lSize ;
  253.  
  254.         if ( DosWrite(Handle,lpBuf,wByteToWrite,&wByteWritten) == 0 )
  255.         {
  256.             printf("Write file error.\n") ;
  257.             RetVal = 0 ;
  258.             break ;
  259.         }
  260.         else
  261.         {
  262.             if (wByteWritten != wByteToWrite)
  263.             {
  264.                 printf("Disk full.\n") ;
  265.                 RetVal = 0 ;
  266.                 break ;
  267.             }
  268.             else
  269.             {
  270.                 if (FPOFF(lpBuf) < 0x8000)
  271.                     FPOFF(lpBuf) += wByteWritten ;
  272.                 else
  273.                 {
  274.                     FPOFF(lpBuf) += wByteWritten ;
  275.                     FPSEG(lpBuf) += 0x1000 ;
  276.                 }
  277.  
  278.                 lSize -= wByteWritten ;
  279.             }
  280.         }
  281.     }
  282.  
  283.  
  284.     return(RetVal) ;
  285. }
  286.  
  287.  
  288. /* ------------------------------------------------------------------------ */
  289. /*  @@ Usage                                                                */
  290. /*                                                                          */
  291. /*   DosWrite (int Handle, char far *Buffer,                                */
  292. /*             unsigned wLen, unsigned *lpByteRead)                         */
  293. /*                                                                          */
  294. /*   DESCRIPTION:                                                           */
  295. /*       Write data from file using DOS interrupt 0x21 function 0x40.       */
  296. /*                                                                          */
  297. /*   ENTRY:                                                                 */
  298. /*       Handle :- File handle to read.                                     */
  299. /*       Buffer :- Buffer to write to.                                      */
  300. /*       wLen   :- Number of byte to read.                                  */
  301. /*       wByteWritten :- pointer to number of byte actually written.        */
  302. /*                                                                          */
  303. /*   EXIT:                                                                  */
  304. /*       Byte written if successful, else returns 0.                        */
  305. /*                                                                          */
  306. /* ------------------------------------------------------------------------ */
  307.  
  308. DosWrite (int Handle, char far *Buffer, unsigned wLen, unsigned *wByteWritten)
  309. {
  310.     union REGS regs;
  311.     struct SREGS segregs;
  312.  
  313.  
  314.     regs.h.ah = 0x40 ;
  315.     regs.x.bx = Handle;
  316.     regs.x.dx = FPOFF(Buffer);
  317.     regs.x.cx = wLen;
  318.     segregs.ds = FPSEG(Buffer);
  319.  
  320.     intdosx(®s, ®s, &segregs);
  321.  
  322.     if(regs.x.cflag)    /* error */
  323.         *wByteWritten = 0;
  324.     else
  325.         *wByteWritten = regs.x.ax ;
  326.  
  327.  
  328.     return(*wByteWritten);
  329. }
  330.  
  331.