Files
itgmania212121/stepmania/src/arch/Sound/RageSoundDriver_ALSA9_Software.cpp
T

256 lines
6.3 KiB
C++
Raw Normal View History

2003-10-22 02:34:44 +00:00
#include "global.h"
#include "RageSoundDriver_ALSA9_Software.h"
#include "RageLog.h"
#include "RageSound.h"
#include "RageSoundManager.h"
#include "RageUtil.h"
2003-10-28 08:28:18 +00:00
#include "RageTimer.h"
#include "ALSA9Dynamic.h"
2003-12-02 20:56:04 +00:00
#include "PrefsManager.h"
2003-10-28 08:28:18 +00:00
2003-11-28 21:17:49 +00:00
#include "archutils/Unix/GetSysInfo.h"
2003-10-28 08:28:18 +00:00
#include <sys/time.h>
#include <sys/resource.h>
2003-10-22 02:34:44 +00:00
const int channels = 2;
int samplerate = 44100;
2003-10-22 02:34:44 +00:00
const int samples_per_frame = channels;
const int bytes_per_frame = sizeof(Sint16) * samples_per_frame;
2003-10-28 08:28:18 +00:00
/* Linux 2.6 has a fine-grained scheduler; use a small buffer size. Otherwise, use a larger one. */
static const unsigned max_writeahead_linux_26 = 512;
static const unsigned safe_writeahead = 1024*4;
2003-10-28 08:28:18 +00:00
static unsigned max_writeahead;
const int num_chunks = 8;
2003-10-22 02:34:44 +00:00
int RageSound_ALSA9_Software::MixerThread_start(void *p)
{
((RageSound_ALSA9_Software *) p)->MixerThread();
return 0;
}
void RageSound_ALSA9_Software::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-28 08:28:18 +00:00
setpriority( PRIO_PROCESS, getpid(), -15 );
// RageTimer UnderrunTest;
2003-10-22 02:34:44 +00:00
while(!shutdown)
{
2004-01-03 04:10:57 +00:00
while( !shutdown && GetData() )
2003-10-28 08:28:18 +00:00
;
2003-10-22 02:34:44 +00:00
const float delay_ms = 1000 * float(max_writeahead) / samplerate;
2003-10-28 08:28:18 +00:00
SDL_Delay( int(delay_ms) / 4 );
// if( UnderrunTest.PeekDeltaTime() > 10 )
// {
// UnderrunTest.GetDeltaTime();
// SDL_Delay( 250 );
// }
2003-10-22 02:34:44 +00:00
}
}
/* Returns the number of frames processed */
2003-10-28 08:28:18 +00:00
bool RageSound_ALSA9_Software::GetData()
2003-10-22 02:34:44 +00:00
{
2003-10-28 08:28:18 +00:00
const int chunksize = max_writeahead / num_chunks;
const int frames_to_fill = min( chunksize, pcm->GetNumFramesToFill( max_writeahead ) );
2003-10-22 02:34:44 +00:00
if( frames_to_fill <= 0 )
2003-10-28 08:28:18 +00:00
return false;
2003-10-22 02:34:44 +00:00
/* Sint16 represents a single sample
* each frame contains one sample per channel
*/
static Sint16 *buf = NULL;
if (!buf)
2004-01-12 03:22:43 +00:00
buf = new Sint16[max_writeahead*samples_per_frame];
2003-10-22 02:34:44 +00:00
static SoundMixBuffer mix;
mix.SetVolume( SOUNDMAN->GetMixVolume() );
2004-01-20 01:00:01 +00:00
const int64_t play_pos = pcm->GetPlayPos();
const int64_t cur_play_pos = pcm->GetPosition();
2004-01-12 03:22:43 +00:00
2003-10-22 02:34:44 +00:00
LockMutex L(SOUNDMAN->lock);
for(unsigned i = 0; i < sounds.size(); ++i)
{
if(sounds[i]->stopping)
continue;
2004-01-12 03:22:43 +00:00
int bytes_read = 0;
int bytes_left = frames_to_fill*bytes_per_frame;
/* Does the sound have a start time? */
if( !sounds[i]->start_time.IsZero() )
{
/* If the sound is supposed to start at a time past this buffer, insert silence. */
2004-01-20 01:00:01 +00:00
const int64_t iFramesUntilThisBuffer = play_pos - cur_play_pos;
2004-01-12 03:22:43 +00:00
const float fSecondsBeforeStart = -sounds[i]->start_time.Ago();
2004-01-20 01:00:01 +00:00
const int64_t iFramesBeforeStart = int64_t( fSecondsBeforeStart * samplerate );
const int64_t iSilentFramesInThisBuffer = iFramesBeforeStart-iFramesUntilThisBuffer;
const int iSilentBytesInThisBuffer = clamp( int(iSilentFramesInThisBuffer * bytes_per_frame), 0, bytes_left );
2004-01-12 03:22:43 +00:00
memset( buf+bytes_read, 0, iSilentBytesInThisBuffer );
bytes_read += iSilentBytesInThisBuffer;
bytes_left -= iSilentBytesInThisBuffer;
if( !iSilentBytesInThisBuffer )
sounds[i]->start_time.SetZero();
}
2003-10-22 02:34:44 +00:00
/* Call the callback.
* Get the units straight,
* <bytes> = GetPCM(<bytes*>, <bytes>, <frames>)
*/
2004-01-12 03:22:43 +00:00
int got = sounds[i]->snd->GetPCM( (char *) buf+bytes_read, bytes_left, play_pos+bytes_read/bytes_per_frame );
bytes_read += got;
bytes_left -= got;
2003-10-22 02:34:44 +00:00
2004-01-12 03:22:43 +00:00
mix.write( (Sint16 *) buf, bytes_read / sizeof(Sint16) );
if( bytes_left > 0 )
2003-10-22 02:34:44 +00:00
{
/* This sound is finishing. */
sounds[i]->stopping = true;
2004-01-12 03:22:43 +00:00
sounds[i]->flush_pos = pcm->GetPlayPos() + (bytes_read / bytes_per_frame);
2003-10-22 02:34:44 +00:00
}
}
L.Unlock();
2004-01-12 03:22:43 +00:00
memset( buf, 0, frames_to_fill * bytes_per_frame );
2003-10-22 02:34:44 +00:00
mix.read( buf );
pcm->Write( buf, frames_to_fill );
2003-10-28 08:28:18 +00:00
return true;
2003-10-22 02:34:44 +00:00
}
2004-01-15 03:03:57 +00:00
void RageSound_ALSA9_Software::StartMixing(RageSoundBase *snd)
2003-10-22 02:34:44 +00:00
{
sound *s = new sound;
s->snd = snd;
2004-01-12 03:22:43 +00:00
s->start_time = snd->GetStartTime();
2003-10-22 02:34:44 +00:00
LockMutex L(SOUNDMAN->lock);
sounds.push_back(s);
}
void RageSound_ALSA9_Software::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)
{
2004-01-04 03:24:04 +00:00
if(!snds[i]->stopping) continue;
2003-10-22 02:34:44 +00:00
2004-01-04 03:24:04 +00:00
if(GetPosition(snds[i]->snd) < snds[i]->flush_pos)
2003-10-22 02:34:44 +00:00
continue; /* stopping but still flushing */
/* This sound is done. */
snds[i]->snd->StopPlaying();
}
}
2004-01-15 03:03:57 +00:00
void RageSound_ALSA9_Software::StopMixing(RageSoundBase *snd)
2003-10-22 02:34:44 +00:00
{
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);
2004-01-04 07:14:13 +00:00
/* If nothing is playing, reset the sample count; this is just to
* prevent eventual overflow. */
if( sounds.empty() )
pcm->Reset();
2003-10-22 02:34:44 +00:00
}
2004-01-20 01:00:01 +00:00
int64_t RageSound_ALSA9_Software::GetPosition(const RageSoundBase *snd) const
2003-10-22 02:34:44 +00:00
{
return pcm->GetPosition();
}
RageSound_ALSA9_Software::RageSound_ALSA9_Software()
{
CString err = LoadALSA();
if( err != "" )
RageException::ThrowNonfatal("Driver unusable: %s", err.c_str());
2003-11-04 06:40:17 +00:00
try {
2003-10-22 02:34:44 +00:00
shutdown = false;
2003-10-28 08:28:18 +00:00
max_writeahead = safe_writeahead;
CString sys;
int vers;
GetKernel( sys, vers );
LOG->Trace( "OS: %s ver %06x", sys.c_str(), vers );
2004-01-02 07:01:27 +00:00
if( sys == "Linux" && vers >= 20600 )
2003-10-28 08:28:18 +00:00
max_writeahead = max_writeahead_linux_26;
2003-12-02 20:56:04 +00:00
if( PREFSMAN->m_iSoundWriteAhead )
max_writeahead = PREFSMAN->m_iSoundWriteAhead;
pcm = new Alsa9Buf( Alsa9Buf::HW_DONT_CARE, channels );
samplerate = pcm->FindSampleRate( samplerate );
pcm->SetSampleRate( samplerate );
2004-01-03 02:59:14 +00:00
LOG->Info( "ALSA: Software mixing at %ihz", samplerate );
2003-10-22 02:34:44 +00:00
MixingThread.SetName( "RageSound_ALSA9_Software" );
MixingThread.Create( MixerThread_start, this );
2003-11-04 06:40:17 +00:00
} catch(...) {
UnloadALSA();
throw;
}
2003-10-22 02:34:44 +00:00
}
RageSound_ALSA9_Software::~RageSound_ALSA9_Software()
{
/* Signal the mixing thread to quit. */
shutdown = true;
LOG->Trace("Shutting down mixer thread ...");
MixingThread.Wait();
LOG->Trace("Mixer thread shut down.");
delete pcm;
UnloadALSA();
2003-10-22 02:34:44 +00:00
}
float RageSound_ALSA9_Software::GetPlayLatency() const
{
return float(max_writeahead)/samplerate;
}
int RageSound_ALSA9_Software::GetSampleRate( int rate ) const
{
return samplerate;
}
2003-10-22 02:34:44 +00:00
/*
2004-01-12 03:22:43 +00:00
* Copyright (c) 2002-2004 by the person(s) listed below. All rights reserved.
2003-10-22 02:34:44 +00:00
*
2004-01-12 03:22:43 +00:00
* Glenn Maynard
2003-10-22 02:34:44 +00:00
*/