2010-11-13 23:34:53 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2010
|
|
|
|
* by Dimok
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any
|
|
|
|
* damages arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any
|
|
|
|
* purpose, including commercial applications, and to alter it and
|
|
|
|
* redistribute it freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you
|
|
|
|
* must not claim that you wrote the original software. If you use
|
|
|
|
* this software in a product, an acknowledgment in the product
|
|
|
|
* documentation would be appreciated but is not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and
|
|
|
|
* must not be misrepresented as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* for WiiXplorer 2010
|
|
|
|
***************************************************************************/
|
|
|
|
#include <unistd.h>
|
2011-06-14 19:53:19 +02:00
|
|
|
#include "GUI/gui.h"
|
2010-11-13 23:34:53 +01:00
|
|
|
#include "utils/uncompress.h"
|
|
|
|
#include "FileOperations/fileops.h"
|
|
|
|
#include "SoundHandler.hpp"
|
|
|
|
#include "WavDecoder.hpp"
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
#define MAX_SND_VOICES 16
|
2010-11-13 23:34:53 +01:00
|
|
|
|
|
|
|
static bool VoiceUsed[MAX_SND_VOICES] =
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
true, false, false, false, false, false,
|
|
|
|
false, false, false, false, false, false,
|
|
|
|
false, false, false, false
|
2010-11-13 23:34:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline int GetFirstUnusedVoice()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
for(int i = 1; i < MAX_SND_VOICES; i++)
|
|
|
|
{
|
|
|
|
if(VoiceUsed[i] == false)
|
|
|
|
return i;
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return -1;
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void SoundCallback(s32 voice)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
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();
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GuiSound::GuiSound(const char * filepath)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
sound = NULL;
|
2010-11-13 23:34:53 +01:00
|
|
|
length = 0;
|
2011-07-26 00:28:22 +02:00
|
|
|
voice = GetFirstUnusedVoice();
|
|
|
|
if(voice > 0)
|
|
|
|
VoiceUsed[voice] = true;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
volume = 255;
|
2010-11-13 23:34:53 +01:00
|
|
|
SoundEffectLength = 0;
|
2011-01-08 13:35:41 +01:00
|
|
|
loop = 0;
|
2010-11-13 23:34:53 +01:00
|
|
|
allocated = false;
|
|
|
|
Load(filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
GuiSound::GuiSound(const u8 * snd, s32 len, int vol, bool isallocated, int v)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
sound = NULL;
|
2010-11-13 23:34:53 +01:00
|
|
|
length = 0;
|
|
|
|
if(v < 0)
|
2011-07-26 00:28:22 +02:00
|
|
|
voice = GetFirstUnusedVoice();
|
|
|
|
else
|
|
|
|
voice = v;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if(voice > 0)
|
|
|
|
VoiceUsed[voice] = true;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
volume = vol;
|
2010-11-13 23:34:53 +01:00
|
|
|
SoundEffectLength = 0;
|
2011-01-08 13:35:41 +01:00
|
|
|
loop = 0;
|
2010-11-13 23:34:53 +01:00
|
|
|
allocated = false;
|
|
|
|
Load(snd, len, isallocated);
|
|
|
|
}
|
|
|
|
|
|
|
|
GuiSound::~GuiSound()
|
|
|
|
{
|
|
|
|
FreeMemory();
|
|
|
|
if(voice > 0)
|
2011-07-26 00:28:22 +02:00
|
|
|
VoiceUsed[voice] = false;
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::FreeMemory()
|
|
|
|
{
|
|
|
|
this->Stop();
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
SoundHandler::Instance()->RemoveDecoder(voice);
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if(allocated && sound != NULL)
|
|
|
|
{
|
|
|
|
free(sound);
|
|
|
|
sound = NULL;
|
|
|
|
allocated = false;
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
SoundEffectLength = 0;
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiSound::Load(const char * filepath)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
FreeMemory();
|
2010-11-13 23:34:53 +01:00
|
|
|
|
|
|
|
if(!filepath)
|
2011-07-26 00:28:22 +02:00
|
|
|
return false;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
u32 magic;
|
|
|
|
FILE * f = fopen(filepath, "rb");
|
|
|
|
if(!f)
|
|
|
|
return false;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
fread(&magic, 1, 4, f);
|
|
|
|
fclose(f);
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if(magic == 'IMD5')
|
|
|
|
{
|
|
|
|
u8 * snd = NULL;
|
2011-11-13 10:09:27 +01:00
|
|
|
u32 filesize = 0;
|
2011-07-26 00:28:22 +02:00
|
|
|
LoadFileToMem(filepath, &snd, &filesize);
|
|
|
|
return Load(snd, filesize, true);
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
SoundHandler::Instance()->AddDecoder(voice, filepath);
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice);
|
|
|
|
if(!decoder)
|
|
|
|
return false;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!decoder->IsBufferReady())
|
|
|
|
{
|
|
|
|
SoundHandler::Instance()->RemoveDecoder(voice);
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
SetLoop(loop);
|
2010-11-13 23:34:53 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiSound::Load(const u8 * snd, s32 len, bool isallocated)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
FreeMemory();
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!snd)
|
|
|
|
return false;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!isallocated && *((u32 *) snd) == 'RIFF')
|
|
|
|
{
|
|
|
|
return LoadSoundEffect(snd, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*((u32 *) snd) == 'IMD5')
|
|
|
|
{
|
|
|
|
UncompressSoundbin(snd, len, isallocated);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sound = (u8 *) snd;
|
|
|
|
length = len;
|
|
|
|
allocated = isallocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
SoundHandler::Instance()->AddDecoder(voice, sound, length);
|
|
|
|
|
|
|
|
SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice);
|
|
|
|
if(!decoder)
|
|
|
|
return false;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if(!decoder->IsBufferReady())
|
|
|
|
{
|
|
|
|
SoundHandler::Instance()->RemoveDecoder(voice);
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
SetLoop(loop);
|
2010-11-13 23:34:53 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiSound::LoadSoundEffect(const u8 * snd, s32 len)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
WavDecoder decoder(snd, len);
|
|
|
|
decoder.Rewind();
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
u32 done = 0;
|
|
|
|
sound = (u8 *) malloc(4096);
|
|
|
|
memset(sound, 0, 4096);
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
u8 * tmpsnd = (u8 *) realloc(sound, done+4096);
|
|
|
|
if(!tmpsnd)
|
|
|
|
{
|
|
|
|
free(sound);
|
|
|
|
sound = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
sound = tmpsnd;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
int read = decoder.Read(sound+done, 4096, done);
|
|
|
|
if(read <= 0)
|
|
|
|
break;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
done += read;
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
sound = (u8 *) realloc(sound, done);
|
|
|
|
SoundEffectLength = done;
|
|
|
|
allocated = true;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return true;
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::Play()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if(SoundEffectLength > 0)
|
|
|
|
{
|
|
|
|
ASND_StopVoice(voice);
|
|
|
|
ASND_SetVoice(voice, VOICE_STEREO_16BIT, 32000, 0, sound, SoundEffectLength, volume, volume, NULL);
|
|
|
|
return;
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if(IsPlaying())
|
|
|
|
return;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
|
|
|
if(voice < 0 || voice >= 16)
|
|
|
|
return;
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice);
|
|
|
|
if(!decoder)
|
|
|
|
return;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if(decoder->IsEOF())
|
|
|
|
{
|
|
|
|
ASND_StopVoice(voice);
|
|
|
|
decoder->ClearBuffer();
|
|
|
|
decoder->Rewind();
|
|
|
|
decoder->Decode();
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
u8 * curbuffer = decoder->GetBuffer();
|
|
|
|
int bufsize = decoder->GetBufferSize();
|
|
|
|
decoder->LoadNext();
|
|
|
|
SoundHandler::Instance()->ThreadSignal();
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
ASND_SetVoice(voice, decoder->GetFormat(), decoder->GetSampleRate(), 0, curbuffer, bufsize, volume, volume, SoundCallback);
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::Stop()
|
|
|
|
{
|
|
|
|
if(voice < 0 || voice >= 16)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ASND_StopVoice(voice);
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice);
|
|
|
|
if(!decoder)
|
|
|
|
return;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
decoder->ClearBuffer();
|
|
|
|
Rewind();
|
|
|
|
SoundHandler::Instance()->ThreadSignal();
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::Pause()
|
|
|
|
{
|
|
|
|
if(voice < 0 || voice >= 16)
|
|
|
|
return;
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
ASND_StopVoice(voice);
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::Resume()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
Play();
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiSound::IsPlaying()
|
|
|
|
{
|
|
|
|
if(voice < 0 || voice >= 16)
|
|
|
|
return false;
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
int result = ASND_StatusVoice(voice);
|
2010-11-13 23:34:53 +01:00
|
|
|
|
|
|
|
if(result == SND_WORKING || result == SND_WAITING)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::SetVolume(int vol)
|
|
|
|
{
|
|
|
|
if(voice < 0 || voice >= 16)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(vol < 0)
|
|
|
|
return;
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
volume = 255*(vol/100.0);
|
|
|
|
ASND_ChangeVolumeVoice(voice, volume, volume);
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::SetLoop(u8 l)
|
|
|
|
{
|
|
|
|
loop = l;
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice);
|
|
|
|
if(!decoder)
|
|
|
|
return;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
decoder->SetLoop(l == 1);
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::Rewind()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice);
|
|
|
|
if(!decoder)
|
|
|
|
return;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
decoder->Rewind();
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::UncompressSoundbin(const u8 * snd, int len, bool isallocated)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
const u8 * file = snd+32;
|
|
|
|
if(*((u32 *) file) == 'LZ77')
|
|
|
|
{
|
|
|
|
u32 size = 0;
|
|
|
|
sound = uncompressLZ77(file, len-32, &size);
|
|
|
|
length = size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
length = len-32;
|
|
|
|
sound = (u8 *) malloc(length);
|
|
|
|
memcpy(sound, file, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isallocated)
|
|
|
|
free((u8 *) snd);
|
|
|
|
|
|
|
|
allocated = true;
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|