src/audio/SDL_audio.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Mon, 23 Mar 2015 20:24:04 +0100
changeset 9421 fd5dd91ec0ce
parent 9410 e25530faeda0
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Android: Removed not needed include statement.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
     2
  Simple DirectMedia Layer
8149
681eb46b8ac4 Fixed bug 2374 - Update copyright for 2014...
Sam Lantinga <slouken@libsdl.org>
parents: 8094
diff changeset
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
     7
  arising from the use of this software.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    11
  freely, subject to the following restrictions:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
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: 7981
diff changeset
    21
#include "../SDL_internal.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
/* Allow access to a raw mixing buffer */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    24
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
#include "SDL.h"
2984
0b160c970b7e Fixed some dependency issues with SDL_revision.h
Sam Lantinga <slouken@libsdl.org>
parents: 2979
diff changeset
    26
#include "SDL_audio.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
#include "SDL_audio_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
#include "SDL_audiomem.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    29
#include "SDL_sysaudio.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
4865
fff50e86c891 Update VS2010 project to add new files; update new files so code builds on Win32/Win64
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 4729
diff changeset
    31
#define _THIS SDL_AudioDevice *_this
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    32
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    33
static SDL_AudioDriver current_audio;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    34
static SDL_AudioDevice *open_devices[16];
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    35
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    36
/* !!! FIXME: These are wordy and unlocalized... */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    37
#define DEFAULT_OUTPUT_DEVNAME "System audio output device"
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    38
#define DEFAULT_INPUT_DEVNAME "System audio capture device"
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    39
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    40
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    41
/*
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    42
 * Not all of these will be compiled and linked in, but it's convenient
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    43
 *  to have a complete list here and saves yet-another block of #ifdefs...
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    44
 *  Please see bootstrap[], below, for the actual #ifdef mess.
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    45
 */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    46
extern AudioBootStrap BSD_AUDIO_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    47
extern AudioBootStrap DSP_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    48
extern AudioBootStrap ALSA_bootstrap;
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2147
diff changeset
    49
extern AudioBootStrap PULSEAUDIO_bootstrap;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 3096
diff changeset
    50
extern AudioBootStrap QSAAUDIO_bootstrap;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    51
extern AudioBootStrap SUNAUDIO_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    52
extern AudioBootStrap ARTS_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    53
extern AudioBootStrap ESD_bootstrap;
8833
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
    54
extern AudioBootStrap NACLAUD_bootstrap;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    55
extern AudioBootStrap NAS_bootstrap;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    56
extern AudioBootStrap XAUDIO2_bootstrap;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    57
extern AudioBootStrap DSOUND_bootstrap;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents: 5586
diff changeset
    58
extern AudioBootStrap WINMM_bootstrap;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    59
extern AudioBootStrap PAUDIO_bootstrap;
7981
6d538bc1b395 Renamed things named after BeOS to be named after Haiku instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 7904
diff changeset
    60
extern AudioBootStrap HAIKUAUDIO_bootstrap;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    61
extern AudioBootStrap COREAUDIO_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    62
extern AudioBootStrap SNDMGR_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    63
extern AudioBootStrap DISKAUD_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    64
extern AudioBootStrap DUMMYAUD_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    65
extern AudioBootStrap DCAUD_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    66
extern AudioBootStrap DART_bootstrap;
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
    67
extern AudioBootStrap NDSAUD_bootstrap;
2947
fec0db6c44b7 Date: Thu, 01 Jan 2009 21:32:12 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2942
diff changeset
    68
extern AudioBootStrap FUSIONSOUND_bootstrap;
4718
79aa6c61f33b Added stub android sound system
Paul Hunkin <paul@bieh.net>
parents: 3699
diff changeset
    69
extern AudioBootStrap ANDROIDAUD_bootstrap;
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6977
diff changeset
    70
extern AudioBootStrap PSPAUD_bootstrap;
7367
d6eabafa8776 Added an SDL2 OpenBSD sndio(7) audio target.
Ryan C. Gordon <icculus@icculus.org>
parents: 7348
diff changeset
    71
extern AudioBootStrap SNDIO_bootstrap;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9148
diff changeset
    72
extern AudioBootStrap EmscriptenAudio_bootstrap;
1190
173c063d4f55 OS/2 port!
Ryan C. Gordon <icculus@icculus.org>
parents: 955
diff changeset
    73
8833
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
    74
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    75
/* Available audio drivers */
3162
dc1eb82ffdaa Von: Thomas Zimmermann
Sam Lantinga <slouken@libsdl.org>
parents: 3139
diff changeset
    76
static const AudioBootStrap *const bootstrap[] = {
2939
084e5b4fc5be Audio driver priority: favor PulseAudio and ALSA over OSS.
Ryan C. Gordon <icculus@icculus.org>
parents: 2938
diff changeset
    77
#if SDL_AUDIO_DRIVER_PULSEAUDIO
084e5b4fc5be Audio driver priority: favor PulseAudio and ALSA over OSS.
Ryan C. Gordon <icculus@icculus.org>
parents: 2938
diff changeset
    78
    &PULSEAUDIO_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    79
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    80
#if SDL_AUDIO_DRIVER_ALSA
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
    81
    &ALSA_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    82
#endif
7367
d6eabafa8776 Added an SDL2 OpenBSD sndio(7) audio target.
Ryan C. Gordon <icculus@icculus.org>
parents: 7348
diff changeset
    83
#if SDL_AUDIO_DRIVER_SNDIO
d6eabafa8776 Added an SDL2 OpenBSD sndio(7) audio target.
Ryan C. Gordon <icculus@icculus.org>
parents: 7348
diff changeset
    84
    &SNDIO_bootstrap,
d6eabafa8776 Added an SDL2 OpenBSD sndio(7) audio target.
Ryan C. Gordon <icculus@icculus.org>
parents: 7348
diff changeset
    85
#endif
4548
b135d19f9764 RedHat patch: SDL-1.2.14-audiodriver.patch
Sam Lantinga <slouken@libsdl.org>
parents: 4507
diff changeset
    86
#if SDL_AUDIO_DRIVER_BSD
b135d19f9764 RedHat patch: SDL-1.2.14-audiodriver.patch
Sam Lantinga <slouken@libsdl.org>
parents: 4507
diff changeset
    87
    &BSD_AUDIO_bootstrap,
b135d19f9764 RedHat patch: SDL-1.2.14-audiodriver.patch
Sam Lantinga <slouken@libsdl.org>
parents: 4507
diff changeset
    88
#endif
2939
084e5b4fc5be Audio driver priority: favor PulseAudio and ALSA over OSS.
Ryan C. Gordon <icculus@icculus.org>
parents: 2938
diff changeset
    89
#if SDL_AUDIO_DRIVER_OSS
084e5b4fc5be Audio driver priority: favor PulseAudio and ALSA over OSS.
Ryan C. Gordon <icculus@icculus.org>
parents: 2938
diff changeset
    90
    &DSP_bootstrap,
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2147
diff changeset
    91
#endif
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 3096
diff changeset
    92
#if SDL_AUDIO_DRIVER_QSA
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 3096
diff changeset
    93
    &QSAAUDIO_bootstrap,
663
8bedd6d61642 Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents: 654
diff changeset
    94
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    95
#if SDL_AUDIO_DRIVER_SUNAUDIO
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
    96
    &SUNAUDIO_bootstrap,
148
8758b8d42cd9 Audio subsystem no longer assumes sun audio API on UNIX systems
Sam Lantinga <slouken@libsdl.org>
parents: 121
diff changeset
    97
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    98
#if SDL_AUDIO_DRIVER_ARTS
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
    99
    &ARTS_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   100
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   101
#if SDL_AUDIO_DRIVER_ESD
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   102
    &ESD_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   103
#endif
8833
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
   104
#if SDL_AUDIO_DRIVER_NACL
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
   105
   &NACLAUD_bootstrap,
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
   106
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   107
#if SDL_AUDIO_DRIVER_NAS
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   108
    &NAS_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   109
#endif
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
   110
#if SDL_AUDIO_DRIVER_XAUDIO2
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
   111
    &XAUDIO2_bootstrap,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
   112
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   113
#if SDL_AUDIO_DRIVER_DSOUND
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   114
    &DSOUND_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   115
#endif
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents: 5586
diff changeset
   116
#if SDL_AUDIO_DRIVER_WINMM
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents: 5586
diff changeset
   117
    &WINMM_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   118
#endif
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   119
#if SDL_AUDIO_DRIVER_PAUDIO
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   120
    &PAUDIO_bootstrap,
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   121
#endif
7981
6d538bc1b395 Renamed things named after BeOS to be named after Haiku instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 7904
diff changeset
   122
#if SDL_AUDIO_DRIVER_HAIKU
6d538bc1b395 Renamed things named after BeOS to be named after Haiku instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 7904
diff changeset
   123
    &HAIKUAUDIO_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   124
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   125
#if SDL_AUDIO_DRIVER_COREAUDIO
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   126
    &COREAUDIO_bootstrap,
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents: 808
diff changeset
   127
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   128
#if SDL_AUDIO_DRIVER_DISK
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   129
    &DISKAUD_bootstrap,
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents: 38
diff changeset
   130
#endif
1532
30f189cdd82b Implemented dummy audio driver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1487
diff changeset
   131
#if SDL_AUDIO_DRIVER_DUMMY
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   132
    &DUMMYAUD_bootstrap,
1532
30f189cdd82b Implemented dummy audio driver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1487
diff changeset
   133
#endif
2947
fec0db6c44b7 Date: Thu, 01 Jan 2009 21:32:12 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2942
diff changeset
   134
#if SDL_AUDIO_DRIVER_FUSIONSOUND
fec0db6c44b7 Date: Thu, 01 Jan 2009 21:32:12 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2942
diff changeset
   135
    &FUSIONSOUND_bootstrap,
fec0db6c44b7 Date: Thu, 01 Jan 2009 21:32:12 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2942
diff changeset
   136
#endif
4718
79aa6c61f33b Added stub android sound system
Paul Hunkin <paul@bieh.net>
parents: 3699
diff changeset
   137
#if SDL_AUDIO_DRIVER_ANDROID
79aa6c61f33b Added stub android sound system
Paul Hunkin <paul@bieh.net>
parents: 3699
diff changeset
   138
    &ANDROIDAUD_bootstrap,
79aa6c61f33b Added stub android sound system
Paul Hunkin <paul@bieh.net>
parents: 3699
diff changeset
   139
#endif
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6977
diff changeset
   140
#if SDL_AUDIO_DRIVER_PSP
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6977
diff changeset
   141
    &PSPAUD_bootstrap,
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6977
diff changeset
   142
#endif
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9148
diff changeset
   143
#if SDL_AUDIO_DRIVER_EMSCRIPTEN
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9148
diff changeset
   144
    &EmscriptenAudio_bootstrap,
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9148
diff changeset
   145
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   146
    NULL
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   147
};
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   148
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   149
static SDL_AudioDevice *
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   150
get_audio_device(SDL_AudioDeviceID id)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   151
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   152
    id--;
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   153
    if ((id >= SDL_arraysize(open_devices)) || (open_devices[id] == NULL)) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   154
        SDL_SetError("Invalid audio device ID");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   155
        return NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   156
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   157
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   158
    return open_devices[id];
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   159
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   160
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   161
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   162
/* stubs for audio drivers that don't need a specific entry point... */
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   163
static void
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   164
SDL_AudioDetectDevices_Default(void)
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   165
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   166
    /* you have to write your own implementation if these assertions fail. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   167
    SDL_assert(current_audio.impl.OnlyHasDefaultOutputDevice);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   168
    SDL_assert(current_audio.impl.OnlyHasDefaultInputDevice || !current_audio.impl.HasCaptureSupport);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   169
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   170
    SDL_AddAudioDevice(SDL_FALSE, DEFAULT_OUTPUT_DEVNAME, (void *) ((size_t) 0x1));
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   171
    if (current_audio.impl.HasCaptureSupport) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   172
        SDL_AddAudioDevice(SDL_TRUE, DEFAULT_INPUT_DEVNAME, (void *) ((size_t) 0x2));
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   173
    }
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   174
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   175
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   176
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   177
SDL_AudioThreadInit_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   178
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   179
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   180
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   181
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   182
SDL_AudioWaitDevice_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   183
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   184
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   185
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   186
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   187
SDL_AudioPlayDevice_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   188
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   189
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   190
9031
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   191
static int
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   192
SDL_AudioGetPendingBytes_Default(_THIS)
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   193
{
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   194
    return 0;
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   195
}
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   196
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   197
static Uint8 *
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   198
SDL_AudioGetDeviceBuf_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   199
{
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   200
    return NULL;
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   201
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   202
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   203
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   204
SDL_AudioWaitDone_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   205
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   206
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   207
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   208
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   209
SDL_AudioCloseDevice_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   210
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   211
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   212
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   213
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   214
SDL_AudioDeinitialize_Default(void)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   215
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   216
}
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   217
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   218
static void
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   219
SDL_AudioFreeDeviceHandle_Default(void *handle)
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   220
{                               /* no-op. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   221
}
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   222
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   223
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   224
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   225
SDL_AudioOpenDevice_Default(_THIS, void *handle, const char *devname, int iscapture)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   226
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   227
    return SDL_Unsupported();
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   228
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   229
9010
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   230
static SDL_INLINE SDL_bool
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   231
is_in_audio_device_thread(SDL_AudioDevice * device)
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   232
{
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   233
    /* The device thread locks the same mutex, but not through the public API.
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   234
       This check is in case the application, in the audio callback,
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   235
       tries to lock the thread that we've already locked from the
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   236
       device thread...just in case we only have non-recursive mutexes. */
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   237
    if (device->thread && (SDL_ThreadID() == device->threadid)) {
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   238
        return SDL_TRUE;
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   239
    }
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   240
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   241
    return SDL_FALSE;
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   242
}
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   243
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   244
static void
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   245
SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   246
{
9010
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   247
    if (!is_in_audio_device_thread(device)) {
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   248
        SDL_LockMutex(device->mixer_lock);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   249
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   250
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   251
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   252
static void
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   253
SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   254
{
9010
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   255
    if (!is_in_audio_device_thread(device)) {
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   256
        SDL_UnlockMutex(device->mixer_lock);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   257
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   258
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   259
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   260
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   261
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   262
finalize_audio_entry_points(void)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   263
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   264
    /*
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   265
     * Fill in stub functions for unused driver entry points. This lets us
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   266
     *  blindly call them without having to check for validity first.
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   267
     */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   268
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   269
#define FILL_STUB(x) \
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   270
        if (current_audio.impl.x == NULL) { \
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   271
            current_audio.impl.x = SDL_Audio##x##_Default; \
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   272
        }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   273
    FILL_STUB(DetectDevices);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   274
    FILL_STUB(OpenDevice);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   275
    FILL_STUB(ThreadInit);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   276
    FILL_STUB(WaitDevice);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   277
    FILL_STUB(PlayDevice);
9031
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   278
    FILL_STUB(GetPendingBytes);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   279
    FILL_STUB(GetDeviceBuf);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   280
    FILL_STUB(WaitDone);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   281
    FILL_STUB(CloseDevice);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   282
    FILL_STUB(LockDevice);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   283
    FILL_STUB(UnlockDevice);
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   284
    FILL_STUB(FreeDeviceHandle);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   285
    FILL_STUB(Deinitialize);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   286
#undef FILL_STUB
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   287
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   288
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   289
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   290
/* device hotplug support... */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   291
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   292
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   293
add_audio_device(const char *name, void *handle, SDL_AudioDeviceItem **devices, int *devCount)
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   294
{
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   295
    int retval = -1;
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   296
    const size_t size = sizeof (SDL_AudioDeviceItem) + SDL_strlen(name) + 1;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   297
    SDL_AudioDeviceItem *item = (SDL_AudioDeviceItem *) SDL_malloc(size);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   298
    if (item == NULL) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   299
        return -1;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   300
    }
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   301
9410
e25530faeda0 Audio hotplug fixes for winmm and XAudio2 backends.
Ryan C. Gordon <icculus@icculus.org>
parents: 9405
diff changeset
   302
    SDL_assert(handle != NULL);  /* we reserve NULL, audio backends can't use it. */
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   303
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   304
    item->handle = handle;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   305
    SDL_strlcpy(item->name, name, size - sizeof (SDL_AudioDeviceItem));
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   306
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   307
    SDL_LockMutex(current_audio.detectionLock);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   308
    item->next = *devices;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   309
    *devices = item;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   310
    retval = (*devCount)++;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   311
    SDL_UnlockMutex(current_audio.detectionLock);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   312
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   313
    return retval;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   314
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   315
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   316
static SDL_INLINE int
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   317
add_capture_device(const char *name, void *handle)
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   318
{
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   319
    /* !!! FIXME: add this later. SDL_assert(current_audio.impl.HasCaptureSupport);*/
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   320
    return add_audio_device(name, handle, &current_audio.inputDevices, &current_audio.inputDeviceCount);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   321
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   322
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   323
static SDL_INLINE int
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   324
add_output_device(const char *name, void *handle)
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   325
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   326
    return add_audio_device(name, handle, &current_audio.outputDevices, &current_audio.outputDeviceCount);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   327
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   328
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   329
static void
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   330
free_device_list(SDL_AudioDeviceItem **devices, int *devCount)
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   331
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   332
    SDL_AudioDeviceItem *item, *next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   333
    for (item = *devices; item != NULL; item = next) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   334
        next = item->next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   335
        if (item->handle != NULL) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   336
            current_audio.impl.FreeDeviceHandle(item->handle);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   337
        }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   338
        SDL_free(item);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   339
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   340
    *devices = NULL;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   341
    *devCount = 0;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   342
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   343
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   344
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   345
/* The audio backends call this when a new device is plugged in. */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   346
void
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   347
SDL_AddAudioDevice(const int iscapture, const char *name, void *handle)
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   348
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   349
    const int device_index = iscapture ? add_capture_device(name, handle) : add_output_device(name, handle);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   350
    if (device_index != -1) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   351
        /* Post the event, if desired */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   352
        if (SDL_GetEventState(SDL_AUDIODEVICEADDED) == SDL_ENABLE) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   353
            SDL_Event event;
9401
529007547fb6 Zero out the audio hotplug event structure, so the "padded" fields are sane.
Ryan C. Gordon <icculus@icculus.org>
parents: 9399
diff changeset
   354
            SDL_zero(event);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   355
            event.adevice.type = SDL_AUDIODEVICEADDED;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   356
            event.adevice.which = device_index;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   357
            event.adevice.iscapture = iscapture;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   358
            SDL_PushEvent(&event);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   359
        }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   360
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   361
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   362
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   363
/* The audio backends call this when a currently-opened device is lost. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   364
void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device)
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   365
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   366
    SDL_assert(get_audio_device(device->id) == device);
9396
69c501ed36f3 Don't assert if an audio backend reports the same device disconnected twice.
Ryan C. Gordon <icculus@icculus.org>
parents: 9394
diff changeset
   367
69c501ed36f3 Don't assert if an audio backend reports the same device disconnected twice.
Ryan C. Gordon <icculus@icculus.org>
parents: 9394
diff changeset
   368
    if (!device->enabled) {
69c501ed36f3 Don't assert if an audio backend reports the same device disconnected twice.
Ryan C. Gordon <icculus@icculus.org>
parents: 9394
diff changeset
   369
        return;
69c501ed36f3 Don't assert if an audio backend reports the same device disconnected twice.
Ryan C. Gordon <icculus@icculus.org>
parents: 9394
diff changeset
   370
    }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   371
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   372
    /* Ends the audio callback and mark the device as STOPPED, but the
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   373
       app still needs to close the device to free resources. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   374
    current_audio.impl.LockDevice(device);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   375
    device->enabled = 0;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   376
    current_audio.impl.UnlockDevice(device);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   377
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   378
    /* Post the event, if desired */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   379
    if (SDL_GetEventState(SDL_AUDIODEVICEREMOVED) == SDL_ENABLE) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   380
        SDL_Event event;
9401
529007547fb6 Zero out the audio hotplug event structure, so the "padded" fields are sane.
Ryan C. Gordon <icculus@icculus.org>
parents: 9399
diff changeset
   381
        SDL_zero(event);
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   382
        event.adevice.type = SDL_AUDIODEVICEREMOVED;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   383
        event.adevice.which = device->id;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   384
        event.adevice.iscapture = device->iscapture ? 1 : 0;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   385
        SDL_PushEvent(&event);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   386
    }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   387
}
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   388
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   389
static void
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   390
mark_device_removed(void *handle, SDL_AudioDeviceItem *devices, SDL_bool *removedFlag)
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   391
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   392
    SDL_AudioDeviceItem *item;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   393
    SDL_assert(handle != NULL);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   394
    for (item = devices; item != NULL; item = item->next) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   395
        if (item->handle == handle) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   396
            item->handle = NULL;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   397
            *removedFlag = SDL_TRUE;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   398
            return;
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   399
        }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   400
    }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   401
}
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   402
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   403
/* The audio backends call this when a device is removed from the system. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   404
void
9399
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   405
SDL_RemoveAudioDevice(const int iscapture, void *handle)
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   406
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   407
    SDL_LockMutex(current_audio.detectionLock);
9399
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   408
    if (iscapture) {
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   409
        mark_device_removed(handle, current_audio.inputDevices, &current_audio.captureDevicesRemoved);
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   410
    } else {
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   411
        mark_device_removed(handle, current_audio.outputDevices, &current_audio.outputDevicesRemoved);
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   412
    }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   413
    SDL_UnlockMutex(current_audio.detectionLock);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   414
    current_audio.impl.FreeDeviceHandle(handle);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   415
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   416
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   417
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   418
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   419
/* buffer queueing support... */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   420
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   421
/* this expects that you managed thread safety elsewhere. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   422
static void
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   423
free_audio_queue(SDL_AudioBufferQueue *buffer)
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   424
{
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   425
    while (buffer) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   426
        SDL_AudioBufferQueue *next = buffer->next;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   427
        SDL_free(buffer);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   428
        buffer = next;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   429
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   430
}
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   431
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   432
static void SDLCALL
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   433
SDL_BufferQueueDrainCallback(void *userdata, Uint8 *stream, int _len)
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   434
{
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   435
    /* this function always holds the mixer lock before being called. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   436
    Uint32 len = (Uint32) _len;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   437
    SDL_AudioDevice *device = (SDL_AudioDevice *) userdata;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   438
    SDL_AudioBufferQueue *buffer;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   439
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   440
    SDL_assert(device != NULL);  /* this shouldn't ever happen, right?! */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   441
    SDL_assert(_len >= 0);  /* this shouldn't ever happen, right?! */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   442
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   443
    while ((len > 0) && ((buffer = device->buffer_queue_head) != NULL)) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   444
        const Uint32 avail = buffer->datalen - buffer->startpos;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   445
        const Uint32 cpy = SDL_min(len, avail);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   446
        SDL_assert(device->queued_bytes >= avail);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   447
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   448
        SDL_memcpy(stream, buffer->data + buffer->startpos, cpy);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   449
        buffer->startpos += cpy;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   450
        stream += cpy;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   451
        device->queued_bytes -= cpy;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   452
        len -= cpy;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   453
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   454
        if (buffer->startpos == buffer->datalen) {  /* packet is done, put it in the pool. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   455
            device->buffer_queue_head = buffer->next;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   456
            SDL_assert((buffer->next != NULL) || (buffer == device->buffer_queue_tail));
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   457
            buffer->next = device->buffer_queue_pool;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   458
            device->buffer_queue_pool = buffer;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   459
        }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   460
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   461
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   462
    SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   463
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   464
    if (len > 0) {  /* fill any remaining space in the stream with silence. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   465
        SDL_assert(device->buffer_queue_head == NULL);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   466
        SDL_memset(stream, device->spec.silence, len);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   467
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   468
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   469
    if (device->buffer_queue_head == NULL) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   470
        device->buffer_queue_tail = NULL;  /* in case we drained the queue entirely. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   471
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   472
}
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   473
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   474
int
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   475
SDL_QueueAudio(SDL_AudioDeviceID devid, const void *_data, Uint32 len)
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   476
{
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   477
    SDL_AudioDevice *device = get_audio_device(devid);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   478
    const Uint8 *data = (const Uint8 *) _data;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   479
    SDL_AudioBufferQueue *orighead;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   480
    SDL_AudioBufferQueue *origtail;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   481
    Uint32 origlen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   482
    Uint32 datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   483
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   484
    if (!device) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   485
        return -1;  /* get_audio_device() will have set the error state */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   486
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   487
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   488
    if (device->spec.callback != SDL_BufferQueueDrainCallback) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   489
        return SDL_SetError("Audio device has a callback, queueing not allowed");
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   490
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   491
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   492
    current_audio.impl.LockDevice(device);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   493
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   494
    orighead = device->buffer_queue_head;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   495
    origtail = device->buffer_queue_tail;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   496
    origlen = origtail ? origtail->datalen : 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   497
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   498
    while (len > 0) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   499
        SDL_AudioBufferQueue *packet = device->buffer_queue_tail;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   500
        SDL_assert(!packet || (packet->datalen <= SDL_AUDIOBUFFERQUEUE_PACKETLEN));
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   501
        if (!packet || (packet->datalen >= SDL_AUDIOBUFFERQUEUE_PACKETLEN)) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   502
            /* tail packet missing or completely full; we need a new packet. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   503
            packet = device->buffer_queue_pool;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   504
            if (packet != NULL) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   505
                /* we have one available in the pool. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   506
                device->buffer_queue_pool = packet->next;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   507
            } else {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   508
                /* Have to allocate a new one! */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   509
                packet = (SDL_AudioBufferQueue *) SDL_malloc(sizeof (SDL_AudioBufferQueue));
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   510
                if (packet == NULL) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   511
                    /* uhoh, reset so we've queued nothing new, free what we can. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   512
                    if (!origtail) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   513
                        packet = device->buffer_queue_head;  /* whole queue. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   514
                    } else {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   515
                        packet = origtail->next;  /* what we added to existing queue. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   516
                        origtail->next = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   517
                        origtail->datalen = origlen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   518
                    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   519
                    device->buffer_queue_head = orighead;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   520
                    device->buffer_queue_tail = origtail;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   521
                    device->buffer_queue_pool = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   522
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   523
                    current_audio.impl.UnlockDevice(device);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   524
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   525
                    free_audio_queue(packet);  /* give back what we can. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   526
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   527
                    return SDL_OutOfMemory();
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   528
                }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   529
            }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   530
            packet->datalen = 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   531
            packet->startpos = 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   532
            packet->next = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   533
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   534
            SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   535
            if (device->buffer_queue_tail == NULL) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   536
                device->buffer_queue_head = packet;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   537
            } else {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   538
                device->buffer_queue_tail->next = packet;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   539
            }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   540
            device->buffer_queue_tail = packet;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   541
        }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   542
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   543
        datalen = SDL_min(len, SDL_AUDIOBUFFERQUEUE_PACKETLEN - packet->datalen);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   544
        SDL_memcpy(packet->data + packet->datalen, data, datalen);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   545
        data += datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   546
        len -= datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   547
        packet->datalen += datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   548
        device->queued_bytes += datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   549
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   550
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   551
    current_audio.impl.UnlockDevice(device);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   552
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   553
    return 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   554
}
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   555
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   556
Uint32
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   557
SDL_GetQueuedAudioSize(SDL_AudioDeviceID devid)
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   558
{
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   559
    Uint32 retval = 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   560
    SDL_AudioDevice *device = get_audio_device(devid);
9032
36c472422d64 SDL_GetQueuedAudioSize() shouldn't grab lock when not set up for queueing.
Ryan C. Gordon <icculus@icculus.org>
parents: 9031
diff changeset
   561
36c472422d64 SDL_GetQueuedAudioSize() shouldn't grab lock when not set up for queueing.
Ryan C. Gordon <icculus@icculus.org>
parents: 9031
diff changeset
   562
    /* Nothing to do unless we're set up for queueing. */
36c472422d64 SDL_GetQueuedAudioSize() shouldn't grab lock when not set up for queueing.
Ryan C. Gordon <icculus@icculus.org>
parents: 9031
diff changeset
   563
    if (device && (device->spec.callback == SDL_BufferQueueDrainCallback)) {
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   564
        current_audio.impl.LockDevice(device);
9031
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   565
        retval = device->queued_bytes + current_audio.impl.GetPendingBytes(device);
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   566
        current_audio.impl.UnlockDevice(device);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   567
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   568
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   569
    return retval;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   570
}
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   571
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   572
void
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   573
SDL_ClearQueuedAudio(SDL_AudioDeviceID devid)
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   574
{
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   575
    SDL_AudioDevice *device = get_audio_device(devid);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   576
    SDL_AudioBufferQueue *buffer = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   577
    if (!device) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   578
        return;  /* nothing to do. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   579
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   580
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   581
    /* Blank out the device and release the mutex. Free it afterwards. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   582
    current_audio.impl.LockDevice(device);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   583
    buffer = device->buffer_queue_head;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   584
    device->buffer_queue_tail = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   585
    device->buffer_queue_head = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   586
    device->queued_bytes = 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   587
    current_audio.impl.UnlockDevice(device);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   588
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   589
    free_audio_queue(buffer);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   590
}
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   591
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   592
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   593
/* The general mixing thread function */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   594
int SDLCALL
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   595
SDL_RunAudio(void *devicep)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   596
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   597
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   598
    const int silence = (int) device->spec.silence;
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   599
    const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   600
    const int stream_len = (device->convert.needed) ? device->convert.len : device->spec.size;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   601
    Uint8 *stream;
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   602
    void *udata = device->spec.userdata;
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   603
    void (SDLCALL *fill) (void *, Uint8 *, int) = device->spec.callback;
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   604
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   605
    /* The audio mixing is always a high priority thread */
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   606
    SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   607
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   608
    /* Perform any thread setup */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   609
    device->threadid = SDL_ThreadID();
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   610
    current_audio.impl.ThreadInit(device);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   611
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   612
    /* Loop, filling the audio buffers */
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   613
    while (!device->shutdown) {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   614
        /* Fill the current buffer with sound */
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   615
        if (device->convert.needed) {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   616
            stream = device->convert.buf;
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   617
        } else if (device->enabled) {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   618
            stream = current_audio.impl.GetDeviceBuf(device);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   619
        } else {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   620
            /* if the device isn't enabled, we still write to the
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   621
               fake_stream, so the app's callback will fire with
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   622
               a regular frequency, in case they depend on that
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   623
               for timing or progress. They can use hotplug
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   624
               now to know if the device failed. */
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   625
            stream = NULL;
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   626
        }
3336
00fab0ebfe54 Fixed a bug where when the audio starts paused all the DirectSound buffers
Sam Lantinga <slouken@libsdl.org>
parents: 3283
diff changeset
   627
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   628
        if (stream == NULL) {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   629
            stream = device->fake_stream;
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   630
        }
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   631
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   632
        /* !!! FIXME: this should be LockDevice. */
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   633
        SDL_LockMutex(device->mixer_lock);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   634
        if (device->paused) {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   635
            SDL_memset(stream, silence, stream_len);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   636
        } else {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   637
            (*fill) (udata, stream, stream_len);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   638
        }
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   639
        SDL_UnlockMutex(device->mixer_lock);
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   640
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   641
        /* Convert the audio if necessary */
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   642
        if (device->enabled && device->convert.needed) {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   643
            SDL_ConvertAudio(&device->convert);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   644
            stream = current_audio.impl.GetDeviceBuf(device);
9397
d72d2aa46341 Disconnected/broken/lost audio devices now continue to fire their callback.
Ryan C. Gordon <icculus@icculus.org>
parents: 9396
diff changeset
   645
            if (stream == NULL) {
d72d2aa46341 Disconnected/broken/lost audio devices now continue to fire their callback.
Ryan C. Gordon <icculus@icculus.org>
parents: 9396
diff changeset
   646
                stream = device->fake_stream;
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   647
            } else {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   648
                SDL_memcpy(stream, device->convert.buf,
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   649
                           device->convert.len_cvt);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   650
            }
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   651
        }
1562
1d487a3fab41 Fixed bug #18
Sam Lantinga <slouken@libsdl.org>
parents: 1561
diff changeset
   652
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   653
        /* Ready current buffer for play and change current buffer */
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   654
        if (stream == device->fake_stream) {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   655
            SDL_Delay(delay);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   656
        } else {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   657
            current_audio.impl.PlayDevice(device);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   658
            current_audio.impl.WaitDevice(device);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   659
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   660
    }
1562
1d487a3fab41 Fixed bug #18
Sam Lantinga <slouken@libsdl.org>
parents: 1561
diff changeset
   661
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   662
    /* Wait for the audio to drain. */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   663
    current_audio.impl.WaitDone(device);
21
75a95f82bc1f Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   664
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   665
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   666
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   667
322
fd93a09655e3 The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   668
1982
3b4ce57c6215 First shot at new audio data types (int32 and float32).
Ryan C. Gordon <icculus@icculus.org>
parents: 1978
diff changeset
   669
static SDL_AudioFormat
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   670
SDL_ParseAudioFormat(const char *string)
1794
5605a9820134 Fixed bug #137
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   671
{
2076
939420dee849 Use SDL_strcmp(), not strcmp().
Ryan C. Gordon <icculus@icculus.org>
parents: 2070
diff changeset
   672
#define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   673
    CHECK_FMT_STRING(U8);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   674
    CHECK_FMT_STRING(S8);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   675
    CHECK_FMT_STRING(U16LSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   676
    CHECK_FMT_STRING(S16LSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   677
    CHECK_FMT_STRING(U16MSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   678
    CHECK_FMT_STRING(S16MSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   679
    CHECK_FMT_STRING(U16SYS);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   680
    CHECK_FMT_STRING(S16SYS);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   681
    CHECK_FMT_STRING(U16);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   682
    CHECK_FMT_STRING(S16);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   683
    CHECK_FMT_STRING(S32LSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   684
    CHECK_FMT_STRING(S32MSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   685
    CHECK_FMT_STRING(S32SYS);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   686
    CHECK_FMT_STRING(S32);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   687
    CHECK_FMT_STRING(F32LSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   688
    CHECK_FMT_STRING(F32MSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   689
    CHECK_FMT_STRING(F32SYS);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   690
    CHECK_FMT_STRING(F32);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   691
#undef CHECK_FMT_STRING
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   692
    return 0;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   693
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   694
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   695
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   696
SDL_GetNumAudioDrivers(void)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   697
{
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   698
    return SDL_arraysize(bootstrap) - 1;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   699
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   700
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   701
const char *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   702
SDL_GetAudioDriver(int index)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   703
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   704
    if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   705
        return bootstrap[index]->name;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   706
    }
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   707
    return NULL;
1794
5605a9820134 Fixed bug #137
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   708
}
5605a9820134 Fixed bug #137
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   709
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   710
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   711
SDL_AudioInit(const char *driver_name)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   712
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   713
    int i = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   714
    int initialized = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   715
    int tried_to_init = 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   716
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   717
    if (SDL_WasInit(SDL_INIT_AUDIO)) {
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   718
        SDL_AudioQuit();        /* shutdown driver if already running. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   719
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   720
9392
92e23eff9b89 Changed some SDL_memset() calls to SDL_zero().
Ryan C. Gordon <icculus@icculus.org>
parents: 9391
diff changeset
   721
    SDL_zero(current_audio);
92e23eff9b89 Changed some SDL_memset() calls to SDL_zero().
Ryan C. Gordon <icculus@icculus.org>
parents: 9391
diff changeset
   722
    SDL_zero(open_devices);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   723
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   724
    /* Select the proper audio driver */
1909
8f1ab2f7c722 Look at environment variables in SDL_VideoInit() and SDL_AudioInit()
Sam Lantinga <slouken@libsdl.org>
parents: 1903
diff changeset
   725
    if (driver_name == NULL) {
8f1ab2f7c722 Look at environment variables in SDL_VideoInit() and SDL_AudioInit()
Sam Lantinga <slouken@libsdl.org>
parents: 1903
diff changeset
   726
        driver_name = SDL_getenv("SDL_AUDIODRIVER");
8f1ab2f7c722 Look at environment variables in SDL_VideoInit() and SDL_AudioInit()
Sam Lantinga <slouken@libsdl.org>
parents: 1903
diff changeset
   727
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   728
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   729
    for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   730
        /* make sure we should even try this driver before doing so... */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   731
        const AudioBootStrap *backend = bootstrap[i];
6900
767690d1c013 Allow abbreviated strings to be used for driver names.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   732
        if ((driver_name && (SDL_strncasecmp(backend->name, driver_name, SDL_strlen(driver_name)) != 0)) ||
767690d1c013 Allow abbreviated strings to be used for driver names.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   733
            (!driver_name && backend->demand_only)) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   734
            continue;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   735
        }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   736
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   737
        tried_to_init = 1;
9392
92e23eff9b89 Changed some SDL_memset() calls to SDL_zero().
Ryan C. Gordon <icculus@icculus.org>
parents: 9391
diff changeset
   738
        SDL_zero(current_audio);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   739
        current_audio.name = backend->name;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   740
        current_audio.desc = backend->desc;
3699
4160ba33b597 Removed test for "driver is valid, but doesn't see any audio devices."
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
   741
        initialized = backend->init(&current_audio.impl);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   742
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   743
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   744
    if (!initialized) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   745
        /* specific drivers will set the error message if they fail... */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   746
        if (!tried_to_init) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   747
            if (driver_name) {
3699
4160ba33b597 Removed test for "driver is valid, but doesn't see any audio devices."
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
   748
                SDL_SetError("Audio target '%s' not available", driver_name);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   749
            } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   750
                SDL_SetError("No available audio device");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   751
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   752
        }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   753
9392
92e23eff9b89 Changed some SDL_memset() calls to SDL_zero().
Ryan C. Gordon <icculus@icculus.org>
parents: 9391
diff changeset
   754
        SDL_zero(current_audio);
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   755
        return -1;            /* No driver was available, so fail. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   756
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   757
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   758
    current_audio.detectionLock = SDL_CreateMutex();
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   759
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   760
    finalize_audio_entry_points();
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   761
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   762
    /* Make sure we have a list of devices available at startup. */
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   763
    current_audio.impl.DetectDevices();
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   764
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   765
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   766
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   767
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   768
/*
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   769
 * Get the current audio driver name
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   770
 */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   771
const char *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   772
SDL_GetCurrentAudioDriver()
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   773
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   774
    return current_audio.name;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   775
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   776
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   777
/* Clean out devices that we've removed but had to keep around for stability. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   778
static void
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   779
clean_out_device_list(SDL_AudioDeviceItem **devices, int *devCount, SDL_bool *removedFlag)
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   780
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   781
    SDL_AudioDeviceItem *item = *devices;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   782
    SDL_AudioDeviceItem *prev = NULL;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   783
    int total = 0;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   784
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   785
    while (item) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   786
        SDL_AudioDeviceItem *next = item->next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   787
        if (item->handle != NULL) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   788
            total++;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   789
            prev = item;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   790
        } else {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   791
            if (prev) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   792
                prev->next = next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   793
            } else {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   794
                *devices = next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   795
            }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   796
            SDL_free(item);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   797
        }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   798
        item = next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   799
    }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   800
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   801
    *devCount = total;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   802
    *removedFlag = SDL_FALSE;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   803
}
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   804
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   805
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   806
int
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   807
SDL_GetNumAudioDevices(int iscapture)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   808
{
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   809
    int retval = 0;
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   810
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   811
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   812
        return -1;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   813
    }
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   814
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   815
    SDL_LockMutex(current_audio.detectionLock);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   816
    if (iscapture && current_audio.captureDevicesRemoved) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   817
        clean_out_device_list(&current_audio.inputDevices, &current_audio.inputDeviceCount, &current_audio.captureDevicesRemoved);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   818
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   819
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   820
    if (!iscapture && current_audio.outputDevicesRemoved) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   821
        clean_out_device_list(&current_audio.outputDevices, &current_audio.outputDeviceCount, &current_audio.outputDevicesRemoved);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   822
        current_audio.outputDevicesRemoved = SDL_FALSE;
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   823
    }
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   824
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   825
    retval = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   826
    SDL_UnlockMutex(current_audio.detectionLock);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   827
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   828
    return retval;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   829
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   830
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   831
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   832
const char *
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   833
SDL_GetAudioDeviceName(int index, int iscapture)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   834
{
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   835
    const char *retval = NULL;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   836
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   837
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   838
        SDL_SetError("Audio subsystem is not initialized");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   839
        return NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   840
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   841
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   842
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   843
        SDL_SetError("No capture support");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   844
        return NULL;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   845
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   846
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   847
    if (index >= 0) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   848
        SDL_AudioDeviceItem *item;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   849
        int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   850
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   851
        SDL_LockMutex(current_audio.detectionLock);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   852
        item = iscapture ? current_audio.inputDevices : current_audio.outputDevices;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   853
        i = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   854
        if (index < i) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   855
            for (i--; i > index; i--, item = item->next) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   856
                SDL_assert(item != NULL);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   857
            }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   858
            SDL_assert(item != NULL);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   859
            retval = item->name;
7904
915b9b6c9fa0 Fixed bug 2205 - SDL_GetAudioDeviceName returns default-device name on invalid index for default-device only drivers
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   860
        }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   861
        SDL_UnlockMutex(current_audio.detectionLock);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   862
    }
262
ba5363e21df8 Don't allow multiple audio opens to succeed (until SDL 1.3)
Sam Lantinga <slouken@libsdl.org>
parents: 252
diff changeset
   863
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   864
    if (retval == NULL) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   865
        SDL_SetError("No such device");
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   866
    }
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   867
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   868
    return retval;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   869
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   870
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   871
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   872
static void
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   873
close_audio_device(SDL_AudioDevice * device)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   874
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   875
    device->enabled = 0;
9397
d72d2aa46341 Disconnected/broken/lost audio devices now continue to fire their callback.
Ryan C. Gordon <icculus@icculus.org>
parents: 9396
diff changeset
   876
    device->shutdown = 1;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   877
    if (device->thread != NULL) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   878
        SDL_WaitThread(device->thread, NULL);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   879
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   880
    if (device->mixer_lock != NULL) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   881
        SDL_DestroyMutex(device->mixer_lock);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   882
    }
7719
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7678
diff changeset
   883
    SDL_FreeAudioMem(device->fake_stream);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   884
    if (device->convert.needed) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   885
        SDL_FreeAudioMem(device->convert.buf);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   886
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   887
    if (device->opened) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   888
        current_audio.impl.CloseDevice(device);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   889
        device->opened = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   890
    }
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   891
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   892
    free_audio_queue(device->buffer_queue_head);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   893
    free_audio_queue(device->buffer_queue_pool);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   894
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   895
    SDL_FreeAudioMem(device);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   896
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   897
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   898
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   899
/*
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   900
 * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig).
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   901
 *  Fills in a sanitized copy in (prepared).
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   902
 *  Returns non-zero if okay, zero on fatal parameters in (orig).
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   903
 */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   904
static int
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   905
prepare_audiospec(const SDL_AudioSpec * orig, SDL_AudioSpec * prepared)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   906
{
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   907
    SDL_memcpy(prepared, orig, sizeof(SDL_AudioSpec));
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   908
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   909
    if (orig->freq == 0) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   910
        const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   911
        if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) {
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   912
            prepared->freq = 22050;     /* a reasonable default */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   913
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   914
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   915
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   916
    if (orig->format == 0) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   917
        const char *env = SDL_getenv("SDL_AUDIO_FORMAT");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   918
        if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) {
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   919
            prepared->format = AUDIO_S16;       /* a reasonable default */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   920
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   921
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   922
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   923
    switch (orig->channels) {
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   924
    case 0:{
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   925
            const char *env = SDL_getenv("SDL_AUDIO_CHANNELS");
2141
e1a70460c354 stupid indent
Sam Lantinga <slouken@libsdl.org>
parents: 2130
diff changeset
   926
            if ((!env) || ((prepared->channels = (Uint8) SDL_atoi(env)) == 0)) {
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   927
                prepared->channels = 2; /* a reasonable default */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   928
            }
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   929
            break;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   930
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   931
    case 1:                    /* Mono */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   932
    case 2:                    /* Stereo */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   933
    case 4:                    /* surround */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   934
    case 6:                    /* surround with center and lfe */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   935
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   936
    default:
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   937
        SDL_SetError("Unsupported number of audio channels.");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   938
        return 0;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   939
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   940
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   941
    if (orig->samples == 0) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   942
        const char *env = SDL_getenv("SDL_AUDIO_SAMPLES");
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   943
        if ((!env) || ((prepared->samples = (Uint16) SDL_atoi(env)) == 0)) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   944
            /* Pick a default of ~46 ms at desired frequency */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   945
            /* !!! FIXME: remove this when the non-Po2 resampling is in. */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   946
            const int samples = (prepared->freq / 1000) * 46;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   947
            int power2 = 1;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   948
            while (power2 < samples) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   949
                power2 *= 2;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   950
            }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   951
            prepared->samples = power2;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   952
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   953
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   954
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   955
    /* Calculate the silence and size of the audio specification */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   956
    SDL_CalculateAudioSpec(prepared);
21
75a95f82bc1f Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   957
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   958
    return 1;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   959
}
1408
ceb5c2fec4f1 Fixed some preprocessor mangling
Sam Lantinga <slouken@libsdl.org>
parents: 1406
diff changeset
   960
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   961
static SDL_AudioDeviceID
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   962
open_audio_device(const char *devname, int iscapture,
2866
e532417a6977 Fixed SDL 1.2 compatibility problem.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   963
                  const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
e532417a6977 Fixed SDL 1.2 compatibility problem.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   964
                  int allowed_changes, int min_id)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   965
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   966
    SDL_AudioDeviceID id = 0;
2866
e532417a6977 Fixed SDL 1.2 compatibility problem.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   967
    SDL_AudioSpec _obtained;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   968
    SDL_AudioDevice *device;
2866
e532417a6977 Fixed SDL 1.2 compatibility problem.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   969
    SDL_bool build_cvt;
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   970
    void *handle = NULL;
9404
f48039db971c Fixed a compiler warning on Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 9401
diff changeset
   971
    Uint32 stream_len;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   972
    int i = 0;
21
75a95f82bc1f Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   973
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   974
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   975
        SDL_SetError("Audio subsystem is not initialized");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   976
        return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   977
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   978
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   979
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   980
        SDL_SetError("No capture support");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   981
        return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   982
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   983
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   984
    /* Find an available device ID... */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   985
    for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   986
        if (open_devices[id] == NULL) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   987
            break;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   988
        }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   989
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   990
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   991
    if (id == SDL_arraysize(open_devices)) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   992
        SDL_SetError("Too many open audio devices");
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   993
        return 0;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   994
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   995
2866
e532417a6977 Fixed SDL 1.2 compatibility problem.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   996
    if (!obtained) {
e532417a6977 Fixed SDL 1.2 compatibility problem.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   997
        obtained = &_obtained;
e532417a6977 Fixed SDL 1.2 compatibility problem.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   998
    }
e532417a6977 Fixed SDL 1.2 compatibility problem.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   999
    if (!prepare_audiospec(desired, obtained)) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1000
        return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1001
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1002
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1003
    /* If app doesn't care about a specific device, let the user override. */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1004
    if (devname == NULL) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1005
        devname = SDL_getenv("SDL_AUDIO_DEVICE_NAME");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
  1006
    }
21
75a95f82bc1f Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
  1007
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1008
    /*
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1009
     * Catch device names at the high level for the simple case...
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1010
     * This lets us have a basic "device enumeration" for systems that
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1011
     *  don't have multiple devices, but makes sure the device name is
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1012
     *  always NULL when it hits the low level.
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1013
     *
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1014
     * Also make sure that the simple case prevents multiple simultaneous
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1015
     *  opens of the default system device.
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1016
     */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1017
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1018
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1019
        if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1020
            SDL_SetError("No such device");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1021
            return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1022
        }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1023
        devname = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1024
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1025
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1026
            if ((open_devices[i]) && (open_devices[i]->iscapture)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1027
                SDL_SetError("Audio device already open");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1028
                return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1029
            }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1030
        }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1031
    } else if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1032
        if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1033
            SDL_SetError("No such device");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1034
            return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1035
        }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1036
        devname = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1037
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1038
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1039
            if ((open_devices[i]) && (!open_devices[i]->iscapture)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1040
                SDL_SetError("Audio device already open");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1041
                return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1042
            }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1043
        }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1044
    } else if (dev