zenilib  0.5.3.0
 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);
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);
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 */
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 {
208 }
210  "baudio", "BeOS BSoundPlayer", BEOSAUDIO_Init, 0
211 };
212 
213 #endif /* SDL_AUDIO_DRIVER_BEOSAUDIO */
214 
215 /* vi: set ts=4 sw=4 expandtab: */
void(* CloseDevice)(_THIS)
Definition: SDL_sysaudio.h:45
SDL_mutex * mixer_lock
Definition: SDL_sysaudio.h:116
SDL_AudioFormat SDL_FirstAudioFormat(SDL_AudioFormat format)
Definition: SDL_audio.c:1226
#define AUDIO_S32MSB
Definition: SDL_audio.h:104
#define NULL
Definition: ftobjs.h:61
Uint8 * buf
Definition: SDL_audio.h:203
DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.c:73
int SDL_InitBeApp(void)
GLuint GLuint stream
Definition: glew.h:6573
void SDL_QuitBeApp(void)
SDL_AudioSpec spec
Definition: SDL_sysaudio.h:97
Uint16 SDL_AudioFormat
Audio format flags.
Definition: SDL_audio.h:64
GLenum GLsizei len
Definition: glew.h:7035
int(* OpenDevice)(_THIS, const char *devname, int iscapture)
Definition: SDL_sysaudio.h:39
DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.c:160
SDL_AudioFormat SDL_NextAudioFormat(void)
Definition: SDL_audio.c:1238
void SDL_UnmaskSignals(sigset_t *omask)
#define AUDIO_F32MSB
Definition: SDL_audio.h:113
static SDL_VideoDevice * _this
Definition: SDL_video.c:92
#define AUDIO_U8
Definition: SDL_audio.h:89
#define _THIS
#define AUDIO_F32LSB
Definition: SDL_audio.h:112
DECLSPEC void *SDLCALL SDL_memset(void *dst, int c, size_t len)
Definition: SDL_string.c:261
#define AUDIO_S32LSB
Definition: SDL_audio.h:103
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl2ext.h:845
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
Definition: SDL_error.c:53
void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
Definition: SDL_audio.c:1247
void(* Deinitialize)(void)
Definition: SDL_sysaudio.h:48
AudioBootStrap BEOSAUDIO_bootstrap
SDL_AudioCallback callback
Definition: SDL_audio.h:174
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
DECLSPEC void *SDLCALL SDL_memcpy(void *dst, const void *src, size_t len)
Definition: SDL_string.c:293
#define AUDIO_S16MSB
Definition: SDL_audio.h:94
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:129
#define AUDIO_S16LSB
Definition: SDL_audio.h:92
void * userdata
Definition: SDL_audio.h:175
while(1)
SDL_AudioCVT convert
Definition: SDL_sysaudio.h:100
#define AUDIO_S8
Definition: SDL_audio.h:90
void SDL_MaskSignals(sigset_t *omask)
DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT *cvt)
Definition: SDL_audiocvt.c:774