home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_6 / 3digi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-20  |  5.1 KB  |  225 lines

  1.  
  2. // 3DIGI.C - A simple 3-D sound demo using the parallel ports and D/A
  3. // convertors
  4.  
  5. // I N C L U D E S ///////////////////////////////////////////////////////////
  6.  
  7. #include <io.h>
  8. #include <conio.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <dos.h>
  12. #include <bios.h>
  13. #include <fcntl.h>
  14. #include <memory.h>
  15. #include <malloc.h>
  16. #include <math.h>
  17. #include <string.h>
  18.  
  19. #include "black3.h"
  20. #include "black6.h"
  21.  
  22. // D E F I N E S /////////////////////////////////////////////////////////////
  23.  
  24. // the parallel port offsets
  25.  
  26. #define PAR_1_PORT     0
  27. #define PAR_2_PORT     1
  28. #define PAR_3_PORT     2
  29. #define PAR_4_PORT     3
  30.  
  31. // G L O B A L S ////////////////////////////////////////////////////////////
  32.  
  33. // pointer to base parallel port
  34.  
  35. unsigned int far *par_port = (unsigned int far*)0x00000408L;
  36.  
  37. // the parallel ports to be used for right and left
  38.  
  39. int right_3D = PAR_1_PORT,
  40.      left_3D  = PAR_2_PORT;
  41.  
  42. // the amount of power going to each channel (i.e. the volume)
  43.  
  44. float power_3D = .5; // each channel is at 50%, this will range 0..1
  45.  
  46. // F U N C T I O N S ////////////////////////////////////////////////////////
  47.  
  48. void Par_Write(int port,unsigned char data)
  49. {
  50.  
  51. outp(*(par_port+port),data);
  52.  
  53. } // end Par_Write
  54.  
  55. ///////////////////////////////////////////////////////////////////////////////
  56.  
  57. void Sound_Play_3D(_sound_ptr the_sound,float power_right, float power_left,int speed)
  58. {
  59.  
  60. // this function will play a digitized sound through each of the parallel
  61. // ports. Each channels output is controlled by the input powers. Note that
  62. // a local replica of the sound is made so that the attenuated version
  63. // can be computed in real-time without destroying the original
  64.  
  65. unsigned int freq,      // used to slow or speed up playback rate
  66.                  index,     // looping variable
  67.                  size;      // size of sound data
  68.  
  69. unsigned char far *right_buffer; // working buffers
  70. unsigned char far *left_buffer;
  71.  
  72. // create new sound based on right and left power
  73.  
  74. size   = the_sound->SS.sndlen;
  75.  
  76. right_buffer = (unsigned char far *)farmalloc(size);
  77. left_buffer  = (unsigned char far *)farmalloc(size);
  78.  
  79. // create the new sounds, one for each channel
  80.  
  81. for (index=0; index<size; index++)
  82.     {
  83.  
  84.     // compute proper value based on power level
  85.  
  86.     right_buffer[index] =
  87.              (unsigned char)((float)the_sound->SS.sound[index] * power_right);
  88.  
  89.      left_buffer[index] =
  90.                  (unsigned char)((float)the_sound->SS.sound[index] * power_left);
  91.  
  92.     } // end for index
  93.  
  94.       // play the sound
  95.  
  96.       for (index=0; index<size; index++)
  97.             {
  98.  
  99.             // delay a bit to slow frequency down
  100.  
  101.             for (freq=0; freq<speed; freq++)
  102.                  {
  103.                  // write left channel
  104.  
  105.                  Par_Write(left_3D,left_buffer[index]);
  106.  
  107.                  // write right channel
  108.  
  109.                  Par_Write(right_3D,right_buffer[index]);
  110.  
  111.                  } // end time delay frequency control
  112.  
  113.             } // end for index
  114.  
  115. // release the memory
  116.  
  117. farfree((void far *)right_buffer);
  118. farfree((void far *)left_buffer);
  119.  
  120. } // end Sound_Play_3D
  121.  
  122. // M A I N ///////////////////////////////////////////////////////////////////
  123.  
  124. void main(int argc, char **argv)
  125. {
  126.  
  127. unsigned int index,    // looping variable
  128.                  done=0,   // exit flag
  129.                  delay=5;
  130.  
  131. _sound effect_r,effect_l;  // the sound effects
  132.  
  133. float power_delta = .1;
  134.  
  135. // load the sound test sound effect into memory without translation
  136.  
  137. if (!Sound_Load("3dleft.voc",(_sound_ptr)&effect_l,0))
  138.     {
  139.     printf("\nCouldn't load test sound 3DLEFT.VOC");
  140.    return;
  141.  
  142.    } // end if
  143.  
  144. if (!Sound_Load("3dright.voc",(_sound_ptr)&effect_r,0))
  145.    {
  146.    printf("\nCouldn't load test sound 3DRIGHT.VOC");
  147.    return;
  148.  
  149.    } // end if
  150.  
  151.  
  152. // display menu
  153.  
  154. printf("\n3D DIGITAL SOUND DEMO\n");
  155. printf("\nUse the <S> and <F> keys to slow down and speed up the sound.");
  156. printf("\nPress <Q> to exit.\n");
  157.  
  158. // enter event loop
  159.  
  160. while(!done)
  161.       {
  162.  
  163.       // test which size sound source is on
  164.  
  165.      if (power_3D > .5)
  166.         Sound_Play_3D((_sound_ptr)&effect_r,power_3D, (1-power_3D),delay);
  167.      else
  168.         Sound_Play_3D((_sound_ptr)&effect_l,power_3D, (1-power_3D),delay);
  169.  
  170.      // pan sound from right to left left to right
  171.      // make you dizzy!
  172.  
  173.       power_3D+=power_delta;
  174.  
  175.      if (power_3D > 1 || power_3D < 0)
  176.         {
  177.           power_delta=-power_delta;
  178.  
  179.         power_3D+=power_delta;
  180.  
  181.         } // end of reverse pan
  182.  
  183.      // test if user is hitting keyboard
  184.  
  185.      if (kbhit())
  186.         {
  187.         // what does user want to do
  188.  
  189.         switch(getch())
  190.               {
  191.  
  192.               case 'f': // speed up the sound
  193.                    {
  194.                    if (--delay < 1)
  195.                       delay=1;
  196.  
  197.                    } break;
  198.  
  199.               case 's': // slow down the sound
  200.                          {
  201.                    ++delay;
  202.  
  203.                    } break;
  204.  
  205.               case 'q': // exit
  206.                    {
  207.                    done=1;
  208.                    } break;
  209.  
  210.               } // end switch
  211.  
  212.         } // end if kbhit
  213.  
  214.      } // end while
  215.  
  216. // unload the sounds
  217.  
  218. Sound_Unload((_sound_ptr)&effect_l);
  219. Sound_Unload((_sound_ptr)&effect_r);
  220.  
  221. } // end main
  222.  
  223.  
  224.  
  225.