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