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

  1. /****************************************************************************
  2. *
  3. *                   Digital Sound Interface Kit (DSIK)
  4. *                            Version 2.00
  5. *
  6. *                           by Carlos Hasan
  7. *
  8. * Filename:     example6.c
  9. * Version:      Revision 1.0
  10. *
  11. * Language:     WATCOM C
  12. * Environment:  IBM PC (DOS/4GW)
  13. *
  14. * Description:  Play a huge 8-bit mono RAW sample file from disk.
  15. *
  16. * Note:         You will hear small clicks between frames, because there
  17. *               is a small delay when you start playing a new a sample.
  18. *               Increasing the buffer length (see BUFLEN below) and polling
  19. *               the sound system more frequently will decrease the amount
  20. *               of clicks/cracks in the sound.
  21. *               This sound system was not designed to do this sort of
  22. *               things. Anyway, I think that would be useful to show
  23. *               how it can be done.
  24. *
  25. ****************************************************************************/
  26.  
  27. #include <io.h>
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <malloc.h>
  33. #include <conio.h>
  34. #include "audio.h"
  35. #include "import.h"
  36. #include "timer.h"
  37.  
  38. /* 8-bit SIGNED mono raw sample file at 11kHz */
  39. /* NOTE: you can use M2DSM.EXE to convert VOC/WAV/IFF to RAW files */
  40. #define SMPPATH "TEST.RAW"
  41. #define SMPFREQ 11025
  42.  
  43. /* chunk length in bytes (should be less than 256 Kb) */
  44. #define BUFLEN  0x4000
  45.  
  46.  
  47. int main(void)
  48. {
  49.     SoundCard SC;
  50.     Sample *S[2];
  51.     int Handle;
  52.     long pos,len,filelen;
  53.  
  54.     dRegisterDrivers();
  55.     if (dLoadSetup(&SC,"SETUP.CFG")) {
  56.         printf("Please run SETUP.EXE to configure.\n");
  57.         exit(EXIT_FAILURE);
  58.     }
  59.     if (dInit(&SC)) {
  60.         printf("Error initializing the sound system.\n");
  61.         exit(EXIT_FAILURE);
  62.     }
  63.     atexit((void(*)(void))dDone);
  64.     dInitTimer();
  65.     atexit((void(*)(void))dDoneTimer);
  66.     dStartTimer((void(*)(void))dPoll,TICKS(200));
  67.     dSetupVoices(1,255);
  68.     dSetVoiceBalance(0,PAN_MIDDLE);
  69.  
  70.     /* initialize sample structures */
  71.     S[0] = S[1] = NULL;
  72.  
  73.     /* open huge sample file */
  74.     if ((Handle = open(SMPPATH,O_RDONLY|O_BINARY)) < 0) {
  75.         printf("Error opening sample file: %s\n",SMPPATH);
  76.         exit(EXIT_FAILURE);
  77.     }
  78.     if ((filelen = filelength(Handle)) < 0) {
  79.         printf("Error getting file length.\n");
  80.         exit(EXIT_FAILURE);
  81.     }
  82.  
  83.     printf("Playing %ld samples from file %s\n", filelen, SMPPATH);
  84.     for (pos = 0; filelen > 0; pos ^= 1) {
  85.         /* print progress indicator on screen */
  86.         putch('.');
  87.         /* release sample structure (if previously allocated) */
  88.         if (S[pos]) {
  89.             dFreeSample(S[pos]);
  90.             S[pos] = NULL;
  91.         }
  92.         /* get length of the next chunk of samples */
  93.         len = (filelen > BUFLEN) ? BUFLEN : filelen;
  94.         filelen -= len;
  95.         /* load chunk of samples and set default frequency */
  96.         if (!(S[pos] = dImportSampleFile(Handle,len,FORM_RAW))) {
  97.             printf("Error (%03d) loading chunk of samples: %s.\n",
  98.                 dError, dErrorMsg[dError]);
  99.             exit(EXIT_FAILURE);
  100.         }
  101.         S[pos]->Rate = SMPFREQ;
  102.         /* wait until previous chunk of samples finishes... */
  103.         while (dGetVoiceStatus(0) == PS_PLAYING) dPoll();
  104.         /* start playing new chunk of samples */
  105.         dPlayVoice(0,S[pos]);
  106.     }
  107.     /* wait until the last chunk of samples finishes */
  108.     while (dGetVoiceStatus(0) == PS_PLAYING) continue;
  109.  
  110.     /* release sample buffers */
  111.     if (S[0]) dFreeSample(S[0]);
  112.     if (S[1]) dFreeSample(S[1]);
  113.  
  114.     close(Handle);
  115.     return 0;
  116. }
  117.