Mercurial > sdl-ios-xcode
annotate src/audio/SDL_audio.c @ 3884:dba874934f0c SDL-1.2
Fix for buffer overflow in audio code, when feeding a device that needs
resampling.
Fixes Bugzilla #298.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Fri, 27 Oct 2006 20:18:24 +0000 |
parents | c1b6bb5205f1 |
children | 6d2e1961661a |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1204
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1204
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1204
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1204
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1204
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1204
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1204
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
148
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1379
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* Allow access to a raw mixing buffer */ | |
25 | |
26 #include "SDL.h" | |
27 #include "SDL_audio_c.h" | |
28 #include "SDL_audiomem.h" | |
29 #include "SDL_sysaudio.h" | |
30 | |
1190 | 31 #ifdef __OS2__ |
1487
dc6b59e925a2
Cleaning up warnings on MacOS X
Sam Lantinga <slouken@libsdl.org>
parents:
1442
diff
changeset
|
32 /* We'll need the DosSetPriority() API! */ |
1190 | 33 #define INCL_DOSPROCESS |
34 #include <os2.h> | |
35 #endif | |
36 | |
0 | 37 /* Available audio drivers */ |
38 static AudioBootStrap *bootstrap[] = { | |
1567
12b6d331d82a
Good idea, renaming OpenBSD audio to BSD audio.
Sam Lantinga <slouken@libsdl.org>
parents:
1562
diff
changeset
|
39 #if SDL_AUDIO_DRIVER_BSD |
12b6d331d82a
Good idea, renaming OpenBSD audio to BSD audio.
Sam Lantinga <slouken@libsdl.org>
parents:
1562
diff
changeset
|
40 &BSD_AUDIO_bootstrap, |
94
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
68
diff
changeset
|
41 #endif |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
42 #if SDL_AUDIO_DRIVER_OSS |
0 | 43 &DSP_bootstrap, |
44 &DMA_bootstrap, | |
45 #endif | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
46 #if SDL_AUDIO_DRIVER_ALSA |
0 | 47 &ALSA_bootstrap, |
48 #endif | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
49 #if SDL_AUDIO_DRIVER_QNXNTO |
663
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
654
diff
changeset
|
50 &QNXNTOAUDIO_bootstrap, |
8bedd6d61642
Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
654
diff
changeset
|
51 #endif |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
52 #if SDL_AUDIO_DRIVER_SUNAUDIO |
148
8758b8d42cd9
Audio subsystem no longer assumes sun audio API on UNIX systems
Sam Lantinga <slouken@libsdl.org>
parents:
121
diff
changeset
|
53 &SUNAUDIO_bootstrap, |
8758b8d42cd9
Audio subsystem no longer assumes sun audio API on UNIX systems
Sam Lantinga <slouken@libsdl.org>
parents:
121
diff
changeset
|
54 #endif |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
55 #if SDL_AUDIO_DRIVER_DMEDIA |
148
8758b8d42cd9
Audio subsystem no longer assumes sun audio API on UNIX systems
Sam Lantinga <slouken@libsdl.org>
parents:
121
diff
changeset
|
56 &DMEDIA_bootstrap, |
35
d3bc792e136d
Added detection of Open Sound System on Solaris x86
Sam Lantinga <slouken@lokigames.com>
parents:
21
diff
changeset
|
57 #endif |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
58 #if SDL_AUDIO_DRIVER_ARTS |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
59 &ARTS_bootstrap, |
0 | 60 #endif |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
61 #if SDL_AUDIO_DRIVER_ESD |
0 | 62 &ESD_bootstrap, |
63 #endif | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
64 #if SDL_AUDIO_DRIVER_NAS |
0 | 65 &NAS_bootstrap, |
66 #endif | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
67 #if SDL_AUDIO_DRIVER_DSOUND |
0 | 68 &DSOUND_bootstrap, |
69 #endif | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
70 #if SDL_AUDIO_DRIVER_WAVEOUT |
0 | 71 &WAVEOUT_bootstrap, |
72 #endif | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
73 #if SDL_AUDIO_DRIVER_PAUD |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
74 &Paud_bootstrap, |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
75 #endif |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
76 #if SDL_AUDIO_DRIVER_BAUDIO |
0 | 77 &BAUDIO_bootstrap, |
78 #endif | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
79 #if SDL_AUDIO_DRIVER_COREAUDIO |
935
f8d5ddc7aef1
Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
808
diff
changeset
|
80 &COREAUDIO_bootstrap, |
f8d5ddc7aef1
Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
808
diff
changeset
|
81 #endif |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
82 #if SDL_AUDIO_DRIVER_SNDMGR |
0 | 83 &SNDMGR_bootstrap, |
84 #endif | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
85 #if SDL_AUDIO_DRIVER_AHI |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
86 &AHI_bootstrap, |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
87 #endif |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
88 #if SDL_AUDIO_DRIVER_MINT |
644
594422ab8f9f
Atari MiNT: added more audio drivers
Patrice Mandin <patmandin@gmail.com>
parents:
630
diff
changeset
|
89 &MINTAUDIO_GSXB_bootstrap, |
594422ab8f9f
Atari MiNT: added more audio drivers
Patrice Mandin <patmandin@gmail.com>
parents:
630
diff
changeset
|
90 &MINTAUDIO_MCSN_bootstrap, |
594422ab8f9f
Atari MiNT: added more audio drivers
Patrice Mandin <patmandin@gmail.com>
parents:
630
diff
changeset
|
91 &MINTAUDIO_STFA_bootstrap, |
594422ab8f9f
Atari MiNT: added more audio drivers
Patrice Mandin <patmandin@gmail.com>
parents:
630
diff
changeset
|
92 &MINTAUDIO_XBIOS_bootstrap, |
594422ab8f9f
Atari MiNT: added more audio drivers
Patrice Mandin <patmandin@gmail.com>
parents:
630
diff
changeset
|
93 &MINTAUDIO_DMA8_bootstrap, |
398
d219b0e02f5f
Added Atari audio support (thanks Patrice!)
Sam Lantinga <slouken@libsdl.org>
parents:
322
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_DISK |
68
ac6645260d31
Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
38
diff
changeset
|
96 &DISKAUD_bootstrap, |
ac6645260d31
Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
38
diff
changeset
|
97 #endif |
1532
30f189cdd82b
Implemented dummy audio driver.
Ryan C. Gordon <icculus@icculus.org>
parents:
1487
diff
changeset
|
98 #if SDL_AUDIO_DRIVER_DUMMY |
30f189cdd82b
Implemented dummy audio driver.
Ryan C. Gordon <icculus@icculus.org>
parents:
1487
diff
changeset
|
99 &DUMMYAUD_bootstrap, |
30f189cdd82b
Implemented dummy audio driver.
Ryan C. Gordon <icculus@icculus.org>
parents:
1487
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_DC |
509
dad72daf44b3
Added initial support for Dreamcast (thanks HERO!)
Sam Lantinga <slouken@libsdl.org>
parents:
398
diff
changeset
|
102 &DCAUD_bootstrap, |
dad72daf44b3
Added initial support for Dreamcast (thanks HERO!)
Sam Lantinga <slouken@libsdl.org>
parents:
398
diff
changeset
|
103 #endif |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
104 #if SDL_AUDIO_DRIVER_MMEAUDIO |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
105 &MMEAUDIO_bootstrap, |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
106 #endif |
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_DART |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
108 &DART_bootstrap, |
1190 | 109 #endif |
0 | 110 NULL |
111 }; | |
112 SDL_AudioDevice *current_audio = NULL; | |
113 | |
114 /* Various local functions */ | |
115 int SDL_AudioInit(const char *driver_name); | |
116 void SDL_AudioQuit(void); | |
117 | |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1379
diff
changeset
|
118 #if SDL_AUDIO_DRIVER_AHI |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
119 static int audio_configured = 0; |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
120 #endif |
0 | 121 |
122 /* The general mixing thread function */ | |
1769 | 123 int SDLCALL SDL_RunAudio(void *audiop) |
0 | 124 { |
125 SDL_AudioDevice *audio = (SDL_AudioDevice *)audiop; | |
126 Uint8 *stream; | |
127 int stream_len; | |
128 void *udata; | |
1769 | 129 void (SDLCALL *fill)(void *userdata,Uint8 *stream, int len); |
0 | 130 int silence; |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1379
diff
changeset
|
131 #if SDL_AUDIO_DRIVER_AHI |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
132 int started = 0; |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
133 |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
134 /* AmigaOS NEEDS that the audio driver is opened in the thread that uses it! */ |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
135 |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
136 D(bug("Task audio started audio struct:<%lx>...\n",audiop)); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
137 |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
138 D(bug("Before Openaudio...")); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
139 if(audio->OpenAudio(audio, &audio->spec)==-1) |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
140 { |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
141 D(bug("Open audio failed...\n")); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
142 return(-1); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
143 } |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
144 D(bug("OpenAudio...OK\n")); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
145 #endif |
0 | 146 |
147 /* Perform any thread setup */ | |
148 if ( audio->ThreadInit ) { | |
149 audio->ThreadInit(audio); | |
150 } | |
151 audio->threadid = SDL_ThreadID(); | |
152 | |
153 /* Set up the mixing function */ | |
154 fill = audio->spec.callback; | |
155 udata = audio->spec.userdata; | |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
156 |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1379
diff
changeset
|
157 #if SDL_AUDIO_DRIVER_AHI |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
158 audio_configured = 1; |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
159 |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
160 D(bug("Audio configured... Checking for conversion\n")); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
161 SDL_mutexP(audio->mixer_lock); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
162 D(bug("Semaphore obtained...\n")); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
163 #endif |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
164 |
0 | 165 if ( audio->convert.needed ) { |
166 if ( audio->convert.src_format == AUDIO_U8 ) { | |
167 silence = 0x80; | |
168 } else { | |
169 silence = 0; | |
170 } | |
171 stream_len = audio->convert.len; | |
172 } else { | |
173 silence = audio->spec.silence; | |
174 stream_len = audio->spec.size; | |
175 } | |
1561
c724c4a99759
Try to fill the hardware audio buffer immediately.
Ryan C. Gordon <icculus@icculus.org>
parents:
1532
diff
changeset
|
176 |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1379
diff
changeset
|
177 #if SDL_AUDIO_DRIVER_AHI |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
178 SDL_mutexV(audio->mixer_lock); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
179 D(bug("Entering audio loop...\n")); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
180 #endif |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
181 |
1190 | 182 #ifdef __OS2__ |
1487
dc6b59e925a2
Cleaning up warnings on MacOS X
Sam Lantinga <slouken@libsdl.org>
parents:
1442
diff
changeset
|
183 /* Increase the priority of this thread to make sure that |
dc6b59e925a2
Cleaning up warnings on MacOS X
Sam Lantinga <slouken@libsdl.org>
parents:
1442
diff
changeset
|
184 the audio will be continuous all the time! */ |
1190 | 185 #ifdef USE_DOSSETPRIORITY |
1442
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1408
diff
changeset
|
186 if (SDL_getenv("SDL_USE_TIMECRITICAL_AUDIO")) |
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1408
diff
changeset
|
187 { |
1190 | 188 #ifdef DEBUG_BUILD |
1442
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1408
diff
changeset
|
189 printf("[SDL_RunAudio] : Setting priority to TimeCritical+0! (TID%d)\n", SDL_ThreadID()); |
1190 | 190 #endif |
1442
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1408
diff
changeset
|
191 DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0); |
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1408
diff
changeset
|
192 } |
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1408
diff
changeset
|
193 else |
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1408
diff
changeset
|
194 { |
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1408
diff
changeset
|
195 #ifdef DEBUG_BUILD |
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1408
diff
changeset
|
196 printf("[SDL_RunAudio] : Setting priority to ForegroundServer+0! (TID%d)\n", SDL_ThreadID()); |
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1408
diff
changeset
|
197 #endif |
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1408
diff
changeset
|
198 DosSetPriority(PRTYS_THREAD, PRTYC_FOREGROUNDSERVER, 0, 0); |
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1408
diff
changeset
|
199 } |
1190 | 200 #endif |
201 #endif | |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
202 |
0 | 203 /* Loop, filling the audio buffers */ |
204 while ( audio->enabled ) { | |
205 | |
206 /* Fill the current buffer with sound */ | |
207 if ( audio->convert.needed ) { | |
208 if ( audio->convert.buf ) { | |
209 stream = audio->convert.buf; | |
210 } else { | |
211 continue; | |
212 } | |
213 } else { | |
214 stream = audio->GetAudioBuf(audio); | |
215 if ( stream == NULL ) { | |
216 stream = audio->fake_stream; | |
217 } | |
218 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
219 SDL_memset(stream, silence, stream_len); |
0 | 220 |
221 if ( ! audio->paused ) { | |
222 SDL_mutexP(audio->mixer_lock); | |
223 (*fill)(udata, stream, stream_len); | |
224 SDL_mutexV(audio->mixer_lock); | |
225 } | |
226 | |
227 /* Convert the audio if necessary */ | |
228 if ( audio->convert.needed ) { | |
229 SDL_ConvertAudio(&audio->convert); | |
230 stream = audio->GetAudioBuf(audio); | |
231 if ( stream == NULL ) { | |
232 stream = audio->fake_stream; | |
233 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
234 SDL_memcpy(stream, audio->convert.buf, |
0 | 235 audio->convert.len_cvt); |
236 } | |
237 | |
238 /* Ready current buffer for play and change current buffer */ | |
239 if ( stream != audio->fake_stream ) { | |
240 audio->PlayAudio(audio); | |
1562 | 241 } |
242 | |
243 /* Wait for an audio buffer to become available */ | |
244 if ( stream == audio->fake_stream ) { | |
245 SDL_Delay((audio->spec.samples*1000)/audio->spec.freq); | |
246 } else { | |
247 audio->WaitAudio(audio); | |
0 | 248 } |
249 } | |
1562 | 250 |
0 | 251 /* Wait for the audio to drain.. */ |
252 if ( audio->WaitDone ) { | |
253 audio->WaitDone(audio); | |
254 } | |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
255 |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1379
diff
changeset
|
256 #if SDL_AUDIO_DRIVER_AHI |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
257 D(bug("WaitAudio...Done\n")); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
258 |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
259 audio->CloseAudio(audio); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
260 |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
261 D(bug("CloseAudio..Done, subtask exiting...\n")); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
262 audio_configured = 0; |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
263 #endif |
1190 | 264 #ifdef __OS2__ |
265 #ifdef DEBUG_BUILD | |
266 printf("[SDL_RunAudio] : Task exiting. (TID%d)\n", SDL_ThreadID()); | |
267 #endif | |
268 #endif | |
0 | 269 return(0); |
270 } | |
271 | |
322
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
272 static void SDL_LockAudio_Default(SDL_AudioDevice *audio) |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
273 { |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
274 if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) { |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
275 return; |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
276 } |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
277 SDL_mutexP(audio->mixer_lock); |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
278 } |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
279 |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
280 static void SDL_UnlockAudio_Default(SDL_AudioDevice *audio) |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
281 { |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
282 if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) { |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
283 return; |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
284 } |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
285 SDL_mutexV(audio->mixer_lock); |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
286 } |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
287 |
1794 | 288 static Uint16 SDL_ParseAudioFormat(const char *string) |
289 { | |
290 Uint16 format = 0; | |
291 | |
292 switch (*string) { | |
293 case 'U': | |
294 ++string; | |
295 format |= 0x0000; | |
296 break; | |
297 case 'S': | |
298 ++string; | |
299 format |= 0x8000; | |
300 break; | |
301 default: | |
302 return 0; | |
303 } | |
304 switch (SDL_atoi(string)) { | |
305 case 8: | |
306 string += 1; | |
307 format |= 8; | |
308 break; | |
309 case 16: | |
310 string += 2; | |
311 format |= 16; | |
312 if ( SDL_strcmp(string, "LSB") == 0 | |
313 #if SDL_BYTEORDER == SDL_LIL_ENDIAN | |
314 || SDL_strcmp(string, "SYS") == 0 | |
315 #endif | |
316 ) { | |
317 format |= 0x0000; | |
318 } | |
319 if ( SDL_strcmp(string, "MSB") == 0 | |
320 #if SDL_BYTEORDER == SDL_BIG_ENDIAN | |
321 || SDL_strcmp(string, "SYS") == 0 | |
322 #endif | |
323 ) { | |
324 format |= 0x1000; | |
325 } | |
326 break; | |
327 default: | |
328 return 0; | |
329 } | |
330 return format; | |
331 } | |
332 | |
0 | 333 int SDL_AudioInit(const char *driver_name) |
334 { | |
335 SDL_AudioDevice *audio; | |
336 int i = 0, idx; | |
337 | |
338 /* Check to make sure we don't overwrite 'current_audio' */ | |
339 if ( current_audio != NULL ) { | |
340 SDL_AudioQuit(); | |
341 } | |
342 | |
343 /* Select the proper audio driver */ | |
344 audio = NULL; | |
345 idx = 0; | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
346 #if SDL_AUDIO_DRIVER_ESD |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
347 if ( (driver_name == NULL) && (SDL_getenv("ESPEAKER") != NULL) ) { |
0 | 348 /* Ahem, we know that if ESPEAKER is set, user probably wants |
349 to use ESD, but don't start it if it's not already running. | |
350 This probably isn't the place to do this, but... Shh! :) | |
351 */ | |
352 for ( i=0; bootstrap[i]; ++i ) { | |
3881
c1b6bb5205f1
1.2 backport: Replaced strncmp for SDL_AUDIODRIVER test with strcasecmp.
Ryan C. Gordon <icculus@icculus.org>
parents:
1794
diff
changeset
|
353 if ( SDL_strcasecmp(bootstrap[i]->name, "esd") == 0 ) { |
1341
d02b552e5304
Configure dynamically generates SDL_config.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
354 #ifdef HAVE_PUTENV |
0 | 355 const char *esd_no_spawn; |
356 | |
357 /* Don't start ESD if it's not running */ | |
1341
d02b552e5304
Configure dynamically generates SDL_config.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
358 esd_no_spawn = getenv("ESD_NO_SPAWN"); |
0 | 359 if ( esd_no_spawn == NULL ) { |
360 putenv("ESD_NO_SPAWN=1"); | |
361 } | |
1341
d02b552e5304
Configure dynamically generates SDL_config.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
362 #endif |
0 | 363 if ( bootstrap[i]->available() ) { |
364 audio = bootstrap[i]->create(0); | |
365 break; | |
366 } | |
1341
d02b552e5304
Configure dynamically generates SDL_config.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
367 #ifdef HAVE_UNSETENV |
0 | 368 if ( esd_no_spawn == NULL ) { |
369 unsetenv("ESD_NO_SPAWN"); | |
370 } | |
371 #endif | |
372 } | |
373 } | |
374 } | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
375 #endif /* SDL_AUDIO_DRIVER_ESD */ |
0 | 376 if ( audio == NULL ) { |
377 if ( driver_name != NULL ) { | |
378 #if 0 /* This will be replaced with a better driver selection API */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
379 if ( SDL_strrchr(driver_name, ':') != NULL ) { |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
380 idx = atoi(SDL_strrchr(driver_name, ':')+1); |
0 | 381 } |
382 #endif | |
383 for ( i=0; bootstrap[i]; ++i ) { | |
3881
c1b6bb5205f1
1.2 backport: Replaced strncmp for SDL_AUDIODRIVER test with strcasecmp.
Ryan C. Gordon <icculus@icculus.org>
parents:
1794
diff
changeset
|
384 if (SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) { |
0 | 385 if ( bootstrap[i]->available() ) { |
386 audio=bootstrap[i]->create(idx); | |
387 break; | |
388 } | |
389 } | |
390 } | |
391 } else { | |
392 for ( i=0; bootstrap[i]; ++i ) { | |
393 if ( bootstrap[i]->available() ) { | |
394 audio = bootstrap[i]->create(idx); | |
395 if ( audio != NULL ) { | |
396 break; | |
397 } | |
398 } | |
399 } | |
400 } | |
401 if ( audio == NULL ) { | |
402 SDL_SetError("No available audio device"); | |
403 #if 0 /* Don't fail SDL_Init() if audio isn't available. | |
404 SDL_OpenAudio() will handle it at that point. *sigh* | |
405 */ | |
406 return(-1); | |
407 #endif | |
408 } | |
409 } | |
410 current_audio = audio; | |
411 if ( current_audio ) { | |
412 current_audio->name = bootstrap[i]->name; | |
322
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
413 if ( !current_audio->LockAudio && !current_audio->UnlockAudio ) { |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
414 current_audio->LockAudio = SDL_LockAudio_Default; |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
415 current_audio->UnlockAudio = SDL_UnlockAudio_Default; |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
416 } |
0 | 417 } |
418 return(0); | |
419 } | |
420 | |
421 char *SDL_AudioDriverName(char *namebuf, int maxlen) | |
422 { | |
423 if ( current_audio != NULL ) { | |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
424 SDL_strlcpy(namebuf, current_audio->name, maxlen); |
0 | 425 return(namebuf); |
426 } | |
427 return(NULL); | |
428 } | |
429 | |
430 int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained) | |
431 { | |
432 SDL_AudioDevice *audio; | |
1794 | 433 const char *env; |
0 | 434 |
435 /* Start up the audio driver, if necessary */ | |
436 if ( ! current_audio ) { | |
437 if ( (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) || | |
438 (current_audio == NULL) ) { | |
439 return(-1); | |
440 } | |
441 } | |
442 audio = current_audio; | |
443 | |
262
ba5363e21df8
Don't allow multiple audio opens to succeed (until SDL 1.3)
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
444 if (audio->opened) { |
ba5363e21df8
Don't allow multiple audio opens to succeed (until SDL 1.3)
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
445 SDL_SetError("Audio device is already opened"); |
ba5363e21df8
Don't allow multiple audio opens to succeed (until SDL 1.3)
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
446 return(-1); |
ba5363e21df8
Don't allow multiple audio opens to succeed (until SDL 1.3)
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
447 } |
ba5363e21df8
Don't allow multiple audio opens to succeed (until SDL 1.3)
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
448 |
0 | 449 /* Verify some parameters */ |
1794 | 450 if ( desired->freq == 0 ) { |
451 env = SDL_getenv("SDL_AUDIO_FREQUENCY"); | |
452 if ( env ) { | |
453 desired->freq = SDL_atoi(env); | |
454 } | |
455 } | |
456 if ( desired->freq == 0 ) { | |
457 /* Pick some default audio frequency */ | |
458 desired->freq = 22050; | |
459 } | |
460 if ( desired->format == 0 ) { | |
461 env = SDL_getenv("SDL_AUDIO_FORMAT"); | |
462 if ( env ) { | |
463 desired->format = SDL_ParseAudioFormat(env); | |
464 } | |
465 } | |
466 if ( desired->format == 0 ) { | |
467 /* Pick some default audio format */ | |
468 desired->format = AUDIO_S16; | |
469 } | |
470 if ( desired->channels == 0 ) { | |
471 env = SDL_getenv("SDL_AUDIO_CHANNELS"); | |
472 if ( env ) { | |
473 desired->channels = SDL_atoi(env); | |
474 } | |
475 } | |
476 if ( desired->channels == 0 ) { | |
477 /* Pick a default number of channels */ | |
478 desired->channels = 2; | |
0 | 479 } |
480 switch ( desired->channels ) { | |
481 case 1: /* Mono */ | |
482 case 2: /* Stereo */ | |
942
41a59de7f2ed
Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents:
936
diff
changeset
|
483 case 4: /* surround */ |
41a59de7f2ed
Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents:
936
diff
changeset
|
484 case 6: /* surround with center and lfe */ |
0 | 485 break; |
486 default: | |
487 SDL_SetError("1 (mono) and 2 (stereo) channels supported"); | |
488 return(-1); | |
489 } | |
1794 | 490 if ( desired->samples == 0 ) { |
491 env = SDL_getenv("SDL_AUDIO_SAMPLES"); | |
492 if ( env ) { | |
493 desired->samples = SDL_atoi(env); | |
494 } | |
495 } | |
496 if ( desired->samples == 0 ) { | |
497 /* Pick a default of ~46 ms at desired frequency */ | |
498 int samples = (desired->freq / 1000) * 46; | |
499 int power2 = 1; | |
500 while ( power2 < samples ) { | |
501 power2 *= 2; | |
502 } | |
503 desired->samples = power2; | |
504 } | |
505 if ( desired->callback == NULL ) { | |
506 SDL_SetError("SDL_OpenAudio() passed a NULL callback"); | |
507 return(-1); | |
508 } | |
0 | 509 |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1379
diff
changeset
|
510 #if defined(__MACOS__) || (defined(__RISCOS__) && SDL_THREADS_DISABLED) |
0 | 511 /* FIXME: Need to implement PPC interrupt asm for SDL_LockAudio() */ |
512 #else | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
513 #if defined(__MINT__) && SDL_THREADS_DISABLED |
398
d219b0e02f5f
Added Atari audio support (thanks Patrice!)
Sam Lantinga <slouken@libsdl.org>
parents:
322
diff
changeset
|
514 /* Uses interrupt driven audio, without thread */ |
d219b0e02f5f
Added Atari audio support (thanks Patrice!)
Sam Lantinga <slouken@libsdl.org>
parents:
322
diff
changeset
|
515 #else |
0 | 516 /* Create a semaphore for locking the sound buffers */ |
517 audio->mixer_lock = SDL_CreateMutex(); | |
518 if ( audio->mixer_lock == NULL ) { | |
519 SDL_SetError("Couldn't create mixer lock"); | |
520 SDL_CloseAudio(); | |
521 return(-1); | |
522 } | |
398
d219b0e02f5f
Added Atari audio support (thanks Patrice!)
Sam Lantinga <slouken@libsdl.org>
parents:
322
diff
changeset
|
523 #endif /* __MINT__ */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1379
diff
changeset
|
524 #endif /* __MACOS__ */ |
0 | 525 |
526 /* Calculate the silence and size of the audio specification */ | |
527 SDL_CalculateAudioSpec(desired); | |
528 | |
529 /* Open the audio subsystem */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
530 SDL_memcpy(&audio->spec, desired, sizeof(audio->spec)); |
0 | 531 audio->convert.needed = 0; |
532 audio->enabled = 1; | |
533 audio->paused = 1; | |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
534 |
1406
39ca9a4b22f3
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
1402
diff
changeset
|
535 #if !SDL_AUDIO_DRIVER_AHI |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
536 |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
537 /* AmigaOS opens audio inside the main loop */ |
0 | 538 audio->opened = audio->OpenAudio(audio, &audio->spec)+1; |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
539 |
0 | 540 if ( ! audio->opened ) { |
541 SDL_CloseAudio(); | |
542 return(-1); | |
543 } | |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
544 #else |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
545 D(bug("Locking semaphore...")); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
546 SDL_mutexP(audio->mixer_lock); |
1408
ceb5c2fec4f1
Fixed some preprocessor mangling
Sam Lantinga <slouken@libsdl.org>
parents:
1406
diff
changeset
|
547 |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
548 |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
549 audio->thread = SDL_CreateThread(SDL_RunAudio, audio); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
550 D(bug("Created thread...\n")); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
551 |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
552 if ( audio->thread == NULL ) { |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
553 SDL_mutexV(audio->mixer_lock); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
554 SDL_CloseAudio(); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
555 SDL_SetError("Couldn't create audio thread"); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
556 return(-1); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
557 } |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
558 |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
559 while(!audio_configured) |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
560 SDL_Delay(100); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
561 #endif |
0 | 562 |
563 /* If the audio driver changes the buffer size, accept it */ | |
564 if ( audio->spec.samples != desired->samples ) { | |
565 desired->samples = audio->spec.samples; | |
566 SDL_CalculateAudioSpec(desired); | |
567 } | |
568 | |
569 /* Allocate a fake audio memory buffer */ | |
570 audio->fake_stream = SDL_AllocAudioMem(audio->spec.size); | |
571 if ( audio->fake_stream == NULL ) { | |
572 SDL_CloseAudio(); | |
573 SDL_OutOfMemory(); | |
574 return(-1); | |
575 } | |
576 | |
577 /* See if we need to do any conversion */ | |
808
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
578 if ( obtained != NULL ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
579 SDL_memcpy(obtained, &audio->spec, sizeof(audio->spec)); |
808
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
580 } else if ( desired->freq != audio->spec.freq || |
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
581 desired->format != audio->spec.format || |
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
582 desired->channels != audio->spec.channels ) { |
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
583 /* Build an audio conversion block */ |
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
584 if ( SDL_BuildAudioCVT(&audio->convert, |
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
585 desired->format, desired->channels, |
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
586 desired->freq, |
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
587 audio->spec.format, audio->spec.channels, |
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
588 audio->spec.freq) < 0 ) { |
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
589 SDL_CloseAudio(); |
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
590 return(-1); |
0 | 591 } |
808
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
592 if ( audio->convert.needed ) { |
3884
dba874934f0c
Fix for buffer overflow in audio code, when feeding a device that needs
Ryan C. Gordon <icculus@icculus.org>
parents:
3881
diff
changeset
|
593 audio->convert.len = (int) ( ((double) desired->size) / |
dba874934f0c
Fix for buffer overflow in audio code, when feeding a device that needs
Ryan C. Gordon <icculus@icculus.org>
parents:
3881
diff
changeset
|
594 audio->convert.len_ratio ); |
808
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
595 audio->convert.buf =(Uint8 *)SDL_AllocAudioMem( |
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
596 audio->convert.len*audio->convert.len_mult); |
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
597 if ( audio->convert.buf == NULL ) { |
0 | 598 SDL_CloseAudio(); |
808
0defd90ef27c
Simplify code and clean up Valgrind warning
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
599 SDL_OutOfMemory(); |
0 | 600 return(-1); |
601 } | |
602 } | |
603 } | |
604 | |
1406
39ca9a4b22f3
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
1402
diff
changeset
|
605 #if !SDL_AUDIO_DRIVER_AHI |
0 | 606 /* Start the audio thread if necessary */ |
607 switch (audio->opened) { | |
608 case 1: | |
609 /* Start the audio thread */ | |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1379
diff
changeset
|
610 #if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC) |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
611 #undef SDL_CreateThread |
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
612 audio->thread = SDL_CreateThread(SDL_RunAudio, audio, NULL, NULL); |
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
613 #else |
0 | 614 audio->thread = SDL_CreateThread(SDL_RunAudio, audio); |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
615 #endif |
0 | 616 if ( audio->thread == NULL ) { |
617 SDL_CloseAudio(); | |
618 SDL_SetError("Couldn't create audio thread"); | |
619 return(-1); | |
620 } | |
621 break; | |
622 | |
623 default: | |
624 /* The audio is now playing */ | |
625 break; | |
626 } | |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
627 #else |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
628 SDL_mutexV(audio->mixer_lock); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
629 D(bug("SDL_OpenAudio USCITA...\n")); |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
630 |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
631 #endif |
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
632 |
0 | 633 return(0); |
634 } | |
635 | |
636 SDL_audiostatus SDL_GetAudioStatus(void) | |
637 { | |
638 SDL_AudioDevice *audio = current_audio; | |
639 SDL_audiostatus status; | |
640 | |
641 status = SDL_AUDIO_STOPPED; | |
642 if ( audio && audio->enabled ) { | |
643 if ( audio->paused ) { | |
644 status = SDL_AUDIO_PAUSED; | |
645 } else { | |
646 status = SDL_AUDIO_PLAYING; | |
647 } | |
648 } | |
649 return(status); | |
650 } | |
651 | |
652 void SDL_PauseAudio (int pause_on) | |
653 { | |
654 SDL_AudioDevice *audio = current_audio; | |
655 | |
656 if ( audio ) { | |
657 audio->paused = pause_on; | |
658 } | |
659 } | |
660 | |
661 void SDL_LockAudio (void) | |
662 { | |
663 SDL_AudioDevice *audio = current_audio; | |
664 | |
665 /* Obtain a lock on the mixing buffers */ | |
322
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
666 if ( audio && audio->LockAudio ) { |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
667 audio->LockAudio(audio); |
0 | 668 } |
669 } | |
670 | |
671 void SDL_UnlockAudio (void) | |
672 { | |
673 SDL_AudioDevice *audio = current_audio; | |
674 | |
675 /* Release lock on the mixing buffers */ | |
322
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
676 if ( audio && audio->UnlockAudio ) { |
fd93a09655e3
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
677 audio->UnlockAudio(audio); |
0 | 678 } |
679 } | |
680 | |
681 void SDL_CloseAudio (void) | |
682 { | |
683 SDL_QuitSubSystem(SDL_INIT_AUDIO); | |
684 } | |
685 | |
686 void SDL_AudioQuit(void) | |
687 { | |
688 SDL_AudioDevice *audio = current_audio; | |
689 | |
690 if ( audio ) { | |
691 audio->enabled = 0; | |
692 if ( audio->thread != NULL ) { | |
693 SDL_WaitThread(audio->thread, NULL); | |
694 } | |
695 if ( audio->mixer_lock != NULL ) { | |
696 SDL_DestroyMutex(audio->mixer_lock); | |
697 } | |
698 if ( audio->fake_stream != NULL ) { | |
699 SDL_FreeAudioMem(audio->fake_stream); | |
700 } | |
701 if ( audio->convert.needed ) { | |
702 SDL_FreeAudioMem(audio->convert.buf); | |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
703 |
0 | 704 } |
1406
39ca9a4b22f3
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
1402
diff
changeset
|
705 #if !SDL_AUDIO_DRIVER_AHI |
0 | 706 if ( audio->opened ) { |
707 audio->CloseAudio(audio); | |
708 audio->opened = 0; | |
709 } | |
21
75a95f82bc1f
Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
710 #endif |
0 | 711 /* Free the driver data */ |
712 audio->free(audio); | |
713 current_audio = NULL; | |
714 } | |
715 } | |
716 | |
717 #define NUM_FORMATS 6 | |
718 static int format_idx; | |
719 static int format_idx_sub; | |
720 static Uint16 format_list[NUM_FORMATS][NUM_FORMATS] = { | |
721 { AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB }, | |
722 { AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB }, | |
723 { AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8 }, | |
724 { AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8 }, | |
725 { AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U8, AUDIO_S8 }, | |
726 { AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U8, AUDIO_S8 }, | |
727 }; | |
728 | |
729 Uint16 SDL_FirstAudioFormat(Uint16 format) | |
730 { | |
731 for ( format_idx=0; format_idx < NUM_FORMATS; ++format_idx ) { | |
732 if ( format_list[format_idx][0] == format ) { | |
733 break; | |
734 } | |
735 } | |
736 format_idx_sub = 0; | |
737 return(SDL_NextAudioFormat()); | |
738 } | |
739 | |
740 Uint16 SDL_NextAudioFormat(void) | |
741 { | |
742 if ( (format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS) ) { | |
743 return(0); | |
744 } | |
745 return(format_list[format_idx][format_idx_sub++]); | |
746 } | |
747 | |
748 void SDL_CalculateAudioSpec(SDL_AudioSpec *spec) | |
749 { | |
750 switch (spec->format) { | |
751 case AUDIO_U8: | |
752 spec->silence = 0x80; | |
753 break; | |
754 default: | |
755 spec->silence = 0x00; | |
756 break; | |
757 } | |
758 spec->size = (spec->format&0xFF)/8; | |
759 spec->size *= spec->channels; | |
760 spec->size *= spec->samples; | |
761 } |