Moved mme audio driver to 1.3 API. SDL-ryan-multiple-audio-device
authorRyan C. Gordon <icculus@icculus.org>
Sat, 07 Oct 2006 07:54:13 +0000
branchSDL-ryan-multiple-audio-device
changeset 38271806fd1acba4
parent 3826 5b483ce86357
child 3828 efdfe292bd88
Moved mme audio driver to 1.3 API.
src/audio/mme/SDL_mmeaudio.c
src/audio/mme/SDL_mmeaudio.h
     1.1 --- a/src/audio/mme/SDL_mmeaudio.c	Sat Oct 07 07:53:26 2006 +0000
     1.2 +++ b/src/audio/mme/SDL_mmeaudio.c	Sat Oct 07 07:54:13 2006 +0000
     1.3 @@ -31,70 +31,12 @@
     1.4  
     1.5  static BOOL inUse[NUM_BUFFERS];
     1.6  
     1.7 -/* Audio driver functions */
     1.8 -static int MME_OpenAudio(_THIS, SDL_AudioSpec * spec);
     1.9 -static void MME_WaitAudio(_THIS);
    1.10 -static Uint8 *MME_GetAudioBuf(_THIS);
    1.11 -static void MME_PlayAudio(_THIS);
    1.12 -static void MME_WaitDone(_THIS);
    1.13 -static void MME_CloseAudio(_THIS);
    1.14 -
    1.15 -/* Audio driver bootstrap functions */
    1.16  static int
    1.17 -Audio_Available(void)
    1.18 -{
    1.19 -    return (1);
    1.20 -}
    1.21 -
    1.22 -static void
    1.23 -Audio_DeleteDevice(SDL_AudioDevice * device)
    1.24 -{
    1.25 -    if (device) {
    1.26 -        if (device->hidden) {
    1.27 -            SDL_free(device->hidden);
    1.28 -            device->hidden = NULL;
    1.29 -        }
    1.30 -        SDL_free(device);
    1.31 -        device = NULL;
    1.32 -    }
    1.33 -}
    1.34 -
    1.35 -static SDL_AudioDevice *
    1.36 -Audio_CreateDevice(int devindex)
    1.37 +MME_Available(void)
    1.38  {
    1.39 -    SDL_AudioDevice *this;
    1.40 -
    1.41 -/* Initialize all variables that we clean on shutdown */
    1.42 -    this = SDL_malloc(sizeof(SDL_AudioDevice));
    1.43 -    if (this) {
    1.44 -        SDL_memset(this, 0, (sizeof *this));
    1.45 -        this->hidden = SDL_malloc((sizeof *this->hidden));
    1.46 -    }
    1.47 -    if ((this == NULL) || (this->hidden == NULL)) {
    1.48 -        SDL_OutOfMemory();
    1.49 -        if (this) {
    1.50 -            SDL_free(this);
    1.51 -        }
    1.52 -        return (0);
    1.53 -    }
    1.54 -    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    1.55 -    /* Set the function pointers */
    1.56 -    this->OpenAudio = MME_OpenAudio;
    1.57 -    this->WaitAudio = MME_WaitAudio;
    1.58 -    this->PlayAudio = MME_PlayAudio;
    1.59 -    this->GetAudioBuf = MME_GetAudioBuf;
    1.60 -    this->WaitDone = MME_WaitDone;
    1.61 -    this->CloseAudio = MME_CloseAudio;
    1.62 -    this->free = Audio_DeleteDevice;
    1.63 -
    1.64 -    return this;
    1.65 +    return 1;
    1.66  }
    1.67  
    1.68 -AudioBootStrap MMEAUDIO_bootstrap = {
    1.69 -    "waveout", "Tru64 MME WaveOut",
    1.70 -    Audio_Available, Audio_CreateDevice, 0
    1.71 -};
    1.72 -
    1.73  static void
    1.74  SetMMerror(char *function, MMRESULT code)
    1.75  {
    1.76 @@ -108,7 +50,7 @@
    1.77  }
    1.78  
    1.79  static void CALLBACK
    1.80 -MME_CALLBACK(HWAVEOUT hwo,
    1.81 +MME_Callback(HWAVEOUT hwo,
    1.82               UINT uMsg, DWORD dwInstance, LPARAM dwParam1, LPARAM dwParam2)
    1.83  {
    1.84      WAVEHDR *wp = (WAVEHDR *) dwParam1;
    1.85 @@ -118,116 +60,137 @@
    1.86  }
    1.87  
    1.88  static int
    1.89 -MME_OpenAudio(_THIS, SDL_AudioSpec * spec)
    1.90 +MME_OpenDevice(_THIS, const char *devname, int iscapture)
    1.91  {
    1.92 +    int valid_format = 0;
    1.93      MMRESULT result;
    1.94 +    Uint8 *mixbuf = NULL;
    1.95      int i;
    1.96  
    1.97 -    mixbuf = NULL;
    1.98 +    /* Initialize all variables that we clean on shutdown */
    1.99 +    this->hidden = (struct SDL_PrivateAudioData *)
   1.100 +                        SDL_malloc((sizeof *this->hidden));
   1.101 +    if (this->hidden == NULL) {
   1.102 +        SDL_OutOfMemory();
   1.103 +        return 0;
   1.104 +    }
   1.105 +    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   1.106  
   1.107      /* Set basic WAVE format parameters */
   1.108 -    shm = mmeAllocMem(sizeof(*shm));
   1.109 -    if (shm == NULL) {
   1.110 -        SDL_SetError("Out of memory: shm");
   1.111 -        return (-1);
   1.112 +    this->hidden->shm = mmeAllocMem(sizeof(*this->hidden->shm));
   1.113 +    if (this->hidden->shm == NULL) {
   1.114 +        MME_CloseDevice(this);
   1.115 +        SDL_OutOfMemory();
   1.116 +        return 0;
   1.117      }
   1.118 -    shm->sound = 0;
   1.119 -    shm->wFmt.wf.wFormatTag = WAVE_FORMAT_PCM;
   1.120 +
   1.121 +    memset(this->hidden->shm, '\0', sizeof (*this->hidden->shm));
   1.122 +    this->hidden->shm->sound = 0;
   1.123 +    this->hidden->shm->wFmt.wf.wFormatTag = WAVE_FORMAT_PCM;
   1.124  
   1.125      /* Determine the audio parameters from the AudioSpec */
   1.126 -    switch (SDL_AUDIO_BITSIZE(spec->format)) {
   1.127 -    case 8:
   1.128 -        /* Unsigned 8 bit audio data */
   1.129 -        spec->format = AUDIO_U8;
   1.130 -        shm->wFmt.wBitsPerSample = 8;
   1.131 -        break;
   1.132 -    case 16:
   1.133 -        /* Signed 16 bit audio data */
   1.134 -        spec->format = AUDIO_S16;
   1.135 -        shm->wFmt.wBitsPerSample = 16;
   1.136 -        break;
   1.137 -    case 32:
   1.138 -        /* Signed 32 bit audio data */
   1.139 -        spec->format = AUDIO_S32;
   1.140 -        shm->wFmt.wBitsPerSample = 32;
   1.141 -        break;
   1.142 -    default:
   1.143 +    /* Try for a closest match on audio format */
   1.144 +    for (test_format = SDL_FirstAudioFormat(this->spec.format);
   1.145 +         !valid_format && test_format;) {
   1.146 +        valid_format = 1;
   1.147 +        switch (test_format) {
   1.148 +            case AUDIO_U8:
   1.149 +            case AUDIO_S16:
   1.150 +            case AUDIO_S32:
   1.151 +                break;
   1.152 +            default:
   1.153 +                valid_format = 0;
   1.154 +                test_format = SDL_NextAudioFormat();
   1.155 +        }
   1.156 +    }
   1.157 +
   1.158 +    if (!valid_format) {
   1.159 +        MME_CloseDevice(this);
   1.160          SDL_SetError("Unsupported audio format");
   1.161 -        return (-1);
   1.162 +        return 0;
   1.163      }
   1.164  
   1.165 +    this->spec.format = test_format;
   1.166 +    this->hidden->shm->wFmt.wBitsPerSample = SDL_AUDIO_BITSIZE(test_format);
   1.167 +
   1.168      /* !!! FIXME: Can this handle more than stereo? */
   1.169 -    shm->wFmt.wf.nChannels = spec->channels;
   1.170 -    shm->wFmt.wf.nSamplesPerSec = spec->freq;
   1.171 -    shm->wFmt.wf.nBlockAlign =
   1.172 -        shm->wFmt.wf.nChannels * shm->wFmt.wBitsPerSample / 8;
   1.173 -    shm->wFmt.wf.nAvgBytesPerSec =
   1.174 -        shm->wFmt.wf.nSamplesPerSec * shm->wFmt.wf.nBlockAlign;
   1.175 +    this->hidden->shm->wFmt.wf.nChannels = this->spec.channels;
   1.176 +    this->hidden->shm->wFmt.wf.nSamplesPerSec = this->spec.freq;
   1.177 +    this->hidden->shm->wFmt.wf.nBlockAlign =
   1.178 +                                this->hidden->shm->wFmt.wf.nChannels *
   1.179 +                                this->hidden->shm->wFmt.wBitsPerSample / 8;
   1.180 +    this->hidden->shm->wFmt.wf.nAvgBytesPerSec =
   1.181 +                                this->hidden->shm->wFmt.wf.nSamplesPerSec *
   1.182 +                                this->hidden->shm->wFmt.wf.nBlockAlign;
   1.183  
   1.184      /* Check the buffer size -- minimum of 1/4 second (word aligned) */
   1.185 -    if (spec->samples < (spec->freq / 4))
   1.186 -        spec->samples = ((spec->freq / 4) + 3) & ~3;
   1.187 +    if (this->spec.samples < (this->spec.freq / 4))
   1.188 +        this->spec.samples = ((this->spec.freq / 4) + 3) & ~3;
   1.189  
   1.190      /* Update the fragment size as size in bytes */
   1.191 -    SDL_CalculateAudioSpec(spec);
   1.192 +    SDL_CalculateAudioSpec(&this->spec);
   1.193  
   1.194      /* Open the audio device */
   1.195 -    result = waveOutOpen(&(shm->sound),
   1.196 +    result = waveOutOpen(&(this->hidden->shm->sound),
   1.197                           WAVE_MAPPER,
   1.198 -                         &(shm->wFmt.wf),
   1.199 -                         MME_CALLBACK,
   1.200 +                         &(this->hidden->shm->wFmt.wf),
   1.201 +                         MME_Callback,
   1.202                           NULL, (CALLBACK_FUNCTION | WAVE_OPEN_SHAREABLE));
   1.203      if (result != MMSYSERR_NOERROR) {
   1.204 +        MME_CloseDevice(this);
   1.205          SetMMerror("waveOutOpen()", result);
   1.206 -        return (-1);
   1.207 +        return 0;
   1.208      }
   1.209  
   1.210      /* Create the sound buffers */
   1.211 -    mixbuf = (Uint8 *) mmeAllocBuffer(NUM_BUFFERS * (spec->size));
   1.212 +    mixbuf = (Uint8 *) mmeAllocBuffer(NUM_BUFFERS * (this->spec.size));
   1.213      if (mixbuf == NULL) {
   1.214 -        SDL_SetError("Out of memory: mixbuf");
   1.215 -        return (-1);
   1.216 +        MME_CloseDevice(this);
   1.217 +        SDL_OutOfMemory();
   1.218 +        return 0;
   1.219      }
   1.220 +    this->hidden->mixbuf = mixbuf;
   1.221  
   1.222      for (i = 0; i < NUM_BUFFERS; i++) {
   1.223 -        shm->wHdr[i].lpData = &mixbuf[i * (spec->size)];
   1.224 -        shm->wHdr[i].dwBufferLength = spec->size;
   1.225 -        shm->wHdr[i].dwFlags = 0;
   1.226 -        shm->wHdr[i].dwUser = i;
   1.227 -        shm->wHdr[i].dwLoops = 0;       /* loop control counter */
   1.228 -        shm->wHdr[i].lpNext = NULL;     /* reserved for driver */
   1.229 -        shm->wHdr[i].reserved = 0;
   1.230 +        this->hidden->shm->wHdr[i].lpData = &mixbuf[i * (this->spec.size)];
   1.231 +        this->hidden->shm->wHdr[i].dwBufferLength = this->spec.size;
   1.232 +        this->hidden->shm->wHdr[i].dwFlags = 0;
   1.233 +        this->hidden->shm->wHdr[i].dwUser = i;
   1.234 +        this->hidden->shm->wHdr[i].dwLoops = 0;    /* loop control counter */
   1.235 +        this->hidden->shm->wHdr[i].lpNext = NULL;  /* reserved for driver */
   1.236 +        this->hidden->shm->wHdr[i].reserved = 0;
   1.237          inUse[i] = FALSE;
   1.238      }
   1.239 -    next_buffer = 0;
   1.240 -    return 0;
   1.241 +    this->hidden->next_buffer = 0;
   1.242 +
   1.243 +    return 1;
   1.244  }
   1.245  
   1.246  static void
   1.247 -MME_WaitAudio(_THIS)
   1.248 +MME_WaitDevice(_THIS)
   1.249  {
   1.250 -    while (inUse[next_buffer]) {
   1.251 +    while (inUse[this->hidden->next_buffer]) {
   1.252          mmeWaitForCallbacks();
   1.253          mmeProcessCallbacks();
   1.254      }
   1.255  }
   1.256  
   1.257  static Uint8 *
   1.258 -MME_GetAudioBuf(_THIS)
   1.259 +MME_GetDeviceBuf(_THIS)
   1.260  {
   1.261 -    Uint8 *retval;
   1.262 -
   1.263 -    inUse[next_buffer] = TRUE;
   1.264 -    retval = (Uint8 *) (shm->wHdr[next_buffer].lpData);
   1.265 -    return retval;
   1.266 +    void *retval = this->hidden->shm->wHdr[this->hidden->next_buffer].lpData;
   1.267 +    inUse[this->hidden->next_buffer] = TRUE;
   1.268 +    return (Uint8 *) retval;
   1.269  }
   1.270  
   1.271  static void
   1.272 -MME_PlayAudio(_THIS)
   1.273 +MME_PlayDevice(_THIS)
   1.274  {
   1.275      /* Queue it up */
   1.276 -    waveOutWrite(shm->sound, &(shm->wHdr[next_buffer]), sizeof(WAVEHDR));
   1.277 -    next_buffer = (next_buffer + 1) % NUM_BUFFERS;
   1.278 +    waveOutWrite(this->hidden->shm->sound,
   1.279 +                 &(this->hidden->shm->wHdr[this->hidden->next_buffer]),
   1.280 +                 sizeof (WAVEHDR));
   1.281 +    this->hidden->next_buffer = (this->hidden->next_buffer + 1) % NUM_BUFFERS;
   1.282  }
   1.283  
   1.284  static void
   1.285 @@ -236,13 +199,13 @@
   1.286      MMRESULT result;
   1.287      int i;
   1.288  
   1.289 -    if (shm->sound) {
   1.290 +    if (this->hidden->shm->sound) {
   1.291          for (i = 0; i < NUM_BUFFERS; i++)
   1.292              while (inUse[i]) {
   1.293                  mmeWaitForCallbacks();
   1.294                  mmeProcessCallbacks();
   1.295              }
   1.296 -        result = waveOutReset(shm->sound);
   1.297 +        result = waveOutReset(this->hidden->shm->sound);
   1.298          if (result != MMSYSERR_NOERROR)
   1.299              SetMMerror("waveOutReset()", result);
   1.300          mmeProcessCallbacks();
   1.301 @@ -250,29 +213,55 @@
   1.302  }
   1.303  
   1.304  static void
   1.305 -MME_CloseAudio(_THIS)
   1.306 +MME_CloseDevice(_THIS)
   1.307  {
   1.308 -    MMRESULT result;
   1.309 +    if (this->hidden != NULL) {
   1.310 +        MMRESULT result;
   1.311  
   1.312 -    if (mixbuf) {
   1.313 -        result = mmeFreeBuffer(mixbuf);
   1.314 -        if (result != MMSYSERR_NOERROR)
   1.315 -            SetMMerror("mmeFreeBuffer", result);
   1.316 -        mixbuf = NULL;
   1.317 -    }
   1.318 +        if (this->hidden->mixbuf) {
   1.319 +            result = mmeFreeBuffer(this->hidden->mixbuf);
   1.320 +            if (result != MMSYSERR_NOERROR)
   1.321 +                SetMMerror("mmeFreeBuffer", result);
   1.322 +            this->hidden->mixbuf = NULL;
   1.323 +        }
   1.324  
   1.325 -    if (shm) {
   1.326 -        if (shm->sound) {
   1.327 -            result = waveOutClose(shm->sound);
   1.328 +        if (this->hidden->shm) {
   1.329 +            if (this->hidden->shm->sound) {
   1.330 +                result = waveOutClose(this->hidden->shm->sound);
   1.331 +                if (result != MMSYSERR_NOERROR)
   1.332 +                    SetMMerror("waveOutClose()", result);
   1.333 +                mmeProcessCallbacks();
   1.334 +            }
   1.335 +            result = mmeFreeMem(this->hidden->shm);
   1.336              if (result != MMSYSERR_NOERROR)
   1.337 -                SetMMerror("waveOutClose()", result);
   1.338 -            mmeProcessCallbacks();
   1.339 +                SetMMerror("mmeFreeMem()", result);
   1.340 +            this->hidden->shm = NULL;
   1.341          }
   1.342 -        result = mmeFreeMem(shm);
   1.343 -        if (result != MMSYSERR_NOERROR)
   1.344 -            SetMMerror("mmeFreeMem()", result);
   1.345 -        shm = NULL;
   1.346 +
   1.347 +        SDL_free(this->hidden);
   1.348 +        this->hidden = NULL;
   1.349      }
   1.350  }
   1.351  
   1.352 +static int
   1.353 +MME_Init(SDL_AudioDriverImpl *impl)
   1.354 +{
   1.355 +    /* Set the function pointers */
   1.356 +    impl->OpenDevice = MME_OpenDevice;
   1.357 +    impl->WaitDevice = MME_WaitDevice;
   1.358 +    impl->WaitDone = MME_WaitDone;
   1.359 +    impl->PlayDevice = MME_PlayDevice;
   1.360 +    impl->GetDeviceBuf = MME_GetDeviceBuf;
   1.361 +    impl->CloseDevice = MME_CloseDevice;
   1.362 +    impl->OnlyHasDefaultOutputDevice = 1;
   1.363 +
   1.364 +    return 1;
   1.365 +}
   1.366 +
   1.367 +/* !!! FIXME: Windows "windib" driver is called waveout, too */
   1.368 +AudioBootStrap MMEAUDIO_bootstrap = {
   1.369 +    "waveout", "Tru64 MME WaveOut",
   1.370 +    MME_Available, MME_Init, 0
   1.371 +};
   1.372 +
   1.373  /* vi: set ts=4 sw=4 expandtab: */
     2.1 --- a/src/audio/mme/SDL_mmeaudio.h	Sat Oct 07 07:53:26 2006 +0000
     2.2 +++ b/src/audio/mme/SDL_mmeaudio.h	Sat Oct 07 07:54:13 2006 +0000
     2.3 @@ -23,8 +23,8 @@
     2.4  
     2.5  /* Allow access to a raw mixing buffer */
     2.6  
     2.7 -#ifndef _SDL_lowaudio_h
     2.8 -#define _SDL_lowaudio_h
     2.9 +#ifndef _SDL_mmeaudio_h
    2.10 +#define _SDL_mmeaudio_h
    2.11  
    2.12  #include "../SDL_sysaudio.h"
    2.13  
    2.14 @@ -46,9 +46,6 @@
    2.15      int next_buffer;
    2.16  };
    2.17  
    2.18 -#define shm			(this->hidden->shm)
    2.19 -#define mixbuf			(this->hidden->mixbuf)
    2.20 -#define next_buffer		(this->hidden->next_buffer)
    2.21 -/* Old variable names */
    2.22 -#endif /* _SDL_lowaudio_h */
    2.23 +#endif /* _SDL_mmeaudio_h */
    2.24 +
    2.25  /* vi: set ts=4 sw=4 expandtab: */