src/audio/winmm/SDL_winmm.c
changeset 5588 57bfc2a2a452
child 5593 ab22ca13c47f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/winmm/SDL_winmm.c	Thu Aug 04 01:24:22 2011 -0400
     1.3 @@ -0,0 +1,466 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "SDL_config.h"
    1.25 +
    1.26 +/* Allow access to a raw mixing buffer */
    1.27 +
    1.28 +#include "../../core/windows/SDL_windows.h"
    1.29 +#include <mmsystem.h>
    1.30 +
    1.31 +#include "SDL_timer.h"
    1.32 +#include "SDL_audio.h"
    1.33 +#include "../SDL_audio_c.h"
    1.34 +#include "SDL_winmm.h"
    1.35 +#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
    1.36 +#include "win_ce_semaphore.h"
    1.37 +#endif
    1.38 +
    1.39 +
    1.40 +/* !!! FIXME: this is a cut and paste of SDL_FreeUnixAudioDevices(),
    1.41 + * !!! FIXME:  which is more proof this needs to be managed in SDL_audio.c
    1.42 + * !!! FIXME:  and not in drivers.
    1.43 + */
    1.44 +static void
    1.45 +FreeWaveOutAudioDevices(char ***devices, int *devCount)
    1.46 +{
    1.47 +    int i = *devCount;
    1.48 +    if ((i > 0) && (*devices != NULL)) {
    1.49 +        while (i--) {
    1.50 +            SDL_free((*devices)[i]);
    1.51 +        }
    1.52 +    }
    1.53 +
    1.54 +    if (*devices != NULL) {
    1.55 +        SDL_free(*devices);
    1.56 +    }
    1.57 +
    1.58 +    *devices = NULL;
    1.59 +    *devCount = 0;
    1.60 +}
    1.61 +
    1.62 +static char **outputDevices = NULL;
    1.63 +static int outputDeviceCount = 0;
    1.64 +static char **inputDevices = NULL;
    1.65 +static int inputDeviceCount = 0;
    1.66 +
    1.67 +static int
    1.68 +DetectWaveOutDevices(void)
    1.69 +{
    1.70 +    UINT i;
    1.71 +    const UINT devcount = waveOutGetNumDevs();
    1.72 +    WAVEOUTCAPS caps;
    1.73 +    FreeWaveOutAudioDevices(&outputDevices, &outputDeviceCount);
    1.74 +    outputDevices = (const char **) SDL_malloc(sizeof (char *) * devcount);
    1.75 +    for (i = 0; i < devcount; i++) {
    1.76 +        if (waveOutGetDevCaps(i, &caps, sizeof (caps)) == MMSYSERR_NOERROR) {
    1.77 +            outputDevices[outputDeviceCount] = WIN_StringToUTF8(caps.szPname);
    1.78 +            if (outputDevices[outputDeviceCount] != NULL) {
    1.79 +                outputDeviceCount++;
    1.80 +            }
    1.81 +        }
    1.82 +    }
    1.83 +    return outputDeviceCount;
    1.84 +}
    1.85 +
    1.86 +static int
    1.87 +DetectWaveInDevices(void)
    1.88 +{
    1.89 +    UINT i;
    1.90 +    const UINT devcount = waveInGetNumDevs();
    1.91 +    WAVEINCAPS caps;
    1.92 +    FreeWaveInAudioDevices(&inputDevices, &inputDeviceCount);
    1.93 +    inputDevices = (const char **) SDL_malloc(sizeof (char *) * devcount);
    1.94 +    for (i = 0; i < devcount; i++) {
    1.95 +        if (waveInGetDevCaps(i, &caps, sizeof (caps)) == MMSYSERR_NOERROR) {
    1.96 +            inputDevices[inputDeviceCount] = WIN_StringToUTF8(caps.szPname);
    1.97 +            if (inputDevices[inputDeviceCount] != NULL) {
    1.98 +                inputDeviceCount++;
    1.99 +            }
   1.100 +        }
   1.101 +    }
   1.102 +    return inputDeviceCount;
   1.103 +}
   1.104 +
   1.105 +static int
   1.106 +WINMM_DetectDevices(int iscapture)
   1.107 +{
   1.108 +    return (iscapture) ? DetectWaveInDevices() : DetectWaveOutDevices();
   1.109 +}
   1.110 +
   1.111 +static const char *
   1.112 +WINMM_GetDeviceName(int index, int iscapture)
   1.113 +{
   1.114 +    if ((iscapture) && (index < inputDeviceCount)) {
   1.115 +        return inputDevices[index];
   1.116 +    } else if ((!iscapture) && (index < outputDeviceCount)) {
   1.117 +        return outputDevices[index];
   1.118 +    }
   1.119 +
   1.120 +    SDL_SetError("No such device");
   1.121 +    return NULL;
   1.122 +}
   1.123 +
   1.124 +static void CALLBACK
   1.125 +CaptureSound(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance,
   1.126 +          DWORD_PTR dwParam1, DWORD_PTR dwParam2)
   1.127 +{
   1.128 +    SDL_AudioDevice *this = (SDL_AudioDevice *) dwInstance;
   1.129 +
   1.130 +    /* Only service "buffer is filled" messages */
   1.131 +    if (uMsg != WIM_DATA)
   1.132 +        return;
   1.133 +
   1.134 +    /* Signal that we have a new buffer of data */
   1.135 +#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   1.136 +    ReleaseSemaphoreCE(this->hidden->audio_sem, 1, NULL);
   1.137 +#else
   1.138 +    ReleaseSemaphore(this->hidden->audio_sem, 1, NULL);
   1.139 +#endif
   1.140 +}
   1.141 +
   1.142 +
   1.143 +/* The Win32 callback for filling the WAVE device */
   1.144 +static void CALLBACK
   1.145 +FillSound(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
   1.146 +          DWORD_PTR dwParam1, DWORD_PTR dwParam2)
   1.147 +{
   1.148 +    SDL_AudioDevice *this = (SDL_AudioDevice *) dwInstance;
   1.149 +
   1.150 +    /* Only service "buffer done playing" messages */
   1.151 +    if (uMsg != WOM_DONE)
   1.152 +        return;
   1.153 +
   1.154 +    /* Signal that we are done playing a buffer */
   1.155 +#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   1.156 +    ReleaseSemaphoreCE(this->hidden->audio_sem, 1, NULL);
   1.157 +#else
   1.158 +    ReleaseSemaphore(this->hidden->audio_sem, 1, NULL);
   1.159 +#endif
   1.160 +}
   1.161 +
   1.162 +static void
   1.163 +SetMMerror(char *function, MMRESULT code)
   1.164 +{
   1.165 +    size_t len;
   1.166 +    char errbuf[MAXERRORLENGTH];
   1.167 +    wchar_t werrbuf[MAXERRORLENGTH];
   1.168 +
   1.169 +    SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
   1.170 +    len = SDL_strlen(errbuf);
   1.171 +
   1.172 +    waveOutGetErrorText(code, werrbuf, MAXERRORLENGTH - len);
   1.173 +    WideCharToMultiByte(CP_ACP, 0, werrbuf, -1, errbuf + len,
   1.174 +                        MAXERRORLENGTH - len, NULL, NULL);
   1.175 +
   1.176 +    SDL_SetError("%s", errbuf);
   1.177 +}
   1.178 +
   1.179 +static void
   1.180 +WINMM_WaitDevice(_THIS)
   1.181 +{
   1.182 +    /* Wait for an audio chunk to finish */
   1.183 +#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   1.184 +    WaitForSemaphoreCE(this->hidden->audio_sem, INFINITE);
   1.185 +#else
   1.186 +    WaitForSingleObject(this->hidden->audio_sem, INFINITE);
   1.187 +#endif
   1.188 +}
   1.189 +
   1.190 +static Uint8 *
   1.191 +WINMM_GetDeviceBuf(_THIS)
   1.192 +{
   1.193 +    return (Uint8 *) (this->hidden->
   1.194 +                      wavebuf[this->hidden->next_buffer].lpData);
   1.195 +}
   1.196 +
   1.197 +static void
   1.198 +WINMM_PlayDevice(_THIS)
   1.199 +{
   1.200 +    /* Queue it up */
   1.201 +    waveOutWrite(this->hidden->hout,
   1.202 +                 &this->hidden->wavebuf[this->hidden->next_buffer],
   1.203 +                 sizeof(this->hidden->wavebuf[0]));
   1.204 +    this->hidden->next_buffer = (this->hidden->next_buffer + 1) % NUM_BUFFERS;
   1.205 +}
   1.206 +
   1.207 +static void
   1.208 +WINMM_WaitDone(_THIS)
   1.209 +{
   1.210 +    int i, left;
   1.211 +
   1.212 +    do {
   1.213 +        left = NUM_BUFFERS;
   1.214 +        for (i = 0; i < NUM_BUFFERS; ++i) {
   1.215 +            if (this->hidden->wavebuf[i].dwFlags & WHDR_DONE) {
   1.216 +                --left;
   1.217 +            }
   1.218 +        }
   1.219 +        if (left > 0) {
   1.220 +            SDL_Delay(100);
   1.221 +        }
   1.222 +    } while (left > 0);
   1.223 +}
   1.224 +
   1.225 +static void
   1.226 +WINMM_CloseDevice(_THIS)
   1.227 +{
   1.228 +    /* Close up audio */
   1.229 +    if (this->hidden != NULL) {
   1.230 +        int i;
   1.231 +
   1.232 +        if (this->hidden->audio_sem) {
   1.233 +#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   1.234 +            CloseSynchHandle(this->hidden->audio_sem);
   1.235 +#else
   1.236 +            CloseHandle(this->hidden->audio_sem);
   1.237 +#endif
   1.238 +            this->hidden->audio_sem = 0;
   1.239 +        }
   1.240 +
   1.241 +        if (this->hidden->hin) {
   1.242 +            waveInClose(this->hidden->hin);
   1.243 +            this->hidden->hin = 0;
   1.244 +        }
   1.245 +
   1.246 +        if (this->hidden->hout) {
   1.247 +            waveOutClose(this->hidden->hout);
   1.248 +            this->hidden->hout = 0;
   1.249 +        }
   1.250 +
   1.251 +        /* Clean up mixing buffers */
   1.252 +        for (i = 0; i < NUM_BUFFERS; ++i) {
   1.253 +            if (this->hidden->wavebuf[i].dwUser != 0xFFFF) {
   1.254 +                waveOutUnprepareHeader(this->hidden->hout,
   1.255 +                                       &this->hidden->wavebuf[i],
   1.256 +                                       sizeof(this->hidden->wavebuf[i]));
   1.257 +                this->hidden->wavebuf[i].dwUser = 0xFFFF;
   1.258 +            }
   1.259 +        }
   1.260 +
   1.261 +        if (this->hidden->mixbuf != NULL) {
   1.262 +            /* Free raw mixing buffer */
   1.263 +            SDL_free(this->hidden->mixbuf);
   1.264 +            this->hidden->mixbuf = NULL;
   1.265 +        }
   1.266 +
   1.267 +        SDL_free(this->hidden);
   1.268 +        this->hidden = NULL;
   1.269 +    }
   1.270 +}
   1.271 +
   1.272 +static int
   1.273 +WINMM_OpenDevice(_THIS, const char *devname, int iscapture)
   1.274 +{
   1.275 +    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   1.276 +    int valid_datatype = 0;
   1.277 +    MMRESULT result;
   1.278 +    WAVEFORMATEX waveformat;
   1.279 +    UINT_PTR devId = WAVE_MAPPER;  /* WAVE_MAPPER == choose system's default */
   1.280 +    char *utf8 = NULL;
   1.281 +    int i;
   1.282 +
   1.283 +    if (devname != NULL) {  /* specific device requested? */
   1.284 +        if (iscapture) {
   1.285 +            const int devcount = (int) waveInGetNumDevs();
   1.286 +            WAVEINCAPS caps;
   1.287 +            for (i = 0; (i < devcount) && (devId == WAVE_MAPPER); i++) {
   1.288 +                result = waveInGetDevCaps(i, &caps, sizeof (caps));
   1.289 +                if (result != MMSYSERR_NOERROR)
   1.290 +                    continue;
   1.291 +                else if ((utf8 = WIN_StringToUTF8(caps.szPname)) == NULL)
   1.292 +                    continue;
   1.293 +                else if (SDL_strcmp(devname, utf8) == 0)
   1.294 +                    devId = (UINT_PTR) i;
   1.295 +                SDL_free(utf8);
   1.296 +            }
   1.297 +        } else {
   1.298 +            const int devcount = (int) waveOutGetNumDevs();
   1.299 +            WAVEOUTCAPS caps;
   1.300 +            for (i = 0; (i < devcount) && (devId == WAVE_MAPPER); i++) {
   1.301 +                result = waveOutGetDevCaps(i, &caps, sizeof (caps));
   1.302 +                if (result != MMSYSERR_NOERROR)
   1.303 +                    continue;
   1.304 +                else if ((utf8 = WIN_StringToUTF8(caps.szPname)) == NULL)
   1.305 +                    continue;
   1.306 +                else if (SDL_strcmp(devname, utf8) == 0)
   1.307 +                    devId = (UINT_PTR) i;
   1.308 +                SDL_free(utf8);
   1.309 +            }
   1.310 +        }
   1.311 +
   1.312 +        if (devId == WAVE_MAPPER) {
   1.313 +            SDL_SetError("Requested device not found");
   1.314 +            return 0;
   1.315 +        }
   1.316 +    }
   1.317 +
   1.318 +    /* Initialize all variables that we clean on shutdown */
   1.319 +    this->hidden = (struct SDL_PrivateAudioData *)
   1.320 +        SDL_malloc((sizeof *this->hidden));
   1.321 +    if (this->hidden == NULL) {
   1.322 +        SDL_OutOfMemory();
   1.323 +        return 0;
   1.324 +    }
   1.325 +    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   1.326 +
   1.327 +    /* Initialize the wavebuf structures for closing */
   1.328 +    for (i = 0; i < NUM_BUFFERS; ++i)
   1.329 +        this->hidden->wavebuf[i].dwUser = 0xFFFF;
   1.330 +
   1.331 +    while ((!valid_datatype) && (test_format)) {
   1.332 +        valid_datatype = 1;
   1.333 +        this->spec.format = test_format;
   1.334 +        switch (test_format) {
   1.335 +        case AUDIO_U8:
   1.336 +        case AUDIO_S16:
   1.337 +        case AUDIO_S32:
   1.338 +            break;              /* valid. */
   1.339 +
   1.340 +        default:
   1.341 +            valid_datatype = 0;
   1.342 +            test_format = SDL_NextAudioFormat();
   1.343 +            break;
   1.344 +        }
   1.345 +    }
   1.346 +
   1.347 +    if (!valid_datatype) {
   1.348 +        WINMM_CloseDevice(this);
   1.349 +        SDL_SetError("Unsupported audio format");
   1.350 +        return 0;
   1.351 +    }
   1.352 +
   1.353 +    /* Set basic WAVE format parameters */
   1.354 +    SDL_memset(&waveformat, '\0', sizeof(waveformat));
   1.355 +    waveformat.wFormatTag = WAVE_FORMAT_PCM;
   1.356 +    waveformat.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
   1.357 +
   1.358 +    if (this->spec.channels > 2)
   1.359 +        this->spec.channels = 2;        /* !!! FIXME: is this right? */
   1.360 +
   1.361 +    waveformat.nChannels = this->spec.channels;
   1.362 +    waveformat.nSamplesPerSec = this->spec.freq;
   1.363 +    waveformat.nBlockAlign =
   1.364 +        waveformat.nChannels * (waveformat.wBitsPerSample / 8);
   1.365 +    waveformat.nAvgBytesPerSec =
   1.366 +        waveformat.nSamplesPerSec * waveformat.nBlockAlign;
   1.367 +
   1.368 +    /* Check the buffer size -- minimum of 1/4 second (word aligned) */
   1.369 +    if (this->spec.samples < (this->spec.freq / 4))
   1.370 +        this->spec.samples = ((this->spec.freq / 4) + 3) & ~3;
   1.371 +
   1.372 +    /* Update the fragment size as size in bytes */
   1.373 +    SDL_CalculateAudioSpec(&this->spec);
   1.374 +
   1.375 +    /* Open the audio device */
   1.376 +    if (iscapture) {
   1.377 +        result = waveInOpen(&this->hidden->hin, devId, &waveformat,
   1.378 +                             (DWORD_PTR) CaptureSound, (DWORD_PTR) this,
   1.379 +                             CALLBACK_FUNCTION);
   1.380 +    } else {
   1.381 +        result = waveOutOpen(&this->hidden->hout, devId, &waveformat,
   1.382 +                             (DWORD_PTR) FillSound, (DWORD_PTR) this,
   1.383 +                             CALLBACK_FUNCTION);
   1.384 +    }
   1.385 +
   1.386 +    if (result != MMSYSERR_NOERROR) {
   1.387 +        WINMM_CloseDevice(this);
   1.388 +        SetMMerror("waveOutOpen()", result);
   1.389 +        return 0;
   1.390 +    }
   1.391 +#ifdef SOUND_DEBUG
   1.392 +    /* Check the sound device we retrieved */
   1.393 +    {
   1.394 +        WAVEOUTCAPS caps;
   1.395 +
   1.396 +        result = waveOutGetDevCaps((UINT) this->hidden->hout,
   1.397 +                                   &caps, sizeof(caps));
   1.398 +        if (result != MMSYSERR_NOERROR) {
   1.399 +            WINMM_CloseDevice(this);
   1.400 +            SetMMerror("waveOutGetDevCaps()", result);
   1.401 +            return 0;
   1.402 +        }
   1.403 +        printf("Audio device: %s\n", caps.szPname);
   1.404 +    }
   1.405 +#endif
   1.406 +
   1.407 +    /* Create the audio buffer semaphore */
   1.408 +    this->hidden->audio_sem =
   1.409 +#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   1.410 +        CreateSemaphoreCE(NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL);
   1.411 +#else
   1.412 +        CreateSemaphore(NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL);
   1.413 +#endif
   1.414 +    if (this->hidden->audio_sem == NULL) {
   1.415 +        WINMM_CloseDevice(this);
   1.416 +        SDL_SetError("Couldn't create semaphore");
   1.417 +        return 0;
   1.418 +    }
   1.419 +
   1.420 +    /* Create the sound buffers */
   1.421 +    this->hidden->mixbuf =
   1.422 +        (Uint8 *) SDL_malloc(NUM_BUFFERS * this->spec.size);
   1.423 +    if (this->hidden->mixbuf == NULL) {
   1.424 +        WINMM_CloseDevice(this);
   1.425 +        SDL_OutOfMemory();
   1.426 +        return 0;
   1.427 +    }
   1.428 +    for (i = 0; i < NUM_BUFFERS; ++i) {
   1.429 +        SDL_memset(&this->hidden->wavebuf[i], '\0',
   1.430 +                   sizeof(this->hidden->wavebuf[i]));
   1.431 +        this->hidden->wavebuf[i].dwBufferLength = this->spec.size;
   1.432 +        this->hidden->wavebuf[i].dwFlags = WHDR_DONE;
   1.433 +        this->hidden->wavebuf[i].lpData =
   1.434 +            (LPSTR) & this->hidden->mixbuf[i * this->spec.size];
   1.435 +        result = waveOutPrepareHeader(this->hidden->hout,
   1.436 +                                      &this->hidden->wavebuf[i],
   1.437 +                                      sizeof(this->hidden->wavebuf[i]));
   1.438 +        if (result != MMSYSERR_NOERROR) {
   1.439 +            WINMM_CloseDevice(this);
   1.440 +            SetMMerror("waveOutPrepareHeader()", result);
   1.441 +            return 0;
   1.442 +        }
   1.443 +    }
   1.444 +
   1.445 +    return 1;                   /* Ready to go! */
   1.446 +}
   1.447 +
   1.448 +
   1.449 +static int
   1.450 +WINMM_Init(SDL_AudioDriverImpl * impl)
   1.451 +{
   1.452 +    /* Set the function pointers */
   1.453 +    impl->DetectDevices = WINMM_DetectDevices;
   1.454 +    impl->GetDeviceName = WINMM_GetDeviceName;
   1.455 +    impl->OpenDevice = WINMM_OpenDevice;
   1.456 +    impl->PlayDevice = WINMM_PlayDevice;
   1.457 +    impl->WaitDevice = WINMM_WaitDevice;
   1.458 +    impl->WaitDone = WINMM_WaitDone;
   1.459 +    impl->GetDeviceBuf = WINMM_GetDeviceBuf;
   1.460 +    impl->CloseDevice = WINMM_CloseDevice;
   1.461 +
   1.462 +    return 1;   /* this audio target is available. */
   1.463 +}
   1.464 +
   1.465 +AudioBootStrap WINMM_bootstrap = {
   1.466 +    "winmm", "Windows Waveform Audio", WINMM_Init, 0
   1.467 +};
   1.468 +
   1.469 +/* vi: set ts=4 sw=4 expandtab: */