home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / dos / prg / dsik205 / dsik.dat / EXAMPLES / EXAMP04.C < prev    next >
C/C++ Source or Header  |  1995-04-10  |  3KB  |  100 lines

  1. /****************************************************************************
  2. *
  3. *                   Digital Sound Interface Kit (DSIK)
  4. *                            Version 2.00
  5. *
  6. *                           by Carlos Hasan
  7. *
  8. * Filename:     example4.c
  9. * Version:      Revision 1.0
  10. *
  11. * Language:     WATCOM C
  12. * Environment:  IBM PC (DOS/4GW)
  13. *
  14. * Description:  Play a sample using the keyboard like a piano.
  15. *
  16. ****************************************************************************/
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <conio.h>
  21. #include <ctype.h>
  22. #include "audio.h"
  23. #include "import.h"
  24.  
  25. #define NUMVOICES   6
  26.  
  27. #define SMPPATH "SAMPLE.WAV"
  28. #define SMPFORM FORM_WAV
  29.  
  30. #define FREQ(p) (((long)MIDCFREQ*MIDCPERIOD)/p)
  31.  
  32. int periodtable[48] =
  33.     /* C   C#  D   D#  E   F   F#  G   G#  A   A#  B */
  34.     { 856,808,762,720,678,640,604,570,538,508,480,453,
  35.       428,404,381,360,339,320,302,285,269,254,240,226,
  36.       214,202,190,180,170,160,151,143,135,127,120,113,
  37.       107,101,95,90,85,80,75,71,67,63,60,56 };
  38.  
  39. char keytable[36] = "ZSXDCVGBHNJMQ2W3ER5T6Y7UI9O0P";
  40.  
  41.  
  42. int main(void)
  43. {
  44.     SoundCard SC;
  45.     Sample *S;
  46.     int i,chan,key;
  47.  
  48.     dRegisterDrivers();
  49.     if (dLoadSetup(&SC,"SETUP.CFG")) {
  50.         printf("Please run SETUP.EXE to configure.\n");
  51.         exit(EXIT_FAILURE);
  52.     }
  53.     if (dInit(&SC)) {
  54.         printf("Error initializing the sound system.\n");
  55.         exit(EXIT_FAILURE);
  56.     }
  57.     atexit((void(*)(void))dDone);
  58.     if (!(S = dImportSample(SMPPATH,SMPFORM))) {
  59.         printf("Error (%03d) loading %s sample file: %s.\n",
  60.             dError, SMPPATH, dErrorMsg[dError]);
  61.         exit(EXIT_FAILURE);
  62.     }
  63.     /* use N-note polyphony */
  64.     dSetupVoices(NUMVOICES,96);
  65.  
  66.     printf("\n");
  67.     printf("     C# D#    F# G# A#    C# D#    F# G# A#    C# D#   \n");
  68.     printf("  │ │ ││ │ │ │ ││ ││ │ │ │ ││ │ │ │ ││ ││ │ │ │ ││ │ │ \n");
  69.     printf("  │ │ ││ │ │ │ ││ ││ │ │ │ ││ │ │ │ ││ ││ │ │ │ ││ │ │ \n");
  70.     printf("  │ │S││D│ │ │G││H││J│ │ │2││3│ │ │5││6││7│ │ │9││0│ │ \n");
  71.     printf("  │ └┬┘└┬┘ │ └┬┘└┬┘└┬┘ │ └┬┘└┬┘ │ └┬┘└┬┘└┬┘ │ └┬┘└┬┘ │ \n");
  72.     printf("  │ Z│ X│ C│ V│ B│ N│ M│ Q│ W│ E│ R│ T│ Y│ U│ I│ O│ P│ \n");
  73.     printf("  └──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┘ \n");
  74.     printf("    C  D  E  F  G  A  B  C  D  E  F  G  A  B  C  D  E  \n");
  75.     printf("\n");
  76.     printf("   Press keys to play the sample and ESC to exit.      \n");
  77.  
  78.     chan = 0;
  79.     do {
  80.         while (!kbhit()) dPoll();
  81.         if ((key = toupper(getch())) != 27) {
  82.             for (i = 0; i < 36; i++) {
  83.                 if (key == keytable[i]) {
  84.                     /* play chord C-E-G */
  85.                     dPlayVoice(chan+0,S);
  86.                     dPlayVoice(chan+1,S);
  87.                     dPlayVoice(chan+2,S);
  88.                     dSetVoiceFreq(chan+0,FREQ(periodtable[i+0]));
  89.                     dSetVoiceFreq(chan+1,FREQ(periodtable[i+4]));
  90.                     dSetVoiceFreq(chan+2,FREQ(periodtable[i+7]));
  91.                     chan = (chan + 3) % NUMVOICES;
  92.                 }
  93.             }
  94.         }
  95.     } while (key != 27);
  96.  
  97.     dFreeSample(S);
  98.     return 0;
  99. }
  100.