home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame Game Cube 1: DOS / aztechhalloffamegamecubedisc1- / batlemm / sound.c < prev    next >
C/C++ Source or Header  |  1995-02-19  |  10KB  |  452 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <io.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <dos.h>
  8. #include <bios.h>
  9. #include <fcntl.h>
  10.  
  11. // G L O B A L S  ////////////////////////////////////////////////////////////
  12.  
  13. char _far *driver_ptr;
  14. unsigned version;
  15. char _huge *data_ptr;
  16. unsigned ct_voice_status;
  17.  
  18. // F U N C T I O N S /////////////////////////////////////////////////////////
  19.  
  20. void Voc_Get_Version(void)
  21. {
  22. // gets the version of the driver and prints it out
  23.  
  24. _asm
  25.    {
  26.    mov bx,0          ; function 0 get version number
  27.    call driver_ptr   ; call the driver
  28.    mov version,ax    ; store in version variable
  29.  
  30.    } // end inline asm
  31.  
  32. printf("\nVersion of Driver = %X.0%X",((version>>8) & 0x00ff), (version&0x00ff));
  33.  
  34. } // end Voc_Get_Version
  35.  
  36. //////////////////////////////////////////////////////////////////////////////
  37.  
  38. int Voc_Init_Driver(void)
  39. {
  40. // intialize the driver and return the status
  41.  
  42. int status;
  43.  
  44. _asm
  45.    {
  46.    mov bx,3          ; function 3 initialize the driver
  47.    call driver_ptr   ; call the driver
  48.    mov status,ax     ; store in version variable
  49.  
  50.    } // end inline asm
  51.  
  52. // return status
  53.  
  54. printf("\nDriver Initialized");
  55.  
  56. return(status);
  57.  
  58. } // end Voc_Init_Driver
  59.  
  60. //////////////////////////////////////////////////////////////////////////////
  61.  
  62. int Voc_Terminate_Driver(void)
  63. {
  64. // terminate the driver
  65.  
  66. _asm
  67.    {
  68.    mov bx,9          ; function 9 terminate the driver
  69.    call driver_ptr   ; call the driver
  70.  
  71.    } // end inline asm
  72.  
  73. // de-allocate memory
  74.  
  75. _dos_freemem(FP_SEG(driver_ptr));
  76.  
  77. printf("\nDriver Terminated");
  78.  
  79. } // end Voc_Terminate_Driver
  80.  
  81. //////////////////////////////////////////////////////////////////////////////
  82.  
  83. void Voc_Set_Port(unsigned port)
  84. {
  85.  
  86. // sets the I/O port of the sound blaster
  87.  
  88. _asm
  89.    {
  90.    mov bx,1          ; function 1 set port address
  91.    mov ax,port       ; move the port number into ax
  92.    call driver_ptr   ; call the driver
  93.  
  94.    } // end inline asm
  95.  
  96. } // Voc_Set_Port
  97.  
  98. //////////////////////////////////////////////////////////////////////////////
  99.  
  100. void Voc_Set_Speaker(unsigned on)
  101. {
  102.  
  103. // turns the speaker on or off
  104.  
  105. _asm
  106.    {
  107.    mov bx,4          ; function 4 turn speaker on or off
  108.    mov ax,on         ; move the on/off flag into ax
  109.    call driver_ptr   ; call the driver
  110.  
  111.    } // end inline asm
  112.  
  113. } // Voc_Set_Speaker
  114.  
  115. /////////////////////////////////////////////////////////////////////////////
  116.  
  117. int Voc_Play_Sound(unsigned char far *addr,unsigned char header_length)
  118. {
  119. // plays a pre-loaded VOC file
  120.  
  121. unsigned segm,offm;
  122.  
  123.  
  124. segm = FP_SEG(addr);
  125. offm = FP_OFF(addr) + header_length;
  126.  
  127. _asm
  128.    {
  129.    mov bx,6          ; function 6 play a VOC file
  130.    mov ax, segm      ; can only mov a register into segment so we need this
  131.    mov es, ax        ; es gets the segment
  132.    mov di, offm      ; di gets offset
  133.    call driver_ptr   ; call the driver
  134.  
  135.    } // end inline asm
  136.  
  137. } // end Voc_Play_Sound
  138.  
  139. /////////////////////////////////////////////////////////////////////////////
  140.  
  141. int Voc_Stop_Sound(void)
  142. {
  143. // stops a sound that is playing
  144.  
  145. _asm
  146.    {
  147.    mov bx,8          ; function 8 stop a sound
  148.    call driver_ptr   ; call the driver
  149.  
  150.    } // end inline asm
  151.  
  152. } // end Voc_Stop_Sound
  153.  
  154. /////////////////////////////////////////////////////////////////////////////
  155.  
  156. int Voc_Pause_Sound(void)
  157. {
  158. // pauses a sound that is playing
  159.  
  160. _asm
  161.    {
  162.    mov bx,10         ; function 10 pause a sound
  163.    call driver_ptr   ; call the driver
  164.  
  165.    } // end inline asm
  166.  
  167. } // end Voc_Pause_Sound
  168.  
  169. /////////////////////////////////////////////////////////////////////////////
  170.  
  171. int Voc_Continue_Sound(void)
  172. {
  173. // continue a paused sound a sound that is playing
  174.  
  175. _asm
  176.    {
  177.    mov bx,11         ; function 11 continue play
  178.    call driver_ptr   ; call the driver
  179.  
  180.    } // end inline asm
  181.  
  182. } // end Voc_Continue_Sound
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185.  
  186. int Voc_Break_Sound(void)
  187. {
  188. // break a sound loop
  189.  
  190. _asm
  191.    {
  192.    mov bx,12         ; function 12 break loop
  193.    call driver_ptr   ; call the driver
  194.  
  195.    } // end inline asm
  196.  
  197. } // end Voc_Break_Sound
  198.  
  199. /////////////////////////////////////////////////////////////////////////////
  200.  
  201. void Voc_Set_DMA(unsigned dma)
  202. {
  203.  
  204. _asm
  205.    {
  206.    mov bx,2          ; function 2 set DMA interupt number
  207.    mov ax,dma        ; move the dma number into ax
  208.    call driver_ptr   ; call the driver
  209.  
  210.    } // end inline asm
  211.  
  212. } // Voc_Set_DMA
  213.  
  214. //////////////////////////////////////////////////////////////////////////////
  215.  
  216. void Voc_Set_Status_Addr(char _far *status)
  217. {
  218.  
  219. unsigned segm,offm;
  220.  
  221. segm = FP_SEG(status);
  222. offm = FP_OFF(status);
  223.  
  224. _asm
  225.    {
  226.    mov bx,5          ; function 5 set status varible address
  227.    mov es, segm        ; es gets the segment
  228.    mov di, offm      ; di gets offset
  229.    call driver_ptr   ; call the driver
  230.  
  231.    } // end inline asm
  232.  
  233. } // Voc_Set_Status_Addr
  234.  
  235. //////////////////////////////////////////////////////////////////////////////
  236.  
  237. void Voc_Load_Driver(void)
  238. {
  239. // loads the ct-voice.drv
  240.  
  241. int driver_handle;
  242.  
  243. unsigned errno,segment,offset,num_para,bytes_read;
  244.  
  245.  
  246. // open the driver file
  247.  
  248. _dos_open("CT-VOICE.DRV", O_RDONLY, &driver_handle);
  249.  
  250. // allocate the memory
  251.  
  252. num_para = 1 + (filelength(driver_handle))/16;
  253.  
  254. _dos_allocmem(num_para,&segment);
  255.  
  256. // point driver pointer to data area
  257.  
  258. FP_SEG(driver_ptr) = segment;
  259. FP_OFF(driver_ptr) = 0;
  260.  
  261. // load in the driver code
  262.  
  263. data_ptr = driver_ptr;
  264.  
  265. do
  266.  {
  267.  _dos_read(driver_handle,data_ptr, 0x4000, &bytes_read);
  268.  data_ptr += bytes_read;
  269.  
  270.  } while(bytes_read==0x4000);
  271.  
  272. // close the file
  273.  
  274. _dos_close(driver_handle);
  275.  
  276. } // end Voc_Load_Driver
  277.  
  278. //////////////////////////////////////////////////////////////////////////////
  279.  
  280. char far *Voc_Load_Sound(char *filename, unsigned char *header_length)
  281. {
  282. // loads a sound off disk into memory and points a pointer to it
  283.  
  284. char far *temp_ptr;
  285. char far *data_ptr;
  286.  
  287. unsigned int sum;
  288.  
  289. int sound_handle,t;
  290.  
  291. unsigned errno,segment,offset,num_para,bytes_read;
  292.  
  293. // open the sound file
  294.  
  295. _dos_open(filename, O_RDONLY, &sound_handle);
  296.  
  297. // allocate the memory
  298.  
  299. num_para = 1 + (filelength(sound_handle))/16;
  300.  
  301. _dos_allocmem(num_para,&segment);
  302.  
  303. // point data pointer to allocated data area
  304.  
  305. FP_SEG(data_ptr) = segment;
  306. FP_OFF(data_ptr) = 0;
  307.  
  308. // load in the sound data
  309.  
  310. temp_ptr = data_ptr;
  311.  
  312. do
  313.  {
  314.  _dos_read(sound_handle,temp_ptr, 0x4000, &bytes_read);
  315.  temp_ptr += bytes_read;
  316.  
  317.  sum+=bytes_read;
  318.  
  319.  } while(bytes_read==0x4000);
  320.  
  321. // make sure it's a voc file
  322.  
  323.    if ((data_ptr[0] != 'C') || (data_ptr[1] != 'r'))
  324.       {
  325.       printf("\n%s is not a voc file!",filename);
  326.       _dos_freemem(FP_SEG(data_ptr));
  327.       return(0);
  328.  
  329.       } // end if voc file
  330.  
  331.    *header_length = (unsigned char)data_ptr[20];
  332.  
  333.  
  334. // close the file
  335.  
  336. _dos_close(sound_handle);
  337.  
  338. return(data_ptr);
  339.  
  340. } // end Voc_Load_Sound
  341.  
  342. //////////////////////////////////////////////////////////////////////////////
  343.  
  344. void Voc_Unload_Sound(char far *sound_ptr)
  345. {
  346.  
  347. // delete the sound from memory
  348.  
  349. _dos_freemem(FP_SEG(sound_ptr));
  350.  
  351. } // end Voc_Unload_Sound
  352.  
  353.  
  354. //////////////////////////////////////////////////////////////////////////////
  355.  
  356. void test(void)
  357. {
  358.  
  359. char far *sounds[4];
  360. unsigned char lengths[4];
  361. int done=0,sel;
  362.  
  363. Voc_Load_Driver();
  364.  
  365. Voc_Init_Driver();
  366.  
  367. Voc_Set_Port(0x220);
  368.  
  369. Voc_Set_DMA(5);
  370.  
  371. Voc_Get_Version();
  372.  
  373. Voc_Set_Status_Addr((char _far *)&ct_voice_status);
  374.  
  375. // load in sounds
  376.  
  377. sounds[0] = Voc_Load_Sound("woosh.voc" , &lengths[0]);
  378. sounds[1] = Voc_Load_Sound("ouch.voc" ,&lengths[1]);
  379. sounds[2] = Voc_Load_Sound("boom.voc" , &lengths[2]);
  380. sounds[3] = Voc_Load_Sound("vvzz.voc",&lengths[3]);
  381.  
  382. Voc_Set_Speaker(1);
  383.  
  384. // main event loop, let user select a sound to play, note you can interupt
  385. // a sound that is currenlty playing
  386.  
  387. while(!done)
  388.      {
  389.      printf("\n\nSound Demo Menu");
  390.      printf("\n1 - woosh");
  391.      printf("\n2 - ouch");
  392.      printf("\n3 - boom");
  393.      printf("\n4 - Exit");
  394.      printf("\n\nSelect One ? ");
  395.      scanf("%d",&sel);
  396.  
  397.      switch (sel)
  398.             {
  399.             case 1:
  400.                   {
  401.                   Voc_Stop_Sound();
  402.                   Voc_Play_Sound(sounds[0] , lengths[0]);
  403.                   } break;
  404.  
  405.             case 2:
  406.                   {
  407.                   Voc_Stop_Sound();
  408.                   Voc_Play_Sound(sounds[1] , lengths[1]); ;
  409.                   } break;
  410.  
  411.             case 3:
  412.                   {
  413.                   Voc_Stop_Sound();
  414.                   Voc_Play_Sound(sounds[2] , lengths[2]); ;
  415.                   } break;
  416.  
  417.             case 4:
  418.                   {
  419.                   done = 1;
  420.                   } break;
  421.  
  422.             default:
  423.                    {
  424.                    printf("\nFunction %d is not a selection.",sel);
  425.                    } break;
  426.  
  427.             } // end switch
  428.  
  429.      } // end while
  430.  
  431. // terminate
  432.  
  433. Voc_Play_Sound(sounds[3] , lengths[3]); ;
  434.  
  435. // wait for end sequence to stop, the status variable will be -1 when a sound is
  436. // playing and 0 otherwise
  437.  
  438. while(ct_voice_status!=0) {}
  439.  
  440. Voc_Set_Speaker(0);
  441.  
  442. // unload sounds
  443.  
  444. Voc_Unload_Sound(sounds[0]);
  445. Voc_Unload_Sound(sounds[1]);
  446. Voc_Unload_Sound(sounds[2]);
  447. Voc_Unload_Sound(sounds[3]);
  448.  
  449. Voc_Terminate_Driver();
  450.  
  451. } // end main
  452.