home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / tracker-4.13.lha / tracker / Arch / PCux / audio.c next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  3.4 KB  |  160 lines

  1. /* pcux/audio.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4. /* minor mods for pl14 by Mike Battersby */
  5. /* Modified from soundblaster_audio.c by Hannu Savolainen */
  6. /* hsavolai@cs.helsinki.fi */
  7.  
  8. #include "defs.h"
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include "extern.h"
  12.  
  13. #ifdef PL_14
  14. /* For some reason my pl14 kernel had no sys/soundcard.h (???) */
  15. #include "/usr/src/linux/drivers/sound/soundcard.h"
  16. #else
  17. #ifndef __FreeBSD__
  18. /*    This should be sys/soundcard.h    */
  19. #include <sys/soundcard.h>
  20. #else
  21. #include <machine/soundcard.h>
  22. #endif
  23. #endif
  24.  
  25. ID("$Id: audio.c,v 4.2 1995/02/01 16:43:47 espie Exp $")
  26.  
  27. LOCAL unsigned char *buffer;    /* buffer for ready-to-play samples */
  28. LOCAL short *buffer16;            /* Sure this isn't unsigned short ? */
  29. LOCAL int buf_index;               /* index conflicts with index(3) */
  30. LOCAL int buf_max;
  31. LOCAL int audio;               /* /dev/dsp */
  32.  
  33.  
  34. LOCAL int stereo;                    /* are we playing stereo or not ? */
  35. /* 256th of primary/secondary source for that side. */
  36. LOCAL int primary=512, secondary=0;
  37. LOCAL int dsp_samplesize = 16; /* must be 8 or 16 */
  38.  
  39. void set_mix(percent)
  40. int percent;
  41.     {
  42.     percent *= 256;
  43.     percent /= 100;
  44.     primary = percent;
  45.     secondary = 512 - percent;
  46.     }
  47.  
  48. int open_audio(f, s)
  49. int f;
  50. int s;
  51.     {
  52.     audio = open("/dev/dsp", O_WRONLY, 0);
  53.     if (audio == -1)
  54.         end_all("Error opening audio device");
  55.  
  56.     if (ioctl(audio, SNDCTL_DSP_SAMPLESIZE, &dsp_samplesize) == -1)
  57.         end_all("Error setting sample size");
  58.  
  59.     stereo = s;
  60.  
  61.     if (ioctl(audio, SNDCTL_DSP_STEREO, &stereo) == -1)
  62.         end_all("Error setting stereo/mono");
  63.  
  64.     if (f==0) f = 44100;
  65.  
  66.     if (ioctl(audio, SNDCTL_DSP_SPEED, &f) == -1)
  67.         end_all("Error setting frequency");
  68.  
  69.     if (ioctl (audio, SNDCTL_DSP_GETBLKSIZE, &buf_max) == -1)
  70.       end_all("Error getting buffsize");
  71.  
  72.     buffer = malloc(buf_max);
  73.     buffer16 = (short *)buffer;
  74.     buf_index = 0;
  75.  
  76.     return f;
  77.     }
  78.  
  79. LOCAL void actually_flush_buffer()
  80.     {
  81.     int l,i;
  82.  
  83.     l = sizeof(*buffer) * buf_index;
  84.     if (dsp_samplesize !=8) l *= 2;
  85.     write(audio, buffer, l);
  86.  
  87.     buf_index = 0;
  88.     }
  89.  
  90. void output_samples(left, right)
  91. int left, right;
  92.     {
  93.     if (dsp_samplesize != 8)    /* Cool! 16 bits/sample */
  94.     {
  95.         if (stereo)
  96.             {
  97.             if (buf_index * 2 >= buf_max - 1) 
  98.                actually_flush_buffer();
  99.  
  100.             buffer16[buf_index++] = 
  101.                ((left*primary + right*secondary) / 65536);
  102.             buffer16[buf_index++] = 
  103.                ((right*primary + left*secondary) / 65536);
  104.             }
  105.         else
  106.             {
  107.             if (buf_index * 2 >= buf_max) 
  108.                actually_flush_buffer();
  109.             buffer16[buf_index++] = (left + right)/256;
  110.             }
  111.     }
  112.     else
  113.     {
  114.         if (stereo)
  115.             {
  116.             if (buf_index >= buf_max - 1) 
  117.                 actually_flush_buffer();
  118.             buffer[buf_index++] = ((left*primary + right*secondary) >> 24)
  119.                  + 128;
  120.             buffer[buf_index++] = ((right*primary + left*secondary) >> 24)
  121.                  + 128;
  122.             }
  123.         else
  124.             {
  125.             if (buf_index >= buf_max) 
  126.                 actually_flush_buffer();
  127.             buffer[buf_index++] = ((left + right) >> 16) + 128;
  128.             }
  129.         }
  130.     }
  131.  
  132. void flush_buffer()
  133.     {    /* Dummy version */
  134.     }
  135.  
  136. /*
  137.  * Closing the Linux sound device waits for all pending samples to play.
  138.  */
  139. void close_audio()
  140.     {
  141.     actually_flush_buffer();
  142.     close(audio);
  143.     free(buffer);
  144.     }
  145.  
  146. /* dummy system calls, to patch ? */
  147. void set_synchro(s)
  148.     {
  149.     }
  150.  
  151. int update_frequency()
  152.     {
  153.     return 0;
  154.     }
  155.  
  156. void discard_buffer()
  157.     {
  158.     }
  159.  
  160.