Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1.  
  2. #include <stdio.h>
  3. #include "SDL_audio.h"
  4. #include "SDL_byteorder.h"
  5. #include "quakedef.h"
  6.  
  7. static dma_t the_shm;
  8. static int snd_inited;
  9.  
  10. extern int desired_speed;
  11. extern int desired_bits;
  12.  
  13. static void paint_audio(void *unused, Uint8 *stream, int len)
  14. {
  15.         if ( shm ) {
  16.                 shm->buffer = stream;
  17.                 shm->samplepos += len/(shm->samplebits/8)/2;
  18.                 // Check for samplepos overflow?
  19.                 S_PaintChannels (shm->samplepos);
  20.         }
  21. }
  22.  
  23. qboolean SNDDMA_Init(void)
  24. {
  25.         SDL_AudioSpec desired, obtained;
  26.  
  27.         snd_inited = 0;
  28.  
  29.         /* Set up the desired format */
  30.         desired.freq = desired_speed;
  31.         switch (desired_bits) {
  32.                 case 8:
  33.                         desired.format = AUDIO_U8;
  34.                         break;
  35.                 case 16:
  36.                         if ( SDL_BYTEORDER == SDL_BIG_ENDIAN )
  37.                                 desired.format = AUDIO_S16MSB;
  38.                         else
  39.                                 desired.format = AUDIO_S16LSB;
  40.                         break;
  41.                 default:
  42.                         Con_Printf("Unknown number of audio bits: %d\n",
  43.                                                                 desired_bits);
  44.                         return 0;
  45.         }
  46.         desired.channels = 2;
  47.         desired.samples = 512;
  48.         desired.callback = paint_audio;
  49.  
  50.         /* Open the audio device */
  51.         if ( SDL_OpenAudio(&desired, &obtained) < 0 ) {
  52.                 Con_Printf("Couldn't open SDL audio: %s\n", SDL_GetError());
  53.                 return 0;
  54.         }
  55.  
  56.         /* Make sure we can support the audio format */
  57.         switch (obtained.format) {
  58.                 case AUDIO_U8:
  59.                         /* Supported */
  60.                         break;
  61.                 case AUDIO_S16LSB:
  62.                 case AUDIO_S16MSB:
  63.                         if ( ((obtained.format == AUDIO_S16LSB) &&
  64.                              (SDL_BYTEORDER == SDL_LIL_ENDIAN)) ||
  65.                              ((obtained.format == AUDIO_S16MSB) &&
  66.                              (SDL_BYTEORDER == SDL_BIG_ENDIAN)) ) {
  67.                                 /* Supported */
  68.                                 break;
  69.                         }
  70.                         /* Unsupported, fall through */;
  71.                 default:
  72.                         /* Not supported -- force SDL to do our bidding */
  73.                         SDL_CloseAudio();
  74.                         if ( SDL_OpenAudio(&desired, NULL) < 0 ) {
  75.                                 Con_Printf("Couldn't open SDL audio: %s\n",
  76.                                                         SDL_GetError());
  77.                                 return 0;
  78.                         }
  79.                         memcpy(&obtained, &desired, sizeof(desired));
  80.                         break;
  81.         }
  82.         SDL_PauseAudio(0);
  83.  
  84.         /* Fill the audio DMA information block */
  85.         shm = &the_shm;
  86.         shm->splitbuffer = 0;
  87.         shm->samplebits = (obtained.format & 0xFF);
  88.         shm->speed = obtained.freq;
  89.         shm->channels = obtained.channels;
  90.         shm->samples = obtained.samples*shm->channels;
  91.         shm->samplepos = 0;
  92.         shm->submission_chunk = 1;
  93.         shm->buffer = NULL;
  94.  
  95.         snd_inited = 1;
  96.         return 1;
  97. }
  98.  
  99. int SNDDMA_GetDMAPos(void)
  100. {
  101.         return shm->samplepos;
  102. }
  103.  
  104. void SNDDMA_Shutdown(void)
  105. {
  106.         if (snd_inited)
  107.         {
  108.                 SDL_CloseAudio();
  109.                 snd_inited = 0;
  110.         }
  111. }
  112.  
  113.