tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / kits / game / PushGameSound.cpp
blob40e7132f3f41b311190415bf3b462e7c6aec004f
1 /*
2 * Copyright 2001-2012 Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Christopher ML Zumwalt May (zummy@users.sf.net)
7 * Jérôme Duval
8 */
11 #include <PushGameSound.h>
13 #include <List.h>
14 #include <string.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),
24 fLockPos(0),
25 fPlayPos(0)
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),
41 fLockPos(0),
42 fPlayPos(0),
43 fBuffer(NULL),
44 fPageSize(0),
45 fPageCount(0),
46 fBufferSize(0)
48 fPageLocked = new BList;
52 BPushGameSound::~BPushGameSound()
54 delete [] fBuffer;
55 delete fPageLocked;
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)
64 return lock_failed;
66 // the user can't lock a page being played
67 if (fLockPos < fPlayPos
68 && fLockPos + fPageSize > fPlayPos)
69 return lock_failed;
71 // lock the page
72 char * lockPage = &fBuffer[fLockPos];
73 fPageLocked->AddItem(lockPage);
75 // move the locker to the next page
76 fLockPos += fPageSize;
77 if (fLockPos >= fBufferSize)
78 fLockPos = 0;
80 *out_pagePtr = lockPage;
81 *out_pageSize = fPageSize;
83 return lock_ok;
87 status_t
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;
99 return lock_ok;
103 status_t
104 BPushGameSound::UnlockCyclic()
106 return B_OK;
110 size_t
111 BPushGameSound::CurrentPosition()
113 return fPlayPos;
117 BGameSound *
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());
128 status_t
129 BPushGameSound::Perform(int32 selector, void *data)
131 return BStreamingGameSound::Perform(selector, data);
135 status_t
136 BPushGameSound::SetParameters(size_t inBufferFrameCount,
137 const gs_audio_format *format, size_t inBufferCount)
139 return B_UNSUPPORTED;
143 status_t
144 BPushGameSound::SetStreamHook(void (*hook)(void * inCookie, void * inBuffer,
145 size_t inByteCount, BStreamingGameSound * me), void * cookie)
147 return B_UNSUPPORTED;
151 void
152 BPushGameSound::FillBuffer(void *inBuffer, size_t inByteCount)
154 size_t bytes = inByteCount;
156 if (!BytesReady(&bytes))
157 return;
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);
166 fPlayPos = 0;
168 // fill the remainder of the buffer by looping to the start
169 // of the buffer if it isn't locked
170 bytes -= remainder;
171 if (BytesReady(&bytes)) {
172 memcpy(&buffer[remainder], fBuffer, bytes);
173 fPlayPos += bytes;
175 } else {
176 memcpy(inBuffer, &fBuffer[fPlayPos], bytes);
177 fPlayPos += bytes;
180 BStreamingGameSound::FillBuffer(inBuffer, inByteCount);
184 bool
185 BPushGameSound::BytesReady(size_t * bytes)
187 if (fPageLocked->CountItems() <= 0)
188 return true;
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]))
196 return false;
198 while (ready < *bytes) {
199 ready += fPageSize;
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);
205 return true;
209 // all of the bytes are ready
210 return true;
214 /* unimplemented for protection of the user:
216 * BPushGameSound::BPushGameSound()
217 * BPushGameSound::BPushGameSound(const BPushGameSound &)
218 * BPushGameSound &BPushGameSound::operator=(const BPushGameSound &)
222 status_t
223 BPushGameSound::_Reserved_BPushGameSound_0(int32 arg, ...)
225 return B_ERROR;
229 status_t
230 BPushGameSound::_Reserved_BPushGameSound_1(int32 arg, ...)
232 return B_ERROR;
236 status_t
237 BPushGameSound::_Reserved_BPushGameSound_2(int32 arg, ...)
239 return B_ERROR;
243 status_t
244 BPushGameSound::_Reserved_BPushGameSound_3(int32 arg, ...)
246 return B_ERROR;
250 status_t
251 BPushGameSound::_Reserved_BPushGameSound_4(int32 arg, ...)
253 return B_ERROR;
257 status_t
258 BPushGameSound::_Reserved_BPushGameSound_5(int32 arg, ...)
260 return B_ERROR;
264 status_t
265 BPushGameSound::_Reserved_BPushGameSound_6(int32 arg, ...)
267 return B_ERROR;
271 status_t
272 BPushGameSound::_Reserved_BPushGameSound_7(int32 arg, ...)
274 return B_ERROR;
278 status_t
279 BPushGameSound::_Reserved_BPushGameSound_8(int32 arg, ...)
281 return B_ERROR;
285 status_t
286 BPushGameSound::_Reserved_BPushGameSound_9(int32 arg, ...)
288 return B_ERROR;
292 status_t
293 BPushGameSound::_Reserved_BPushGameSound_10(int32 arg, ...)
295 return B_ERROR;
299 status_t
300 BPushGameSound::_Reserved_BPushGameSound_11(int32 arg, ...)
302 return B_ERROR;
306 status_t
307 BPushGameSound::_Reserved_BPushGameSound_12(int32 arg, ...)
309 return B_ERROR;
313 status_t
314 BPushGameSound::_Reserved_BPushGameSound_13(int32 arg, ...)
316 return B_ERROR;
320 status_t
321 BPushGameSound::_Reserved_BPushGameSound_14(int32 arg, ...)
323 return B_ERROR;
327 status_t
328 BPushGameSound::_Reserved_BPushGameSound_15(int32 arg, ...)
330 return B_ERROR;
334 status_t
335 BPushGameSound::_Reserved_BPushGameSound_16(int32 arg, ...)
337 return B_ERROR;
341 status_t
342 BPushGameSound::_Reserved_BPushGameSound_17(int32 arg, ...)
344 return B_ERROR;
348 status_t
349 BPushGameSound::_Reserved_BPushGameSound_18(int32 arg, ...)
351 return B_ERROR;
355 status_t
356 BPushGameSound::_Reserved_BPushGameSound_19(int32 arg, ...)
358 return B_ERROR;
362 status_t
363 BPushGameSound::_Reserved_BPushGameSound_20(int32 arg, ...)
365 return B_ERROR;
369 status_t
370 BPushGameSound::_Reserved_BPushGameSound_21(int32 arg, ...)
372 return B_ERROR;
376 status_t
377 BPushGameSound::_Reserved_BPushGameSound_22(int32 arg, ...)
379 return B_ERROR;
383 status_t
384 BPushGameSound::_Reserved_BPushGameSound_23(int32 arg, ...)
386 return B_ERROR;