From 14e3d1b5768e7110b3d5c0855833267409b71dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Wed, 23 May 2007 19:40:57 +0000 Subject: [PATCH] don't try to stop a non inited BFileGameSound now stops BFileGameSound at the end of the track if not looping GameSoundDevice now checks the sound_id is valid added a header include in GSUtility.h git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21221 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/game/FileGameSound.cpp | 26 +++++++++++++++++++++-- src/kits/game/GSUtility.h | 1 + src/kits/game/GameSoundDevice.cpp | 35 ++++++++++++++++++------------- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/kits/game/FileGameSound.cpp b/src/kits/game/FileGameSound.cpp index 657b48a709..3f8fe0b6ab 100644 --- a/src/kits/game/FileGameSound.cpp +++ b/src/kits/game/FileGameSound.cpp @@ -198,6 +198,9 @@ BFileGameSound::StopPlaying() { status_t error = BStreamingGameSound::StopPlaying(); + if (!fAudioStream || !fAudioStream->stream) + return B_OK; + // start reading next time from the start of the file int64 frame = 0; fAudioStream->stream->SeekToFrame(&frame); @@ -230,7 +233,13 @@ BFileGameSound::FillBuffer(void *inBuffer, if (fPlayPosition == 0 || fPlayPosition + inByteCount >= fBufferSize) { Load(); } + + if (fPlayPosition + bytes > fBufferSize) + bytes = fBufferSize - fPlayPosition; + if (bytes == 0) + return; + if (fPausing) { Lock(); @@ -349,7 +358,9 @@ BFileGameSound::Init(const entry_ref* file) // is this is an audio file? media_format playFormat; - fAudioStream->stream->EncodedFormat(&playFormat); + if ((error = fAudioStream->stream->EncodedFormat(&playFormat)) != B_OK) + return error; + if (!playFormat.IsAudio()) return B_MEDIA_BAD_FORMAT; @@ -394,7 +405,8 @@ BFileGameSound::Load() { if (fPlayPosition != 0) { - memcpy(fBuffer, fBuffer + fPlayPosition, fBufferSize - fPlayPosition); + if (fBufferSize > fPlayPosition) + memcpy(fBuffer, fBuffer + fPlayPosition, fBufferSize - fPlayPosition); fPlayPosition = fBufferSize - fPlayPosition; } @@ -403,6 +415,16 @@ BFileGameSound::Load() fAudioStream->stream->ReadFrames(fBuffer + fPlayPosition, &frames); fBufferSize = fPlayPosition + frames * fFrameSize; fPlayPosition = 0; + + if (fBufferSize == 0) { + if (fLooping) { + // start reading next time from the start of the file + int64 frame = 0; + fAudioStream->stream->SeekToFrame(&frame); + } else { + StopPlaying(); + } + } return true; } diff --git a/src/kits/game/GSUtility.h b/src/kits/game/GSUtility.h index 63afdd1e6a..3dfcc4cc8f 100644 --- a/src/kits/game/GSUtility.h +++ b/src/kits/game/GSUtility.h @@ -33,6 +33,7 @@ // Project Includes ------------------------------------------------------------ #include +#include // Local Includes -------------------------------------------------------------- diff --git a/src/kits/game/GameSoundDevice.cpp b/src/kits/game/GameSoundDevice.cpp index b178db6110..ac57b56565 100644 --- a/src/kits/game/GameSoundDevice.cpp +++ b/src/kits/game/GameSoundDevice.cpp @@ -190,6 +190,9 @@ BGameSoundDevice::CreateBuffer(gs_id * sound, void BGameSoundDevice::ReleaseBuffer(gs_id sound) { + if (sound <= 0) + return; + if (fSounds[sound-1]) { // We must stop playback befor destroying the sound or else @@ -207,12 +210,12 @@ BGameSoundDevice::Buffer(gs_id sound, gs_audio_format * format, void * data) { - if (!format) return B_BAD_VALUE; + if (!format || sound <= 0) + return B_BAD_VALUE; memcpy(format, &fSounds[sound-1]->Format(), sizeof(gs_audio_format)); - if (fSounds[sound-1]->Data()) - { + if (fSounds[sound-1]->Data()) { data = malloc(format->buffer_size); memcpy(data, fSounds[sound-1]->Data(), format->buffer_size); } @@ -224,38 +227,40 @@ BGameSoundDevice::Buffer(gs_id sound, status_t BGameSoundDevice::StartPlaying(gs_id sound) { - status_t error = EALREADY; - - if (!fSounds[sound-1]->IsPlaying()) - { + if (sound <= 0) + return B_BAD_VALUE; + + if (!fSounds[sound-1]->IsPlaying()) { // tell the producer to start playing the sound - error = fSounds[sound-1]->StartPlaying(); + return fSounds[sound-1]->StartPlaying(); } - else fSounds[sound-1]->Reset(); - return error; + fSounds[sound-1]->Reset(); + return EALREADY; } status_t BGameSoundDevice::StopPlaying(gs_id sound) { - status_t error = EALREADY; + if (sound <= 0) + return B_BAD_VALUE; - if (fSounds[sound-1]->IsPlaying()) - { + if (fSounds[sound-1]->IsPlaying()) { // Tell the producer to stop play this sound fSounds[sound-1]->Reset(); - error = fSounds[sound-1]->StopPlaying(); + return fSounds[sound-1]->StopPlaying(); } - return error; + return EALREADY; } bool BGameSoundDevice::IsPlaying(gs_id sound) { + if (sound <= 0) + return false; return fSounds[sound-1]->IsPlaying(); }