home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Emulation / Atari800 / sound.c < prev    next >
C/C++ Source or Header  |  1997-12-17  |  3KB  |  133 lines

  1. #include "config.h"
  2.  
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <sys/ioctl.h>
  6. #ifdef VOXWARE
  7. #include <sys/soundcard.h>
  8. #endif
  9. #include "pokey.h"
  10. #include "pokey11.h"
  11.  
  12. static char *rcsid = "$Id: sound.c,v 1.2 1997/04/18 22:23:38 david Exp $";
  13.  
  14. /* 0002 = 2 Fragments */
  15. /* 0007 = means each fragment is 2^2 or 128 bytes */
  16. /* See voxware docs in /usr/src/linux/drivers/sound for more info */
  17.  
  18. #define FRAG_SPEC 0x0002000a
  19.  
  20. #define FALSE 0
  21. #define TRUE 1
  22.  
  23. #ifdef VOXWARE
  24. static char dsp_buffer[44100];
  25. static int sndbufsize;
  26. static int dsp_fd;
  27. #endif
  28.  
  29. static sound_enabled = TRUE;
  30.  
  31. static int dsp_sample_rate;
  32. static int dsp_sample_rate_divisor = 35;
  33.  
  34. void Voxware_Initialise (int *argc, char *argv[])
  35. {
  36.   int i, j;
  37.  
  38.   for (i=j=1;i<*argc;i++)
  39.     {
  40.       if (strcmp(argv[i],"-sound") == 0)
  41.         sound_enabled = TRUE;
  42.       else if (strcmp(argv[i],"-nosound") == 0)
  43.         sound_enabled = FALSE;
  44.       else if (strcmp(argv[i],"-dsp_divisor") == 0)
  45.         sscanf (argv[++i],"%d",&dsp_sample_rate_divisor);
  46.       else
  47.         argv[j++] = argv[i];
  48.     }
  49.  
  50.   *argc = j;
  51.  
  52. #ifdef VOXWARE
  53.   if (sound_enabled)
  54.     {
  55.       int channel;
  56.       int dspbits;
  57.       unsigned int formats;
  58.       int tmp;
  59.  
  60.       dsp_fd = open ("/dev/dsp", O_WRONLY, 0777);
  61.       if (dsp_fd == -1)
  62.         {
  63.           perror ("/dev/dsp");
  64.           exit (1);
  65.         }
  66.  
  67.       /*
  68.        * Get sound formats
  69.        */
  70.  
  71.       ioctl (dsp_fd, SNDCTL_DSP_GETFMTS, &formats);
  72.  
  73.       /*
  74.        * Set sound of sound fragment to special?
  75.        */
  76.  
  77.       tmp = FRAG_SPEC;
  78.       ioctl (dsp_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
  79.  
  80.       /*
  81.        * Get preferred buffer size
  82.        */
  83.  
  84.       ioctl (dsp_fd, SNDCTL_DSP_GETBLKSIZE, &sndbufsize);
  85.  
  86.       /*
  87.        * Set to 8bit sound
  88.        */
  89.  
  90.       dspbits = 8;
  91.       ioctl (dsp_fd, SNDCTL_DSP_SAMPLESIZE, &dspbits);
  92.       ioctl (dsp_fd, SOUND_PCM_READ_BITS, &dspbits);
  93.  
  94.       /*
  95.        * Set sample rate
  96.        */
  97.  
  98.       ioctl (dsp_fd, SNDCTL_DSP_SPEED, &dsp_sample_rate);
  99.       ioctl (dsp_fd, SOUND_PCM_READ_RATE, &dsp_sample_rate);
  100.  
  101.       Pokey_sound_init (FREQ_17_EXACT, dsp_sample_rate);
  102.     }
  103. #else
  104.   dsp_sample_rate=1;
  105.   Pokey_sound_init(FREQ_17_EXACT,dsp_sample_rate);
  106. #endif
  107. }
  108.  
  109. void Voxware_Exit (void)
  110. {
  111. #ifdef VOXWARE
  112.   if (sound_enabled)
  113.     close (dsp_fd);
  114. #endif
  115. }
  116.  
  117. void Voxware_UpdateSound (void)
  118. {
  119. #ifdef VOXWARE
  120.   if (sound_enabled)
  121.     {
  122.       sndbufsize = dsp_sample_rate / dsp_sample_rate_divisor;
  123.  
  124.       Pokey_process (dsp_buffer, sndbufsize);
  125.       /*
  126.        * Send sound buffer to DSP device
  127.        */
  128.  
  129.       write (dsp_fd, dsp_buffer, sndbufsize);
  130.     }
  131. #endif
  132. }
  133.