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