2003-02-23 08:39:29 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
#include "RageSoundDriver_ALSA9.h"
|
|
|
|
|
|
|
|
|
|
#include "RageTimer.h"
|
|
|
|
|
#include "RageLog.h"
|
|
|
|
|
#include "RageSound.h"
|
2003-07-27 03:35:59 +00:00
|
|
|
#include "RageSoundManager.h"
|
2003-02-23 08:39:29 +00:00
|
|
|
#include "RageUtil.h"
|
|
|
|
|
|
|
|
|
|
const int channels = 2;
|
|
|
|
|
const int samplerate = 44100;
|
2003-03-01 08:09:45 +00:00
|
|
|
|
|
|
|
|
const int samples_per_frame = channels;
|
2003-10-21 09:50:03 +00:00
|
|
|
const int bytes_per_frame = sizeof(Sint16) * samples_per_frame;
|
2003-03-01 08:09:45 +00:00
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
const unsigned max_writeahead = 1024*8;
|
2003-03-01 08:09:45 +00:00
|
|
|
|
2003-02-23 08:39:29 +00:00
|
|
|
int RageSound_ALSA9::MixerThread_start(void *p)
|
|
|
|
|
{
|
|
|
|
|
((RageSound_ALSA9 *) p)->MixerThread();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageSound_ALSA9::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);
|
|
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
while(!shutdown)
|
|
|
|
|
{
|
2003-10-21 22:51:25 +00:00
|
|
|
GetData();
|
2003-10-21 09:50:03 +00:00
|
|
|
const float delay_ms = 1000 * float(max_writeahead) / samplerate;
|
|
|
|
|
SDL_Delay( int(delay_ms) / 2);
|
2003-02-23 08:39:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-27 06:43:50 +00:00
|
|
|
/* Returns the number of frames processed */
|
2003-10-21 22:51:25 +00:00
|
|
|
void RageSound_ALSA9::GetData()
|
2003-02-23 08:39:29 +00:00
|
|
|
{
|
2003-10-21 22:51:25 +00:00
|
|
|
const int frames_to_fill = pcm->GetNumFramesToFill();
|
2003-10-21 09:50:03 +00:00
|
|
|
if( frames_to_fill <= 0 )
|
2003-10-21 22:51:25 +00:00
|
|
|
return;
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-01 08:09:45 +00:00
|
|
|
/* Sint16 represents a single sample
|
|
|
|
|
* each frame contains one sample per channel
|
|
|
|
|
*/
|
2003-10-21 09:50:03 +00:00
|
|
|
static Sint16 *buf = NULL;
|
2003-02-23 08:39:29 +00:00
|
|
|
if (!buf)
|
2003-10-21 09:50:03 +00:00
|
|
|
buf = new Sint16[max_writeahead*samples_per_frame*4];
|
|
|
|
|
|
2003-10-21 19:22:06 +00:00
|
|
|
static SoundMixBuffer mix;
|
2003-10-21 09:50:03 +00:00
|
|
|
mix.SetVolume( SOUNDMAN->GetMixVolume() );
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-10-21 22:51:25 +00:00
|
|
|
LockMutex L(SOUNDMAN->lock);
|
2003-03-17 20:12:00 +00:00
|
|
|
for(unsigned i = 0; i < sounds.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
if(sounds[i]->stopping)
|
|
|
|
|
continue;
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
/* Call the callback.
|
2003-03-01 08:09:45 +00:00
|
|
|
* Get the units straight,
|
|
|
|
|
* <bytes> = GetPCM(<bytes*>, <bytes>, <frames>)
|
|
|
|
|
*/
|
2003-10-21 22:51:25 +00:00
|
|
|
unsigned got = sounds[i]->snd->GetPCM( (char *) buf, frames_to_fill*bytes_per_frame, pcm->GetPlayPos() ) / sizeof(Sint16);
|
2003-10-21 09:50:03 +00:00
|
|
|
mix.write((Sint16 *) buf, got);
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
if( int(got) < frames_to_fill )
|
2003-03-17 20:12:00 +00:00
|
|
|
{
|
|
|
|
|
/* This sound is finishing. */
|
|
|
|
|
sounds[i]->stopping = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-10-21 22:51:25 +00:00
|
|
|
L.Unlock();
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-10-21 09:50:03 +00:00
|
|
|
memset( buf, 0, sizeof(Sint16) * frames_to_fill * samples_per_frame );
|
2003-10-21 22:51:25 +00:00
|
|
|
mix.read( buf );
|
2003-02-23 08:39:29 +00:00
|
|
|
|
2003-10-21 22:51:25 +00:00
|
|
|
pcm->Write( buf, frames_to_fill );
|
2003-02-23 08:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RageSound_ALSA9::StartMixing(RageSound *snd)
|
|
|
|
|
{
|
|
|
|
|
sound *s = new sound;
|
|
|
|
|
s->snd = snd;
|
|
|
|
|
|
2003-04-12 18:10:50 +00:00
|
|
|
LockMutex L(SOUNDMAN->lock);
|
2003-02-23 08:39:29 +00:00
|
|
|
sounds.push_back(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageSound_ALSA9::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;
|
|
|
|
|
|
2003-03-17 20:12:00 +00:00
|
|
|
if(GetPosition(snds[i]->snd) < sounds[i]->flush_pos)
|
|
|
|
|
continue; /* stopping but still flushing */
|
2003-02-23 08:39:29 +00:00
|
|
|
|
|
|
|
|
/* This sound is done. */
|
|
|
|
|
snds[i]->snd->StopPlaying();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageSound_ALSA9::StopMixing(RageSound *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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int RageSound_ALSA9::GetPosition(const RageSound *snd) const
|
|
|
|
|
{
|
2003-10-21 22:51:25 +00:00
|
|
|
return pcm->GetPosition();
|
2003-02-23 08:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RageSound_ALSA9::RageSound_ALSA9()
|
|
|
|
|
{
|
|
|
|
|
shutdown = false;
|
|
|
|
|
|
2003-10-21 22:51:25 +00:00
|
|
|
pcm = new Alsa9Buf( Alsa9Buf::HW_DONT_CARE, channels, samplerate, max_writeahead );
|
|
|
|
|
|
2003-07-27 03:35:59 +00:00
|
|
|
MixingThread.SetName( "RageSound_ALSA9" );
|
|
|
|
|
MixingThread.Create( MixerThread_start, this );
|
2003-02-23 08:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageSound_ALSA9::~RageSound_ALSA9()
|
|
|
|
|
{
|
|
|
|
|
/* Signal the mixing thread to quit. */
|
|
|
|
|
shutdown = true;
|
|
|
|
|
LOG->Trace("Shutting down mixer thread ...");
|
2003-07-27 03:35:59 +00:00
|
|
|
MixingThread.Wait();
|
2003-02-23 08:39:29 +00:00
|
|
|
LOG->Trace("Mixer thread shut down.");
|
|
|
|
|
|
2003-10-21 22:51:25 +00:00
|
|
|
delete pcm;
|
2003-02-23 08:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float RageSound_ALSA9::GetPlayLatency() const
|
|
|
|
|
{
|
2003-10-21 09:50:03 +00:00
|
|
|
return float(max_writeahead)/samplerate;
|
2003-02-23 08:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2002 by the person(s) listed below. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Glenn Maynard (RageSoundDriver_WaveOut)
|
|
|
|
|
*/
|