home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Emulation / Atari800 / nas.c < prev    next >
C/C++ Source or Header  |  1997-10-01  |  6KB  |  264 lines

  1. #include <stdio.h>
  2. #include <audio/audiolib.h>
  3.  
  4. static char *rcsid = "$Id: nas.c,v 1.4 1996/10/09 21:09:06 david Exp $";
  5.  
  6. #define FALSE 0
  7. #define TRUE 1
  8.  
  9. #define SAMPLE_RATE 8000
  10.  
  11. static nasbug = FALSE; /* Debug Flag */
  12. static sound_enabled = FALSE;
  13.  
  14. static AuServer *au_server;
  15. static AuDeviceID *au_device;
  16. static AuFlowID au_channel[4];
  17.  
  18. static int waveform = AuWaveFormSquare;
  19. static int au_channel_clock[4] = { 64000, 64000, 64000, 64000 };
  20. static int M[4] = { 1, 1, 1, 1 };
  21.  
  22. static int AUDCTL = 0x00;
  23. static int AUDF[4] = { 0, 0, 0, 0 };
  24. static int AUDC[4] = { 0, 0, 0, 0 };
  25.  
  26. void NAS_Initialise (int *argc, char *argv[])
  27. {
  28.   int i, j;
  29.  
  30.   for (i=j=1;i<*argc;i++)
  31.     {
  32.       if (strcmp(argv[i],"-sound") == 0)
  33.     sound_enabled = TRUE;
  34.       else if (strcmp(argv[i],"-squarewave") == 0)
  35.     waveform = AuWaveFormSquare;
  36.       else if (strcmp(argv[i],"-sinewave") == 0)
  37.     waveform = AuWaveFormSine;
  38.       else if (strcmp(argv[i],"-nasbug") == 0)
  39.     nasbug = TRUE;
  40.       else
  41.     {
  42.       if (strcmp(argv[i],"-help") == 0)
  43.         {
  44.           printf ("\t-sound        Enable NAS sound support\n");
  45.           printf ("\t-squarewave   Generate sound with a square wave\n");
  46.           printf ("\t-sinewave     Generate sound with a sine wave\n");
  47.           printf ("\t-nasbug       Enable debug code in nas.c\n");
  48.         }
  49.  
  50.       argv[j++] = argv[i];
  51.     }
  52.     }
  53.  
  54.   *argc = j;
  55.  
  56.   if (sound_enabled)
  57.     {
  58.       au_server = AuOpenServer (NULL, 0, NULL, 0, NULL, NULL);
  59.       if (!au_server)
  60.     {
  61.       printf ("Unable to open audio server\n");
  62.       exit (1);
  63.     }
  64.  
  65.       for (au_device=NULL,i=0;i<AuServerNumDevices(au_server);i++)
  66.     {
  67.       if ((AuDeviceKind(AuServerDevice(au_server,i)) ==
  68.            AuComponentKindPhysicalOutput) &&
  69.           AuDeviceNumTracks(AuServerDevice(au_server,i)) == 1)
  70.         {
  71.           au_device = (AuDeviceID*)AuDeviceIdentifier(AuServerDevice(au_server,i));
  72.           break;
  73.         }
  74.     }
  75.  
  76.       au_channel[0] = AuCreateFlow (au_server, NULL);
  77.       au_channel[1] = AuCreateFlow (au_server, NULL);
  78.       au_channel[2] = AuCreateFlow (au_server, NULL);
  79.       au_channel[3] = AuCreateFlow (au_server, NULL);
  80.  
  81.       for (i=0;i<4;i++)
  82.     {
  83.       AuElement elements[3];
  84.       int freq = 0;
  85.       int volume = AuFixedPointFromFraction(0,100);
  86.  
  87.       AuMakeElementImportWaveForm (&elements[0], SAMPLE_RATE,
  88.                        waveform,
  89.                        AuUnlimitedSamples,
  90.                        freq,
  91.                        0, NULL);
  92.       AuMakeElementMultiplyConstant (&elements[1], 0, volume);
  93.       AuMakeElementExportDevice (&elements[2], 1, (int)au_device, SAMPLE_RATE,
  94.                      AuUnlimitedSamples, 0, NULL);
  95.  
  96.       AuSetElements (au_server, au_channel[i], AuTrue, 3, elements, NULL);
  97.       AuStartFlow (au_server, au_channel[i], NULL);
  98.     }
  99.     }
  100. }
  101.  
  102. void NAS_Exit (void)
  103. {
  104.   if (sound_enabled)
  105.     {
  106.       int i;
  107.  
  108.       for (i=0;i<4;i++)
  109.     {
  110.       AuStopFlow (au_server, au_channel[i], NULL);
  111.     }
  112.  
  113.       AuCloseServer (au_server);
  114.     }
  115. }
  116.  
  117. void NAS_SetFrequency (int channel, int frequency)
  118. {
  119.   if (frequency < 8000)
  120.     {
  121.       static AuElementParameters parms;
  122.  
  123.       parms.flow = channel;
  124.       parms.element_num = 0;
  125.       parms.num_parameters = AuParmsImportWaveForm;
  126.       parms.parameters[AuParmsImportWaveFormFrequency] = frequency;
  127.       parms.parameters[AuParmsImportWaveFormNumSamples] = AuUnlimitedSamples;
  128.       AuSetElementParameters (au_server, 1, &parms, NULL);
  129.     }
  130. }
  131.  
  132. void NAS_SetVolume (int channel, int volume)
  133. {
  134.   static AuElementParameters parms;
  135.  
  136.   parms.flow = channel;
  137.   parms.element_num = 1;
  138.   parms.num_parameters = AuParmsMultiplyConstant;
  139.   parms.parameters[AuParmsMultiplyConstantConstant] = volume;
  140.   AuSetElementParameters (au_server, 1, &parms, NULL);
  141. }
  142.  
  143. static int join12 = FALSE;
  144. static int join34 = FALSE;
  145.  
  146. void NAS_UpdateSound (void)
  147. {
  148.   if (sound_enabled)
  149.     {
  150.       int i;
  151.   
  152.       for (i=0;i<4;i++)
  153.     {
  154.       int freqtmp;
  155.       int flag = FALSE;
  156.  
  157.       if ((i < 2) && join12)
  158.         {
  159.           if (i == 1)
  160.         {
  161.           int N = ((AUDF[1] << 8) | AUDF[0]) + M[0];
  162.  
  163.           if (nasbug)
  164.             printf ("join12: N=%d\n", N);
  165.  
  166.           freqtmp = au_channel_clock[0] / (N + N);
  167.  
  168.           flag = TRUE;
  169.         }
  170.         }
  171.       else if ((i >= 2) && join34)
  172.         {
  173.           if (i == 3)
  174.         {
  175.           int N = ((AUDF[3] << 8) | AUDF[2]) + M[2];
  176.  
  177.           if (nasbug)
  178.             printf ("join34: N=%d\n", N);
  179.  
  180.           freqtmp = au_channel_clock[2] / (N + N);
  181.  
  182.           flag = TRUE;
  183.         }
  184.         }
  185.       else
  186.         {
  187.           int N = AUDF[i] + M[i];
  188.  
  189.           freqtmp = au_channel_clock[i] / (N + N);
  190.  
  191.           flag = TRUE;
  192.         }
  193.  
  194.       if (flag)
  195.         {
  196.           int distortion = AUDC[i] & 0xf0;
  197.           int voltmp;
  198.  
  199.           if ((distortion == 0xa0) || (distortion == 0xd0))
  200.         voltmp = AuFixedPointFromFraction ((AUDC[i] & 0x0f), 30);
  201.           else
  202.         voltmp = AuFixedPointFromFraction (0, 30);
  203.  
  204.           NAS_SetFrequency (au_channel[i], freqtmp);
  205.           NAS_SetVolume (au_channel[i], voltmp);
  206.         }
  207.     }
  208.  
  209.       AuFlush (au_server);
  210.     }
  211. }
  212.  
  213. void Atari_AUDC (int channel, int byte)
  214. {
  215.   AUDC[channel-1] = byte;
  216. }
  217.  
  218. void Atari_AUDF (int channel, int byte)
  219. {
  220.   AUDF[channel-1] = byte;
  221. }
  222.  
  223. void Atari_AUDCTL (int byte)
  224. {
  225.   AUDCTL = byte;
  226.  
  227.   if (byte & 0x01)
  228.     {
  229.       au_channel_clock[0] = 15000;
  230.       au_channel_clock[1] = 15000;
  231.       au_channel_clock[2] = 15000;
  232.       au_channel_clock[3] = 15000;
  233.     }
  234.   else
  235.     {
  236.       au_channel_clock[0] = 64000;
  237.       au_channel_clock[1] = 64000;
  238.       au_channel_clock[2] = 64000;
  239.       au_channel_clock[3] = 64000;
  240.     }
  241.  
  242.   M[0] = M[1] = M[2] = M[3] = 1;
  243.  
  244.   join34 = (byte & 0x08) ? TRUE : FALSE; /* Clock channel 4 with channel 3 */
  245.   join12 = (byte & 0x10) ? TRUE : FALSE; /* Clock channel 2 with channel 1 */
  246.  
  247.   if (byte & 0x20)
  248.     {
  249.       au_channel_clock[2] = 1790000; /* Clock channel 3 with 1.79 MHZ */
  250.       M[2] = (join34) ? 7 : 4;
  251.     }
  252.  
  253.   if (byte & 0x40)
  254.     {
  255.       au_channel_clock[0] = 1790000; /* Clock channel 1 with 1.79 MHZ */
  256.       M[0] = (join12) ? 7 : 4;
  257.     }
  258.  
  259.   if (nasbug && (byte & 0xfe))
  260.     printf ("AUDCTL: %02x\n", byte);
  261. }
  262.  
  263.  
  264.