comparison src/audio/baudio/SDL_beaudio.cc @ 3819:b225d9820ee3 SDL-ryan-multiple-audio-device

Updated a bunch of audio backends to 1.3 API (Dreamcast, OS/2, ALSA, and BeOS). None are tested, so anyu could fail to compile.
author Ryan C. Gordon <icculus@icculus.org>
date Fri, 06 Oct 2006 20:36:23 +0000
parents adf732f1f016
children 29e83f221c62
comparison
equal deleted inserted replaced
3818:49eadd6e8962 3819:b225d9820ee3
34 #include "../SDL_audio_c.h" 34 #include "../SDL_audio_c.h"
35 #include "../SDL_sysaudio.h" 35 #include "../SDL_sysaudio.h"
36 #include "../../thread/beos/SDL_systhread_c.h" 36 #include "../../thread/beos/SDL_systhread_c.h"
37 #include "SDL_beaudio.h" 37 #include "SDL_beaudio.h"
38 38
39 39 }
40 /* Audio driver functions */ 40
41 static int BE_OpenAudio(_THIS, SDL_AudioSpec * spec); 41
42 static void BE_WaitAudio(_THIS); 42 static int BEAUDIO_Available(void)
43 static void BE_PlayAudio(_THIS); 43 {
44 static Uint8 *BE_GetAudioBuf(_THIS); 44 return 1; /* Always available on BeOS. */
45 static void BE_CloseAudio(_THIS); 45 }
46 46
47 /* Audio driver bootstrap functions */ 47
48 48 /* !!! FIXME: have the callback call the higher level to avoid code dupe. */
49 static int Audio_Available(void)
50 {
51 return (1);
52 }
53
54 static void Audio_DeleteDevice(SDL_AudioDevice * device)
55 {
56 SDL_free(device->hidden);
57 SDL_free(device);
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 */
65 device = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
66 if (device) {
67 SDL_memset(device, 0, (sizeof *device));
68 device->hidden = (struct SDL_PrivateAudioData *)
69 SDL_malloc((sizeof *device->hidden));
70 }
71 if ((device == NULL) || (device->hidden == NULL)) {
72 SDL_OutOfMemory();
73 if (device) {
74 SDL_free(device);
75 }
76 return (0);
77 }
78 SDL_memset(device->hidden, 0, (sizeof *device->hidden));
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 */ 49 /* The BeOS callback for handling the audio buffer */
98 static void FillSound(void *device, void *stream, size_t len, 50 static void
99 const media_raw_audio_format & format) 51 FillSound(void *device, void *stream, size_t len,
100 { 52 const media_raw_audio_format & format)
101 SDL_AudioDevice *audio = (SDL_AudioDevice *) device; 53 {
102 54 SDL_AudioDevice *audio = (SDL_AudioDevice *) device;
103 /* Silence the buffer, since it's ours */ 55
104 SDL_memset(stream, audio->spec.silence, len); 56 /* Silence the buffer, since it's ours */
105 57 SDL_memset(stream, audio->spec.silence, len);
106 /* Only do soemthing if audio is enabled */ 58
107 if (!audio->enabled) 59 /* Only do soemthing if audio is enabled */
108 return; 60 if (!audio->enabled)
109 61 return;
110 if (!audio->paused) { 62
111 if (audio->convert.needed) { 63 if (!audio->paused) {
112 SDL_mutexP(audio->mixer_lock); 64 if (audio->convert.needed) {
113 (*audio->spec.callback) (audio->spec.userdata, 65 SDL_mutexP(audio->mixer_lock);
66 (*audio->spec.callback) (audio->spec.userdata,
114 (Uint8 *) audio->convert.buf, 67 (Uint8 *) audio->convert.buf,
115 audio->convert.len); 68 audio->convert.len);
116 SDL_mutexV(audio->mixer_lock); 69 SDL_mutexV(audio->mixer_lock);
117 SDL_ConvertAudio(&audio->convert); 70 SDL_ConvertAudio(&audio->convert);
118 SDL_memcpy(stream, audio->convert.buf, 71 SDL_memcpy(stream, audio->convert.buf, audio->convert.len_cvt);
119 audio->convert.len_cvt); 72 } else {
120 } else { 73 SDL_mutexP(audio->mixer_lock);
121 SDL_mutexP(audio->mixer_lock); 74 (*audio->spec.callback) (audio->spec.userdata,
122 (*audio->spec.callback) (audio->spec.userdata, 75 (Uint8 *) stream, len);
123 (Uint8 *) stream, len); 76 SDL_mutexV(audio->mixer_lock);
124 SDL_mutexV(audio->mixer_lock);
125 }
126 } 77 }
127 return; 78 }
128 } 79 }
129 80
130 /* Dummy functions -- we don't use thread-based audio */ 81 static void
131 void BE_WaitAudio(_THIS) 82 BEAUDIO_CloseDevice(_THIS)
132 { 83 {
133 return; 84 if (_this->hidden != NULL) {
134 } 85 if (_this->hidden->audio_obj) {
135 void BE_PlayAudio(_THIS) 86 _this->hidden->audio_obj->Stop();
136 { 87 delete _this->hidden->audio_obj;
137 return; 88 _this->hidden->audio_obj = NULL;
138 }
139 Uint8 *BE_GetAudioBuf(_THIS)
140 {
141 return (NULL);
142 }
143
144 void BE_CloseAudio(_THIS)
145 {
146 if (audio_obj) {
147 audio_obj->Stop();
148 delete audio_obj;
149 audio_obj = NULL;
150 } 89 }
90
91 delete _this->hidden;
92 _this->hidden = NULL;
151 93
152 /* Quit the Be Application, if there's nothing left to do */ 94 /* Quit the Be Application, if there's nothing left to do */
153 SDL_QuitBeApp(); 95 SDL_QuitBeApp();
154 } 96 }
155 97 }
156 int BE_OpenAudio(_THIS, SDL_AudioSpec * spec) 98
157 { 99 static int
158 int valid_datatype = 0; 100 BEAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
159 media_raw_audio_format format; 101 {
160 SDL_AudioFormat test_format = SDL_FirstAudioFormat(spec->format); 102 int valid_datatype = 0;
161 103 media_raw_audio_format format;
162 /* Parse the audio format and fill the Be raw audio format */ 104 SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
163 memset(&format, '\0', sizeof(media_raw_audio_format)); 105
164 format.byte_order = B_MEDIA_LITTLE_ENDIAN; 106 /* Initialize all variables that we clean on shutdown */
165 format.frame_rate = (float) spec->freq; 107 _this->hidden = new SDL_PrivateAudioData;
166 format.channel_count = spec->channels; /* !!! FIXME: support > 2? */ 108 if (_this->hidden == NULL) {
167 while ((!valid_datatype) && (test_format)) { 109 SDL_OutOfMemory();
168 valid_datatype = 1; 110 return 0;
169 spec->format = test_format; 111 }
170 switch (test_format) { 112 SDL_memset(_this->hidden, 0, (sizeof *_this->hidden));
113
114 /* Initialize the Be Application, if it's not already started */
115 if (SDL_InitBeApp() < 0) {
116 return 0;
117 }
118
119 /* Parse the audio format and fill the Be raw audio format */
120 memset(&format, '\0', sizeof(media_raw_audio_format));
121 format.byte_order = B_MEDIA_LITTLE_ENDIAN;
122 format.frame_rate = (float) this->spec.freq;
123 format.channel_count = this->spec.channels; /* !!! FIXME: support > 2? */
124 while ((!valid_datatype) && (test_format)) {
125 valid_datatype = 1;
126 this->spec.format = test_format;
127 switch (test_format) {
171 case AUDIO_S8: 128 case AUDIO_S8:
172 format.format = media_raw_audio_format::B_AUDIO_CHAR; 129 format.format = media_raw_audio_format::B_AUDIO_CHAR;
173 break; 130 break;
174 131
175 case AUDIO_U8: 132 case AUDIO_U8:
205 162
206 default: 163 default:
207 valid_datatype = 0; 164 valid_datatype = 0;
208 test_format = SDL_NextAudioFormat(); 165 test_format = SDL_NextAudioFormat();
209 break; 166 break;
210 }
211 } 167 }
212 168 }
213 format.buffer_size = spec->samples; 169
214 170 format.buffer_size = this->spec.samples;
215 if (!valid_datatype) { /* shouldn't happen, but just in case... */ 171
216 SDL_SetError("Unsupported audio format"); 172 if (!valid_datatype) { /* shouldn't happen, but just in case... */
217 return (-1); 173 SDL_SetError("Unsupported audio format");
218 } 174 return 0;
219 175 }
220 /* Initialize the Be Application, if it's not already started */ 176
221 if (SDL_InitBeApp() < 0) { 177 /* Calculate the final parameters for this audio specification */
222 return (-1); 178 SDL_CalculateAudioSpec(&this->spec);
223 } 179
224 180 /* Subscribe to the audio stream (creates a new thread) */
225 /* Calculate the final parameters for this audio specification */ 181 sigset_t omask;
226 SDL_CalculateAudioSpec(spec); 182 SDL_MaskSignals(&omask);
227 183 _this->hidden->audio_obj = new BSoundPlayer(&format, "SDL Audio",
228 /* Subscribe to the audio stream (creates a new thread) */ 184 FillSound, NULL, _this);
229 { 185 SDL_UnmaskSignals(&omask);
230 sigset_t omask; 186
231 SDL_MaskSignals(&omask); 187 if (_this->hidden->audio_obj->Start() == B_NO_ERROR) {
232 audio_obj = new BSoundPlayer(&format, "SDL Audio", FillSound, 188 _this->hidden->audio_obj->SetHasData(true);
233 NULL, _this); 189 } else {
234 SDL_UnmaskSignals(&omask); 190 SDL_SetError("Unable to start Be audio");
235 } 191 return 0;
236 if (audio_obj->Start() == B_NO_ERROR) { 192 }
237 audio_obj->SetHasData(true); 193
238 } else { 194 /* We're running! */
239 SDL_SetError("Unable to start Be audio"); 195 return 1;
240 return (-1); 196 }
241 } 197
242 198 static int
243 /* We're running! */ 199 BEAUDIO_Init(SDL_AudioDriverImpl *impl)
244 return (1); 200 {
245 } 201 /* Set the function pointers */
246 202 impl->OpenDevice = DSP_OpenDevice;
247 }; /* Extern C */ 203 impl->CloseDevice = DSP_CloseDevice;
204 impl->ProvidesOwnCallbackThread = 1;
205 impl->OnlyHasDefaultOutputDevice = 1;
206
207 return 1;
208 }
209
210
211 AudioBootStrap BEAUDIO_bootstrap = {
212 "baudio", "BeOS BSoundPlayer",
213 BEAUDIO_Available, BEAUDIO_Init, 0
214 };
248 215
249 /* vi: set ts=4 sw=4 expandtab: */ 216 /* vi: set ts=4 sw=4 expandtab: */