xref: /haiku/src/kits/game/GameSoundDevice.cpp (revision 9642f7705b27e5c270c15fa526d14e1848c2c27d)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		BGameSoundDevice.cpp
23 //	Author:			Christopher ML Zumwalt May (zummy@users.sf.net)
24 //	Description:	Manages the game producer. The class may change with out
25 //					notice and was only inteneded for use by the GameKit at
26 //					this time. Use at your own risk.
27 //------------------------------------------------------------------------------
28 
29 
30 #include "GameSoundDevice.h"
31 
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 
36 #include <Autolock.h>
37 #include <List.h>
38 #include <Locker.h>
39 #include <MediaAddOn.h>
40 #include <MediaRoster.h>
41 #include <MediaTheme.h>
42 #include <TimeSource.h>
43 
44 #include "GameSoundBuffer.h"
45 #include "GameProducer.h"
46 #include "GSUtility.h"
47 
48 
49 // BGameSoundDevice definitions ------------------------------------
50 const int32 kInitSoundCount = 32;
51 const int32 kGrowth = 16;
52 
53 static int32 sDeviceCount = 0;
54 static BGameSoundDevice* sDevice = NULL;
55 static BLocker sDeviceRefCountLock("GameSound device lock");
56 
57 
58 BGameSoundDevice*
59 GetDefaultDevice()
60 {
61 	BAutolock _(sDeviceRefCountLock);
62 
63 	if (!sDevice)
64 		sDevice = new BGameSoundDevice();
65 
66 	sDeviceCount++;
67 	return sDevice;
68 }
69 
70 
71 void
72 ReleaseDevice()
73 {
74 	BAutolock _(sDeviceRefCountLock);
75 
76 	sDeviceCount--;
77 
78 	if (sDeviceCount <= 0) {
79 		delete sDevice;
80 		sDevice = NULL;
81 	}
82 }
83 
84 
85 // BGameSoundDevice -------------------------------------------------------
86 BGameSoundDevice::BGameSoundDevice()
87 	:
88 	fIsConnected(false),
89 	fSoundCount(kInitSoundCount)
90 {
91 	memset(&fFormat, 0, sizeof(gs_audio_format));
92 
93 	fInitError = B_OK;
94 
95 	fSounds = new GameSoundBuffer*[kInitSoundCount];
96 	for (int32 i = 0; i < kInitSoundCount; i++)
97 		fSounds[i] = NULL;
98 }
99 
100 
101 BGameSoundDevice::~BGameSoundDevice()
102 {
103 	// We need to stop all the sounds before we stop the mixer
104 	for (int32 i = 0; i < fSoundCount; i++) {
105 		if (fSounds[i])
106 			fSounds[i]->StopPlaying();
107 		delete fSounds[i];
108 	}
109 
110 	delete[] fSounds;
111 }
112 
113 
114 status_t
115 BGameSoundDevice::InitCheck() const
116 {
117 	return fInitError;
118 }
119 
120 
121 const gs_audio_format&
122 BGameSoundDevice::Format() const
123 {
124 	return fFormat;
125 }
126 
127 
128 const gs_audio_format&
129 BGameSoundDevice::Format(gs_id sound) const
130 {
131 	return fSounds[sound - 1]->Format();
132 }
133 
134 
135 void
136 BGameSoundDevice::SetInitError(status_t error)
137 {
138 	fInitError = error;
139 }
140 
141 
142 status_t
143 BGameSoundDevice::CreateBuffer(gs_id* sound, const gs_audio_format* format,
144 	const void* data, int64 frames)
145 {
146 	if (frames <= 0 || !sound)
147 		return B_BAD_VALUE;
148 
149 	status_t err = B_MEDIA_TOO_MANY_BUFFERS;
150 	int32 position = AllocateSound();
151 
152 	if (position >= 0) {
153 		fSounds[position] = new SimpleSoundBuffer(format, data, frames);
154 
155 		media_node systemMixer;
156 		BMediaRoster::Roster()->GetAudioMixer(&systemMixer);
157 		err = fSounds[position]->Connect(&systemMixer);
158 	}
159 
160 	if (err == B_OK)
161 		*sound = gs_id(position + 1);
162 	return err;
163 }
164 
165 
166 status_t
167 BGameSoundDevice::CreateBuffer(gs_id* sound, const void* object,
168 	const gs_audio_format* format, size_t inBufferFrameCount,
169 	size_t inBufferCount)
170 {
171 	if (!object || !sound)
172 		return B_BAD_VALUE;
173 
174 	status_t err = B_MEDIA_TOO_MANY_BUFFERS;
175 	int32 position = AllocateSound();
176 
177 	if (position >= 0) {
178 		fSounds[position] = new StreamingSoundBuffer(format, object,
179 			inBufferFrameCount, inBufferCount);
180 
181 		media_node systemMixer;
182 		BMediaRoster::Roster()->GetAudioMixer(&systemMixer);
183 		err = fSounds[position]->Connect(&systemMixer);
184 	}
185 
186 	if (err == B_OK)
187 		*sound = gs_id(position + 1);
188 	return err;
189 }
190 
191 
192 void
193 BGameSoundDevice::ReleaseBuffer(gs_id sound)
194 {
195 	if (sound <= 0)
196 		return;
197 
198 	if (fSounds[sound - 1]) {
199 		// We must stop playback befor destroying the sound or else
200 		// we may receive fatal errors.
201 		fSounds[sound - 1]->StopPlaying();
202 
203 		delete fSounds[sound - 1];
204 		fSounds[sound - 1] = NULL;
205 	}
206 }
207 
208 
209 status_t
210 BGameSoundDevice::Buffer(gs_id sound, gs_audio_format* format, void*& data)
211 {
212 	if (!format || sound <= 0)
213 		return B_BAD_VALUE;
214 
215 	memcpy(format, &fSounds[sound - 1]->Format(), sizeof(gs_audio_format));
216 	if (fSounds[sound - 1]->Data()) {
217 		data = malloc(format->buffer_size);
218 		memcpy(data, fSounds[sound - 1]->Data(), format->buffer_size);
219 	} else
220 		data = NULL;
221 
222 	return B_OK;
223 }
224 
225 
226 status_t
227 BGameSoundDevice::StartPlaying(gs_id sound)
228 {
229 	if (sound <= 0)
230 		return B_BAD_VALUE;
231 
232 	if (!fSounds[sound - 1]->IsPlaying()) {
233 		// tell the producer to start playing the sound
234 		return fSounds[sound - 1]->StartPlaying();
235 	}
236 
237 	fSounds[sound - 1]->Reset();
238 	return EALREADY;
239 }
240 
241 
242 status_t
243 BGameSoundDevice::StopPlaying(gs_id sound)
244 {
245 	if (sound <= 0)
246 		return B_BAD_VALUE;
247 
248 	if (fSounds[sound - 1]->IsPlaying()) {
249 		// Tell the producer to stop play this sound
250 		fSounds[sound - 1]->Reset();
251 		return fSounds[sound - 1]->StopPlaying();
252 	}
253 
254 	return EALREADY;
255 }
256 
257 
258 bool
259 BGameSoundDevice::IsPlaying(gs_id sound)
260 {
261 	if (sound <= 0)
262 		return false;
263 	return fSounds[sound - 1]->IsPlaying();
264 }
265 
266 
267 status_t
268 BGameSoundDevice::GetAttributes(gs_id sound, gs_attribute* attributes,
269 	size_t attributeCount)
270 {
271 	if (!fSounds[sound - 1])
272 		return B_ERROR;
273 
274 	return fSounds[sound - 1]->GetAttributes(attributes, attributeCount);
275 }
276 
277 
278 status_t
279 BGameSoundDevice::SetAttributes(gs_id sound, gs_attribute* attributes,
280 	size_t attributeCount)
281 {
282 	if (!fSounds[sound - 1])
283 		return B_ERROR;
284 
285 	return fSounds[sound - 1]->SetAttributes(attributes, attributeCount);
286 }
287 
288 
289 int32
290 BGameSoundDevice::AllocateSound()
291 {
292 	for (int32 i = 0; i < fSoundCount; i++)
293 		if (!fSounds[i])
294 			return i;
295 
296 	// we need to allocate new space for the sound
297 	GameSoundBuffer ** sounds = new GameSoundBuffer*[fSoundCount + kGrowth];
298 	for (int32 i = 0; i < fSoundCount; i++)
299 		sounds[i] = fSounds[i];
300 
301 	for (int32 i = fSoundCount; i < fSoundCount + kGrowth; i++)
302 		sounds[i] = NULL;
303 
304 	// replace the old list
305 	delete [] fSounds;
306 	fSounds = sounds;
307 	fSoundCount += kGrowth;
308 
309 	return fSoundCount - kGrowth;
310 }
311 
312