Skip to content

Latest commit

 

History

History
240 lines (194 loc) · 6.77 KB

SDL_haikuaudio.cc

File metadata and controls

240 lines (194 loc) · 6.77 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
Simple DirectMedia Layer
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
Nov 14, 2013
Nov 14, 2013
23
#if SDL_AUDIO_DRIVER_HAIKU
Nov 14, 2013
Nov 14, 2013
25
/* Allow access to the audio stream on Haiku */
26
27
#include <SoundPlayer.h>
Nov 14, 2013
Nov 14, 2013
28
#include <signal.h>
Nov 14, 2013
Nov 14, 2013
30
#include "../../main/haiku/SDL_BeApp.h"
31
32
33
34
35
36
37
extern "C"
{
#include "SDL_audio.h"
#include "../SDL_audio_c.h"
#include "../SDL_sysaudio.h"
Nov 14, 2013
Nov 14, 2013
38
#include "SDL_haikuaudio.h"
39
40
41
42
43
}
/* !!! FIXME: have the callback call the higher level to avoid code dupe. */
Nov 14, 2013
Nov 14, 2013
44
/* The Haiku callback for handling the audio buffer */
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
static void
FillSound(void *device, void *stream, size_t len,
const media_raw_audio_format & format)
{
SDL_AudioDevice *audio = (SDL_AudioDevice *) device;
/* Only do soemthing if audio is enabled */
if (!audio->enabled)
return;
if (!audio->paused) {
if (audio->convert.needed) {
SDL_LockMutex(audio->mixer_lock);
(*audio->spec.callback) (audio->spec.userdata,
(Uint8 *) audio->convert.buf,
audio->convert.len);
SDL_UnlockMutex(audio->mixer_lock);
SDL_ConvertAudio(&audio->convert);
SDL_memcpy(stream, audio->convert.buf, audio->convert.len_cvt);
} else {
SDL_LockMutex(audio->mixer_lock);
(*audio->spec.callback) (audio->spec.userdata,
(Uint8 *) stream, len);
SDL_UnlockMutex(audio->mixer_lock);
}
}
}
static void
Nov 14, 2013
Nov 14, 2013
74
HAIKUAUDIO_CloseDevice(_THIS)
75
76
77
78
79
80
81
82
83
84
85
86
87
{
if (_this->hidden != NULL) {
if (_this->hidden->audio_obj) {
_this->hidden->audio_obj->Stop();
delete _this->hidden->audio_obj;
_this->hidden->audio_obj = NULL;
}
delete _this->hidden;
_this->hidden = NULL;
}
}
Nov 14, 2013
Nov 14, 2013
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
static const int sig_list[] = {
SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGWINCH, 0
};
static inline void
MaskSignals(sigset_t * omask)
{
sigset_t mask;
int i;
sigemptyset(&mask);
for (i = 0; sig_list[i]; ++i) {
sigaddset(&mask, sig_list[i]);
}
sigprocmask(SIG_BLOCK, &mask, omask);
}
static inline void
UnmaskSignals(sigset_t * omask)
{
sigprocmask(SIG_SETMASK, omask, NULL);
}
113
static int
Nov 14, 2013
Nov 14, 2013
114
HAIKUAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
int valid_datatype = 0;
media_raw_audio_format format;
SDL_AudioFormat test_format = SDL_FirstAudioFormat(_this->spec.format);
/* Initialize all variables that we clean on shutdown */
_this->hidden = new SDL_PrivateAudioData;
if (_this->hidden == NULL) {
return SDL_OutOfMemory();
}
SDL_memset(_this->hidden, 0, (sizeof *_this->hidden));
/* Parse the audio format and fill the Be raw audio format */
SDL_memset(&format, '\0', sizeof(media_raw_audio_format));
format.byte_order = B_MEDIA_LITTLE_ENDIAN;
format.frame_rate = (float) _this->spec.freq;
format.channel_count = _this->spec.channels; /* !!! FIXME: support > 2? */
while ((!valid_datatype) && (test_format)) {
valid_datatype = 1;
_this->spec.format = test_format;
switch (test_format) {
case AUDIO_S8:
format.format = media_raw_audio_format::B_AUDIO_CHAR;
break;
case AUDIO_U8:
format.format = media_raw_audio_format::B_AUDIO_UCHAR;
break;
case AUDIO_S16LSB:
format.format = media_raw_audio_format::B_AUDIO_SHORT;
break;
case AUDIO_S16MSB:
format.format = media_raw_audio_format::B_AUDIO_SHORT;
format.byte_order = B_MEDIA_BIG_ENDIAN;
break;
case AUDIO_S32LSB:
format.format = media_raw_audio_format::B_AUDIO_INT;
break;
case AUDIO_S32MSB:
format.format = media_raw_audio_format::B_AUDIO_INT;
format.byte_order = B_MEDIA_BIG_ENDIAN;
break;
case AUDIO_F32LSB:
format.format = media_raw_audio_format::B_AUDIO_FLOAT;
break;
case AUDIO_F32MSB:
format.format = media_raw_audio_format::B_AUDIO_FLOAT;
format.byte_order = B_MEDIA_BIG_ENDIAN;
break;
default:
valid_datatype = 0;
test_format = SDL_NextAudioFormat();
break;
}
}
if (!valid_datatype) { /* shouldn't happen, but just in case... */
Nov 14, 2013
Nov 14, 2013
179
HAIKUAUDIO_CloseDevice(_this);
180
181
182
183
184
185
186
187
188
189
return SDL_SetError("Unsupported audio format");
}
/* Calculate the final parameters for this audio specification */
SDL_CalculateAudioSpec(&_this->spec);
format.buffer_size = _this->spec.size;
/* Subscribe to the audio stream (creates a new thread) */
sigset_t omask;
Nov 14, 2013
Nov 14, 2013
190
MaskSignals(&omask);
191
192
_this->hidden->audio_obj = new BSoundPlayer(&format, "SDL Audio",
FillSound, NULL, _this);
Nov 14, 2013
Nov 14, 2013
193
UnmaskSignals(&omask);
194
195
196
197
if (_this->hidden->audio_obj->Start() == B_NO_ERROR) {
_this->hidden->audio_obj->SetHasData(true);
} else {
Nov 14, 2013
Nov 14, 2013
198
HAIKUAUDIO_CloseDevice(_this);
199
200
201
202
203
204
205
206
return SDL_SetError("Unable to start Be audio");
}
/* We're running! */
return 0;
}
static void
Nov 14, 2013
Nov 14, 2013
207
HAIKUAUDIO_Deinitialize(void)
208
209
210
211
212
{
SDL_QuitBeApp();
}
static int
Nov 14, 2013
Nov 14, 2013
213
HAIKUAUDIO_Init(SDL_AudioDriverImpl * impl)
214
215
216
217
218
219
220
{
/* Initialize the Be Application, if it's not already started */
if (SDL_InitBeApp() < 0) {
return 0;
}
/* Set the function pointers */
Nov 14, 2013
Nov 14, 2013
221
222
223
impl->OpenDevice = HAIKUAUDIO_OpenDevice;
impl->CloseDevice = HAIKUAUDIO_CloseDevice;
impl->Deinitialize = HAIKUAUDIO_Deinitialize;
224
225
226
227
228
229
230
231
impl->ProvidesOwnCallbackThread = 1;
impl->OnlyHasDefaultOutputDevice = 1;
return 1; /* this audio target is available. */
}
extern "C"
{
Nov 14, 2013
Nov 14, 2013
232
extern AudioBootStrap HAIKUAUDIO_bootstrap;
Nov 14, 2013
Nov 14, 2013
234
235
AudioBootStrap HAIKUAUDIO_bootstrap = {
"haiku", "Haiku BSoundPlayer", HAIKUAUDIO_Init, 0
Nov 14, 2013
Nov 14, 2013
238
#endif /* SDL_AUDIO_DRIVER_HAIKU */
239
240
/* vi: set ts=4 sw=4 expandtab: */