updated by Christopher ML Zumwalt May

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1585 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
beveloper
2002-10-21 16:53:45 +00:00
parent eae06be465
commit 6eaf18596d
3 changed files with 74 additions and 103 deletions
+28 -18
View File
@@ -24,27 +24,20 @@
// Description: Interface to a single sound, managed by the GameSoundDevice. // Description: Interface to a single sound, managed by the GameSoundDevice.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Standard Includes -----------------------------------------------------------
#include <stdio.h> #include <stdio.h>
#include <malloc.h> #include <malloc.h>
#include <memory.h> #include <memory.h>
// System Includes -------------------------------------------------------------
#include <MediaAddOn.h>
#include <MediaRoster.h> #include <MediaRoster.h>
#include <MediaAddOn.h>
#include <MediaTheme.h> #include <MediaTheme.h>
#include <TimeSource.h> #include <TimeSource.h>
// Project Includes ------------------------------------------------------------ #include "GameProducer.h"
#include <GameSoundDevice.h> #include "GameSoundBuffer.h"
#include <GameProducer.h> #include "GameSoundDevice.h"
#include <GSUtility.h> #include "StreamingGameSound.h"
#include "GSUtility.h"
// Local Includes --------------------------------------------------------------
#include <GameSoundBuffer.h>
#include <StreamingGameSound.h>
// Local Defines ---------------------------------------------------------------
// Sound Buffer Utility functions ---------------------------------------- // Sound Buffer Utility functions ----------------------------------------
inline void ApplyMod(uint8 * data, uint8 * buffer, int64 index, float gain, float * pan) inline void ApplyMod(uint8 * data, uint8 * buffer, int64 index, float gain, float * pan)
@@ -94,13 +87,12 @@ GameSoundBuffer::GameSoundBuffer(const gs_audio_format * format)
memcpy(&fFormat, format, sizeof(gs_audio_format)); memcpy(&fFormat, format, sizeof(gs_audio_format));
} }
// Play must stop before the distructor is called; otherwise, a fatel
// error occures if the playback is in a subclass.
GameSoundBuffer::~GameSoundBuffer() GameSoundBuffer::~GameSoundBuffer()
{ {
BMediaRoster* r = BMediaRoster::Roster(); BMediaRoster* r = BMediaRoster::Roster();
if (fIsPlaying) r->StopNode(fConnection->producer, 0, true);
if (fIsConnected) if (fIsConnected)
{ {
// Ordinarily we'd stop *all* of the nodes in the chain at this point. However, // Ordinarily we'd stop *all* of the nodes in the chain at this point. However,
@@ -482,6 +474,8 @@ SimpleSoundBuffer::SimpleSoundBuffer(const gs_audio_format * format,
{ {
fBuffer = new char[frames * fFrameSize]; fBuffer = new char[frames * fFrameSize];
fBufferSize = frames * fFrameSize; fBufferSize = frames * fFrameSize;
memcpy(fBuffer, data, fBufferSize);
} }
@@ -507,12 +501,28 @@ SimpleSoundBuffer::FillBuffer(void * data, int64 frames)
if (fPosition + bytes >= fBufferSize) if (fPosition + bytes >= fBufferSize)
{ {
if (fPosition < fBufferSize)
{
// copy the remaining frames
size_t remainder = fBufferSize - fPosition; size_t remainder = fBufferSize - fPosition;
memcpy(buffer, &fBuffer[fPosition], remainder); memcpy(buffer, &fBuffer[fPosition], remainder);
if (fLooping) memcpy(&buffer[remainder], fBuffer, bytes - remainder); if (fLooping)
{
// restart the sound from the begging
memcpy(&buffer[remainder], fBuffer, bytes - remainder);
fPosition = bytes - remainder;
}
else fPosition = fBufferSize;
}
else memset(data, 0, bytes);
// there is nothing left to play
}
else
{
memcpy(buffer, &fBuffer[fPosition], bytes);
fPosition += bytes;
} }
else memcpy(buffer, &fBuffer[fPosition], bytes);
} }
+15 -13
View File
@@ -26,26 +26,18 @@
// this time. Use at your own risk. // this time. Use at your own risk.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Standard Includes -----------------------------------------------------------
#include <stdio.h> #include <stdio.h>
#include <malloc.h> #include <malloc.h>
// System Includes -------------------------------------------------------------
#include <List.h> #include <List.h>
#include <MediaAddOn.h>
#include <MediaRoster.h> #include <MediaRoster.h>
#include <MediaTheme.h> #include <MediaAddOn.h>
#include <TimeSource.h> #include <TimeSource.h>
#include <MediaTheme.h>
// Project Includes ------------------------------------------------------------ #include "GameSoundDevice.h"
#include <GameSoundBuffer.h> #include "GameSoundBuffer.h"
#include <GameProducer.h> #include "GameProducer.h"
#include <GSUtility.h>
// Local Includes --------------------------------------------------------------
#include <GameSoundDevice.h>
// Local Defines ---------------------------------------------------------------
// BGameSoundDevice definitions ------------------------------------ // BGameSoundDevice definitions ------------------------------------
const int32 kInitSoundCount = 32; const int32 kInitSoundCount = 32;
@@ -96,7 +88,10 @@ BGameSoundDevice::~BGameSoundDevice()
// We need to stop all the sounds before we stop the mixer // We need to stop all the sounds before we stop the mixer
for(int32 i = 0; i < fSoundCount; i++) for(int32 i = 0; i < fSoundCount; i++)
{
if (fSounds[i]) fSounds[i]->StopPlaying();
delete fSounds[i]; delete fSounds[i];
}
if (fIsConnected) if (fIsConnected)
{ {
@@ -193,8 +188,15 @@ BGameSoundDevice::CreateBuffer(gs_id * sound,
void void
BGameSoundDevice::ReleaseBuffer(gs_id sound) BGameSoundDevice::ReleaseBuffer(gs_id sound)
{ {
if (fSounds[sound-1])
{
// We must stop playback befor destroying the sound or else
// we may recieve fatel errors.
fSounds[sound-1]->StopPlaying();
delete fSounds[sound-1]; delete fSounds[sound-1];
fSounds[sound-1] = NULL; fSounds[sound-1] = NULL;
}
} }
+19 -60
View File
@@ -25,25 +25,20 @@
// short, and consists of non-changing samples in memory. // short, and consists of non-changing samples in memory.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Standard Includes -----------------------------------------------------------
#include <malloc.h> #include <malloc.h>
#include <memory.h> #include <memory.h>
// System Includes -------------------------------------------------------------
#include <Entry.h> #include <Entry.h>
#include <MediaFile.h> #include <MediaFile.h>
#include <MediaTrack.h> #include <MediaTrack.h>
// Project Includes ------------------------------------------------------------ #include "GameSoundDefs.h"
#include <GameSoundBuffer.h> #include "GameSoundBuffer.h"
#include <GameSoundDevice.h> #include "GameSoundDevice.h"
#include "GSUtility.h"
// Local Includes --------------------------------------------------------------
#include <SimpleGameSound.h> #include <SimpleGameSound.h>
// Local Defines ---------------------------------------------------------------
// BSimpleGameSound ------------------------------------------------------------
BSimpleGameSound::BSimpleGameSound(const entry_ref *inFile, BSimpleGameSound::BSimpleGameSound(const entry_ref *inFile,
BGameSoundDevice *device) BGameSoundDevice *device)
: BGameSound(device) : BGameSound(device)
@@ -140,13 +135,6 @@ BSimpleGameSound::IsLooping() const
return bool(attribute.value); return bool(attribute.value);
} }
status_t
BSimpleGameSound::Perform(int32 selector,
void *data)
{
return B_ERROR;
}
status_t status_t
BSimpleGameSound::Init(const entry_ref* inFile) BSimpleGameSound::Init(const entry_ref* inFile)
{ {
@@ -165,14 +153,12 @@ BSimpleGameSound::Init(const entry_ref* inFile)
memset(&mformat, 0, sizeof(media_format)); memset(&mformat, 0, sizeof(media_format));
mformat.type = B_MEDIA_RAW_AUDIO; mformat.type = B_MEDIA_RAW_AUDIO;
mformat.u.raw_audio.byte_order = (B_HOST_IS_BENDIAN) ? B_MEDIA_BIG_ENDIAN : B_MEDIA_LITTLE_ENDIAN; // mformat.u.raw_audio.byte_order = (B_HOST_IS_BENDIAN) ? B_MEDIA_BIG_ENDIAN : B_MEDIA_LITTLE_ENDIAN;
audioStream->DecodedFormat(&mformat); status_t error = audioStream->DecodedFormat(&mformat);
if (error != B_OK) return error;
memset(&gsformat, 0, sizeof(gs_audio_format)); memset(&gsformat, 0, sizeof(gs_audio_format));
gsformat.frame_rate = mformat.u.raw_audio.frame_rate; media_to_gs_format(&gsformat, &mformat.u.raw_audio);
gsformat.channel_count = mformat.u.raw_audio.channel_count;
gsformat.byte_order = mformat.u.raw_audio.byte_order;
gsformat.buffer_size = mformat.u.raw_audio.buffer_size;
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)
{ {
@@ -197,59 +183,32 @@ BSimpleGameSound::Init(const entry_ref* inFile)
gsformat.format = gs_audio_format::B_GS_U8; gsformat.format = gs_audio_format::B_GS_U8;
status_t error = Init(data, frames, &gsformat); error = Init(data, frames, &gsformat);
// free the buffers we no longer need // free the buffers we no longer need
delete [] buffer; delete [] buffer;
delete [] data; delete [] data;
file.ReleaseTrack(audioStream);
return error;
} }
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 sampleSize; size_t frameSize = get_sample_size(gsformat.format) * gsformat.channel_count;
switch(mformat.u.raw_audio.format) char * data = new char[frames * frameSize];
{ gsformat.buffer_size = frames * frameSize;
case media_raw_audio_format::B_AUDIO_UCHAR:
sampleSize = sizeof(uchar);
gsformat.format = gs_audio_format::B_GS_U8;
break;
case media_raw_audio_format::B_AUDIO_SHORT:
sampleSize = sizeof(int16);
gsformat.format = gs_audio_format::B_GS_S16;
break;
case media_raw_audio_format::B_AUDIO_INT:
sampleSize = sizeof(int32);
gsformat.format = gs_audio_format::B_GS_S32;
break;
case media_raw_audio_format::B_AUDIO_FLOAT:
sampleSize = sizeof(float);
gsformat.format = gs_audio_format::B_GS_F;
break;
default: return B_ERROR;
}
char * data = (char*)malloc(frames * gsformat.channel_count * sampleSize);
gsformat.buffer_size = frames * gsformat.channel_count * sampleSize;
while(framesTotal < frames) while(framesTotal < frames)
{ {
int64 position = framesTotal * gsformat.channel_count * sampleSize; char * position = &data[framesTotal * frameSize];
audioStream->ReadFrames(&data[position], &framesRead); audioStream->ReadFrames(position, &framesRead);
framesTotal += framesRead; framesTotal += framesRead;
} }
status_t error = Init(data, frames, &gsformat); error = Init(data, frames, &gsformat);
free(data); delete [] data;
}
file.ReleaseTrack(audioStream); file.ReleaseTrack(audioStream);
return error; return error;