improve codestyle

fix bad init in BPushGameSound


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20670 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval
2007-04-12 19:20:45 +00:00
parent 72b4b94081
commit c7509fce9d
2 changed files with 78 additions and 89 deletions
+64 -70
View File
@@ -32,20 +32,18 @@
#include "GSUtility.h" #include "GSUtility.h"
BPushGameSound::BPushGameSound(size_t inBufferFrameCount, BPushGameSound::BPushGameSound(size_t inBufferFrameCount, const gs_audio_format *format,
const gs_audio_format *format, size_t inBufferCount, BGameSoundDevice *device)
size_t inBufferCount,
BGameSoundDevice *device)
: BStreamingGameSound(inBufferFrameCount, format, inBufferCount, device) : BStreamingGameSound(inBufferFrameCount, format, inBufferCount, device)
{ {
if (InitCheck() == B_OK) if (InitCheck() != B_OK)
{ return;
status_t error = SetParameters(inBufferFrameCount, format, inBufferCount);
if (error != B_OK) status_t error = SetParameters(inBufferFrameCount, format, inBufferCount);
fPageLocked = new BList; if (error != B_OK)
else fPageLocked = new BList;
SetInitError(error); else
} SetInitError(error);
} }
@@ -69,14 +67,16 @@ BPushGameSound::~BPushGameSound()
BPushGameSound::lock_status BPushGameSound::lock_status
BPushGameSound::LockNextPage(void **out_pagePtr, BPushGameSound::LockNextPage(void **out_pagePtr, size_t *out_pageSize)
size_t *out_pageSize)
{ {
// the user can not lock every page // the user can not lock every page
if (fPageLocked->CountItems() > fPageCount - 3) return lock_failed; if (fPageLocked->CountItems() > fPageCount - 3)
return lock_failed;
// the user cann't lock a page being played // the user cann't lock a page being played
if (fLockPos < fPlayPos && fLockPos + fPageSize > fPlayPos) return lock_failed; if (fLockPos < fPlayPos
&& fLockPos + fPageSize > fPlayPos)
return lock_failed;
// lock the page // lock the page
char * lockPage = &fBuffer[fLockPos]; char * lockPage = &fBuffer[fLockPos];
@@ -84,7 +84,8 @@ BPushGameSound::LockNextPage(void **out_pagePtr,
// move the locker to the next page // move the locker to the next page
fLockPos += fPageSize; fLockPos += fPageSize;
if (fLockPos > fBufferSize) fLockPos = 0; if (fLockPos > fBufferSize)
fLockPos = 0;
*out_pagePtr = lockPage; *out_pagePtr = lockPage;
*out_pageSize = fPageSize; *out_pageSize = fPageSize;
@@ -101,8 +102,7 @@ BPushGameSound::UnlockPage(void *in_pagePtr)
BPushGameSound::lock_status BPushGameSound::lock_status
BPushGameSound::LockForCyclic(void **out_basePtr, BPushGameSound::LockForCyclic(void **out_basePtr, size_t *out_size)
size_t *out_size)
{ {
*out_basePtr = fBuffer; *out_basePtr = fBuffer;
*out_size = fBufferSize; *out_size = fBufferSize;
@@ -136,8 +136,7 @@ BPushGameSound::Clone() const
status_t status_t
BPushGameSound::Perform(int32 selector, BPushGameSound::Perform(int32 selector, void *data)
void *data)
{ {
return B_ERROR; return B_ERROR;
} }
@@ -149,7 +148,8 @@ BPushGameSound::SetParameters(size_t inBufferFrameCount,
size_t inBufferCount) size_t inBufferCount)
{ {
status_t error = BStreamingGameSound::SetParameters(inBufferFrameCount, format, inBufferCount); status_t error = BStreamingGameSound::SetParameters(inBufferFrameCount, format, inBufferCount);
if (error != B_OK) return error; if (error != B_OK)
return error;
size_t frameSize = get_sample_size(format->format) * format->channel_count; size_t frameSize = get_sample_size(format->format) * format->channel_count;
@@ -172,67 +172,61 @@ BPushGameSound::SetStreamHook(void (*hook)(void * inCookie, void * inBuffer, siz
void void
BPushGameSound::FillBuffer(void *inBuffer, BPushGameSound::FillBuffer(void *inBuffer, size_t inByteCount)
size_t inByteCount)
{ {
size_t bytes = inByteCount; size_t bytes = inByteCount;
if (BytesReady(&bytes)) if (!BytesReady(&bytes))
{ return;
if (fPlayPos + bytes > fBufferSize)
{ if (fPlayPos + bytes > fBufferSize) {
size_t remainder = fPlayPos + bytes - fBufferSize; size_t remainder = fPlayPos + bytes - fBufferSize;
char * buffer = (char*)inBuffer; char * buffer = (char*)inBuffer;
// fill the buffer with the samples left at the end of our buffer
memcpy(buffer, &fBuffer[fPlayPos], remainder);
fPlayPos = 0;
// fill the remainder of the buffer by looping to the start
// of the buffer if it isn't locked
bytes -= remainder;
if (BytesReady(&bytes))
{
memcpy(&buffer[remainder], fBuffer, bytes);
fPlayPos += bytes;
}
}
else
{
memcpy(inBuffer, &fBuffer[fPlayPos], bytes);
fPlayPos += bytes;
}
BStreamingGameSound::FillBuffer(inBuffer, inByteCount); // fill the buffer with the samples left at the end of our buffer
} memcpy(buffer, &fBuffer[fPlayPos], remainder);
fPlayPos = 0;
// fill the remainder of the buffer by looping to the start
// of the buffer if it isn't locked
bytes -= remainder;
if (BytesReady(&bytes)) {
memcpy(&buffer[remainder], fBuffer, bytes);
fPlayPos += bytes;
}
} else {
memcpy(inBuffer, &fBuffer[fPlayPos], bytes);
fPlayPos += bytes;
}
BStreamingGameSound::FillBuffer(inBuffer, inByteCount);
} }
bool bool
BPushGameSound::BytesReady(size_t * bytes) BPushGameSound::BytesReady(size_t * bytes)
{ {
if (fPageLocked->CountItems() > 0) if (fPageLocked->CountItems() <= 0)
{ return true;
size_t start = fPlayPos;
size_t ready = fPlayPos;
int32 page = int32(start / fPageSize);
// return if there is nothing to do size_t start = fPlayPos;
if (fPageLocked->HasItem(&fBuffer[page * fPageSize])) return false; size_t ready = fPlayPos;
int32 page = int32(start / fPageSize);
while (ready < *bytes) // return if there is nothing to do
{ if (fPageLocked->HasItem(&fBuffer[page * fPageSize]))
ready += fPageSize; return false;
page = int32(ready / fPageSize);
while (ready < *bytes) {
ready += fPageSize;
page = int32(ready / fPageSize);
if (fPageLocked->HasItem(&fBuffer[page * fPageSize])) if (fPageLocked->HasItem(&fBuffer[page * fPageSize])) {
{ // we have found a locked page
// we have found a locked page *bytes = ready - start - (ready - page * fPageSize);
*bytes = ready - start - (ready - page * fPageSize); return true;
return true; }
} }
}
}
// all of the bytes are ready // all of the bytes are ready
return true; return true;
+14 -19
View File
@@ -39,8 +39,7 @@
#include <SimpleGameSound.h> #include <SimpleGameSound.h>
BSimpleGameSound::BSimpleGameSound(const entry_ref *inFile, BSimpleGameSound::BSimpleGameSound(const entry_ref *inFile, BGameSoundDevice *device)
BGameSoundDevice *device)
: BGameSound(device) : BGameSound(device)
{ {
if (InitCheck() == B_OK) if (InitCheck() == B_OK)
@@ -48,8 +47,7 @@ BSimpleGameSound::BSimpleGameSound(const entry_ref *inFile,
} }
BSimpleGameSound::BSimpleGameSound(const char *inFile, BSimpleGameSound::BSimpleGameSound(const char *inFile, BGameSoundDevice *device)
BGameSoundDevice *device)
: BGameSound(device) : BGameSound(device)
{ {
if (InitCheck() == B_OK) if (InitCheck() == B_OK)
@@ -71,7 +69,7 @@ BSimpleGameSound::BSimpleGameSound(const void *inData,
: BGameSound(device) : BGameSound(device)
{ {
if (InitCheck() == B_OK) if (InitCheck() == B_OK)
SetInitError(Init(inData, inFrameCount, format)); SetInitError(Init(inData, inFrameCount, format));
} }
@@ -155,11 +153,13 @@ BSimpleGameSound::Init(const entry_ref* inFile)
media_format mformat; media_format mformat;
int64 framesRead, framesTotal = 0; int64 framesRead, framesTotal = 0;
if (file.InitCheck() != B_OK) return file.InitCheck(); if (file.InitCheck() != B_OK)
return file.InitCheck();
BMediaTrack* audioStream = file.TrackAt(0); BMediaTrack* audioStream = file.TrackAt(0);
audioStream->EncodedFormat(&mformat); audioStream->EncodedFormat(&mformat);
if (!mformat.IsAudio()) return B_ERROR; if (!mformat.IsAudio())
return B_ERROR;
int64 frames = audioStream->CountFrames(); int64 frames = audioStream->CountFrames();
@@ -172,15 +172,13 @@ BSimpleGameSound::Init(const entry_ref* inFile)
memset(&gsformat, 0, sizeof(gs_audio_format)); memset(&gsformat, 0, sizeof(gs_audio_format));
media_to_gs_format(&gsformat, &mformat.u.raw_audio); media_to_gs_format(&gsformat, &mformat.u.raw_audio);
if (mformat.u.raw_audio.format == media_raw_audio_format::B_AUDIO_CHAR) if (mformat.u.raw_audio.format == media_raw_audio_format::B_AUDIO_CHAR) {
{
// The GameKit doesnt support this format so we will have to reformat // The GameKit doesnt support this format so we will have to reformat
// the data into something the GameKit does support. // the data into something the GameKit does support.
char * buffer = new char[gsformat.buffer_size]; char * buffer = new char[gsformat.buffer_size];
uchar * data = new uchar[frames * gsformat.channel_count]; uchar * data = new uchar[frames * gsformat.channel_count];
while (framesTotal < frames) while (framesTotal < frames) {
{
// read the next chunck from the stream // read the next chunck from the stream
memset(buffer, 0, gsformat.buffer_size); memset(buffer, 0, gsformat.buffer_size);
audioStream->ReadFrames(buffer, &framesRead); audioStream->ReadFrames(buffer, &framesRead);
@@ -200,17 +198,14 @@ BSimpleGameSound::Init(const entry_ref* inFile)
// free the buffers we no longer need // free the buffers we no longer need
delete [] buffer; delete [] buffer;
delete [] data; delete [] data;
} } else {
else
{
// We need to detriman the size, in bytes, of a single sample. // We need to detriman the size, in bytes, of a single sample.
// At the same time, we will store the format of the audio buffer // At the same time, we will store the format of the audio buffer
size_t frameSize = get_sample_size(gsformat.format) * gsformat.channel_count; size_t frameSize = get_sample_size(gsformat.format) * gsformat.channel_count;
char * data = new char[frames * frameSize]; char * data = new char[frames * frameSize];
gsformat.buffer_size = frames * frameSize; gsformat.buffer_size = frames * frameSize;
while(framesTotal < frames) while(framesTotal < frames) {
{
char * position = &data[framesTotal * frameSize]; char * position = &data[framesTotal * frameSize];
audioStream->ReadFrames(position, &framesRead); audioStream->ReadFrames(position, &framesRead);
@@ -228,14 +223,14 @@ BSimpleGameSound::Init(const entry_ref* inFile)
status_t status_t
BSimpleGameSound::Init(const void* inData, BSimpleGameSound::Init(const void* inData, int64 inFrameCount,
int64 inFrameCount,
const gs_audio_format* format) const gs_audio_format* format)
{ {
gs_id sound; gs_id sound;
status_t error = Device()->CreateBuffer(&sound, format, inData, inFrameCount); status_t error = Device()->CreateBuffer(&sound, format, inData, inFrameCount);
if (error != B_OK) return error; if (error != B_OK)
return error;
BGameSound::Init(sound); BGameSound::Init(sound);