simplify
This commit is contained in:
@@ -1,156 +1,30 @@
|
|||||||
#include "global.h"
|
#include "global.h"
|
||||||
#include "RageSoundDriver_Null.h"
|
#include "RageSoundDriver_Null.h"
|
||||||
|
|
||||||
#include "RageTimer.h"
|
|
||||||
#include "RageLog.h"
|
#include "RageLog.h"
|
||||||
#include "RageSound.h"
|
|
||||||
#include "RageUtil.h"
|
#include "RageUtil.h"
|
||||||
#include "RageSoundManager.h"
|
|
||||||
|
|
||||||
#include "SDL_utils.h"
|
|
||||||
|
|
||||||
const int channels = 2;
|
const int channels = 2;
|
||||||
const int samplesize = channels*2; /* 16-bit */
|
|
||||||
const int samplerate = 44100;
|
const int samplerate = 44100;
|
||||||
const int buffersize_frames = 1024*8; /* in frames */
|
|
||||||
const int buffersize = buffersize_frames * samplesize; /* in bytes */
|
|
||||||
|
|
||||||
|
void RageSound_Null::Update( float fDeltaTime )
|
||||||
int RageSound_Null::MixerThread_start(void *p)
|
|
||||||
{
|
{
|
||||||
((RageSound_Null *) p)->MixerThread();
|
/* "Play" frames. */
|
||||||
return 0;
|
while( last_cursor_pos < GetPosition(NULL)+1024 )
|
||||||
}
|
|
||||||
|
|
||||||
void RageSound_Null::MixerThread()
|
|
||||||
{
|
|
||||||
/* SOUNDMAN will be set once RageSoundManager's ctor returns and
|
|
||||||
* assigns it; we might get here before that happens, though. */
|
|
||||||
while(!SOUNDMAN && !shutdown) SDL_Delay(10);
|
|
||||||
|
|
||||||
RageTimer Speaker;
|
|
||||||
|
|
||||||
while(!shutdown) {
|
|
||||||
const int delay_ms = 1000 * buffersize_frames / samplerate;
|
|
||||||
SDL_Delay(delay_ms / 2);
|
|
||||||
|
|
||||||
LockMutex L(SOUNDMAN->lock);
|
|
||||||
|
|
||||||
/* "Play" frames. */
|
|
||||||
const float ms_passed = Speaker.GetDeltaTime();
|
|
||||||
const int samples_played = int(samplerate * ms_passed);
|
|
||||||
for(unsigned i = 0; i < sounds.size(); ++i)
|
|
||||||
{
|
|
||||||
sounds[i]->samples_buffered -= samples_played;
|
|
||||||
sounds[i]->samples_buffered = max(sounds[i]->samples_buffered, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
GetData();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool RageSound_Null::GetData()
|
|
||||||
{
|
|
||||||
LockMutex L(SOUNDMAN->lock);
|
|
||||||
|
|
||||||
/* Create a 32-bit buffer to mix sounds. */
|
|
||||||
static Sint16 *buf = NULL;
|
|
||||||
int bufsize = buffersize_frames * channels;
|
|
||||||
if(!buf)
|
|
||||||
buf = new Sint16[bufsize];
|
|
||||||
|
|
||||||
for(unsigned i = 0; i < sounds.size(); ++i)
|
|
||||||
{
|
{
|
||||||
if(sounds[i]->stopping)
|
int16_t buf[512*channels];
|
||||||
continue;
|
this->Mix( buf, 512, last_cursor_pos, GetPosition(NULL) );
|
||||||
|
last_cursor_pos += 512;
|
||||||
unsigned samples_needed = buffersize_frames - sounds[i]->samples_buffered;
|
|
||||||
unsigned bytes_needed = samples_needed*samplesize;
|
|
||||||
int Play_Time = int(RageTimer::GetTimeSinceStart() * samplerate);
|
|
||||||
Play_Time += sounds[i]->samples_buffered;
|
|
||||||
|
|
||||||
/* Call the callback. */
|
|
||||||
unsigned got = sounds[i]->snd->GetPCM((char *) buf, bytes_needed, Play_Time);
|
|
||||||
sounds[i]->samples_buffered += got/samplesize;
|
|
||||||
|
|
||||||
if(got < bytes_needed)
|
|
||||||
{
|
|
||||||
/* This sound is finishing. */
|
|
||||||
sounds[i]->stopping = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// and, do nothing! it's silence
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void RageSound_Null::StartMixing( RageSoundBase *snd )
|
|
||||||
{
|
|
||||||
sound *s = new sound;
|
|
||||||
s->snd = snd;
|
|
||||||
|
|
||||||
LockMutex L(SOUNDMAN->lock);
|
|
||||||
sounds.push_back(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
void RageSound_Null::Update(float delta)
|
|
||||||
{
|
|
||||||
LockMutex L(SOUNDMAN->lock);
|
|
||||||
|
|
||||||
/* SoundStopped might erase sounds out from under us, so make a copy
|
|
||||||
* of the sound list. */
|
|
||||||
vector<sound *> snds = sounds;
|
|
||||||
for(unsigned i = 0; i < snds.size(); ++i)
|
|
||||||
{
|
|
||||||
if(!sounds[i]->stopping) continue;
|
|
||||||
|
|
||||||
if(sounds[i]->samples_buffered)
|
|
||||||
continue; /* stopping but still flushing */
|
|
||||||
|
|
||||||
/* This sound is done. */
|
|
||||||
snds[i]->snd->StopPlaying();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RageSound_Null::StopMixing( RageSoundBase *snd )
|
|
||||||
{
|
|
||||||
LockMutex L(SOUNDMAN->lock);
|
|
||||||
|
|
||||||
/* Find the sound. */
|
|
||||||
unsigned i;
|
|
||||||
for(i = 0; i < sounds.size(); ++i)
|
|
||||||
if(sounds[i]->snd == snd) break;
|
|
||||||
if(i == sounds.size())
|
|
||||||
{
|
|
||||||
LOG->Trace("not stopping a sound because it's not playing");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
delete sounds[i];
|
|
||||||
sounds.erase(sounds.begin()+i, sounds.begin()+i+1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t RageSound_Null::GetPosition( const RageSoundBase *snd ) const
|
int64_t RageSound_Null::GetPosition( const RageSoundBase *snd ) const
|
||||||
{
|
{
|
||||||
return int64_t(RageTimer::GetTimeSinceStart() * samplerate);
|
return int64_t( RageTimer::GetTimeSinceStart() * samplerate );
|
||||||
}
|
}
|
||||||
|
|
||||||
RageSound_Null::RageSound_Null()
|
RageSound_Null::RageSound_Null()
|
||||||
{
|
{
|
||||||
shutdown = false;
|
last_cursor_pos = GetPosition( NULL );
|
||||||
|
|
||||||
MixingThread.SetName( "RageSound_Null" );
|
|
||||||
MixingThread.Create( MixerThread_start, this );
|
|
||||||
}
|
|
||||||
|
|
||||||
RageSound_Null::~RageSound_Null()
|
|
||||||
{
|
|
||||||
/* Signal the mixing thread to quit. */
|
|
||||||
shutdown = true;
|
|
||||||
LOG->Trace("Shutting down mixer thread ...");
|
|
||||||
MixingThread.Wait();
|
|
||||||
LOG->Trace("Mixer thread shut down.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float RageSound_Null::GetPlayLatency() const
|
float RageSound_Null::GetPlayLatency() const
|
||||||
@@ -159,10 +33,8 @@ float RageSound_Null::GetPlayLatency() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2002 by the person(s) listed below. All rights reserved.
|
* Copyright (c) 2002-2004 by the person(s) listed below. All rights reserved.
|
||||||
*
|
*
|
||||||
* Glenn Maynard (RageSoundDriver_WaveOut)
|
* Glenn Maynard
|
||||||
*
|
* Aaron VonderHaar
|
||||||
* 2003-02 RageSoundDriver_Null created from RageSoundDriver_WaveOut
|
|
||||||
* Aaron VonderHaar
|
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,52 +1,20 @@
|
|||||||
#ifndef RAGE_SOUND_NULL
|
#ifndef RAGE_SOUND_NULL
|
||||||
#define RAGE_SOUND_NULL
|
#define RAGE_SOUND_NULL
|
||||||
|
|
||||||
/* RageSound_Null fakes playing sounds having GetPosition()
|
#include "RageSoundDriver_Generic_Software.h"
|
||||||
* return seconds since the constructor was called.
|
|
||||||
*
|
|
||||||
* Only tested in linux, but intended to work across the globe.
|
|
||||||
* ( uses time_t, sleep() and nanosleep() from <time.h> )
|
|
||||||
*
|
|
||||||
* The timing probably isn't accurate, but at least it is fairly
|
|
||||||
* steady. Someone with more knowledge of RageSound, feel free
|
|
||||||
* to play around with this (not that it's any sort of priority).
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "RageSound.h"
|
class RageSound_Null: public RageSound_Generic_Software
|
||||||
#include "RageSoundDriver.h"
|
|
||||||
#include "RageThreads.h"
|
|
||||||
|
|
||||||
class RageSound_Null: public RageSoundDriver
|
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
struct sound {
|
int64_t last_cursor_pos;
|
||||||
RageSoundBase *snd;
|
|
||||||
bool stopping;
|
|
||||||
int samples_buffered; /* fake */
|
|
||||||
sound() { snd = NULL; stopping=false; samples_buffered = 0; }
|
|
||||||
};
|
|
||||||
|
|
||||||
/* List of currently playing sounds: */
|
|
||||||
vector<sound *> sounds;
|
|
||||||
|
|
||||||
bool shutdown;
|
|
||||||
RageThread MixingThread;
|
|
||||||
|
|
||||||
static int MixerThread_start(void *p);
|
|
||||||
void MixerThread();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/* virtuals: */
|
int64_t GetPosition( const RageSoundBase *snd ) const;
|
||||||
virtual void StartMixing( RageSoundBase *snd );
|
float GetPlayLatency() const;
|
||||||
virtual void StopMixing( RageSoundBase *snd );
|
void Update( float fDeltaTime );
|
||||||
virtual int64_t GetPosition( const RageSoundBase *snd ) const;
|
|
||||||
virtual float GetPlayLatency() const;
|
|
||||||
virtual void Update(float delta);
|
|
||||||
virtual bool GetData();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RageSound_Null();
|
RageSound_Null();
|
||||||
~RageSound_Null();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user