src/audio/winmm/SDL_winmm.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 06 Aug 2016 23:05:02 -0400
changeset 10249 0abfef2f7fc5
parent 10248 7c430819edd2
child 10250 a3faa23abdbb
permissions -rw-r--r--
winmm: Added a FIXME for truncated device names.
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
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
     3
  Copyright (C) 1997-2016 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
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    30
#include "SDL_assert.h"
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
#include "SDL_timer.h"
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
#include "SDL_audio.h"
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
#include "../SDL_audio_c.h"
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
#include "SDL_winmm.h"
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
7449
5148cfb37948 Implement float32 support for winmm and directsound targets (Thanks, John!).
Ryan C. Gordon <icculus@icculus.org>
parents: 7332
diff changeset
    36
#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
    37
#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
    38
#endif
5148cfb37948 Implement float32 support for winmm and directsound targets (Thanks, John!).
Ryan C. Gordon <icculus@icculus.org>
parents: 7332
diff changeset
    39
10249
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    40
/* !!! FIXME:
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    41
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    42
WAVExxxCAPS gives you 31 bytes for the device name, and just truncates if it's
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    43
longer. However, since WinXP, you can use the WAVExxxCAPS2 structure, which
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    44
will give you a name GUID. The full name is in the Windows Registry under
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    45
that GUID, located here: HKLM\System\CurrentControlSet\Control\MediaCategories
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    46
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    47
Note that drivers can report GUID_NULL for the name GUID, in which case,
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    48
Windows makes a best effort to fill in those 31 bytes in the usual place.
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    49
This info summarized from MSDN:
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    50
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    51
http://web.archive.org/web/20131027093034/http://msdn.microsoft.com/en-us/library/windows/hardware/ff536382(v=vs.85).aspx
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    52
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    53
*/
0abfef2f7fc5 winmm: Added a FIXME for truncated device names.
Ryan C. Gordon <icculus@icculus.org>
parents: 10248
diff changeset
    54
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
    55
#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
    56
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
    57
    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
    58
    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
    59
    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
    60
    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
    61
    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
    62
        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
    63
            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
    64
            if (name != NULL) { \
9410
e25530faeda0 Audio hotplug fixes for winmm and XAudio2 backends.
Ryan C. Gordon <icculus@icculus.org>
parents: 9406
diff changeset
    65
                SDL_AddAudioDevice((int) iscapture, name, (void *) ((size_t) i+1)); \
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
    66
                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
    67
            } \
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    68
        } \
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    69
    } \
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
    72
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
    73
DETECT_DEV_IMPL(SDL_TRUE, In, WAVEINCAPS)
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
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
    75
static void
9404
f48039db971c Fixed a compiler warning on Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 9403
diff changeset
    76
WINMM_DetectDevices(void)
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
    78
    DetectWaveInDevs();
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
    79
    DetectWaveOutDevs();
5588
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
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
CaptureSound(HWAVEIN hwi, 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 is filled" messages */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
    if (uMsg != WIM_DATA)
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 have a new buffer of data */
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
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
/* The Win32 callback for filling the WAVE device */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
static void CALLBACK
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
FillSound(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
          DWORD_PTR dwParam1, DWORD_PTR dwParam2)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    SDL_AudioDevice *this = (SDL_AudioDevice *) dwInstance;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
    /* Only service "buffer done playing" messages */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    if (uMsg != WOM_DONE)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
        return;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    /* Signal that we are done playing a buffer */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    ReleaseSemaphore(this->hidden->audio_sem, 1, NULL);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   112
static int
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
SetMMerror(char *function, MMRESULT code)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
{
8585
2d08b56cbfa9 Fixed 64-bit warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   115
    int len;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    char errbuf[MAXERRORLENGTH];
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    wchar_t werrbuf[MAXERRORLENGTH];
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
    SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
8585
2d08b56cbfa9 Fixed 64-bit warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   120
    len = SDL_static_cast(int, SDL_strlen(errbuf));
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    waveOutGetErrorText(code, werrbuf, MAXERRORLENGTH - len);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    WideCharToMultiByte(CP_ACP, 0, werrbuf, -1, errbuf + len,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
                        MAXERRORLENGTH - len, NULL, NULL);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   126
    return SDL_SetError("%s", errbuf);
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
static void
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
WINMM_WaitDevice(_THIS)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    /* Wait for an audio chunk to finish */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
    WaitForSingleObject(this->hidden->audio_sem, INFINITE);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
}
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
static Uint8 *
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
WINMM_GetDeviceBuf(_THIS)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    return (Uint8 *) (this->hidden->
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
                      wavebuf[this->hidden->next_buffer].lpData);
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
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
static void
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
WINMM_PlayDevice(_THIS)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    /* Queue it up */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
    waveOutWrite(this->hidden->hout,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
                 &this->hidden->wavebuf[this->hidden->next_buffer],
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
                 sizeof(this->hidden->wavebuf[0]));
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    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
   151
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
static void
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
WINMM_WaitDone(_THIS)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
    int i, left;
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
    do {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
        left = NUM_BUFFERS;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
        for (i = 0; i < NUM_BUFFERS; ++i) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
            if (this->hidden->wavebuf[i].dwFlags & WHDR_DONE) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
                --left;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
            }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
        }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
        if (left > 0) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
            SDL_Delay(100);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
        }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    } while (left > 0);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   171
static int
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   172
WINMM_CaptureFromDevice(_THIS, void *buffer, int buflen)
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   173
{
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   174
    const int nextbuf = this->hidden->next_buffer;
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   175
    MMRESULT result;
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   176
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   177
    SDL_assert(buflen == this->spec.size);
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   178
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   179
    /* Wait for an audio chunk to finish */
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   180
    WaitForSingleObject(this->hidden->audio_sem, INFINITE);
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   181
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   182
    /* Copy it to caller's buffer... */
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   183
    SDL_memcpy(buffer, this->hidden->wavebuf[nextbuf].lpData, this->spec.size);
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   184
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   185
    /* requeue the buffer that just finished. */
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   186
    result = waveInAddBuffer(this->hidden->hin,
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   187
                             &this->hidden->wavebuf[nextbuf],
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   188
                             sizeof (this->hidden->wavebuf[nextbuf]));
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   189
    if (result != MMSYSERR_NOERROR) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   190
        return -1;  /* uhoh! Disable the device. */
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   191
    }
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   192
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   193
    /* queue the next buffer in sequence, next time. */
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   194
    this->hidden->next_buffer = (nextbuf + 1) % NUM_BUFFERS;
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   195
    return this->spec.size;
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   196
}
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   197
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   198
static void
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   199
WINMM_FlushCapture(_THIS)
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   200
{
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   201
    /* Wait for an audio chunk to finish */
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   202
    if (WaitForSingleObject(this->hidden->audio_sem, 0) == WAIT_OBJECT_0) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   203
        const int nextbuf = this->hidden->next_buffer;
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   204
        /* requeue the buffer that just finished without reading from it. */
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   205
        waveInAddBuffer(this->hidden->hin,
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   206
                        &this->hidden->wavebuf[nextbuf],
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   207
                        sizeof (this->hidden->wavebuf[nextbuf]));
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   208
        this->hidden->next_buffer = (nextbuf + 1) % NUM_BUFFERS;
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   209
    }
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   210
}
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   211
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
static void
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
WINMM_CloseDevice(_THIS)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
{
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   215
    int i;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   217
    if (this->hidden->hout) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   218
        waveOutReset(this->hidden->hout);
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   220
        /* Clean up mixing buffers */
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   221
        for (i = 0; i < NUM_BUFFERS; ++i) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   222
            if (this->hidden->wavebuf[i].dwUser != 0xFFFF) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   223
                waveOutUnprepareHeader(this->hidden->hout,
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   224
                                       &this->hidden->wavebuf[i],
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   225
                                       sizeof (this->hidden->wavebuf[i]));
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   226
            }
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
        }
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   228
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   229
        waveOutClose(this->hidden->hout);
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   230
    }
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   232
    if (this->hidden->hin) {
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   233
        waveInReset(this->hidden->hin);
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   234
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   235
        /* Clean up mixing buffers */
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   236
        for (i = 0; i < NUM_BUFFERS; ++i) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   237
            if (this->hidden->wavebuf[i].dwUser != 0xFFFF) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   238
                waveInUnprepareHeader(this->hidden->hin,
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   239
                                       &this->hidden->wavebuf[i],
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   240
                                       sizeof (this->hidden->wavebuf[i]));
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   241
            }
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   242
        }
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   243
        waveInClose(this->hidden->hin);
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   244
    }
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   246
    if (this->hidden->audio_sem) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   247
        CloseHandle(this->hidden->audio_sem);
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   248
    }
7332
b53acf6ab70b Fixed some Visual Studio analyze warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   249
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   250
    SDL_free(this->hidden->mixbuf);
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   251
    SDL_free(this->hidden);
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
7461
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   254
static SDL_bool
8585
2d08b56cbfa9 Fixed 64-bit warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   255
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
   256
{
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   257
    SDL_zerop(pfmt);
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   258
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   259
    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
   260
        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
   261
    } else {
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   262
        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
   263
    }
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   264
    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
   265
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   266
    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
   267
    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
   268
    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
   269
    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
   270
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   271
    if (iscapture) {
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   272
        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
   273
    } else {
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   274
        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
   275
    }
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   276
}
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   277
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
   279
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
   280
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    int valid_datatype = 0;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
    MMRESULT result;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
    WAVEFORMATEX waveformat;
8585
2d08b56cbfa9 Fixed 64-bit warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   285
    UINT devId = WAVE_MAPPER;  /* WAVE_MAPPER == choose system's default */
2d08b56cbfa9 Fixed 64-bit warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   286
    UINT i;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
   288
    if (handle != NULL) {  /* specific device requested? */
9410
e25530faeda0 Audio hotplug fixes for winmm and XAudio2 backends.
Ryan C. Gordon <icculus@icculus.org>
parents: 9406
diff changeset
   289
        /* -1 because we increment the original value to avoid NULL. */
e25530faeda0 Audio hotplug fixes for winmm and XAudio2 backends.
Ryan C. Gordon <icculus@icculus.org>
parents: 9406
diff changeset
   290
        const size_t val = ((size_t) handle) - 1;
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 8585
diff changeset
   291
        devId = (UINT) val;
5588
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
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
    /* Initialize all variables that we clean on shutdown */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
    this->hidden = (struct SDL_PrivateAudioData *)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
        SDL_malloc((sizeof *this->hidden));
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
    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
   298
        return SDL_OutOfMemory();
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
    }
10239
f17581d00c26 audio: changed some SDL_memset() calls to SDL_zero(), other minor corrections.
Ryan C. Gordon <icculus@icculus.org>
parents: 10237
diff changeset
   300
    SDL_zerop(this->hidden);
5588
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
    /* Initialize the wavebuf structures for closing */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
    for (i = 0; i < NUM_BUFFERS; ++i)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
        this->hidden->wavebuf[i].dwUser = 0xFFFF;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
7461
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   306
    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
   307
        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
   308
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
    while ((!valid_datatype) && (test_format)) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
        switch (test_format) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
        case AUDIO_U8:
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
        case AUDIO_S16:
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
        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
   314
        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
   315
            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
   316
            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
   317
                valid_datatype = 1;
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   318
            } else {
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   319
                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
   320
            }
489d2bbcf4aa Make winmm and directsound audio targets robust against unsupported formats.
Ryan C. Gordon <icculus@icculus.org>
parents: 7449
diff changeset
   321
            break;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
        default:
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
            test_format = SDL_NextAudioFormat();
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
            break;
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
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
    if (!valid_datatype) {
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   330
        return SDL_SetError("Unsupported audio format");
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
    /* Update the fragment size as size in bytes */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
    SDL_CalculateAudioSpec(&this->spec);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
    /* Open the audio device */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
    if (iscapture) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
        result = waveInOpen(&this->hidden->hin, devId, &waveformat,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
                             (DWORD_PTR) CaptureSound, (DWORD_PTR) this,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
                             CALLBACK_FUNCTION);
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   341
        if (result != MMSYSERR_NOERROR) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   342
            return SetMMerror("waveInOpen()", result);
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   343
        }
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
    } else {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
        result = waveOutOpen(&this->hidden->hout, devId, &waveformat,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
                             (DWORD_PTR) FillSound, (DWORD_PTR) this,
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
                             CALLBACK_FUNCTION);
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   348
        if (result != MMSYSERR_NOERROR) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   349
            return SetMMerror("waveOutOpen()", result);
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   350
        }
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
#ifdef SOUND_DEBUG
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    /* Check the sound device we retrieved */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
    {
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   356
        if (iscapture) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   357
            WAVEINCAPS caps;
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   358
            result = waveInGetDevCaps((UINT) this->hidden->hout,
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   359
                                      &caps, sizeof (caps));
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   360
            if (result != MMSYSERR_NOERROR) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   361
                return SetMMerror("waveInGetDevCaps()", result);
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   362
            }
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   363
            printf("Audio device: %s\n", caps.szPname);
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   364
        } else {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   365
            WAVEOUTCAPS caps;
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   366
            result = waveOutGetDevCaps((UINT) this->hidden->hout,
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   367
                                       &caps, sizeof(caps));
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   368
            if (result != MMSYSERR_NOERROR) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   369
                return SetMMerror("waveOutGetDevCaps()", result);
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   370
            }
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   371
            printf("Audio device: %s\n", caps.szPname);
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
        }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
#endif
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
    /* Create the audio buffer semaphore */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
    this->hidden->audio_sem =
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   378
		CreateSemaphore(NULL, iscapture ? 0 : NUM_BUFFERS - 1, NUM_BUFFERS, NULL);
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
    if (this->hidden->audio_sem == NULL) {
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   380
        return SDL_SetError("Couldn't create semaphore");
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
    /* Create the sound buffers */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
    this->hidden->mixbuf =
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
        (Uint8 *) SDL_malloc(NUM_BUFFERS * this->spec.size);
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
    if (this->hidden->mixbuf == NULL) {
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   387
        return SDL_OutOfMemory();
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
    }
10239
f17581d00c26 audio: changed some SDL_memset() calls to SDL_zero(), other minor corrections.
Ryan C. Gordon <icculus@icculus.org>
parents: 10237
diff changeset
   389
f17581d00c26 audio: changed some SDL_memset() calls to SDL_zero(), other minor corrections.
Ryan C. Gordon <icculus@icculus.org>
parents: 10237
diff changeset
   390
    SDL_zero(this->hidden->wavebuf);
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
    for (i = 0; i < NUM_BUFFERS; ++i) {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
        this->hidden->wavebuf[i].dwBufferLength = this->spec.size;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
        this->hidden->wavebuf[i].dwFlags = WHDR_DONE;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
        this->hidden->wavebuf[i].lpData =
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
            (LPSTR) & this->hidden->mixbuf[i * this->spec.size];
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   396
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   397
        if (iscapture) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   398
            result = waveInPrepareHeader(this->hidden->hin,
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   399
                                          &this->hidden->wavebuf[i],
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   400
                                          sizeof(this->hidden->wavebuf[i]));
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   401
            if (result != MMSYSERR_NOERROR) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   402
                return SetMMerror("waveInPrepareHeader()", result);
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   403
            }
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   404
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   405
            result = waveInAddBuffer(this->hidden->hin,
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   406
                                     &this->hidden->wavebuf[i],
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   407
                                     sizeof(this->hidden->wavebuf[i]));
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   408
            if (result != MMSYSERR_NOERROR) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   409
                return SetMMerror("waveInAddBuffer()", result);
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   410
            }
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   411
        } else {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   412
            result = waveOutPrepareHeader(this->hidden->hout,
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   413
                                          &this->hidden->wavebuf[i],
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   414
                                          sizeof(this->hidden->wavebuf[i]));
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   415
            if (result != MMSYSERR_NOERROR) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   416
                return SetMMerror("waveOutPrepareHeader()", result);
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   417
            }
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   418
        }
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   419
    }
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   420
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   421
    if (iscapture) {
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   422
        result = waveInStart(this->hidden->hin);
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
        if (result != MMSYSERR_NOERROR) {
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   424
            return SetMMerror("waveInStart()", result);
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
        }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
    }
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   428
    return 0;                   /* Ready to go! */
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
static int
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
WINMM_Init(SDL_AudioDriverImpl * impl)
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
{
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
    /* Set the function pointers */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
    impl->DetectDevices = WINMM_DetectDevices;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
    impl->OpenDevice = WINMM_OpenDevice;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
    impl->PlayDevice = WINMM_PlayDevice;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
    impl->WaitDevice = WINMM_WaitDevice;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
    impl->WaitDone = WINMM_WaitDone;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
    impl->GetDeviceBuf = WINMM_GetDeviceBuf;
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   442
    impl->CaptureFromDevice = WINMM_CaptureFromDevice;
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   443
    impl->FlushCapture = WINMM_FlushCapture;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
    impl->CloseDevice = WINMM_CloseDevice;
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
10248
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   446
    impl->HasCaptureSupport = SDL_TRUE;
7c430819edd2 winmm: Implemented audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   447
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
    return 1;   /* this audio target is available. */
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
}
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
AudioBootStrap WINMM_bootstrap = {
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
    "winmm", "Windows Waveform Audio", WINMM_Init, 0
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
};
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5593
diff changeset
   455
#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
   456
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
/* vi: set ts=4 sw=4 expandtab: */