xref: /haiku/src/kits/game/GameSound.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
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:		GameSound.cpp
23 //	Author:			Christopher ML Zumwalt May (zummy@users.sf.net)
24 //	Description:	Provides much of interfaces with BGameSoundDevice on behalf
25 //					of the rest of it's childern.
26 //------------------------------------------------------------------------------
27 
28 #include <stdio.h>
29 #include <string.h>
30 
31 #include <GameSound.h>
32 
33 #include "GameSoundBuffer.h"
34 #include "GameSoundDevice.h"
35 
36 
37 using std::nothrow;
38 
39 // Local Defines ---------------------------------------------------------------
40 
41 // BGameSound class ------------------------------------------------------------
42 BGameSound::BGameSound(BGameSoundDevice *device)
43 		:	fSound(-1)
44 {
45 	fDevice = GetDefaultDevice();
46 	fInitError = fDevice->InitCheck();
47 }
48 
49 
50 BGameSound::BGameSound(const BGameSound &other)
51 		:	fSound(-1)
52 {
53 	memcpy(&fFormat, &other.fFormat, sizeof(gs_audio_format));
54 	fDevice = GetDefaultDevice();
55 
56 	fInitError = fDevice->InitCheck();
57 }
58 
59 
60 BGameSound::~BGameSound()
61 {
62 	if (fSound >= 0)
63 		fDevice->ReleaseBuffer(fSound);
64 
65 	ReleaseDevice();
66 }
67 
68 
69 status_t
70 BGameSound::InitCheck() const
71 {
72 	return fInitError;
73 }
74 
75 
76 BGameSoundDevice *
77 BGameSound::Device() const
78 {
79 	return fDevice;
80 }
81 
82 
83 gs_id
84 BGameSound::ID() const
85 {
86 	return fSound;
87 }
88 
89 
90 const gs_audio_format &
91 BGameSound::Format() const
92 {
93 	return fDevice->Format(fSound);
94 }
95 
96 
97 status_t
98 BGameSound::StartPlaying()
99 {
100 	fDevice->StartPlaying(fSound);
101 	return B_OK;
102 }
103 
104 
105 bool
106 BGameSound::IsPlaying()
107 {
108 	return fDevice->IsPlaying(fSound);
109 }
110 
111 
112 status_t
113 BGameSound::StopPlaying()
114 {
115 	fDevice->StopPlaying(fSound);
116 	return B_OK;
117 }
118 
119 
120 status_t
121 BGameSound::SetGain(float gain,
122 					bigtime_t duration)
123 {
124 	gs_attribute attribute;
125 
126 	attribute.attribute = B_GS_GAIN;
127 	attribute.value = gain;
128 	attribute.duration = duration;
129 	attribute.flags = 0;
130 
131 	return fDevice->SetAttributes(fSound, &attribute, 1);
132 }
133 
134 
135 status_t
136 BGameSound::SetPan(float pan,
137 				   bigtime_t duration)
138 {
139 	gs_attribute attribute;
140 
141 	attribute.attribute = B_GS_PAN;
142 	attribute.value = pan;
143 	attribute.duration = duration;
144 	attribute.flags = 0;
145 
146 	return fDevice->SetAttributes(fSound, &attribute, 1);
147 }
148 
149 
150 float
151 BGameSound::Gain()
152 {
153 	gs_attribute attribute;
154 
155 	attribute.attribute = B_GS_GAIN;
156 	attribute.flags = 0;
157 
158 	if (fDevice->GetAttributes(fSound, &attribute, 1) != B_OK)
159 		return 0.0;
160 
161 	return attribute.value;
162 }
163 
164 
165 float
166 BGameSound::Pan()
167 {
168 	gs_attribute attribute;
169 
170 	attribute.attribute = B_GS_PAN;
171 	attribute.flags = 0;
172 
173 	if (fDevice->GetAttributes(fSound, &attribute, 1) != B_OK)
174 		return 0.0;
175 
176 	return attribute.value;
177 }
178 
179 
180 status_t
181 BGameSound::SetAttributes(gs_attribute *inAttributes,
182 						  size_t inAttributeCount)
183 {
184 	return fDevice->SetAttributes(fSound, inAttributes, inAttributeCount);
185 }
186 
187 
188 status_t
189 BGameSound::GetAttributes(gs_attribute *outAttributes,
190 						  size_t inAttributeCount)
191 {
192 	return fDevice->GetAttributes(fSound, outAttributes, inAttributeCount);
193 }
194 
195 
196 status_t
197 BGameSound::Perform(int32 selector,
198 					void *data)
199 {
200 	return B_ERROR;
201 }
202 
203 
204 void *
205 BGameSound::operator new(size_t size)
206 {
207 	return ::operator new(size);
208 }
209 
210 
211 void *
212 BGameSound::operator new(size_t size, const std::nothrow_t &nt) throw()
213 {
214 	return ::operator new(size, nt);
215 }
216 
217 
218 void
219 BGameSound::operator delete(void *ptr)
220 {
221 	::operator delete(ptr);
222 }
223 
224 
225 #if !__MWERKS__
226 //	there's a bug in MWCC under R4.1 and earlier
227 void
228 BGameSound::operator delete(void *ptr, const std::nothrow_t &nt) throw()
229 {
230 	::operator delete(ptr, nt);
231 }
232 #endif
233 
234 
235 status_t
236 BGameSound::SetMemoryPoolSize(size_t in_poolSize)
237 {
238 	return B_ERROR;
239 }
240 
241 
242 status_t
243 BGameSound::LockMemoryPool(bool in_lockInCore)
244 {
245 	return B_ERROR;
246 }
247 
248 
249 int32
250 BGameSound::SetMaxSoundCount(int32 in_maxCount)
251 {
252 	return in_maxCount;
253 }
254 
255 
256 status_t
257 BGameSound::SetInitError(status_t in_initError)
258 {
259 	fInitError = in_initError;
260 	return B_OK;
261 }
262 
263 
264 status_t
265 BGameSound::Init(gs_id handle)
266 {
267 	if (fSound < 0) fSound = handle;
268 
269 	return B_OK;
270 }
271 
272 /*
273 BGameSound &
274 BGameSound::operator=(const BGameSound &other)
275 {
276 	if (fSound)
277 		fDevice->ReleaseBuffer(fSound);
278 
279 	fSound = other.fSound;
280 	fInitError = other.fInitError;
281 
282 	return this;
283 }
284 */
285 
286 /* unimplemented for protection of the user:
287  *
288  * BGameSound::BGameSound()
289  */
290 
291 
292 status_t
293 BGameSound::_Reserved_BGameSound_0(int32 arg, ...)
294 {
295 	return B_ERROR;
296 }
297 
298 
299 status_t
300 BGameSound::_Reserved_BGameSound_1(int32 arg, ...)
301 {
302 	return B_ERROR;
303 }
304 
305 
306 status_t
307 BGameSound::_Reserved_BGameSound_2(int32 arg, ...)
308 {
309 	return B_ERROR;
310 }
311 
312 
313 status_t
314 BGameSound::_Reserved_BGameSound_3(int32 arg, ...)
315 {
316 	return B_ERROR;
317 }
318 
319 
320 status_t
321 BGameSound::_Reserved_BGameSound_4(int32 arg, ...)
322 {
323 	return B_ERROR;
324 }
325 
326 
327 status_t
328 BGameSound::_Reserved_BGameSound_5(int32 arg, ...)
329 {
330 	return B_ERROR;
331 }
332 
333 
334 status_t
335 BGameSound::_Reserved_BGameSound_6(int32 arg, ...)
336 {
337 	return B_ERROR;
338 }
339 
340 
341 status_t
342 BGameSound::_Reserved_BGameSound_7(int32 arg, ...)
343 {
344 	return B_ERROR;
345 }
346 
347 
348 status_t
349 BGameSound::_Reserved_BGameSound_8(int32 arg, ...)
350 {
351 	return B_ERROR;
352 }
353 
354 
355 status_t
356 BGameSound::_Reserved_BGameSound_9(int32 arg, ...)
357 {
358 	return B_ERROR;
359 }
360 
361 
362 status_t
363 BGameSound::_Reserved_BGameSound_10(int32 arg, ...)
364 {
365 	return B_ERROR;
366 }
367 
368 
369 status_t
370 BGameSound::_Reserved_BGameSound_11(int32 arg, ...)
371 {
372 	return B_ERROR;
373 }
374 
375 
376 status_t
377 BGameSound::_Reserved_BGameSound_12(int32 arg, ...)
378 {
379 	return B_ERROR;
380 }
381 
382 
383 status_t
384 BGameSound::_Reserved_BGameSound_13(int32 arg, ...)
385 {
386 	return B_ERROR;
387 }
388 
389 
390 status_t
391 BGameSound::_Reserved_BGameSound_14(int32 arg, ...)
392 {
393 	return B_ERROR;
394 }
395 
396 
397 status_t
398 BGameSound::_Reserved_BGameSound_15(int32 arg, ...)
399 {
400 	return B_ERROR;
401 }
402 
403 
404 status_t
405 BGameSound::_Reserved_BGameSound_16(int32 arg, ...)
406 {
407 	return B_ERROR;
408 }
409 
410 
411 status_t
412 BGameSound::_Reserved_BGameSound_17(int32 arg, ...)
413 {
414 	return B_ERROR;
415 }
416 
417 
418 status_t
419 BGameSound::_Reserved_BGameSound_18(int32 arg, ...)
420 {
421 	return B_ERROR;
422 }
423 
424 
425 status_t
426 BGameSound::_Reserved_BGameSound_19(int32 arg, ...)
427 {
428 	return B_ERROR;
429 }
430 
431 
432 status_t
433 BGameSound::_Reserved_BGameSound_20(int32 arg, ...)
434 {
435 	return B_ERROR;
436 }
437 
438 
439 status_t
440 BGameSound::_Reserved_BGameSound_21(int32 arg, ...)
441 {
442 	return B_ERROR;
443 }
444 
445 
446 status_t
447 BGameSound::_Reserved_BGameSound_22(int32 arg, ...)
448 {
449 	return B_ERROR;
450 }
451 
452 
453 status_t
454 BGameSound::_Reserved_BGameSound_23(int32 arg, ...)
455 {
456 	return B_ERROR;
457 }
458 
459 
460 status_t
461 BGameSound::_Reserved_BGameSound_24(int32 arg, ...)
462 {
463 	return B_ERROR;
464 }
465 
466 
467 status_t
468 BGameSound::_Reserved_BGameSound_25(int32 arg, ...)
469 {
470 	return B_ERROR;
471 }
472 
473 
474 status_t
475 BGameSound::_Reserved_BGameSound_26(int32 arg, ...)
476 {
477 	return B_ERROR;
478 }
479 
480 
481 status_t
482 BGameSound::_Reserved_BGameSound_27(int32 arg, ...)
483 {
484 	return B_ERROR;
485 }
486 
487 
488 status_t
489 BGameSound::_Reserved_BGameSound_28(int32 arg, ...)
490 {
491 	return B_ERROR;
492 }
493 
494 
495 status_t
496 BGameSound::_Reserved_BGameSound_29(int32 arg, ...)
497 {
498 	return B_ERROR;
499 }
500 
501 
502 status_t
503 BGameSound::_Reserved_BGameSound_30(int32 arg, ...)
504 {
505 	return B_ERROR;
506 }
507 
508 
509 status_t
510 BGameSound::_Reserved_BGameSound_31(int32 arg, ...)
511 {
512 	return B_ERROR;
513 }
514 
515 
516 status_t
517 BGameSound::_Reserved_BGameSound_32(int32 arg, ...)
518 {
519 	return B_ERROR;
520 }
521 
522 
523 status_t
524 BGameSound::_Reserved_BGameSound_33(int32 arg, ...)
525 {
526 	return B_ERROR;
527 }
528 
529 
530 status_t
531 BGameSound::_Reserved_BGameSound_34(int32 arg, ...)
532 {
533 	return B_ERROR;
534 }
535 
536 
537 status_t
538 BGameSound::_Reserved_BGameSound_35(int32 arg, ...)
539 {
540 	return B_ERROR;
541 }
542 
543 
544 status_t
545 BGameSound::_Reserved_BGameSound_36(int32 arg, ...)
546 {
547 	return B_ERROR;
548 }
549 
550 
551 status_t
552 BGameSound::_Reserved_BGameSound_37(int32 arg, ...)
553 {
554 	return B_ERROR;
555 }
556 
557 
558 status_t
559 BGameSound::_Reserved_BGameSound_38(int32 arg, ...)
560 {
561 	return B_ERROR;
562 }
563 
564 
565 status_t
566 BGameSound::_Reserved_BGameSound_39(int32 arg, ...)
567 {
568 	return B_ERROR;
569 }
570 
571 
572 status_t
573 BGameSound::_Reserved_BGameSound_40(int32 arg, ...)
574 {
575 	return B_ERROR;
576 }
577 
578 
579 status_t
580 BGameSound::_Reserved_BGameSound_41(int32 arg, ...)
581 {
582 	return B_ERROR;
583 }
584 
585 
586 status_t
587 BGameSound::_Reserved_BGameSound_42(int32 arg, ...)
588 {
589 	return B_ERROR;
590 }
591 
592 
593 status_t
594 BGameSound::_Reserved_BGameSound_43(int32 arg, ...)
595 {
596 	return B_ERROR;
597 }
598 
599 
600 status_t
601 BGameSound::_Reserved_BGameSound_44(int32 arg, ...)
602 {
603 	return B_ERROR;
604 }
605 
606 
607 status_t
608 BGameSound::_Reserved_BGameSound_45(int32 arg, ...)
609 {
610 	return B_ERROR;
611 }
612 
613 
614 status_t
615 BGameSound::_Reserved_BGameSound_46(int32 arg, ...)
616 {
617 	return B_ERROR;
618 }
619 
620 
621 status_t
622 BGameSound::_Reserved_BGameSound_47(int32 arg, ...)
623 {
624 	return B_ERROR;
625 }
626 
627 
628