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 / Aix / audio.c next >
Text File  |  1994-11-24  |  5KB  |  202 lines

  1. /* Aix/audio.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4. /* Ported from Linux/audio.c by Sam Hartman <hartmans@mit.edu>*/
  5. /* minor mods for pl14 by Mike Battersby */
  6. /* Modified from soundblaster_audio.c by Hannu Savolainen */
  7. /* hsavolai@cs.helsinki.fi */
  8.  
  9. #include "defs.h"
  10. #ifdef MALLOC_NOT_IN_STDLIB
  11. #include <malloc.h>
  12. #else
  13. #include <stdlib.h>
  14. #endif
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <fcntl.h>
  18. #include "extern.h"
  19.  
  20. #undef SIGNED /*redefined in system include file*/
  21. #include <sys/audio.h>
  22. #include <sys/acpa.h>
  23.  
  24.  
  25. ID("$Id: audio.c,v 1.1 1994/11/15 16:12:31 espie Exp espie $")
  26.  
  27. LOCAL unsigned char *buffer;    /* buffer for ready-to-play samples */
  28. LOCAL unsigned 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/acpa0/1 */
  32.  
  33. LOCAL int stereo;                    /* are we playing stereo or not ? */
  34. /* 256th of primary/secondary source for that side. */
  35. LOCAL int primary=512, secondary=0;
  36. LOCAL int dsp_samplesize = 16; /* must be 8 or 16 */
  37.  
  38. void set_mix(percent)
  39. int percent;
  40.     {
  41.     percent *= 256;
  42.     percent /= 100;
  43.     primary = percent;
  44.     secondary = 512 - percent;
  45.     }
  46.  
  47. int open_audio(f, s)
  48. int f;
  49. int s;
  50.       {
  51.       audio_init init;
  52.     audio_control control;
  53.     audio_change change;
  54.     audio = open("/dev/acpa0/1", O_WRONLY, 0);
  55.    if (audio == -1)
  56.         end_all("Error opening audio device");
  57.  
  58.     if (f==0) f = 44100;
  59.  
  60.    init.srate = f;
  61.    init.bits_per_sample = 16;
  62.    init.mode = PCM;
  63.     init.channels = s?2:1;
  64.    init.flags = BIG_ENDIAN;
  65.     init.operation = PLAY;
  66.    if (ioctl(audio, AUDIO_INIT, &init)!= 0)
  67.         end_all("Error initializing ACPA");
  68.  
  69.     stereo = (init.channels == 2)?1:0;
  70.     buf_max = init.bsize*20;    /* This is set by the ioctl. */
  71.  
  72.    buffer = malloc(buf_max);
  73.    buffer16 = (short *)buffer;
  74.    buf_index = 0;
  75.  
  76.    control.ioctl_request = AUDIO_CHANGE;
  77.     control.request_info = &change;
  78.     control.position = 0;
  79.  
  80.     change.dev_info = 0;
  81.     change.input = AUDIO_IGNORE;
  82.     change.output = OUTPUT_1;
  83.     change.monitor = AUDIO_IGNORE;
  84.     change.volume = 0x7fff0000;
  85.     change.volume_delay = 0;
  86.     change.balance = 0x3fff0000;
  87.     change.balance_delay = 0;
  88.     change.treble = AUDIO_IGNORE;
  89.     change.bass = AUDIO_IGNORE;
  90.     change.pitch = AUDIO_IGNORE;
  91.     
  92.     if (ioctl(audio, AUDIO_CONTROL, &control) != 0)
  93.         end_all( "Error changing ACPA parameters");
  94.  
  95.     control.ioctl_request = AUDIO_START;
  96.  
  97.       if (ioctl(audio, AUDIO_CONTROL, &control) != 0)
  98.         end_all("Error starting ACPA");
  99.  
  100.       return init.srate;
  101.  
  102.    }
  103.  
  104. LOCAL void actually_flush_buffer()
  105.    {
  106.    int l,i;
  107.  
  108.    l = sizeof(*buffer) * buf_index;
  109.    if (dsp_samplesize !=8) l *= 2;
  110.    write(audio, buffer, l);
  111.  
  112.    buf_index = 0;
  113.    }
  114.  
  115. void output_samples(left, right)
  116. unsigned int left, right;
  117.    {
  118.    if (dsp_samplesize != 8)    /* Cool! 16 bits/sample */
  119.         {
  120.        if (stereo)
  121.            {
  122.           if (buf_index * 2 >= buf_max - 1) 
  123.               actually_flush_buffer();
  124.  
  125.           buffer16[buf_index++] = 
  126.                 32768+((left*primary + right*secondary) / 256);
  127.           buffer16[buf_index++] = 
  128.                 32768+((right*primary + left*secondary) / 256);
  129.               }
  130.          else
  131.            {
  132.           if (buf_index * 2 >= buf_max) 
  133.               actually_flush_buffer();
  134.               buffer16[buf_index++] = 32768+(left + right);
  135.           }
  136.         }
  137.     else
  138.        {
  139.        if (stereo)
  140.            {
  141.           if (buf_index >= buf_max - 1) 
  142.                 actually_flush_buffer();
  143.           buffer[buf_index++] = ((left*primary + right*secondary) >> 16)
  144.               + 128;
  145.           buffer[buf_index++] = ((right*primary + left*secondary) >> 16)
  146.              + 128;
  147.           }
  148.        else
  149.            {
  150.           if (buf_index >= buf_max) 
  151.                 actually_flush_buffer();
  152.           buffer[buf_index++] = ((left + right) >> 8) + 128;
  153.           }
  154.        }
  155.     }
  156.  
  157. void flush_buffer()
  158.    {    /* Dummy version */
  159.    }
  160.  
  161. /* We must wait for all samples to be played before closing.*/
  162. void close_audio()
  163.    {
  164.    audio_control control;
  165.    if (buf_index != 0)
  166.         actually_flush_buffer();
  167.    ioctl(audio, AUDIO_WAIT, 0);
  168.    control.position = 0;
  169.     control.ioctl_request = AUDIO_STOP;
  170.     control.request_info = 0;
  171.     ioctl(audio, AUDIO_CONTROL, &control);
  172.  
  173.    close(audio);
  174.    free(buffer);
  175.    }
  176.  
  177. /* dummy system calls, to patch ? */
  178. void set_synchro(s)
  179.     {
  180.     }
  181.  
  182. int update_frequency()
  183.     {
  184.     return 0;
  185.     }
  186.  
  187. void discard_buffer()
  188.     {
  189.    audio_control control;
  190.    control.ioctl_request = AUDIO_STOP;
  191.     control.request_info = 0;
  192.     control.position = 0;
  193.     ioctl(audio, AUDIO_CONTROL, &control);
  194.    usleep(150000);
  195.    control.ioctl_request = AUDIO_START;
  196.    if (ioctl(audio, AUDIO_CONTROL, &control) == -1)
  197.         end_all ("Unable to restart AACPA");
  198.     buf_index = 0;
  199.     }
  200.  
  201.  
  202.