home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_09 / snddemo.c < prev    next >
Encoding:
Text File  |  1994-08-27  |  12.0 KB  |  534 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <io.h>
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <dos.h>
  9. #include <bios.h>
  10. #include <fcntl.h>
  11. #include <memory.h>
  12. #include <malloc.h>
  13. #include <math.h>
  14. #include <string.h>
  15.  
  16. #include "graph3.h"  // include our graphics stuff
  17. #include "graph4.h"
  18.  
  19. // D E F I N E S /////////////////////////////////////////////////////////////
  20.  
  21. #define NUM_SOUNDS   5  // number of sounds in this demo
  22.  
  23. #define DUCK_SOUND   0  // take a guess Einstein!
  24. #define BEE_SOUND    1
  25. #define CAT_SOUND    2
  26. #define FROG_SOUND   3
  27. #define EXIT_SOUND   4
  28.  
  29. // G L O B A L S /////////////////////////////////////////////////////////////
  30.  
  31. char far *driver_ptr;      // pointer to the sound driver ct-voice.drv
  32. unsigned version;          // holds the version of the driver
  33. char far *data_ptr;        // pointer to sound file
  34. unsigned ct_voice_status;  // global status variable
  35.  
  36. char far *sounds[NUM_SOUNDS];   // array that holds pointers to sound files
  37. unsigned char lengths[NUM_SOUNDS]; // the length of each sound
  38. pcx_picture animals_pcx;           // the PCX image that is loaded
  39.  
  40. // F U N C T I O N S /////////////////////////////////////////////////////////
  41.  
  42. void Voc_Get_Version(void)
  43. {
  44. // this function prints out the version of the driver
  45.  
  46. _asm
  47.    {
  48.    mov bx,0          ; function 0 get version number
  49.    call driver_ptr   ; call the driver
  50.    mov version,ax    ; store in version variable
  51.  
  52.    } // end inline asm
  53.  
  54. printf("\nVersion of Driver = %X.0%X",((version>>8) & 0x00ff), (version&0x00ff));
  55.  
  56. } // end Voc_Get_Version
  57.  
  58. //////////////////////////////////////////////////////////////////////////////
  59.  
  60. int Voc_Init_Driver(void)
  61. {
  62. // this function intializes the driver and returns the status
  63.  
  64. int status;
  65.  
  66. _asm
  67.    {
  68.    mov bx,3          ; function 3 initialize the driver
  69.    call driver_ptr   ; call the driver
  70.    mov status,ax     ; store in version variable
  71.  
  72.    } // end inline asm
  73.  
  74. // return status
  75.  
  76. printf("\nDriver Initialized");
  77.  
  78. return(status);
  79.  
  80. } // end Voc_Init_Driver
  81.  
  82. //////////////////////////////////////////////////////////////////////////////
  83.  
  84. int Voc_Terminate_Driver(void)
  85. {
  86. // this function terminates the driver and de-installes it from memory
  87.  
  88. _asm
  89.    {
  90.    mov bx,9          ; function 9 terminate the driver
  91.    call driver_ptr   ; call the driver
  92.  
  93.    } // end inline asm
  94.  
  95. // de-allocate memory
  96.  
  97. _dos_freemem(_FP_SEG(driver_ptr));
  98.  
  99. printf("\nDriver Terminated");
  100.  
  101. } // end Voc_Terminate_Driver
  102.  
  103. //////////////////////////////////////////////////////////////////////////////
  104.  
  105. void Voc_Set_Port(unsigned int port)
  106. {
  107.  
  108. // this function sets the I/O port of the sound blaster
  109.  
  110. _asm
  111.    {
  112.    mov bx,1          ; function 1 set port address
  113.    mov ax,port       ; move the port number into ax
  114.    call driver_ptr   ; call the driver
  115.  
  116.    } // end inline asm
  117.  
  118. } // Voc_Set_Port
  119.  
  120. //////////////////////////////////////////////////////////////////////////////
  121.  
  122. void Voc_Set_Speaker(unsigned int on)
  123. {
  124.  
  125. // this function turns the speaker on or off
  126.  
  127. _asm
  128.    {
  129.    mov bx,4          ; function 4 turn speaker on or off
  130.    mov ax,on         ; move the on/off flag into ax
  131.    call driver_ptr   ; call the driver
  132.  
  133.    } // end inline asm
  134.  
  135. } // Voc_Set_Speaker
  136.  
  137. /////////////////////////////////////////////////////////////////////////////
  138.  
  139. int Voc_Play_Sound(unsigned char far *addr,unsigned char header_length)
  140. {
  141. // this function plays a pre-loaded VOC file
  142.  
  143. unsigned segm,offm;
  144.  
  145. segm = _FP_SEG(addr);
  146. offm = _FP_OFF(addr) + header_length;
  147.  
  148. _asm
  149.    {
  150.    mov bx,6          ; function 6 play a VOC file
  151.    mov ax, segm      ; can only mov a register into segment so we need this
  152.    mov es, ax        ; es gets the segment
  153.    mov di, offm      ; di gets offset
  154.    call driver_ptr   ; call the driver
  155.  
  156.    } // end inline asm
  157.  
  158. } // end Voc_Play_Sound
  159.  
  160. /////////////////////////////////////////////////////////////////////////////
  161.  
  162. int Voc_Stop_Sound(void)
  163. {
  164. // this function will stop a currently playing sound
  165.  
  166. _asm
  167.    {
  168.    mov bx,8          ; function 8 stop a sound
  169.    call driver_ptr   ; call the driver
  170.  
  171.    } // end inline asm
  172.  
  173. } // end Voc_Stop_Sound
  174.  
  175. /////////////////////////////////////////////////////////////////////////////
  176.  
  177. int Voc_Pause_Sound(void)
  178. {
  179. // this function pauses a sound that is playing
  180.  
  181. _asm
  182.    {
  183.    mov bx,10         ; function 10 pause a sound
  184.    call driver_ptr   ; call the driver
  185.  
  186.    } // end inline asm
  187.  
  188. } // end Voc_Pause_Sound
  189.  
  190. /////////////////////////////////////////////////////////////////////////////
  191.  
  192. int Voc_Continue_Sound(void)
  193. {
  194. // this function continues a sound that had been paused
  195.  
  196. _asm
  197.    {
  198.    mov bx,11         ; function 11 continue play
  199.    call driver_ptr   ; call the driver
  200.  
  201.    } // end inline asm
  202.  
  203. } // end Voc_Continue_Sound
  204.  
  205. /////////////////////////////////////////////////////////////////////////////
  206.  
  207. int Voc_Break_Sound(void)
  208. {
  209. // this function breaks a sound that is in a loop
  210.  
  211. _asm
  212.    {
  213.    mov bx,12         ; function 12 break loop
  214.    call driver_ptr   ; call the driver
  215.  
  216.    } // end inline asm
  217.  
  218. } // end Voc_Break_Sound
  219.  
  220. /////////////////////////////////////////////////////////////////////////////
  221.  
  222. void Voc_Set_IRQ(unsigned int irq)
  223. {
  224. // this function sets the irq for the sound blaster
  225.  
  226. _asm
  227.    {
  228.    mov bx,2          ; function 2 set the irq number
  229.    mov ax,irq        ; move the irq number into ax
  230.    call driver_ptr   ; call the driver
  231.  
  232.    } // end inline asm
  233.  
  234. } // Voc_Set_IRQ
  235.  
  236. //////////////////////////////////////////////////////////////////////////////
  237.  
  238. void Voc_Set_Status_Addr(char far *status)
  239. {
  240.  
  241. // this function sets the address of the global status word in the driver
  242.  
  243. unsigned segm,offm;
  244.  
  245. // exract the segment and offset of status variable
  246.  
  247. segm = _FP_SEG(status);
  248. offm = _FP_OFF(status);
  249.  
  250. _asm
  251.    {
  252.    mov bx,5          ; function 5 set status varible address
  253.    mov es, segm      ; es gets the segment
  254.    mov di, offm      ; di gets offset
  255.    call driver_ptr   ; call the driver
  256.  
  257.    } // end inline asm
  258.  
  259. } // Voc_Set_Status_Addr
  260.  
  261. //////////////////////////////////////////////////////////////////////////////
  262.  
  263. void Voc_Load_Driver(void)
  264. {
  265. // this functions loads the ct-voice.drv which allows digitized effects
  266. // to be played
  267.  
  268. int driver_handle;
  269.  
  270. unsigned segment,num_para,bytes_read;
  271.  
  272. // open the driver file
  273.  
  274. _dos_open("CT-VOICE.DRV", _O_RDONLY, &driver_handle);
  275.  
  276. // allocate the memory
  277.  
  278. num_para = 1 + (filelength(driver_handle))/16;
  279.  
  280. _dos_allocmem(num_para,&segment);
  281.  
  282. // point driver pointer to data area
  283.  
  284. _FP_SEG(driver_ptr) = segment;
  285. _FP_OFF(driver_ptr) = 0;
  286.  
  287. // load in the driver code
  288.  
  289. data_ptr = driver_ptr;
  290.  
  291. do
  292.  {
  293.  _dos_read(driver_handle,data_ptr, 0x4000, &bytes_read);
  294.  data_ptr += bytes_read;
  295.  
  296.  } while(bytes_read==0x4000);
  297.  
  298. // close the file
  299.  
  300. _dos_close(driver_handle);
  301.  
  302. } // end Voc_Load_Driver
  303.  
  304. //////////////////////////////////////////////////////////////////////////////
  305.  
  306. char far *Voc_Load_Sound(char *filename, unsigned char *header_length)
  307. {
  308. // thid function loads a sound off disk into memory and points returns
  309. // a pointer to the data
  310.  
  311. char far *temp_ptr;
  312. char far *data_ptr;
  313.  
  314. unsigned int sum;
  315.  
  316. int sound_handle;
  317.  
  318. unsigned segment,num_para,bytes_read;
  319.  
  320. // open the sound file
  321.  
  322. _dos_open(filename, _O_RDONLY, &sound_handle);
  323.  
  324. // allocate the memory
  325.  
  326. num_para = 1 + (filelength(sound_handle))/16;
  327.  
  328. _dos_allocmem(num_para,&segment);
  329.  
  330. // point data pointer to allocated data area
  331.  
  332. _FP_SEG(data_ptr) = segment;
  333. _FP_OFF(data_ptr) = 0;
  334.  
  335. // load in the sound data
  336.  
  337. temp_ptr = data_ptr;
  338.  
  339. do
  340.  {
  341.  _dos_read(sound_handle,temp_ptr, 0x4000, &bytes_read);
  342.  temp_ptr += bytes_read;
  343.  
  344.  sum+=bytes_read;
  345.  
  346.  } while(bytes_read==0x4000);
  347.  
  348. // make sure it's a voc file
  349.  
  350.    if ((data_ptr[0] != 'C') || (data_ptr[1] != 'r'))
  351.       {
  352.       printf("\n%s is not a voc file!",filename);
  353.       _dos_freemem(_FP_SEG(data_ptr));
  354.       return(0);
  355.  
  356.       } // end if voc file
  357.  
  358.    *header_length = (unsigned char)data_ptr[20];
  359.  
  360. // close the file
  361.  
  362. _dos_close(sound_handle);
  363.  
  364. return(data_ptr);
  365.  
  366. } // end Voc_Load_Sound
  367.  
  368. //////////////////////////////////////////////////////////////////////////////
  369.  
  370. void Voc_Unload_Sound(char far *sound_ptr)
  371. {
  372.  
  373. // this functions deletes the sound from memory
  374.  
  375. _dos_freemem(_FP_SEG(sound_ptr));
  376.  
  377. } // end Voc_Unload_Sound
  378.  
  379.  
  380. // M A I N ///////////////////////////////////////////////////////////////////
  381.  
  382. void main(void)
  383. {
  384.  
  385. int done=0;  // exit flag
  386. long index;  // loop counter
  387.  
  388. // SECTION 1 /////////////////////////////////////////////////////////////////
  389.  
  390. // load the sound driver ct-voice.drv into memory
  391.  
  392. Voc_Load_Driver();
  393.  
  394. // set the I/O port of the sound card
  395.  
  396. Voc_Set_Port(0x220);
  397.  
  398. // set the DMA channel used by the sound card
  399.  
  400. Voc_Set_IRQ(5);
  401.  
  402. // initialize the driver
  403.  
  404. Voc_Init_Driver();
  405.  
  406. // print out the version of the driver
  407.  
  408. Voc_Get_Version();
  409.  
  410. // set the global status variable in the driver
  411.  
  412. Voc_Set_Status_Addr((char far *)&ct_voice_status);
  413.  
  414. // load in the sounds
  415.  
  416. sounds[DUCK_SOUND ] = Voc_Load_Sound("duck.voc" ,&lengths[DUCK_SOUND ]);
  417. sounds[BEE_SOUND  ] = Voc_Load_Sound("bee.voc" , &lengths[BEE_SOUND  ]);
  418. sounds[CAT_SOUND  ] = Voc_Load_Sound("cat.voc" , &lengths[CAT_SOUND  ]);
  419. sounds[FROG_SOUND ] = Voc_Load_Sound("frog.voc", &lengths[FROG_SOUND ]);
  420. sounds[EXIT_SOUND ] = Voc_Load_Sound("exit.voc", &lengths[EXIT_SOUND ]);
  421.  
  422. Voc_Set_Speaker(1);
  423.  
  424.  
  425. // SECTION 2 ///////////////////////////////////////////////////////////////
  426.  
  427. // set video mode to 320x200 256 color mode
  428.  
  429. Set_Video_Mode(VGA256);
  430.  
  431. // load in background
  432.  
  433. PCX_Init((pcx_picture_ptr)&animals_pcx);
  434.  
  435. PCX_Load("animals.pcx", (pcx_picture_ptr)&animals_pcx,1);
  436.  
  437. PCX_Show_Buffer((pcx_picture_ptr)&animals_pcx);
  438.  
  439. PCX_Delete((pcx_picture_ptr)&animals_pcx);
  440.  
  441. // main event loop, let user select a sound to play, note you can interupt
  442. // a sound that is currenlty playing
  443.  
  444. // SECTION 3 ///////////////////////////////////////////////////////////////
  445.  
  446. while(!done)
  447.      {
  448.  
  449.      // has user hit a key
  450.  
  451.      if (kbhit())
  452.      {
  453.      // get the key
  454.  
  455.      switch (getch())
  456.             {
  457.             case '1':
  458.                   {
  459.                   Voc_Stop_Sound();
  460.                   Voc_Play_Sound(sounds[DUCK_SOUND] , lengths[DUCK_SOUND]);
  461.                   } break;
  462.  
  463.             case '2':
  464.                   {
  465.                   Voc_Stop_Sound();
  466.                   Voc_Play_Sound(sounds[BEE_SOUND] , lengths[BEE_SOUND]);
  467.                   } break;
  468.  
  469.             case '3':
  470.                   {
  471.                   Voc_Stop_Sound();
  472.                   Voc_Play_Sound(sounds[CAT_SOUND] , lengths[CAT_SOUND]);
  473.                   } break;
  474.  
  475.             case '4':
  476.                   {
  477.                   Voc_Stop_Sound();
  478.                   Voc_Play_Sound(sounds[FROG_SOUND] , lengths[FROG_SOUND]);
  479.                   } break;
  480.  
  481.             case 'q':
  482.                   {
  483.                   done = 1;
  484.                   } break;
  485.  
  486.             default:break;
  487.  
  488.  
  489.             } // end switch
  490.  
  491.      } // end if keyboard hit
  492.  
  493.      } // end while
  494.  
  495. // SECTION 4 ///////////////////////////////////////////////////////////////
  496.  
  497. // say goodbye
  498.  
  499. Voc_Play_Sound(sounds[EXIT_SOUND] , lengths[EXIT_SOUND]); ;
  500.  
  501. // wait for end sequence to stop, the status variable will be -1 when
  502. // a sound is playing and 0 otherwise
  503.  
  504. while(ct_voice_status!=0) {}
  505.  
  506. // turn the speaker off
  507.  
  508. Voc_Set_Speaker(0);
  509.  
  510. // SECTION 5 ///////////////////////////////////////////////////////////////
  511.  
  512. // unload sounds
  513.  
  514. Voc_Unload_Sound(sounds[DUCK_SOUND ]);
  515. Voc_Unload_Sound(sounds[BEE_SOUND  ]);
  516. Voc_Unload_Sound(sounds[CAT_SOUND  ]);
  517. Voc_Unload_Sound(sounds[FROG_SOUND ]);
  518. Voc_Unload_Sound(sounds[EXIT_SOUND ]);
  519.  
  520. // unload the sound driver from memory
  521.  
  522. Voc_Terminate_Driver();
  523.  
  524. // disolve the screen...in one line I might add!
  525.  
  526. for (index=0; index<=300000; index++,Plot_Pixel_Fast(rand()%320, rand()%200, 0));
  527.  
  528. // go back to text mode
  529.  
  530. Set_Video_Mode(TEXT_MODE);
  531.  
  532. } // end main
  533.  
  534.