2 * Copyright 2001-2012 Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Christopher ML Zumwalt May (zummy@users.sf.net)
11 #include <PushGameSound.h>
16 #include "GSUtility.h"
19 BPushGameSound::BPushGameSound(size_t inBufferFrameCount
,
20 const gs_audio_format
*format
, size_t inBufferCount
,
21 BGameSoundDevice
*device
)
23 BStreamingGameSound(inBufferFrameCount
, format
, inBufferCount
, device
),
27 fPageLocked
= new BList
;
29 size_t frameSize
= get_sample_size(format
->format
) * format
->channel_count
;
31 fPageCount
= inBufferCount
;
32 fPageSize
= frameSize
* inBufferFrameCount
;
33 fBufferSize
= fPageSize
* fPageCount
;
35 fBuffer
= new char[fBufferSize
];
39 BPushGameSound::BPushGameSound(BGameSoundDevice
* device
)
40 : BStreamingGameSound(device
),
48 fPageLocked
= new BList
;
52 BPushGameSound::~BPushGameSound()
59 BPushGameSound::lock_status
60 BPushGameSound::LockNextPage(void **out_pagePtr
, size_t *out_pageSize
)
62 // the user can not lock every page
63 if (fPageLocked
->CountItems() > fPageCount
- 1)
66 // the user can't lock a page being played
67 if (fLockPos
< fPlayPos
68 && fLockPos
+ fPageSize
> fPlayPos
)
72 char * lockPage
= &fBuffer
[fLockPos
];
73 fPageLocked
->AddItem(lockPage
);
75 // move the locker to the next page
76 fLockPos
+= fPageSize
;
77 if (fLockPos
>= fBufferSize
)
80 *out_pagePtr
= lockPage
;
81 *out_pageSize
= fPageSize
;
88 BPushGameSound::UnlockPage(void *in_pagePtr
)
90 return (fPageLocked
->RemoveItem(in_pagePtr
)) ? B_OK
: B_ERROR
;
94 BPushGameSound::lock_status
95 BPushGameSound::LockForCyclic(void **out_basePtr
, size_t *out_size
)
97 *out_basePtr
= fBuffer
;
98 *out_size
= fBufferSize
;
104 BPushGameSound::UnlockCyclic()
111 BPushGameSound::CurrentPosition()
118 BPushGameSound::Clone() const
120 gs_audio_format format
= Format();
121 size_t frameSize
= get_sample_size(format
.format
) * format
.channel_count
;
122 size_t bufferFrameCount
= fPageSize
/ frameSize
;
124 return new BPushGameSound(bufferFrameCount
, &format
, fPageCount
, Device());
129 BPushGameSound::Perform(int32 selector
, void *data
)
131 return BStreamingGameSound::Perform(selector
, data
);
136 BPushGameSound::SetParameters(size_t inBufferFrameCount
,
137 const gs_audio_format
*format
, size_t inBufferCount
)
139 return B_UNSUPPORTED
;
144 BPushGameSound::SetStreamHook(void (*hook
)(void * inCookie
, void * inBuffer
,
145 size_t inByteCount
, BStreamingGameSound
* me
), void * cookie
)
147 return B_UNSUPPORTED
;
152 BPushGameSound::FillBuffer(void *inBuffer
, size_t inByteCount
)
154 size_t bytes
= inByteCount
;
156 if (!BytesReady(&bytes
))
159 if (fPlayPos
+ bytes
> fBufferSize
) {
160 size_t remainder
= fBufferSize
- fPlayPos
;
161 // Space left in buffer
162 char * buffer
= (char*)inBuffer
;
164 // fill the buffer with the samples left at the end of our buffer
165 memcpy(buffer
, &fBuffer
[fPlayPos
], remainder
);
168 // fill the remainder of the buffer by looping to the start
169 // of the buffer if it isn't locked
171 if (BytesReady(&bytes
)) {
172 memcpy(&buffer
[remainder
], fBuffer
, bytes
);
176 memcpy(inBuffer
, &fBuffer
[fPlayPos
], bytes
);
180 BStreamingGameSound::FillBuffer(inBuffer
, inByteCount
);
185 BPushGameSound::BytesReady(size_t * bytes
)
187 if (fPageLocked
->CountItems() <= 0)
190 size_t start
= fPlayPos
;
191 size_t ready
= fPlayPos
;
192 int32 page
= int32(start
/ fPageSize
);
194 // return if there is nothing to do
195 if (fPageLocked
->HasItem(&fBuffer
[page
* fPageSize
]))
198 while (ready
< *bytes
) {
200 page
= int32(ready
/ fPageSize
);
202 if (fPageLocked
->HasItem(&fBuffer
[page
* fPageSize
])) {
203 // we have found a locked page
204 *bytes
= ready
- start
- (ready
- page
* fPageSize
);
209 // all of the bytes are ready
214 /* unimplemented for protection of the user:
216 * BPushGameSound::BPushGameSound()
217 * BPushGameSound::BPushGameSound(const BPushGameSound &)
218 * BPushGameSound &BPushGameSound::operator=(const BPushGameSound &)
223 BPushGameSound::_Reserved_BPushGameSound_0(int32 arg
, ...)
230 BPushGameSound::_Reserved_BPushGameSound_1(int32 arg
, ...)
237 BPushGameSound::_Reserved_BPushGameSound_2(int32 arg
, ...)
244 BPushGameSound::_Reserved_BPushGameSound_3(int32 arg
, ...)
251 BPushGameSound::_Reserved_BPushGameSound_4(int32 arg
, ...)
258 BPushGameSound::_Reserved_BPushGameSound_5(int32 arg
, ...)
265 BPushGameSound::_Reserved_BPushGameSound_6(int32 arg
, ...)
272 BPushGameSound::_Reserved_BPushGameSound_7(int32 arg
, ...)
279 BPushGameSound::_Reserved_BPushGameSound_8(int32 arg
, ...)
286 BPushGameSound::_Reserved_BPushGameSound_9(int32 arg
, ...)
293 BPushGameSound::_Reserved_BPushGameSound_10(int32 arg
, ...)
300 BPushGameSound::_Reserved_BPushGameSound_11(int32 arg
, ...)
307 BPushGameSound::_Reserved_BPushGameSound_12(int32 arg
, ...)
314 BPushGameSound::_Reserved_BPushGameSound_13(int32 arg
, ...)
321 BPushGameSound::_Reserved_BPushGameSound_14(int32 arg
, ...)
328 BPushGameSound::_Reserved_BPushGameSound_15(int32 arg
, ...)
335 BPushGameSound::_Reserved_BPushGameSound_16(int32 arg
, ...)
342 BPushGameSound::_Reserved_BPushGameSound_17(int32 arg
, ...)
349 BPushGameSound::_Reserved_BPushGameSound_18(int32 arg
, ...)
356 BPushGameSound::_Reserved_BPushGameSound_19(int32 arg
, ...)
363 BPushGameSound::_Reserved_BPushGameSound_20(int32 arg
, ...)
370 BPushGameSound::_Reserved_BPushGameSound_21(int32 arg
, ...)
377 BPushGameSound::_Reserved_BPushGameSound_22(int32 arg
, ...)
384 BPushGameSound::_Reserved_BPushGameSound_23(int32 arg
, ...)