void GuiSound::Play()
{
	if(IsPlaying())
		return;

	if(voice < 0 || voice >= 16)
		return;

	SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice);
	if(!decoder)
		return;

	if(decoder->IsEOF())
	{
		ASND_StopVoice(voice);
		decoder->ClearBuffer();
		decoder->Rewind();
		decoder->Decode();
	}

	u8 * curbuffer = decoder->GetBuffer();
	int bufsize = decoder->GetBufferSize();
	decoder->LoadNext();
	SoundHandler::Instance()->ThreadSignal();

	ASND_SetVoice(voice, decoder->GetFormat(), decoder->GetSampleRate(), 0, curbuffer, bufsize, volume, volume, SoundCallback);
}
extern "C" void SoundCallback(s32 voice)
{
	SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice);
	if(!decoder)
		return;

	if(decoder->IsBufferReady())
	{
		if(ASND_AddVoice(voice, decoder->GetBuffer(), decoder->GetBufferSize()) == SND_OK)
		{
			decoder->LoadNext();
			SoundHandler::Instance()->ThreadSignal();
		}
	}
	else if(decoder->IsEOF())
	{
		ASND_StopVoice(voice);
		//if(voice == 0)
			//MusicPlayer::Instance()->SetPlaybackFinished(true); //see if next music must be played
	}
	else
	{
		SoundHandler::Instance()->ThreadSignal();
	}
}
Пример #3
0
void SoundHandler::axFrameCallback(void)
{
    for (u32 i = 0; i < MAX_DECODERS; i++)
    {
        Voice *voice = handlerInstance->getVoice(i);

        switch (voice->getState())
        {
            default:
            case Voice::STATE_STOPPED:
                break;

            case Voice::STATE_START: {
                SoundDecoder * decoder = handlerInstance->getDecoder(i);
                decoder->Lock();
                if(decoder->IsBufferReady())
                {
                    const u8 *buffer = decoder->GetBuffer();
                    const u32 bufferSize = decoder->GetBufferSize();
                    decoder->LoadNext();

                    const u8 *nextBuffer = NULL;
                    u32 nextBufferSize = 0;

                    if(decoder->IsBufferReady())
                    {
                        nextBuffer = decoder->GetBuffer();
                        nextBufferSize = decoder->GetBufferSize();
                        decoder->LoadNext();
                    }

                    voice->play(buffer, bufferSize, nextBuffer, nextBufferSize, decoder->GetFormat() & 0xff, decoder->GetSampleRate());

                    handlerInstance->ThreadSignal();

                    voice->setState(Voice::STATE_PLAYING);
                }
                decoder->Unlock();
                break;
            }
            case Voice::STATE_PLAYING:
                if(voice->getInternState() == 1)
                {
                    if(voice->isBufferSwitched())
                    {
                        SoundDecoder * decoder = handlerInstance->getDecoder(i);
                        decoder->Lock();
                        if(decoder->IsBufferReady())
                        {
                            voice->setNextBuffer(decoder->GetBuffer(), decoder->GetBufferSize());
                            decoder->LoadNext();
                            handlerInstance->ThreadSignal();
                        }
                        else if(decoder->IsEOF())
                        {
                            voice->setState(Voice::STATE_STOP);
                        }
                        decoder->Unlock();
                    }
                }
                else
                {
                    voice->setState(Voice::STATE_STOPPED);
                }
                break;
            case Voice::STATE_STOP:
                if(voice->getInternState() != 0)
                    voice->stop();
                voice->setState(Voice::STATE_STOPPED);
                break;
        }
    }
}