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

  1.  
  2. // DIGIDEMO.C - Digital sound demo
  3.  
  4. // I N C L U D E S ///////////////////////////////////////////////////////////
  5.  
  6. #include <io.h>
  7. #include <conio.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <dos.h>
  11. #include <bios.h>
  12. #include <fcntl.h>
  13. #include <memory.h>
  14. #include <malloc.h>
  15. #include <math.h>
  16. #include <string.h>
  17.  
  18. // include our the sound library
  19.  
  20. #include "black3.h"
  21. #include "black6.h"
  22.  
  23. // D E F I N E S /////////////////////////////////////////////////////////////
  24.  
  25. // defines for the phrases
  26.  
  27. #define SOUND_WHAT       0
  28. #define SOUND_WRONG      1
  29. #define SOUND_CORRECT    2
  30. #define SOUND_PLUS       3
  31. #define SOUND_EQUAL      4
  32.  
  33. // G L O B A L S /////////////////////////////////////////////////////////////
  34.  
  35. _sound ones[11];   // this will hold the digital samples for 1..9
  36. _sound teens[11];  // this will hold the digital samples for 11-19
  37. _sound tens[11];   // this will hold the digital samples for 10,20,30,40...100
  38. _sound phrases[6]; // this will hold the phrases
  39.  
  40. // F U N C T I O N S /////////////////////////////////////////////////////////
  41.  
  42. void Say_Number(int number)
  43. {
  44. // this function uses digitized samples to construct whole numbers
  45. // note that the teens i.e. numbers from 11-19 have to be spoken as a
  46. // special case and can't be concatenated as the numbers 20-100 can!
  47.  
  48. int ones_place,
  49.     tens_place;
  50.  
  51. // compute place values, use simple logic, more complex logic can be
  52. // derived that uses MODING and so forth, but better to see whats going
  53. // on. However, the main point of this is to see the digital library
  54. // in action, so focus on that aspect.
  55.  
  56. // test for 0..9
  57.  
  58. if (number<10)
  59.    {
  60.    Sound_Play((_sound_ptr)&ones[number]);
  61.    while(Sound_Status()==1);
  62.    return;
  63.    } // end if 0..9
  64.  
  65. // test for 11..19
  66.  
  67. if (number >= 11 && number <= 19)
  68.    {
  69.    Sound_Play((_sound_ptr)&teens[number-11]);
  70.    while(Sound_Status()==1);
  71.    return;
  72.    } // end if 11..19
  73.  
  74. // now break number down into tens and ones
  75.  
  76. tens_place = number / 10;
  77. ones_place = number % 10;
  78.  
  79. // first say tens place
  80.  
  81. Sound_Play((_sound_ptr)&tens[tens_place-1]);
  82. while(Sound_Status()==1);
  83.  
  84. // now say ones place (if any)
  85.  
  86. if (ones_place)
  87.    {
  88.    Sound_Play((_sound_ptr)&ones[ones_place]);
  89.    while(Sound_Status()==1);
  90.     } // end if
  91.  
  92. } // end Say_Number
  93.  
  94. // M A I N ///////////////////////////////////////////////////////////////////
  95.  
  96. void main(int argc, char **argv)
  97. {
  98.  
  99. char filename[16]; // used to build up filename
  100.  
  101. int number,    // loop variables
  102.     number_1,
  103.      number_2,
  104.     answer,
  105.     done=0;    // exit flag
  106.  
  107. float num_problems=0,  // used to track performance of player
  108.       num_correct=0;
  109.  
  110. // load in the samples for the ones
  111.  
  112. for (number=1; number<=9; number++)
  113.     {
  114.     // build the filename
  115.  
  116.      sprintf(filename,"N%d.VOC",number);
  117.     printf("\nLoading file %s",filename);
  118.  
  119.     // load the sound
  120.  
  121.     Sound_Load(filename,(_sound_ptr)&ones[number],1);
  122.  
  123.     } // end for ones
  124.  
  125. // load in the samples for the teens
  126.  
  127. for (number=11; number<=19; number++)
  128.     {
  129.      // build the filename
  130.  
  131.     sprintf(filename,"N%d.VOC",number);
  132.     printf("\nLoading file %s",filename);
  133.  
  134.     // load the sound
  135.  
  136.     Sound_Load(filename,(_sound_ptr)&teens[number-11],1);
  137.  
  138.     } // end for teens
  139.  
  140. // load in the samples for the tens
  141.  
  142. for (number=10; number<=100; number+=10)
  143.     {
  144.     // build the filename
  145.  
  146.     sprintf(filename,"N%d.VOC",number);
  147.     printf("\nLoading file %s",filename);
  148.  
  149.     // load the sound
  150.  
  151.      Sound_Load(filename,(_sound_ptr)&tens[-1+number/10],1);
  152.  
  153.     } // end for tens
  154.  
  155. // load the phrases
  156.  
  157. printf("\nLoading the phrases...");
  158.  
  159. Sound_Load("what.voc",   (_sound_ptr)&phrases[SOUND_WHAT     ],1);
  160. Sound_Load("wrong.voc",  (_sound_ptr)&phrases[SOUND_WRONG    ],1);
  161. Sound_Load("correct.voc",(_sound_ptr)&phrases[SOUND_CORRECT  ],1);
  162. Sound_Load("plus.voc",   (_sound_ptr)&phrases[SOUND_PLUS     ],1);
  163. Sound_Load("equal.voc",  (_sound_ptr)&phrases[SOUND_EQUAL    ],1);
  164.  
  165. // main event loop, note this one is not real-time since it waits
  166. // for user input!
  167.  
  168. printf("\n                       S P E A K    N    A D D  !!!");
  169. printf("\n\n\nThis program will test your skills of addition while demonstrating");
  170. printf("\nthe digital sound channel in action!");
  171.  
  172. printf("\n\nJust answer each addition problem. To exit type in 0.\n");
  173. printf("\n\nPress any key to begin!!!\n\n");
  174.  
  175. getch();
  176.  
  177. // the main event loop
  178.  
  179. while(!done)
  180.      {
  181.  
  182.      // select two random numbers to add, but make sure their sum is less
  183.      // than or equal to 100
  184.  
  185.      do
  186.        {
  187.  
  188.        number_1 = 1 + rand()%99;
  189.        number_2 = 1 + rand()%99;
  190.  
  191.        } while (number_1 + number_2 > 100);
  192.  
  193.      // ask user question
  194.  
  195.      printf("\nWhat is ");
  196.  
  197.      Sound_Play((_sound_ptr)&phrases[SOUND_WHAT]);
  198.      while(Sound_Status()==1);
  199.  
  200.      printf("%d",number_1);
  201.      Say_Number(number_1);
  202.      printf(" + ");
  203.  
  204.      Sound_Play((_sound_ptr)&phrases[SOUND_PLUS]);
  205.      while(Sound_Status()==1);
  206.  
  207.      Time_Delay(15);
  208.  
  209.      printf("%d",number_2);
  210.      Say_Number(number_2);
  211.      printf(" = ?",number_2);
  212.  
  213.       Sound_Play((_sound_ptr)&phrases[SOUND_EQUAL]);
  214.       while(Sound_Status()==1);
  215.  
  216.      scanf("%d",&answer);
  217.  
  218.      // make sure user isn't exiting
  219.  
  220.      if (answer!=0)
  221.         {
  222.         num_problems++;
  223.  
  224.         // test if user is correct
  225.  
  226.         if (answer == (number_1 + number_2))
  227.            {
  228.               Sound_Play((_sound_ptr)&phrases[SOUND_CORRECT]);
  229.               while(Sound_Status()==1);
  230.               num_correct++;
  231.               }
  232.           else
  233.            {
  234.            // oops wrong answer!
  235.  
  236.            Sound_Play((_sound_ptr)&phrases[SOUND_WRONG]);
  237.            while(Sound_Status()==1);
  238.  
  239.            Say_Number(number_1+number_2);
  240.            Time_Delay(25);
  241.  
  242.            } // end else wrong
  243.  
  244.         } // end if
  245.      else
  246.         {
  247.         done = 1;
  248.         } // end else user is exiting
  249.  
  250.      } // end main event loop
  251.  
  252. // unload all the sounds
  253.  
  254. for (number=1; number<=9; number++)
  255.     Sound_Unload((_sound_ptr)&ones[number]);
  256.  
  257. for (number=11; number<=19; number++)
  258.     Sound_Unload((_sound_ptr)&teens[number]);
  259.  
  260. for (number=10; number<=100; number+=10)
  261.      Sound_Unload((_sound_ptr)&tens[-1+number/10]);
  262.  
  263. for (number=0; number<=4; number++)
  264.      Sound_Unload((_sound_ptr)&phrases[number]);
  265.  
  266. // tell user his/her statistics
  267.  
  268. printf("\nYou got %.0f percent of the problems correct.",100*num_correct/num_problems);
  269.  
  270. } // end main
  271.  
  272.