|
|
|
@ -33,13 +33,8 @@ CMusicChannelAL::CMusicChannelAL(CSoundDriverAL *soundDriver)
|
|
|
|
|
{
|
|
|
|
|
// create a default source for music streaming
|
|
|
|
|
_Source = static_cast<CSourceAL*>(_SoundDriver->createSource());
|
|
|
|
|
_Source->setPos(CVector(0, 0, 0));
|
|
|
|
|
_Source->setVelocity(CVector(0, 0, 0));
|
|
|
|
|
_Source->setDirection(CVector(0, 0, 0));
|
|
|
|
|
_Source->setSourceRelativeMode(true);
|
|
|
|
|
_Source->setStreamingBuffersMax(4);
|
|
|
|
|
_Source->setType(SourceMusic);
|
|
|
|
|
_Source->setStreamingBufferSize(32768);
|
|
|
|
|
// _Source->setStreaming(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CMusicChannelAL::~CMusicChannelAL()
|
|
|
|
@ -110,72 +105,47 @@ void CMusicChannelAL::setBufferFormat(IBuffer *buffer)
|
|
|
|
|
|
|
|
|
|
void CMusicChannelAL::run()
|
|
|
|
|
{
|
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
|
|
if (_Async)
|
|
|
|
|
// use queued buffers
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
bool first = true;
|
|
|
|
|
// buffers to update
|
|
|
|
|
std::vector<CBufferAL*> buffers;
|
|
|
|
|
|
|
|
|
|
// use queued buffers
|
|
|
|
|
do
|
|
|
|
|
if (first)
|
|
|
|
|
{
|
|
|
|
|
// buffers to update
|
|
|
|
|
std::vector<CBufferAL*> buffers;
|
|
|
|
|
// get all buffers to queue
|
|
|
|
|
_Source->getStreamingBuffers(buffers);
|
|
|
|
|
|
|
|
|
|
if (first)
|
|
|
|
|
{
|
|
|
|
|
// get all buffers to queue
|
|
|
|
|
_Source->getStreamingBuffers(buffers);
|
|
|
|
|
|
|
|
|
|
// set format for each buffer
|
|
|
|
|
for(uint i = 0; i < buffers.size(); ++i)
|
|
|
|
|
setBufferFormat(buffers[i]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// get unqueued buffers
|
|
|
|
|
_Source->getProcessedStreamingBuffers(buffers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fill buffers
|
|
|
|
|
// set format for each buffer
|
|
|
|
|
for(uint i = 0; i < buffers.size(); ++i)
|
|
|
|
|
fillBuffer(buffers[i], _Source->getStreamingBufferSize());
|
|
|
|
|
|
|
|
|
|
// play the source
|
|
|
|
|
if (first)
|
|
|
|
|
{
|
|
|
|
|
_Source->play();
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// wait 100ms before rechecking buffers
|
|
|
|
|
nlSleep(100);
|
|
|
|
|
setBufferFormat(buffers[i]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// get unqueued buffers
|
|
|
|
|
_Source->getProcessedStreamingBuffers(buffers);
|
|
|
|
|
}
|
|
|
|
|
while(!_MusicBuffer->isMusicEnded() && _Playing);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// use an unique buffer managed by CMusicChannelAL
|
|
|
|
|
_Buffer = _SoundDriver->createBuffer();
|
|
|
|
|
|
|
|
|
|
// set format
|
|
|
|
|
setBufferFormat(_Buffer);
|
|
|
|
|
// fill buffers
|
|
|
|
|
for(uint i = 0; i < buffers.size(); ++i)
|
|
|
|
|
fillBuffer(buffers[i], _Source->getStreamingBufferSize());
|
|
|
|
|
|
|
|
|
|
// fill data
|
|
|
|
|
fillBuffer(_Buffer, _MusicBuffer->getUncompressedSize());
|
|
|
|
|
// _Source->updateManualRolloff();
|
|
|
|
|
|
|
|
|
|
// we don't need _MusicBuffer anymore because all is loaded into memory
|
|
|
|
|
if (_MusicBuffer)
|
|
|
|
|
// play the source
|
|
|
|
|
if (first)
|
|
|
|
|
{
|
|
|
|
|
delete _MusicBuffer;
|
|
|
|
|
_MusicBuffer = NULL;
|
|
|
|
|
_Source->play();
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// use this buffer as source
|
|
|
|
|
_Source->setStaticBuffer(_Buffer);
|
|
|
|
|
|
|
|
|
|
// play the source
|
|
|
|
|
_Source->play();
|
|
|
|
|
// wait 100ms before rechecking buffers
|
|
|
|
|
nlSleep(100);
|
|
|
|
|
}
|
|
|
|
|
while(!_MusicBuffer->isMusicEnded() && _Playing);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// music finished without interruption
|
|
|
|
|
if (_Playing)
|
|
|
|
@ -189,6 +159,35 @@ void CMusicChannelAL::run()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Play sync music
|
|
|
|
|
bool CMusicChannelAL::playSync()
|
|
|
|
|
{
|
|
|
|
|
// use an unique buffer managed by CMusicChannelAL
|
|
|
|
|
_Buffer = _SoundDriver->createBuffer();
|
|
|
|
|
|
|
|
|
|
// set format
|
|
|
|
|
setBufferFormat(_Buffer);
|
|
|
|
|
|
|
|
|
|
// fill data
|
|
|
|
|
fillBuffer(_Buffer, _MusicBuffer->getUncompressedSize());
|
|
|
|
|
|
|
|
|
|
// we don't need _MusicBuffer anymore because all is loaded into memory
|
|
|
|
|
if (_MusicBuffer)
|
|
|
|
|
{
|
|
|
|
|
delete _MusicBuffer;
|
|
|
|
|
_MusicBuffer = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// delete previous queued buffers
|
|
|
|
|
_Source->setStreamingBuffersMax(0);
|
|
|
|
|
|
|
|
|
|
// use this buffer as source
|
|
|
|
|
_Source->setStaticBuffer(_Buffer);
|
|
|
|
|
|
|
|
|
|
// play the source
|
|
|
|
|
return _Source->play();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Play some music (.ogg etc...)
|
|
|
|
|
* NB: if an old music was played, it is first stop with stopMusic()
|
|
|
|
|
* \param filepath file path, CPath::lookup is done here
|
|
|
|
@ -205,23 +204,38 @@ bool CMusicChannelAL::play(const std::string &filepath, bool async, bool loop)
|
|
|
|
|
|
|
|
|
|
if (_MusicBuffer)
|
|
|
|
|
{
|
|
|
|
|
// create the thread if it's not yet created
|
|
|
|
|
if (!_Thread) _Thread = IThread::create(this);
|
|
|
|
|
_Async = async;
|
|
|
|
|
_Playing = true;
|
|
|
|
|
|
|
|
|
|
_Source->setSourceRelativeMode(true);
|
|
|
|
|
|
|
|
|
|
if (!_Thread)
|
|
|
|
|
if (_Async)
|
|
|
|
|
{
|
|
|
|
|
nlwarning("AL: Can't create a new thread");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// create the thread if it's not yet created
|
|
|
|
|
if (!_Thread) _Thread = IThread::create(this);
|
|
|
|
|
|
|
|
|
|
_Async = async;
|
|
|
|
|
_Playing = true;
|
|
|
|
|
if (!_Thread)
|
|
|
|
|
{
|
|
|
|
|
nlwarning("AL: Can't create a new thread");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// use 4 queued buffers
|
|
|
|
|
_Source->setStreamingBuffersMax(4);
|
|
|
|
|
|
|
|
|
|
// we need to loop the source only if not async
|
|
|
|
|
_Source->setLooping(async ? false:loop);
|
|
|
|
|
// we need to loop the source only if not async
|
|
|
|
|
_Source->setLooping(false);
|
|
|
|
|
|
|
|
|
|
// start the thread
|
|
|
|
|
_Thread->start();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// we need to loop the source only if not async
|
|
|
|
|
_Source->setLooping(loop);
|
|
|
|
|
|
|
|
|
|
// start the thread
|
|
|
|
|
_Thread->start();
|
|
|
|
|
return playSync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
@ -298,6 +312,16 @@ void CMusicChannelAL::setVolume(float gain)
|
|
|
|
|
_Source->setGain(gain);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Update music
|
|
|
|
|
void CMusicChannelAL::update()
|
|
|
|
|
{
|
|
|
|
|
// stop sync music once finished playing
|
|
|
|
|
if (_Playing && !_Async && !_Source->isPlaying())
|
|
|
|
|
{
|
|
|
|
|
stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} /* namespace NLSOUND */
|
|
|
|
|
|
|
|
|
|
/* end of file */
|
|
|
|
|