src/audio/winmm/SDL_winmm.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 19 Mar 2015 23:45:34 -0400
changeset 9406 3547065d936d
parent 9404 f48039db971c
child 9410 e25530faeda0
permissions -rw-r--r--
Removed unused variable.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
  Simple DirectMedia Layer
8149
681eb46b8ac4 Fixed bug 2374 - Update copyright for 2014...
Sam Lantinga <slouken@libsdl.org>
parents: 8093
diff changeset
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
  This software is provided 'as-is', without any express or implied
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
  arising from the use of this software.
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
  freely, subject to the following restrictions:
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
     claim that you wrote the original software. If you use this software
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
     appreciated but is not required.
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
     misrepresented as being the original software.
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7719
diff changeset
    21
#include "../../SDL_internal.h"
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5593
diff changeset
    23
#if SDL_AUDIO_DRIVER_WINMM
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5593
diff changeset
    24
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
/* Allow access to a raw mixing buffer */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
#include "../../core/windows/SDL_windows.h"
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#include <mmsystem.h>
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#include "SDL_timer.h"
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
#include "SDL_audio.h"
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
#include "../SDL_audio_c.h"
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
#include "SDL_winmm.h"
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
7449
5148cfb37948 Implement float32 support for winmm and directsound targets (Thanks, John!).
Ryan C. Gordon <icculus@icculus.org>
parents: 7332
diff changeset
    35
#ifndef WAVE_FORMAT_IEEE_FLOAT
5148cfb37948 Implement float32 support for winmm and directsound targets (Thanks, John!).
Ryan C. Gordon <icculus@icculus.org>
parents: 7332
diff changeset
    36
#define WAVE_FORMAT_IEEE_FLOAT 0x0003
5148cfb37948 Implement float32 support for winmm and directsound targets (Thanks, John!).
Ryan C. Gordon <icculus@icculus.org>
parents: 7332
diff changeset
    37
#endif
5148cfb37948 Implement float32 support for winmm and directsound targets (Thanks, John!).
Ryan C. Gordon <icculus@icculus.org>
parents: 7332
diff changeset
    38
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
    39
#define DETECT_DEV_IMPL(iscap, typ, capstyp) \
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
    40
static void DetectWave##typ##Devs(void) { \
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
    41
    const UINT iscapture = iscap ? 1 : 0; \
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    42
    const UINT devcount = wave##typ##GetNumDevs(); \
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    43
    capstyp caps; \
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    44
    UINT i; \
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    45
    for (i = 0; i < devcount; i++) { \
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    46
        if (wave##typ##GetDevCaps(i,&caps,sizeof(caps))==MMSYSERR_NOERROR) { \
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    47
            char *name = WIN_StringToUTF8(caps.szPname); \
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    48
            if (name != NULL) { \
9399
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9394
diff changeset
    49
                SDL_AddAudioDevice((int) iscapture, name, (void *) ((size_t) i)); \
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    50
                SDL_free(name); \
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    51
            } \
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    52
        } \
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    53
    } \
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
    56
DETECT_DEV_IMPL(SDL_FALSE, Out, WAVEOUTCAPS)
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
    57
DETECT_DEV_IMPL(SDL_TRUE, In, WAVEINCAPS)
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    59
static void
9404
f48039db971c Fixed a compiler warning on Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 9403
diff changeset
    60
WINMM_DetectDevices(void)
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
    62
    DetectWaveInDevs();
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
    63
    DetectWaveOutDevs();
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
static void CALLBACK
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
CaptureSound(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
          DWORD_PTR dwParam1, DWORD_PTR dwParam2)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    SDL_AudioDevice *this = (SDL_AudioDevice *) dwInstance;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    /* Only service "buffer is filled" messages */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
    if (uMsg != WIM_DATA)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
        return;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    /* Signal that we have a new buffer of data */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    ReleaseSemaphore(this->hidden->audio_sem, 1, NULL);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
/* The Win32 callback for filling the WAVE device */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
static void CALLBACK
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
FillSound(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
          DWORD_PTR dwParam1, DWORD_PTR dwParam2)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
    SDL_AudioDevice *this = (SDL_AudioDevice *) dwInstance;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    /* Only service "buffer done playing" messages */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
    if (uMsg != WOM_DONE)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
        return;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
    /* Signal that we are done playing a buffer */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
    ReleaseSemaphore(this->hidden->audio_sem, 1, NULL);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    96
static int
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
SetMMerror(char *function, MMRESULT code)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
{
8585
2d08b56cbfa9 Fixed 64-bit warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
    99
    int len;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
    char errbuf[MAXERRORLENGTH];
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    wchar_t werrbuf[MAXERRORLENGTH];
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
    SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
8585
2d08b56cbfa9 Fixed 64-bit warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   104
    len = SDL_static_cast(int, SDL_strlen(errbuf));
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
    waveOutGetErrorText(code, werrbuf, MAXERRORLENGTH - len);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    WideCharToMultiByte(CP_ACP, 0, werrbuf, -1, errbuf + len,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
                        MAXERRORLENGTH - len, NULL, NULL);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   110
    return SDL_SetError("%s", errbuf);
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
static void
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
WINMM_WaitDevice(_THIS)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    /* Wait for an audio chunk to finish */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    WaitForSingleObject(this->hidden->audio_sem, INFINITE);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
static Uint8 *
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
WINMM_GetDeviceBuf(_THIS)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    return (Uint8 *) (this->hidden->
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
                      wavebuf[this->hidden->next_buffer].lpData);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
static void
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
WINMM_PlayDevice(_THIS)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    /* Queue it up */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    waveOutWrite(this->hidden->hout,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
                 &this->hidden->wavebuf[this->hidden->next_buffer],
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
                 sizeof(this->hidden->wavebuf[0]));
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
    this->hidden->next_buffer = (this->hidden->next_buffer + 1) % NUM_BUFFERS;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
static void
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
WINMM_WaitDone(_THIS)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    int i, left;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    do {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
        left = NUM_BUFFERS;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
        for (i = 0; i < NUM_BUFFERS; ++i) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
            if (this->hidden->wavebuf[i].dwFlags & WHDR_DONE) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
                --left;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
            }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
        }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
        if (left > 0) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
            SDL_Delay(100);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
        }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    } while (left > 0);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
static void
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
WINMM_CloseDevice(_THIS)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    /* Close up audio */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    if (this->hidden != NULL) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
        int i;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
        if (this->hidden->audio_sem) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
            CloseHandle(this->hidden->audio_sem);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
            this->hidden->audio_sem = 0;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
        }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
        /* Clean up mixing buffers */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
        for (i = 0; i < NUM_BUFFERS; ++i) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
            if (this->hidden->wavebuf[i].dwUser != 0xFFFF) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
                waveOutUnprepareHeader(this->hidden->hout,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
                                       &this->hidden->wavebuf[i],
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
                                       sizeof(this->hidden->wavebuf[i]));
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
                this->hidden->wavebuf[i].dwUser = 0xFFFF;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
            }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
        }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
7719
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7461
diff changeset
   177
        /* Free raw mixing buffer */
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7461
diff changeset
   178
        SDL_free(this->hidden->mixbuf);
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7461
diff changeset
   179
        this->hidden->mixbuf = NULL;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
7332
b53acf6ab70b Fixed some Visual Studio analyze warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   181
        if (this->hidden->hin) {
b53acf6ab70b Fixed some Visual Studio analyze warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   182
            waveInClose(this->hidden->hin);
b53acf6ab70b Fixed some Visual Studio analyze warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   183
            this->hidden->hin = 0;
b53acf6ab70b Fixed some Visual Studio analyze warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   184
        }
b53acf6ab70b Fixed some Visual Studio analyze warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   185
b53acf6ab70b Fixed some Visual Studio analyze warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   186
        if (this->hidden->hout) {
b53acf6ab70b Fixed some Visual Studio analyze warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   187
            waveOutClose(this->hidden->hout);
b53acf6ab70b Fixed some Visual Studio analyze warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   188
            this->hidden->hout = 0;
b53acf6ab70b Fixed some Visual Studio analyze warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   189
        }
b53acf6ab70b Fixed some Visual Studio analyze warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   190
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
        SDL_free(this->hidden);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
        this->hidden = NULL;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
7461
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   196
static SDL_bool
8585
2d08b56cbfa9 Fixed 64-bit warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   197
PrepWaveFormat(_THIS, UINT devId, WAVEFORMATEX *pfmt, const int iscapture)
7461
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   198
{
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   199
    SDL_zerop(pfmt);
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   200
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   201
    if (SDL_AUDIO_ISFLOAT(this->spec.format)) {
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   202
        pfmt->wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   203
    } else {
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   204
        pfmt->wFormatTag = WAVE_FORMAT_PCM;
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   205
    }
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   206
    pfmt->wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   207
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   208
    pfmt->nChannels = this->spec.channels;
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   209
    pfmt->nSamplesPerSec = this->spec.freq;
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   210
    pfmt->nBlockAlign = pfmt->nChannels * (pfmt->wBitsPerSample / 8);
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   211
    pfmt->nAvgBytesPerSec = pfmt->nSamplesPerSec * pfmt->nBlockAlign;
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   212
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   213
    if (iscapture) {
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   214
        return (waveInOpen(0, devId, pfmt, 0, 0, WAVE_FORMAT_QUERY) == 0);
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   215
    } else {
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   216
        return (waveOutOpen(0, devId, pfmt, 0, 0, WAVE_FORMAT_QUERY) == 0);
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   217
    }
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   218
}
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   219
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
   221
WINMM_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
    int valid_datatype = 0;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    MMRESULT result;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
    WAVEFORMATEX waveformat;
8585
2d08b56cbfa9 Fixed 64-bit warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   227
    UINT devId = WAVE_MAPPER;  /* WAVE_MAPPER == choose system's default */
2d08b56cbfa9 Fixed 64-bit warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   228
    UINT i;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
   230
    if (handle != NULL) {  /* specific device requested? */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
   231
        const size_t val = (size_t) handle;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
   232
        devId = (UINT) val;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    /* Initialize all variables that we clean on shutdown */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    this->hidden = (struct SDL_PrivateAudioData *)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
        SDL_malloc((sizeof *this->hidden));
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    if (this->hidden == NULL) {
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   239
        return SDL_OutOfMemory();
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
    /* Initialize the wavebuf structures for closing */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
    for (i = 0; i < NUM_BUFFERS; ++i)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
        this->hidden->wavebuf[i].dwUser = 0xFFFF;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
7461
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   247
    if (this->spec.channels > 2)
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   248
        this->spec.channels = 2;        /* !!! FIXME: is this right? */
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   249
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   250
    /* Check the buffer size -- minimum of 1/4 second (word aligned) */
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   251
    if (this->spec.samples < (this->spec.freq / 4))
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   252
        this->spec.samples = ((this->spec.freq / 4) + 3) & ~3;
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   253
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
    while ((!valid_datatype) && (test_format)) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
        switch (test_format) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
        case AUDIO_U8:
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
        case AUDIO_S16:
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
        case AUDIO_S32:
7449
5148cfb37948 Implement float32 support for winmm and directsound targets (Thanks, John!).
Ryan C. Gordon <icculus@icculus.org>
parents: 7332
diff changeset
   259
        case AUDIO_F32:
7461
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   260
            this->spec.format = test_format;
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   261
            if (PrepWaveFormat(this, devId, &waveformat, iscapture)) {
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   262
                valid_datatype = 1;
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   263
            } else {
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   264
                test_format = SDL_NextAudioFormat();
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   265
            }
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   266
            break;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
        default:
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
            test_format = SDL_NextAudioFormat();
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
            break;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
        }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
    if (!valid_datatype) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
        WINMM_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   276
        return SDL_SetError("Unsupported audio format");
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
    /* Update the fragment size as size in bytes */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
    SDL_CalculateAudioSpec(&this->spec);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    /* Open the audio device */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
    if (iscapture) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
        result = waveInOpen(&this->hidden->hin, devId, &waveformat,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
                             (DWORD_PTR) CaptureSound, (DWORD_PTR) this,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
                             CALLBACK_FUNCTION);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
    } else {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
        result = waveOutOpen(&this->hidden->hout, devId, &waveformat,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
                             (DWORD_PTR) FillSound, (DWORD_PTR) this,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
                             CALLBACK_FUNCTION);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
    if (result != MMSYSERR_NOERROR) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
        WINMM_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   295
        return SetMMerror("waveOutOpen()", result);
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
#ifdef SOUND_DEBUG
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    /* Check the sound device we retrieved */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
    {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
        WAVEOUTCAPS caps;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
        result = waveOutGetDevCaps((UINT) this->hidden->hout,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
                                   &caps, sizeof(caps));
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
        if (result != MMSYSERR_NOERROR) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
            WINMM_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   306
            return SetMMerror("waveOutGetDevCaps()", result);
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
        }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
        printf("Audio device: %s\n", caps.szPname);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
#endif
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
    /* Create the audio buffer semaphore */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
    this->hidden->audio_sem =
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
        CreateSemaphore(NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
    if (this->hidden->audio_sem == NULL) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
        WINMM_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   317
        return SDL_SetError("Couldn't create semaphore");
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
    /* Create the sound buffers */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
    this->hidden->mixbuf =
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
        (Uint8 *) SDL_malloc(NUM_BUFFERS * this->spec.size);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
    if (this->hidden->mixbuf == NULL) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
        WINMM_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   325
        return SDL_OutOfMemory();
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
    for (i = 0; i < NUM_BUFFERS; ++i) {
6352
a9bcd26e7105 Removed unneeded audio buffer memset() for consistent behavior on all platforms.
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   328
        SDL_memset(&this->hidden->wavebuf[i], 0,
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
                   sizeof(this->hidden->wavebuf[i]));
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
        this->hidden->wavebuf[i].dwBufferLength = this->spec.size;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
        this->hidden->wavebuf[i].dwFlags = WHDR_DONE;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
        this->hidden->wavebuf[i].lpData =
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
            (LPSTR) & this->hidden->mixbuf[i * this->spec.size];
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
        result = waveOutPrepareHeader(this->hidden->hout,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
                                      &this->hidden->wavebuf[i],
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
                                      sizeof(this->hidden->wavebuf[i]));
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
        if (result != MMSYSERR_NOERROR) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
            WINMM_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   339
            return SetMMerror("waveOutPrepareHeader()", result);
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
        }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   343
    return 0;                   /* Ready to go! */
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
static int
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
WINMM_Init(SDL_AudioDriverImpl * impl)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    /* Set the function pointers */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    impl->DetectDevices = WINMM_DetectDevices;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
    impl->OpenDevice = WINMM_OpenDevice;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
    impl->PlayDevice = WINMM_PlayDevice;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    impl->WaitDevice = WINMM_WaitDevice;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
    impl->WaitDone = WINMM_WaitDone;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
    impl->GetDeviceBuf = WINMM_GetDeviceBuf;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
    impl->CloseDevice = WINMM_CloseDevice;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
    return 1;   /* this audio target is available. */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
AudioBootStrap WINMM_bootstrap = {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
    "winmm", "Windows Waveform Audio", WINMM_Init, 0
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
};
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5593
diff changeset
   366
#endif /* SDL_AUDIO_DRIVER_WINMM */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5593
diff changeset
   367
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
/* vi: set ts=4 sw=4 expandtab: */