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 / Sgi / audio.c next >
Text File  |  1994-11-24  |  4KB  |  181 lines

  1. /* sgi/audio.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: audio.c,v 4.0 1994/01/11 18:01:04 espie Exp espie $
  6.  * $Log: audio.c,v $
  7.  * Revision 4.0  1994/01/11  18:01:04  espie
  8.  * Changed name.
  9.  *
  10.  * Revision 1.1  1993/12/26  00:55:53  Espie
  11.  * Initial revision
  12.  *
  13.  * Revision 3.8  1993/12/04  16:12:50  espie
  14.  * BOOL -> boolean.
  15.  *
  16.  * Revision 3.7  1993/11/11  20:00:03  espie
  17.  * Amiga support.
  18.  *
  19.  * Revision 3.6  1993/07/14  16:33:41  espie
  20.  * Added stuff.
  21.  *
  22.  * Revision 3.5  1993/05/09  14:06:03  espie
  23.  * Corrected mix problem.
  24.  *
  25.  * Revision 3.4  1992/11/27  10:29:00  espie
  26.  * General cleanup
  27.  *
  28.  * Revision 3.3  1992/11/24  10:51:19  espie
  29.  * Added pseudo discardbuffer.
  30.  *
  31.  * Revision 3.2  1992/11/22  17:20:01  espie
  32.  * Checks for finetune ?
  33.  *
  34.  * Revision 3.1  1992/11/19  20:44:47  espie
  35.  * Protracker commands.
  36.  *
  37.  * Revision 3.0  1992/11/18  16:08:05  espie
  38.  * New release.
  39.  *
  40.  * Revision 2.11  1992/11/17  15:38:00  espie
  41.  * Dummy discard_buffer()
  42.  * Changed sync_audio value again.
  43.  * Added synchro for dump.
  44.  * Bug fix: must ask new frequency after we tried to set it to get it
  45.  * rounded up.
  46.  * Added stereo option (kind of).
  47.  * Separated mix/stereo stuff.
  48.  * Checked buffer size.
  49.  * Added possibility to get back to MONO for the sgi.
  50.  * Added stereo capabilities to the indigo version.
  51.  * Ask the frequency to the audio device.
  52.  * Corrected bug: when closing audio,
  53.  * we now wait for the samples queue to be empty.
  54.  */
  55.  
  56. #include <audio.h>
  57. #include <malloc.h>
  58. #include <stdio.h>
  59. #include "defs.h"
  60. #include "extern.h"
  61.  
  62. XT int sginap(long ticks);
  63.      
  64. ID("$Id: audio.c,v 4.0 1994/01/11 18:01:04 espie Exp espie $")
  65.  
  66. LOCAL signed short *buffer;
  67. LOCAL int index;
  68.  
  69. LOCAL int number;
  70. LOCAL int sync = FALSE;
  71.  
  72. LOCAL ALport audio;
  73. LOCAL ALconfig config;
  74.  
  75. LOCAL int donotwait = FALSE;
  76. LOCAL long chpars[] = {AL_OUTPUT_RATE, 0};
  77.  
  78. LOCAL int stereo;  /* are we playing stereo or not ? */
  79. /* 256th of primary/secondary source for that side. */
  80. LOCAL int primary, secondary;
  81.  
  82. void set_mix(percent)
  83. int percent;
  84.     {
  85.     percent *= 256;
  86.     percent /= 100;
  87.     primary = percent;
  88.     secondary = 512 - percent;
  89.     }
  90.  
  91. int open_audio(f, s)
  92. int f, s;
  93.     {
  94.  
  95.     donotwait = FALSE;
  96.     chpars[1] = f;
  97.     if (f != 0)
  98.         ALsetparams(AL_DEFAULT_DEVICE, chpars, 2);
  99.     ALgetparams(AL_DEFAULT_DEVICE, chpars, 2);
  100.     config = ALnewconfig();
  101.     stereo = s;
  102.     if (stereo)
  103.         {
  104.         ALsetchannels(config, AL_STEREO);
  105.         number = 2;
  106.         }
  107.     else
  108.         {
  109.         ALsetchannels(config, AL_MONO);
  110.         number = 1;
  111.         }
  112.     ALsetwidth(config, AL_SAMPLE_16);
  113.     audio = ALopenport("soundtracker mono", "w", config);
  114.     index = 0;
  115.     buffer = malloc(sizeof(signed short) * number * chpars[1]);
  116.     return chpars[1];
  117.     }
  118.  
  119. void set_synchro(s)
  120. int s;
  121.     {
  122.     sync = s;
  123.     }
  124.  
  125. int update_frequency()
  126.     {
  127.     int oldfreq;
  128.  
  129.     oldfreq = chpars[1];
  130.     ALgetparams(AL_DEFAULT_DEVICE, chpars, 2);
  131.     if (chpars[1] != oldfreq)
  132.         {
  133.         buffer = realloc(buffer, sizeof(signed short) * number * chpars[1]);
  134.         return chpars[1];
  135.         }
  136.     else
  137.         return 0;
  138.     }
  139.  
  140.  
  141. void output_samples(int left, int right)
  142.     {
  143.     if (stereo)
  144.         {
  145.         buffer[index++] = (left * primary + right * secondary)/256;
  146.         buffer[index++] = (right * primary + left * secondary)/256;
  147.         }
  148.     else
  149.         buffer[index++] = left + right;
  150.     }
  151.  
  152. void flush_buffer(void)
  153.     {
  154.     ALwritesamps(audio, buffer, index);
  155.     if (sync)
  156.         while(ALgetfilled(audio) > index * 10)
  157.             /* busy wait */
  158.             ;
  159.     index = 0;
  160.     }
  161.  
  162. void discard_buffer(void)
  163.     {
  164.     donotwait = TRUE;
  165.     /* mostly not implemented, only working when using close_audio
  166.      * right after
  167.      */
  168.     }
  169.  
  170. void close_audio(void)
  171.     {
  172.     if (!donotwait)
  173.         {
  174.         while(ALgetfilled(audio) != 0)
  175.             sginap(1);
  176.         }
  177.     ALcloseport(audio);
  178.     ALfreeconfig(config);
  179.     free(buffer);
  180.     }
  181.