zenilib: zenilib/jni/external/sdl/src/audio/baudio/SDL_beaudio.cc Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_beaudio.cc
Go to the documentation of this file.
1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "SDL_config.h"
22
23 #if SDL_AUDIO_DRIVER_BEOSAUDIO
24
25 /* Allow access to the audio stream on BeOS */
26
27 #include <SoundPlayer.h>
28
29 #include "../../main/beos/SDL_BeApp.h"
30
31 extern "C"
32 {
33
34 #include "SDL_audio.h"
35 #include "../SDL_audio_c.h"
36 #include "../SDL_sysaudio.h"
37 #include "../../thread/beos/SDL_systhread_c.h"
38 #include "SDL_beaudio.h"
39
40 }
41
42
43 /* !!! FIXME: have the callback call the higher level to avoid code dupe. */
44 /* The BeOS callback for handling the audio buffer */
45 static void
46 FillSound(void *device, void *stream, size_t len,
47 const media_raw_audio_format & format)
48 {
49 SDL_AudioDevice *audio = (SDL_AudioDevice *) device;
50
51 /* Only do soemthing if audio is enabled */
52 if (!audio->enabled)
53 return;
54
55 if (!audio->paused) {
56 if (audio->convert.needed) {
57 SDL_LockMutex(audio->mixer_lock);
58 (*audio->spec.callback) (audio->spec.userdata,
59 (Uint8 *) audio->convert.buf,
60 audio->convert.len);
61 SDL_UnlockMutex(audio->mixer_lock);
62 SDL_ConvertAudio(&audio->convert);
63 SDL_memcpy(stream, audio->convert.buf, audio->convert.len_cvt);
64 } else {
65 SDL_LockMutex(audio->mixer_lock);
66 (*audio->spec.callback) (audio->spec.userdata,
67 (Uint8 *) stream, len);
68 SDL_UnlockMutex(audio->mixer_lock);
69 }
70 }
71 }
72
73 static void
74 BEOSAUDIO_CloseDevice(_THIS)
75 {
76 if (_this->hidden != NULL) {
77 if (_this->hidden->audio_obj) {
78 _this->hidden->audio_obj->Stop();
79 delete _this->hidden->audio_obj;
80 _this->hidden->audio_obj = NULL;
81 }
82
83 delete _this->hidden;
84 _this->hidden = NULL;
85 }
86 }
87
88 static int
89 BEOSAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
90 {
91 int valid_datatype = 0;
92 media_raw_audio_format format;
93 SDL_AudioFormat test_format = SDL_FirstAudioFormat(_this->spec.format);
94
95 /* Initialize all variables that we clean on shutdown */
96 _this->hidden = new SDL_PrivateAudioData;
97 if (_this->hidden == NULL) {
98 return SDL_OutOfMemory();
99 }
100 SDL_memset(_this->hidden, 0, (sizeof *_this->hidden));
101
102 /* Parse the audio format and fill the Be raw audio format */
103 SDL_memset(&format, '\0', sizeof(media_raw_audio_format));
104 format.byte_order = B_MEDIA_LITTLE_ENDIAN;
105 format.frame_rate = (float) _this->spec.freq;
106 format.channel_count = _this->spec.channels; /* !!! FIXME: support > 2? */
107 while ((!valid_datatype) && (test_format)) {
108 valid_datatype = 1;
109 _this->spec.format = test_format;
110 switch (test_format) {
111 case AUDIO_S8:
112 format.format = media_raw_audio_format::B_AUDIO_CHAR;
113 break;
114
115 case AUDIO_U8:
116 format.format = media_raw_audio_format::B_AUDIO_UCHAR;
117 break;
118
119 case AUDIO_S16LSB:
120 format.format = media_raw_audio_format::B_AUDIO_SHORT;
121 break;
122
123 case AUDIO_S16MSB:
124 format.format = media_raw_audio_format::B_AUDIO_SHORT;
125 format.byte_order = B_MEDIA_BIG_ENDIAN;
126 break;
127
128 case AUDIO_S32LSB:
129 format.format = media_raw_audio_format::B_AUDIO_INT;
130 break;
131
132 case AUDIO_S32MSB:
133 format.format = media_raw_audio_format::B_AUDIO_INT;
134 format.byte_order = B_MEDIA_BIG_ENDIAN;
135 break;
136
137 case AUDIO_F32LSB:
138 format.format = media_raw_audio_format::B_AUDIO_FLOAT;
139 break;
140
141 case AUDIO_F32MSB:
142 format.format = media_raw_audio_format::B_AUDIO_FLOAT;
143 format.byte_order = B_MEDIA_BIG_ENDIAN;
144 break;
145
146 default:
147 valid_datatype = 0;
148 test_format = SDL_NextAudioFormat();
149 break;
150 }
151 }
152
153 if (!valid_datatype) { /* shouldn't happen, but just in case... */
154 BEOSAUDIO_CloseDevice(_this);
155 return SDL_SetError("Unsupported audio format");
156 }
157
158 /* Calculate the final parameters for this audio specification */
159 SDL_CalculateAudioSpec(&_this->spec);
160
161 format.buffer_size = _this->spec.size;
162
163 /* Subscribe to the audio stream (creates a new thread) */
164 sigset_t omask;
165 SDL_MaskSignals(&omask);
166 _this->hidden->audio_obj = new BSoundPlayer(&format, "SDL Audio",
167 FillSound, NULL, _this);
168 SDL_UnmaskSignals(&omask);
169
170 if (_this->hidden->audio_obj->Start() == B_NO_ERROR) {
171 _this->hidden->audio_obj->SetHasData(true);
172 } else {
173 BEOSAUDIO_CloseDevice(_this);
174 return SDL_SetError("Unable to start Be audio");
175 }
176
177 /* We're running! */
178 return 0;
179 }
180
181 static void
182 BEOSAUDIO_Deinitialize(void)
183 {
184 SDL_QuitBeApp();
185 }
186
187 static int
188 BEOSAUDIO_Init(SDL_AudioDriverImpl * impl)
189 {
190 /* Initialize the Be Application, if it's not already started */
191 if (SDL_InitBeApp() < 0) {
192 return 0;
193 }
194
195 /* Set the function pointers */
196 impl->OpenDevice = BEOSAUDIO_OpenDevice;
197 impl->CloseDevice = BEOSAUDIO_CloseDevice;
198 impl->Deinitialize = BEOSAUDIO_Deinitialize;
199 impl->ProvidesOwnCallbackThread = 1;
200 impl->OnlyHasDefaultOutputDevice = 1;
201
202 return 1; /* this audio target is available. */
203 }
204
205 extern "C"
206 {
207 extern AudioBootStrap BEOSAUDIO_bootstrap;
208 }
209 AudioBootStrap BEOSAUDIO_bootstrap = {
210 "baudio", "BeOS BSoundPlayer", BEOSAUDIO_Init, 0
211 };
212
213 #endif /* SDL_AUDIO_DRIVER_BEOSAUDIO */
214
215 /* vi: set ts=4 sw=4 expandtab: */
SDL_AudioDriverImpl::CloseDevice
void(* CloseDevice)(_THIS)
Definition: SDL_sysaudio.h:45
SDL_AudioDevice::mixer_lock
SDL_mutex * mixer_lock
Definition: SDL_sysaudio.h:116
SDL_FirstAudioFormat
SDL_AudioFormat SDL_FirstAudioFormat(SDL_AudioFormat format)
Definition: SDL_audio.c:1226
AUDIO_S32MSB
#define AUDIO_S32MSB
Definition: SDL_audio.h:104
SDL_AudioCVT::len
int len
Definition: SDL_audio.h:204
NULL
#define NULL
Definition: ftobjs.h:61
SDL_AudioCVT::buf
Uint8 * buf
Definition: SDL_audio.h:203
SDL_LockMutex
DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.c:73
SDL_InitBeApp
int SDL_InitBeApp(void)
stream
GLuint GLuint stream
Definition: glew.h:6573
SDL_AudioDriverImpl::ProvidesOwnCallbackThread
int ProvidesOwnCallbackThread
Definition: SDL_sysaudio.h:53
SDL_QuitBeApp
void SDL_QuitBeApp(void)
SDL_AudioDevice::spec
SDL_AudioSpec spec
Definition: SDL_sysaudio.h:97
SDL_AudioFormat
Uint16 SDL_AudioFormat
Audio format flags.
Definition: SDL_audio.h:64
len
GLenum GLsizei len
Definition: glew.h:7035
SDL_AudioDriverImpl::OpenDevice
int(* OpenDevice)(_THIS, const char *devname, int iscapture)
Definition: SDL_sysaudio.h:39
SDL_PrivateAudioData
Definition: SDL_alsa_audio.h:33
SDL_config.h
SDL_UnlockMutex
DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.c:160
SDL_NextAudioFormat
SDL_AudioFormat SDL_NextAudioFormat(void)
Definition: SDL_audio.c:1238
SDL_UnmaskSignals
void SDL_UnmaskSignals(sigset_t *omask)
AUDIO_F32MSB
#define AUDIO_F32MSB
Definition: SDL_audio.h:113
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:92
AUDIO_U8
#define AUDIO_U8
Definition: SDL_audio.h:89
AudioBootStrap
Definition: SDL_sysaudio.h:128
SDL_AudioDriverImpl::OnlyHasDefaultOutputDevice
int OnlyHasDefaultOutputDevice
Definition: SDL_sysaudio.h:56
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
AUDIO_F32LSB
#define AUDIO_F32LSB
Definition: SDL_audio.h:112
SDL_AudioDriverImpl
Definition: SDL_sysaudio.h:36
SDL_memset
DECLSPEC void *SDLCALL SDL_memset(void *dst, int c, size_t len)
Definition: SDL_string.c:261
AUDIO_S32LSB
#define AUDIO_S32LSB
Definition: SDL_audio.h:103
format
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl2ext.h:845
SDL_SetError
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
Definition: SDL_error.c:53
SDL_AudioCVT::needed
int needed
Definition: SDL_audio.h:199
SDL_CalculateAudioSpec
void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
Definition: SDL_audio.c:1247
SDL_AudioDriverImpl::Deinitialize
void(* Deinitialize)(void)
Definition: SDL_sysaudio.h:48
BEOSAUDIO_bootstrap
AudioBootStrap BEOSAUDIO_bootstrap
SDL_AudioSpec::callback
SDL_AudioCallback callback
Definition: SDL_audio.h:174
SDL_AudioCVT::len_cvt
int len_cvt
Definition: SDL_audio.h:205
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_memcpy
DECLSPEC void *SDLCALL SDL_memcpy(void *dst, const void *src, size_t len)
Definition: SDL_string.c:293
AUDIO_S16MSB
#define AUDIO_S16MSB
Definition: SDL_audio.h:94
SDL_audio.h
SDL_AudioDevice
Definition: SDL_sysaudio.h:91
Uint8
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:129
SDL_AudioDevice::paused
int paused
Definition: SDL_sysaudio.h:109
AUDIO_S16LSB
#define AUDIO_S16LSB
Definition: SDL_audio.h:92
SDL_AudioSpec::userdata
void * userdata
Definition: SDL_audio.h:175
SDL_beaudio.h
while
while(1)
Definition: glslang_lex.cpp:1063
SDL_AudioDevice::enabled
int enabled
Definition: SDL_sysaudio.h:108
SDL_AudioDevice::convert
SDL_AudioCVT convert
Definition: SDL_sysaudio.h:100
AUDIO_S8
#define AUDIO_S8
Definition: SDL_audio.h:90
SDL_MaskSignals
void SDL_MaskSignals(sigset_t *omask)
SDL_ConvertAudio
DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT *cvt)
Definition: SDL_audiocvt.c:774