Mercurial > sdl-ios-xcode
annotate src/audio/baudio/SDL_beaudio.cc @ 3953:d3c1161652d5 SDL-1.2
C fallback for SDL_revcpy() was off by one. Thanks to Emmanuel Jeandel for
the catch.
Fixes Bugzilla #424.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 04 Jun 2007 11:43:47 +0000 |
parents | 5b5e549382b3 |
children | a1b03ba2fcd0 |
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:
252
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:
252
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:
252
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:
252
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:
252
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:
252
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:
252
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:
114
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
1403
376665398b25
Catch the C++ and Objective C sources too...
Sam Lantinga <slouken@libsdl.org>
parents:
1367
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* Allow access to the audio stream on BeOS */ | |
25 | |
26 #include <SoundPlayer.h> | |
27 | |
1367
e440d5c488c1
Fixes for BeOS and Solaris builds
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
28 #include "../../main/beos/SDL_BeApp.h" |
0 | 29 |
30 extern "C" { | |
31 | |
32 #include "SDL_audio.h" | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
33 #include "../SDL_audio_c.h" |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
34 #include "../SDL_sysaudio.h" |
1367
e440d5c488c1
Fixes for BeOS and Solaris builds
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
35 #include "../../thread/beos/SDL_systhread_c.h" |
0 | 36 #include "SDL_beaudio.h" |
37 | |
38 | |
39 /* Audio driver functions */ | |
40 static int BE_OpenAudio(_THIS, SDL_AudioSpec *spec); | |
41 static void BE_WaitAudio(_THIS); | |
42 static void BE_PlayAudio(_THIS); | |
43 static Uint8 *BE_GetAudioBuf(_THIS); | |
44 static void BE_CloseAudio(_THIS); | |
45 | |
46 /* Audio driver bootstrap functions */ | |
47 | |
48 static int Audio_Available(void) | |
49 { | |
50 return(1); | |
51 } | |
52 | |
53 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
54 { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
55 SDL_free(device->hidden); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
56 SDL_free(device); |
0 | 57 } |
58 | |
59 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
60 { | |
61 SDL_AudioDevice *device; | |
62 | |
63 /* Initialize all variables that we clean on shutdown */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
64 device = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice)); |
0 | 65 if ( device ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
66 SDL_memset(device, 0, (sizeof *device)); |
0 | 67 device->hidden = (struct SDL_PrivateAudioData *) |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
68 SDL_malloc((sizeof *device->hidden)); |
0 | 69 } |
70 if ( (device == NULL) || (device->hidden == NULL) ) { | |
71 SDL_OutOfMemory(); | |
72 if ( device ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
73 SDL_free(device); |
0 | 74 } |
75 return(0); | |
76 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
77 SDL_memset(device->hidden, 0, (sizeof *device->hidden)); |
0 | 78 |
79 /* Set the function pointers */ | |
80 device->OpenAudio = BE_OpenAudio; | |
81 device->WaitAudio = BE_WaitAudio; | |
82 device->PlayAudio = BE_PlayAudio; | |
83 device->GetAudioBuf = BE_GetAudioBuf; | |
84 device->CloseAudio = BE_CloseAudio; | |
85 | |
86 device->free = Audio_DeleteDevice; | |
87 | |
88 return device; | |
89 } | |
90 | |
91 AudioBootStrap BAUDIO_bootstrap = { | |
92 "baudio", "BeOS BSoundPlayer", | |
93 Audio_Available, Audio_CreateDevice | |
94 }; | |
95 | |
96 /* The BeOS callback for handling the audio buffer */ | |
97 static void FillSound(void *device, void *stream, size_t len, | |
98 const media_raw_audio_format &format) | |
99 { | |
100 SDL_AudioDevice *audio = (SDL_AudioDevice *)device; | |
101 | |
102 /* Silence the buffer, since it's ours */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
103 SDL_memset(stream, audio->spec.silence, len); |
0 | 104 |
105 /* Only do soemthing if audio is enabled */ | |
106 if ( ! audio->enabled ) | |
107 return; | |
108 | |
109 if ( ! audio->paused ) { | |
110 if ( audio->convert.needed ) { | |
111 SDL_mutexP(audio->mixer_lock); | |
112 (*audio->spec.callback)(audio->spec.userdata, | |
113 (Uint8 *)audio->convert.buf,audio->convert.len); | |
114 SDL_mutexV(audio->mixer_lock); | |
115 SDL_ConvertAudio(&audio->convert); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
116 SDL_memcpy(stream,audio->convert.buf,audio->convert.len_cvt); |
0 | 117 } else { |
118 SDL_mutexP(audio->mixer_lock); | |
119 (*audio->spec.callback)(audio->spec.userdata, | |
120 (Uint8 *)stream, len); | |
121 SDL_mutexV(audio->mixer_lock); | |
122 } | |
123 } | |
124 return; | |
125 } | |
126 | |
127 /* Dummy functions -- we don't use thread-based audio */ | |
128 void BE_WaitAudio(_THIS) | |
129 { | |
130 return; | |
131 } | |
132 void BE_PlayAudio(_THIS) | |
133 { | |
134 return; | |
135 } | |
136 Uint8 *BE_GetAudioBuf(_THIS) | |
137 { | |
138 return(NULL); | |
139 } | |
140 | |
141 void BE_CloseAudio(_THIS) | |
142 { | |
143 if ( audio_obj ) { | |
144 audio_obj->Stop(); | |
145 delete audio_obj; | |
146 audio_obj = NULL; | |
147 } | |
148 | |
149 /* Quit the Be Application, if there's nothing left to do */ | |
150 SDL_QuitBeApp(); | |
151 } | |
152 | |
153 int BE_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
154 { | |
3851
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
155 int valid_datatype = 0; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
156 media_raw_audio_format format; |
3852
5b5e549382b3
Removed some new 1.3 symbols from code backported to 1.2.
Ryan C. Gordon <icculus@icculus.org>
parents:
3851
diff
changeset
|
157 Uint16 test_format = SDL_FirstAudioFormat(spec->format); |
0 | 158 |
3851
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
159 /* Parse the audio format and fill the Be raw audio format */ |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
160 memset(&format, '\0', sizeof (media_raw_audio_format)); |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
161 format.byte_order = B_MEDIA_LITTLE_ENDIAN; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
162 format.frame_rate = (float) spec->freq; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
163 format.channel_count = spec->channels; /* !!! FIXME: support > 2? */ |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
164 while ((!valid_datatype) && (test_format)) { |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
165 valid_datatype = 1; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
166 spec->format = test_format; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
167 switch (test_format) { |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
168 case AUDIO_S8: |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
169 format.format = media_raw_audio_format::B_AUDIO_CHAR; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
170 break; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
171 |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
172 case AUDIO_U8: |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
173 format.format = media_raw_audio_format::B_AUDIO_UCHAR; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
174 break; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
175 |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
176 case AUDIO_S16LSB: |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
177 format.format = media_raw_audio_format::B_AUDIO_SHORT; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
178 break; |
0 | 179 |
3851
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
180 case AUDIO_S16MSB: |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
181 format.format = media_raw_audio_format::B_AUDIO_SHORT; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
182 format.byte_order = B_MEDIA_BIG_ENDIAN; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
183 break; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
184 |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
185 default: |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
186 valid_datatype = 0; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
187 test_format = SDL_NextAudioFormat(); |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
188 break; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
189 } |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
190 } |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
191 |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
192 if (!valid_datatype) { /* shouldn't happen, but just in case... */ |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
193 SDL_SetError("Unsupported audio format"); |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
194 return (-1); |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
195 } |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
196 |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
197 /* Initialize the Be Application, if it's not already started */ |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
198 if (SDL_InitBeApp() < 0) { |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
199 return (-1); |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
200 } |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
201 |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
202 format.buffer_size = spec->samples; |
405a192b68e7
Backport from 1.3: most of the audio drivers can now handle data
Ryan C. Gordon <icculus@icculus.org>
parents:
1403
diff
changeset
|
203 |
0 | 204 /* Calculate the final parameters for this audio specification */ |
205 SDL_CalculateAudioSpec(spec); | |
206 | |
207 /* Subscribe to the audio stream (creates a new thread) */ | |
208 { sigset_t omask; | |
209 SDL_MaskSignals(&omask); | |
210 audio_obj = new BSoundPlayer(&format, "SDL Audio", FillSound, | |
211 NULL, _this); | |
212 SDL_UnmaskSignals(&omask); | |
213 } | |
114
dabc453ce7f7
Now returns an error if unable to open audio on BeOS
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
214 if ( audio_obj->Start() == B_NO_ERROR ) { |
dabc453ce7f7
Now returns an error if unable to open audio on BeOS
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
215 audio_obj->SetHasData(true); |
dabc453ce7f7
Now returns an error if unable to open audio on BeOS
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
216 } else { |
dabc453ce7f7
Now returns an error if unable to open audio on BeOS
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
217 SDL_SetError("Unable to start Be audio"); |
dabc453ce7f7
Now returns an error if unable to open audio on BeOS
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
218 return(-1); |
dabc453ce7f7
Now returns an error if unable to open audio on BeOS
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
219 } |
0 | 220 |
221 /* We're running! */ | |
222 return(1); | |
223 } | |
224 | |
225 }; /* Extern C */ |