Mercurial > sdl-ios-xcode
annotate src/audio/baudio/SDL_beaudio.cc @ 1339:62802d9d7c87
*** empty log message ***
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Feb 2006 10:40:14 +0000 |
parents | 604d73db6802 |
children | c71e05b4dc2e |
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 */ |
22 | |
23 /* Allow access to the audio stream on BeOS */ | |
24 | |
25 #include <SoundPlayer.h> | |
26 | |
27 #include "SDL_BeApp.h" | |
28 | |
29 extern "C" { | |
30 | |
1338
604d73db6802
Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
31 #include "SDL_stdlib.h" |
604d73db6802
Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
32 #include "SDL_string.h" |
0 | 33 #include "SDL_audio.h" |
34 #include "SDL_audio_c.h" | |
35 #include "SDL_sysaudio.h" | |
36 #include "SDL_systhread_c.h" | |
37 #include "SDL_beaudio.h" | |
38 | |
39 | |
40 /* Audio driver functions */ | |
41 static int BE_OpenAudio(_THIS, SDL_AudioSpec *spec); | |
42 static void BE_WaitAudio(_THIS); | |
43 static void BE_PlayAudio(_THIS); | |
44 static Uint8 *BE_GetAudioBuf(_THIS); | |
45 static void BE_CloseAudio(_THIS); | |
46 | |
47 /* Audio driver bootstrap functions */ | |
48 | |
49 static int Audio_Available(void) | |
50 { | |
51 return(1); | |
52 } | |
53 | |
54 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
55 { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
56 SDL_free(device->hidden); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
57 SDL_free(device); |
0 | 58 } |
59 | |
60 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
61 { | |
62 SDL_AudioDevice *device; | |
63 | |
64 /* 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
|
65 device = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice)); |
0 | 66 if ( device ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
67 SDL_memset(device, 0, (sizeof *device)); |
0 | 68 device->hidden = (struct SDL_PrivateAudioData *) |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
69 SDL_malloc((sizeof *device->hidden)); |
0 | 70 } |
71 if ( (device == NULL) || (device->hidden == NULL) ) { | |
72 SDL_OutOfMemory(); | |
73 if ( device ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
74 SDL_free(device); |
0 | 75 } |
76 return(0); | |
77 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
78 SDL_memset(device->hidden, 0, (sizeof *device->hidden)); |
0 | 79 |
80 /* Set the function pointers */ | |
81 device->OpenAudio = BE_OpenAudio; | |
82 device->WaitAudio = BE_WaitAudio; | |
83 device->PlayAudio = BE_PlayAudio; | |
84 device->GetAudioBuf = BE_GetAudioBuf; | |
85 device->CloseAudio = BE_CloseAudio; | |
86 | |
87 device->free = Audio_DeleteDevice; | |
88 | |
89 return device; | |
90 } | |
91 | |
92 AudioBootStrap BAUDIO_bootstrap = { | |
93 "baudio", "BeOS BSoundPlayer", | |
94 Audio_Available, Audio_CreateDevice | |
95 }; | |
96 | |
97 /* The BeOS callback for handling the audio buffer */ | |
98 static void FillSound(void *device, void *stream, size_t len, | |
99 const media_raw_audio_format &format) | |
100 { | |
101 SDL_AudioDevice *audio = (SDL_AudioDevice *)device; | |
102 | |
103 /* 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
|
104 SDL_memset(stream, audio->spec.silence, len); |
0 | 105 |
106 /* Only do soemthing if audio is enabled */ | |
107 if ( ! audio->enabled ) | |
108 return; | |
109 | |
110 if ( ! audio->paused ) { | |
111 if ( audio->convert.needed ) { | |
112 SDL_mutexP(audio->mixer_lock); | |
113 (*audio->spec.callback)(audio->spec.userdata, | |
114 (Uint8 *)audio->convert.buf,audio->convert.len); | |
115 SDL_mutexV(audio->mixer_lock); | |
116 SDL_ConvertAudio(&audio->convert); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
117 SDL_memcpy(stream,audio->convert.buf,audio->convert.len_cvt); |
0 | 118 } else { |
119 SDL_mutexP(audio->mixer_lock); | |
120 (*audio->spec.callback)(audio->spec.userdata, | |
121 (Uint8 *)stream, len); | |
122 SDL_mutexV(audio->mixer_lock); | |
123 } | |
124 } | |
125 return; | |
126 } | |
127 | |
128 /* Dummy functions -- we don't use thread-based audio */ | |
129 void BE_WaitAudio(_THIS) | |
130 { | |
131 return; | |
132 } | |
133 void BE_PlayAudio(_THIS) | |
134 { | |
135 return; | |
136 } | |
137 Uint8 *BE_GetAudioBuf(_THIS) | |
138 { | |
139 return(NULL); | |
140 } | |
141 | |
142 void BE_CloseAudio(_THIS) | |
143 { | |
144 if ( audio_obj ) { | |
145 audio_obj->Stop(); | |
146 delete audio_obj; | |
147 audio_obj = NULL; | |
148 } | |
149 | |
150 /* Quit the Be Application, if there's nothing left to do */ | |
151 SDL_QuitBeApp(); | |
152 } | |
153 | |
154 int BE_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
155 { | |
156 media_raw_audio_format format; | |
157 | |
158 /* Initialize the Be Application, if it's not already started */ | |
159 if ( SDL_InitBeApp() < 0 ) { | |
160 return(-1); | |
161 } | |
162 | |
163 /* Parse the audio format and fill the Be raw audio format */ | |
164 format.frame_rate = (float)spec->freq; | |
165 format.channel_count = spec->channels; | |
166 switch (spec->format&~0x1000) { | |
167 case AUDIO_S8: | |
168 /* Signed 8-bit audio unsupported, convert to U8 */ | |
169 spec->format = AUDIO_U8; | |
170 case AUDIO_U8: | |
171 format.format = media_raw_audio_format::B_AUDIO_UCHAR; | |
172 format.byte_order = 0; | |
173 break; | |
174 case AUDIO_U16: | |
175 /* Unsigned 16-bit audio unsupported, convert to S16 */ | |
176 spec->format ^= 0x8000; | |
177 case AUDIO_S16: | |
178 format.format = media_raw_audio_format::B_AUDIO_SHORT; | |
179 if ( spec->format & 0x1000 ) { | |
180 format.byte_order = 1; /* Big endian */ | |
181 } else { | |
182 format.byte_order = 2; /* Little endian */ | |
183 } | |
184 break; | |
185 } | |
186 format.buffer_size = spec->samples; | |
187 | |
188 /* Calculate the final parameters for this audio specification */ | |
189 SDL_CalculateAudioSpec(spec); | |
190 | |
191 /* Subscribe to the audio stream (creates a new thread) */ | |
192 { sigset_t omask; | |
193 SDL_MaskSignals(&omask); | |
194 audio_obj = new BSoundPlayer(&format, "SDL Audio", FillSound, | |
195 NULL, _this); | |
196 SDL_UnmaskSignals(&omask); | |
197 } | |
114
dabc453ce7f7
Now returns an error if unable to open audio on BeOS
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
198 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
|
199 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
|
200 } else { |
dabc453ce7f7
Now returns an error if unable to open audio on BeOS
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
201 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
|
202 return(-1); |
dabc453ce7f7
Now returns an error if unable to open audio on BeOS
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
203 } |
0 | 204 |
205 /* We're running! */ | |
206 return(1); | |
207 } | |
208 | |
209 }; /* Extern C */ |