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 / SBOS2 / audio.c next >
Text File  |  1994-11-24  |  6KB  |  218 lines

  1. /* soundblaster_audio.c */
  2.  
  3. /* modified by David Nichols for this PM MOD player */
  4.  
  5. /* MODIFIED BY Michael Fulbright (MSF) to work with os/2 device driver */
  6.  
  7. /* $Author: steve $
  8.  * $Id: soundblaster_audio.c,v 1.1 1992/06/24 06:24:17 steve Exp steve $
  9.  * $Revision: 1.1 $
  10.  * $Log: soundblaster_audio.c,v $
  11.  * Revision 1.1  1992/06/24  06:24:17  steve
  12.  * Initial revision
  13.  */
  14.  
  15. #define INCL_DOS
  16.  
  17. #include <os2.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include "sbos2_user.h"
  21. #include "defs.h"
  22. #include "extern.h"
  23. #include "song.h"
  24.  
  25. int _Stereo = TRUE;
  26.  
  27. struct sb_mixer_levels sbLevels;
  28. struct sb_mixer_params sbParams;
  29.  
  30. ID("$Id: sbos2_audio.c,v 2.0 1993/11/29 15:31:16 monsuez Exp monsuez $");
  31.  
  32. unsigned char *buffer;        /* buffer for ready-to-play samples */
  33. LOCAL int buf_index;   /* can't call this index, conflicts with index(3) */
  34.  
  35. HFILE hAudio;      /* audio handle */
  36.  
  37. int fixparams = 0;
  38. int filterout;
  39. int filterin;
  40. int filterhi;
  41.  
  42. /* 256th of primary/secondary source for that side. */
  43. static int primary, secondary;
  44.  
  45. void restoreparams()
  46. {
  47.    ULONG parlen, datlen;
  48.  
  49.    if (fixparams)
  50.    {
  51.       parlen = 0;
  52.       datlen = sizeof(struct sb_mixer_params);
  53.       DosDevIOCtl(hAudio, DSP_CAT, MIXER_IOCTL_READ_PARAMS,
  54.              NULL, 0, &parlen, &sbParams, datlen, &datlen);
  55.       sbParams.hifreq_filter = filterhi;
  56.       sbParams.filter_output = filterout;
  57.       sbParams.filter_input = filterin;
  58.       parlen = 0;
  59.       datlen = sizeof(struct sb_mixer_params);
  60.       DosDevIOCtl(hAudio, DSP_CAT, MIXER_IOCTL_SET_PARAMS,
  61.               NULL, 0, &parlen, &sbParams, datlen, &datlen);
  62.    }
  63. }
  64.  
  65. void set_mix (int percent)
  66. {
  67.   percent *= 256;
  68.   percent /= 100;
  69.   primary = percent;
  70.   secondary = 512 - percent;
  71. }
  72.  
  73. int open_audio(int frequency, int DMAbuffersize)
  74. {
  75.   USHORT status, freq;
  76.   USHORT   flag;
  77.   ULONG  datlen, parlen, action, temp;
  78.  
  79.   /* MSF - open SBDSP for output */
  80.   status = DosOpen( "SBDSP$", &hAudio, &action, 0, FILE_NORMAL, FILE_OPEN,
  81.    OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYREADWRITE |
  82.    OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_NOINHERIT |
  83.    OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_FAIL_ON_ERROR, NULL );
  84.  
  85.   if (status != 0) 
  86.     end_all("Error opening audio device SBDSP$");
  87.  
  88.   /* see if we are on a SBREG or SBPRO */
  89.   status = DosOpen( "SBMIX$", &temp, &action, 0, FILE_NORMAL, FILE_OPEN,
  90.    OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE |
  91.    OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_NOINHERIT | 
  92.    OPEN_FLAGS_NO_CACHE, NULL );
  93.  
  94.   if (status !=0) _Stereo=FALSE;
  95.   else
  96.    {
  97.       fixparams = TRUE;
  98.       parlen = 0;
  99.       datlen = sizeof(struct sb_mixer_params);
  100.       DosDevIOCtl(hAudio, DSP_CAT, MIXER_IOCTL_READ_PARAMS,
  101.              NULL, 0, &parlen, &sbParams, datlen, &datlen);
  102.       filterhi = sbParams.hifreq_filter;
  103.       filterout = sbParams.filter_output;
  104.       filterin = sbParams.filter_input;
  105.       sbParams.hifreq_filter = TRUE;
  106.       sbParams.filter_output = FALSE;
  107.       sbParams.filter_input = TRUE;
  108.       parlen = 0;
  109.       datlen = sizeof(struct sb_mixer_params);
  110.       DosDevIOCtl(hAudio, DSP_CAT, MIXER_IOCTL_SET_PARAMS,
  111.                NULL, 0, &parlen, &sbParams, datlen, &datlen);
  112.       datlen=1;
  113.       parlen=0;
  114.       flag=_Stereo;
  115.       status=DosDevIOCtl(hAudio, DSP_CAT, DSP_IOCTL_STEREO,
  116.              NULL, 0, &parlen, &flag, 1, &datlen);
  117.       if (status != 0)
  118.          end_all("Error setting stereo/mono");
  119.       datlen = 1;
  120.       flag = DMAbuffersize * 1024;
  121.       DMAbuffersize = flag;
  122.       status=DosDevIOCtl(hAudio, DSP_CAT, DSP_IOCTL_BUFSIZE,
  123.                     NULL, 0, &parlen, &DMAbuffersize, datlen, &datlen);
  124.       if (status != 0)
  125.           end_all("Error setting DMA buffer size");
  126.    }
  127.  
  128.   if (_Stereo) frequency *= 2;  /* XXX Stereo takes twice the speed */
  129.  
  130.   if (frequency == 0) frequency = -1;  /* read current frequency from driver */
  131.  
  132.   /* set speed */
  133.   datlen=2;
  134.   parlen=0;
  135.   freq = (USHORT) frequency;
  136.   status=DosDevIOCtl(hAudio, DSP_CAT, DSP_IOCTL_SPEED,
  137.              NULL, 0, &parlen, &freq, 2, &datlen);
  138.   frequency=freq;
  139.   if (status!=0)
  140.       end_all("Error setting frequency");
  141.  
  142.   buffer = malloc (sizeof(SAMPLE) * frequency);    /* Stereo makes x2 */
  143.   buf_index = 0;
  144.  
  145.   if (_Stereo) return (frequency / 2);
  146.   else return (frequency);
  147. }
  148.  
  149. void output_samples (int left, int right)
  150. {
  151.   if (_Stereo)
  152.     {
  153.       buffer[buf_index++] = (((left * primary + right * secondary) / 256) + (1 << 15)) >> 8;
  154.       buffer[buf_index++] = (((right * primary + left * secondary) / 256) + (1 << 15)) >> 8;
  155.     }
  156.   else buffer[buf_index++] = (left + right + (1 << 15)) >> 8;
  157. }
  158.  
  159. void discard_buffer()
  160.     {
  161.     /* not implemented */
  162.     }
  163.  
  164. void flush_buffer ()
  165. {
  166.   ULONG numread, status;
  167.  
  168.   status = DosWrite(hAudio, buffer, buf_index, &numread);
  169.    if (status != 0)
  170.    {
  171.       char buf[80];
  172.       sprintf(buf, "Error writing to audio device: %d, tried to write: %d, wrote: %d", status, buf_index, numread);
  173.       end_all(buf);
  174.    }
  175.   if (numread != buf_index)
  176.    {
  177.       char buf[80];
  178.  
  179.       sprintf(buf, "DosWrite mismatch, buf_index: %d, numread: %d", buf_index, numread);
  180.       notice(buf);
  181.    }      
  182.   buf_index = 0;
  183. }
  184.  
  185. void flush_DMA_buffers()
  186. {
  187.   ULONG status, datlen, parlen;
  188.  
  189.   /* now tell device driver to flush out internal buffers */
  190.   parlen=0;
  191.   datlen=0;
  192.   status=DosDevIOCtl(hAudio, DSP_CAT, DSP_IOCTL_FLUSH,
  193.                     NULL, 0, &parlen, NULL, 0, &datlen);
  194.    if (status != 0)
  195.    {
  196.       char buf[80];
  197.  
  198.       sprintf(buf, "Error flushing DMA buffers: %d", status);
  199.       notice(buf);
  200.    }
  201. }
  202.  
  203. void close_audio ()
  204. {
  205.    DosClose(hAudio);
  206. }
  207.  
  208. int update_frequency()
  209.     {
  210.     /* not implemented */
  211.     return 0;
  212.     }
  213.  
  214. void set_synchro(int s)
  215.     {
  216.     /* not implemented */
  217.     }
  218.