home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / MM1 / SOUNDUTILS / tracker.4.6.lzh / TRACKER4.6 / Soundblaster / audio.c next >
Text File  |  1994-11-24  |  3KB  |  137 lines

  1. /* soundblaster/audio.c 
  2.     vi:se ts=3 sw=3:
  3.  */
  4. /* IMPORTANT NOTE: I can't check that this file works.
  5.  */
  6.  
  7. /* $Id: audio.c,v 4.0 1994/01/11 18:01:44 espie Exp espie $
  8.  * $Log: audio.c,v $
  9.  * Revision 4.0  1994/01/11  18:01:44  espie
  10.  * *** empty log message ***
  11.  *
  12.  * Revision 1.2  1993/12/26  18:54:21  Espie
  13.  * Handle errors better.
  14.  *
  15.  * Revision 1.1  1993/12/26  00:55:53  Espie
  16.  * Initial revision
  17.  *
  18.  * Revision 3.4  1993/11/17  15:31:16  espie
  19.  * *** empty log message ***
  20.  *
  21.  * Revision 3.3  1992/12/03  15:00:50  espie
  22.  * restore stty.
  23.  *
  24.  * Revision 3.1  1992/11/19  20:44:47  espie
  25.  * Protracker commands.
  26.  *
  27.  * Revision 3.0  1992/11/18  16:08:05  espie
  28.  * New release.
  29.  *
  30.  * Revision 1.5  1992/11/17  15:38:00  espie
  31.  * Dummy discard_buffer()
  32.  * Added stereo option (kind of).
  33.  */
  34.  
  35. #include <malloc.h>
  36. #include <stdio.h>
  37. #include "defs.h"
  38. #include "extern.h"
  39. #include <i386at/sblast.h>
  40.  
  41. ID("$Id: audio.c,v 4.0 1994/01/11 18:01:44 espie Exp espie $")
  42.  
  43. LOCAL unsigned char *buffer;/* buffer for ready-to-play samples */
  44. LOCAL int buf_index;   /* can't call this index, conflicts with index(3) */
  45. FILE *audio;            /* /dev/sb_dsp */
  46.  
  47. /* are we playing stereo or not ? */
  48. LOCAL int stereo;
  49. /* 256th of primary/secondary source for that side. */
  50. LOCAL int primary, secondary;
  51.  
  52. void set_mix(percent)
  53. int percent;
  54.     {
  55.     percent *= 256;
  56.     percent /= 100;
  57.     primary = percent;
  58.     secondary = 512 - percent;
  59.     }
  60.  
  61. int open_audio(f, s)
  62. int f;
  63. int s;
  64.     {
  65.     audio = fopen("/dev/sb_dsp", "w");
  66.     if (!audio)
  67.         end_all("Error opening audio device");
  68.  
  69.     stereo = s;
  70.     if (ioctl(fileno(audio), DSP_IOCTL_STEREO, stereo) == -1)
  71.         end_all("Error setting stereo/mono");
  72.  
  73.     if (stereo)
  74.         f *= 2;     /* XXX Stereo takes twice the speed */
  75.  
  76.     if (f == 0)
  77.         f = -1;     /* read current frequency from driver */
  78.  
  79.     if (ioctl(fileno(audio), DSP_IOCTL_SPEED, &f) == -1)
  80.         end_all("Error setting frequency");
  81.  
  82.     buffer = malloc(sizeof(SAMPLE) * f);    /* Stereo makes x2 */
  83.     buf_index = 0;
  84.  
  85.     if (stereo)         /* tacky, I know.. */
  86.         return f/ 2;
  87.     else
  88.         return f;
  89.     }
  90.  
  91. void output_samples(left, right)
  92. int left, right;
  93.     {
  94.     if (stereo)
  95.         {
  96.         buffer[buf_index++] = (((left * primary + right * secondary) / 256)
  97.              + (1 << 15)) >> 8;
  98.         buffer[buf_index++] = (((right * primary + left * secondary) / 256)
  99.              + (1 << 15)) >> 8;
  100.         }
  101.     else
  102.         buffer[buf_index++] = (left + right + (1 << 15)) >> 8;
  103.     }
  104.  
  105. void discard_buffer()
  106.     {
  107.     /* not implemented */
  108.     }
  109.  
  110. void flush_buffer()
  111.     {
  112.     if (fwrite(buffer, sizeof(*buffer), buf_index, audio) != buf_index)
  113.         notice("fwrite didn't write all the bytes ?");
  114.     buf_index = 0;
  115.     }
  116.  
  117. /*
  118.  * Closing the BSD SBlast sound device waits for all pending samples to play.
  119.  * I think SysV aborts, so you might have to flush manually with ioctl()
  120.  */
  121. void close_audio()
  122.     {
  123.     fclose(audio);
  124.     free(buffer);
  125.     }
  126.  
  127. int update_frequency()
  128.     {
  129.     /* not implemented */
  130.     return 0;
  131.     }
  132.  
  133. void set_synchro()
  134.     {
  135.     /* not implemented */
  136.     }
  137.