Make this compile in Windows.
Don't use nanosleep; it's nonportable. Use SDL_Delay() instead.
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
#include "RageSound.h"
|
#include "RageSound.h"
|
||||||
#include "RageUtil.h"
|
#include "RageUtil.h"
|
||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL_utils.h"
|
||||||
|
|
||||||
const int channels = 2;
|
const int channels = 2;
|
||||||
const int samplesize = channels*2; /* 16-bit */
|
const int samplesize = channels*2; /* 16-bit */
|
||||||
@@ -14,10 +14,6 @@ const int samplerate = 44100;
|
|||||||
const int buffersize_frames = 1024*8; /* in frames */
|
const int buffersize_frames = 1024*8; /* in frames */
|
||||||
const int buffersize = buffersize_frames * samplesize; /* in bytes */
|
const int buffersize = buffersize_frames * samplesize; /* in bytes */
|
||||||
|
|
||||||
const int num_chunks = 8;
|
|
||||||
const int chunksize_frames = buffersize_frames / num_chunks;
|
|
||||||
const int chunksize = buffersize / num_chunks;
|
|
||||||
|
|
||||||
|
|
||||||
int RageSound_Null::MixerThread_start(void *p)
|
int RageSound_Null::MixerThread_start(void *p)
|
||||||
{
|
{
|
||||||
@@ -31,15 +27,24 @@ void RageSound_Null::MixerThread()
|
|||||||
* assigns it; we might get here before that happens, though. */
|
* assigns it; we might get here before that happens, though. */
|
||||||
while(!SOUNDMAN && !shutdown) SDL_Delay(10);
|
while(!SOUNDMAN && !shutdown) SDL_Delay(10);
|
||||||
|
|
||||||
/* not sure if a nansleep is needed, but certainly helps
|
RageTimer Speaker;
|
||||||
* when there is LOG->Trace in GetData() */
|
|
||||||
struct timespec delay, remaining;
|
|
||||||
delay.tv_sec = 0;
|
|
||||||
delay.tv_nsec = 1000;
|
|
||||||
|
|
||||||
while(!shutdown) {
|
while(!shutdown) {
|
||||||
|
float delay_ms = float(buffersize_frames) / samplerate;
|
||||||
|
SDL_Delay(int(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();
|
GetData();
|
||||||
nanosleep(&delay, &remaining);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +54,7 @@ bool RageSound_Null::GetData()
|
|||||||
|
|
||||||
/* Create a 32-bit buffer to mix sounds. */
|
/* Create a 32-bit buffer to mix sounds. */
|
||||||
static Sint16 *buf = NULL;
|
static Sint16 *buf = NULL;
|
||||||
int bufsize = chunksize_frames * channels;
|
int bufsize = buffersize_frames * channels;
|
||||||
if(!buf)
|
if(!buf)
|
||||||
buf = new Sint16[bufsize];
|
buf = new Sint16[bufsize];
|
||||||
|
|
||||||
@@ -58,25 +63,24 @@ bool RageSound_Null::GetData()
|
|||||||
if(sounds[i]->stopping)
|
if(sounds[i]->stopping)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* Call the callback. */
|
unsigned samples_needed = buffersize_frames - sounds[i]->samples_buffered;
|
||||||
unsigned got = sounds[i]->snd->GetPCM((char *) buf, chunksize, last_cursor_pos);
|
unsigned bytes_needed = samples_needed*samplesize;
|
||||||
|
int Play_Time = int(RageTimer::GetTimeSinceStart() * samplerate);
|
||||||
|
Play_Time += sounds[i]->samples_buffered;
|
||||||
|
|
||||||
if(got < chunksize)
|
/* 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. */
|
/* This sound is finishing. */
|
||||||
sounds[i]->stopping = true;
|
sounds[i]->stopping = true;
|
||||||
sounds[i]->flush_pos = last_cursor_pos + (got / samplesize);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// and, do nothing! it's silence
|
// and, do nothing! it's silence
|
||||||
|
|
||||||
//XXX to make timing more accurate, nanosleep here
|
|
||||||
// as if playing the chunk
|
|
||||||
|
|
||||||
/* Increment last_cursor_pos. */
|
|
||||||
last_cursor_pos += chunksize_frames;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,7 +105,7 @@ void RageSound_Null::Update(float delta)
|
|||||||
{
|
{
|
||||||
if(!sounds[i]->stopping) continue;
|
if(!sounds[i]->stopping) continue;
|
||||||
|
|
||||||
if(GetPosition(snds[i]->snd) < sounds[i]->flush_pos)
|
if(sounds[i]->samples_buffered)
|
||||||
continue; /* stopping but still flushing */
|
continue; /* stopping but still flushing */
|
||||||
|
|
||||||
/* This sound is done. */
|
/* This sound is done. */
|
||||||
@@ -129,16 +133,12 @@ void RageSound_Null::StopMixing(RageSound *snd)
|
|||||||
|
|
||||||
int RageSound_Null::GetPosition(const RageSound *snd) const
|
int RageSound_Null::GetPosition(const RageSound *snd) const
|
||||||
{
|
{
|
||||||
LockMutex L(SOUNDMAN->lock);
|
return int(RageTimer::GetTimeSinceStart() * samplerate);
|
||||||
|
|
||||||
return time(NULL)-startup_time;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RageSound_Null::RageSound_Null()
|
RageSound_Null::RageSound_Null()
|
||||||
{
|
{
|
||||||
shutdown = false;
|
shutdown = false;
|
||||||
last_cursor_pos = 0;
|
|
||||||
startup_time = time(NULL);
|
|
||||||
|
|
||||||
MixerThreadPtr = SDL_CreateThread(MixerThread_start, this);
|
MixerThreadPtr = SDL_CreateThread(MixerThread_start, this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,17 +21,14 @@ public:
|
|||||||
struct sound {
|
struct sound {
|
||||||
RageSound *snd;
|
RageSound *snd;
|
||||||
bool stopping;
|
bool stopping;
|
||||||
int flush_pos; /* state == STOPPING only */
|
int samples_buffered; /* fake */
|
||||||
sound() { snd = NULL; stopping=false; }
|
sound() { snd = NULL; stopping=false; samples_buffered = 0; }
|
||||||
};
|
};
|
||||||
|
|
||||||
/* List of currently playing sounds: */
|
/* List of currently playing sounds: */
|
||||||
vector<sound *> sounds;
|
vector<sound *> sounds;
|
||||||
|
|
||||||
|
|
||||||
bool shutdown;
|
bool shutdown;
|
||||||
int last_cursor_pos;
|
|
||||||
time_t startup_time;
|
|
||||||
|
|
||||||
static int MixerThread_start(void *p);
|
static int MixerThread_start(void *p);
|
||||||
void MixerThread();
|
void MixerThread();
|
||||||
@@ -45,7 +42,7 @@ public:
|
|||||||
int GetPosition(const RageSound *snd) const;
|
int GetPosition(const RageSound *snd) const;
|
||||||
float GetPlayLatency() const;
|
float GetPlayLatency() const;
|
||||||
|
|
||||||
void RageSound_Null::Update(float delta);
|
void Update(float delta);
|
||||||
|
|
||||||
RageSound_Null();
|
RageSound_Null();
|
||||||
~RageSound_Null();
|
~RageSound_Null();
|
||||||
|
|||||||
Reference in New Issue
Block a user