home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / MM1 / SOUNDUTILS / mm1_tracker.lzh / TRACKER4.6 / AF / audio.c next >
Text File  |  1994-11-24  |  4KB  |  187 lines

  1. /* AF/audio.c
  2.     vi:ts=3 sw=3:
  3.  
  4. This code written by:
  5.  
  6. Andrew "Alf" Leahy                         email: alf@st.nepean.uws.edu.au
  7. University of Western Sydney - Nepean.
  8. Sydney, Australia.                         phone: (047) 360622 (work)
  9.  
  10.  
  11. Modified by Marc Espie to adjust to tracker 4.0 API.
  12.  
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include "defs.h"
  17. #include "extern.h"
  18. #include "/usr/local/include/AF/AFlib.h"
  19. ID("$Id$")
  20.  
  21. /* 0 external handset, 1 for internal speaker */
  22. #define SPEAKER 0
  23.      
  24. LOCAL int stereo, primary, secondary;
  25.  
  26. LOCAL char *buffer, *buffer_r;
  27. LOCAL int nbytes=0, nbytes_r=0, ssize;
  28.  
  29. LOCAL ATime t, t_r, act, act_r;
  30. LOCAL AC ac, ac_r;
  31. LOCAL AFAudioConn *aud, *aud_r;
  32.  
  33. int sample_sizes[] = {
  34.     1,    /* MU255 */
  35.     1,    /* ALAW */
  36.     2,    /* Linear PCM, 16 bits, -1.0 <= x < 1.0 */
  37.     2,    /* Linear PCM, 32 bits, -1.0 <= x < 1.0 */
  38.     1,    /* G.721, 64Kbps to/from 32Kbps. */
  39.     1,    /* G.723, 64Kbps to/from 32Kbps. */
  40.     0
  41. };
  42.  
  43. int open_audio(int f, int s)
  44. {
  45.     AFSetACAttributes attributes;
  46.     int srate, device;
  47.     unsigned int channels;
  48.     AEncodeType type;
  49.     char *server;
  50.  
  51.     device = SPEAKER;
  52.     attributes.preempt = Mix;
  53.     attributes.start_timeout = 0;
  54.     attributes.end_silence = 0;
  55.     attributes.play_gain = 0;
  56.     attributes.rec_gain =  0;
  57.  
  58.     if ((server = (char *) getenv("AUDIOFILE")) == NULL)
  59.         end_all("Error: AUDIOFILE unset");
  60.     else
  61.     {
  62.         server = (char *) getenv("AUDIOFILE");
  63.         if ((aud = AFOpenAudioConn( server )) == NULL)
  64.             end_all("Error: can't open connection");
  65.         ac = AFCreateAC(aud, device, ACPlayGain, &attributes);
  66.         srate = ac->device->playSampleFreq;
  67.         type = ac->device->playBufType;
  68.         channels = ac->device->playNchannels;
  69.         ssize = sample_sizes[type] * channels;
  70.  
  71.         if ((buffer = (char *)malloc(ssize * srate)) == NULL)
  72.             end_all("Couldn't allocate play buffer");
  73.  
  74.         t = AFGetTime(ac);
  75.     }
  76.  
  77.     stereo=s;
  78.  
  79.     if (stereo)
  80.     {
  81.         server = (char *) getenv("AUDIORIGHT");
  82.         if ((aud = AFOpenAudioConn(server)) == NULL)
  83.             end_all("Error: can't open connection");
  84.         ac_r = AFCreateAC(aud, device, ACPlayGain, &attributes);
  85.         srate = ac->device->playSampleFreq;
  86.         type = ac->device->playBufType;
  87.         channels = ac->device->playNchannels;
  88.         ssize = sample_sizes[type] * channels;
  89.  
  90.         if ((buffer_r = (char *)malloc(ssize * srate)) == NULL)
  91.             end_all("Couldn't allocate play buffer");
  92.         t_r = AFGetTime(ac_r);
  93.     }
  94.  
  95.     return srate;
  96. }
  97.  
  98. void set_mix(int percent)
  99. {
  100.     percent *= 256;
  101.     percent /= 100;
  102.     primary = percent;
  103.     secondary = 512 - percent;
  104. }
  105.  
  106. void set_synchro(int s)
  107. {
  108. }
  109.  
  110. int update_frequency()
  111. {
  112.     return 0;
  113. }
  114.  
  115. LOCAL unsigned int cvt(int ch)
  116. {
  117.     int mask;
  118.  
  119.     if (ch < 0)
  120.     {
  121.         ch = -ch;
  122.         mask = 0x7f;
  123.     }
  124.     else
  125.         mask = 0xff;
  126.  
  127.     if (ch < 32)        ch = 0xF0 | 15 - (ch / 2);
  128.     else if (ch < 96)   ch = 0xE0 | 15 - (ch - 32) / 4;
  129.     else if (ch < 224)  ch = 0xD0 | 15 - (ch - 96) / 8;
  130.     else if (ch < 480)  ch = 0xC0 | 15 - (ch - 224) / 16;
  131.     else if (ch < 992)  ch = 0xB0 | 15 - (ch - 480) / 32;
  132.     else if (ch < 2016) ch = 0xA0 | 15 - (ch - 992) / 64;
  133.     else if (ch < 4064) ch = 0x90 | 15 - (ch - 2016) / 128;
  134.     else if (ch < 8160) ch = 0x80 | 15 - (ch - 4064) /  256;
  135.     else ch = 0x80;
  136.  
  137.     return (mask & ch);
  138. }
  139.  
  140. void output_samples(int left, int right)
  141. {
  142.     if (stereo)
  143.         if (primary) /* mixing needed */
  144.         {
  145.             buffer[nbytes++] = cvt((left * primary + right * secondary) >>10);
  146.             buffer_r[nbytes_r++] = cvt((right * primary + left * secondary) >>10);
  147.         }
  148.         else /* no mixing */
  149.         {
  150.             buffer[nbytes++] = cvt(left >>2);
  151.             buffer_r[nbytes_r++] = cvt(right >>2);
  152.         }
  153.     else /* mono */
  154.         buffer[nbytes++] = cvt((left + right) >>2);
  155. }
  156.  
  157. void flush_buffer()
  158. {
  159.     act = AFPlaySamples(ac, t, nbytes, buffer);
  160.     t += nbytes/ssize;
  161.     nbytes = 0;
  162.  
  163.     if (stereo) 
  164.     {
  165.         act_r = AFPlaySamples(ac_r, t_r, nbytes_r, buffer_r);
  166.         t_r += nbytes_r/ssize;
  167.         nbytes_r = 0;
  168.     }
  169. }
  170.  
  171. void discard_buffer()
  172. {
  173. }
  174.  
  175. void close_audio()
  176. {
  177.     free(buffer);
  178.  
  179. /* Alf: I'm not sure whether these functions are needed
  180.         I think these are Seg Faulting... */
  181.  
  182.      (void) AFCloseAudioConn(aud);
  183.  
  184.     if (stereo)
  185.         (void) AFCloseAudioConn(aud_r);
  186. }
  187.