2012-01-21 21:57:41 +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>
|
|
|
|
#include <string.h>
|
2012-11-03 20:16:03 +01:00
|
|
|
#include <malloc.h>
|
2012-01-21 21:57:41 +01:00
|
|
|
#include "gui_sound.h"
|
2012-08-05 15:48:15 +02:00
|
|
|
#include "SoundHandler.hpp"
|
2012-09-22 15:47:52 +02:00
|
|
|
#include "MusicPlayer.hpp"
|
2012-01-21 21:57:41 +01:00
|
|
|
#include "WavDecoder.hpp"
|
2013-09-02 00:31:46 +02:00
|
|
|
#include "fileOps/fileOps.h"
|
2012-01-21 21:57:41 +01:00
|
|
|
#include "loader/sys.h"
|
2012-07-27 19:26:49 +02:00
|
|
|
#include "banner/AnimatedBanner.h"
|
2012-08-05 15:48:15 +02:00
|
|
|
#include "memory/mem2.hpp"
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-18 16:09:28 +02:00
|
|
|
#define MAX_SND_VOICES 16
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
static bool VoiceUsed[MAX_SND_VOICES] =
|
|
|
|
{
|
2012-07-27 19:26:49 +02:00
|
|
|
false, false, false, false, false, false,
|
2012-04-03 16:04:47 +02:00
|
|
|
false, false, false, false, false, false,
|
|
|
|
false, false, false, false
|
2012-01-21 21:57:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline int GetFirstUnusedVoice()
|
|
|
|
{
|
2018-11-19 19:14:40 +01:00
|
|
|
for(u8 i = 2; i < MAX_SND_VOICES; i++)// voice 0 is bg music, voice 1 is gamesound
|
2012-04-03 16:04:47 +02:00
|
|
|
{
|
|
|
|
if(VoiceUsed[i] == false)
|
|
|
|
return i;
|
|
|
|
}
|
2012-07-27 19:26:49 +02:00
|
|
|
gprintf("gui_sound.cpp: ALL VOICES USED UP!!\n");
|
2012-04-03 16:04:47 +02:00
|
|
|
return -1;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void SoundCallback(s32 voice)
|
|
|
|
{
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundDecoder *decoder = SoundHandle.Decoder(voice);
|
2012-07-26 00:12:17 +02:00
|
|
|
if(!decoder)
|
|
|
|
return;
|
2012-04-03 16:04:47 +02:00
|
|
|
|
|
|
|
if(decoder->IsBufferReady())
|
|
|
|
{
|
|
|
|
if(ASND_AddVoice(voice, decoder->GetBuffer(), decoder->GetBufferSize()) == SND_OK)
|
|
|
|
{
|
|
|
|
decoder->LoadNext();
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundHandle.ThreadSignal();
|
2012-04-03 16:04:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(decoder->IsEOF())
|
|
|
|
ASND_StopVoice(voice);
|
|
|
|
else
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundHandle.ThreadSignal();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GuiSound::GuiSound()
|
|
|
|
{
|
2012-07-27 19:26:49 +02:00
|
|
|
this->voice = -1;
|
2012-01-21 21:57:41 +01:00
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2012-11-04 15:13:42 +01:00
|
|
|
GuiSound::GuiSound(const char *path, int v)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-11-04 15:13:42 +01:00
|
|
|
if(path == NULL)
|
|
|
|
return;
|
2012-07-27 19:26:49 +02:00
|
|
|
this->voice = v;
|
2012-01-21 21:57:41 +01:00
|
|
|
Init();
|
2012-11-04 15:13:42 +01:00
|
|
|
Load(path);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-11-04 15:13:42 +01:00
|
|
|
GuiSound::GuiSound(const u8 * snd, u32 len, const char *name, bool isallocated, int v)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-07-27 19:26:49 +02:00
|
|
|
this->voice = v;
|
2012-01-21 21:57:41 +01:00
|
|
|
Init();
|
|
|
|
Load(snd, len, isallocated);
|
2012-11-04 15:13:42 +01:00
|
|
|
if(name != NULL)
|
|
|
|
{
|
|
|
|
strncpy(this->filepath, name, 255);
|
|
|
|
this->filepath[255] = '\0';
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GuiSound::GuiSound(GuiSound *g)
|
|
|
|
{
|
2012-07-27 19:26:49 +02:00
|
|
|
this->voice = -1;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
Init();
|
2012-07-26 00:12:17 +02:00
|
|
|
if(g == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(g->sound != NULL)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-08-05 15:48:15 +02:00
|
|
|
u8 *snd = (u8 *)MEM2_memalign(32, g->length);
|
2012-07-18 16:09:28 +02:00
|
|
|
memcpy(snd, g->sound, g->length);
|
2012-01-21 21:57:41 +01:00
|
|
|
Load(snd, g->length, true);
|
|
|
|
}
|
|
|
|
else
|
2012-11-04 15:13:42 +01:00
|
|
|
Load(g->filepath);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GuiSound::~GuiSound()
|
|
|
|
{
|
|
|
|
FreeMemory();
|
2012-07-27 19:26:49 +02:00
|
|
|
VoiceUsed[this->voice] = false;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::Init()
|
|
|
|
{
|
2012-11-04 15:13:42 +01:00
|
|
|
memset(this->filepath, 0, 256);
|
2012-01-21 21:57:41 +01:00
|
|
|
sound = NULL;
|
|
|
|
length = 0;
|
|
|
|
|
2012-07-27 19:26:49 +02:00
|
|
|
if(this->voice == -1)
|
|
|
|
this->voice = GetFirstUnusedVoice();
|
|
|
|
if(this->voice != -1)
|
|
|
|
VoiceUsed[this->voice] = true;
|
2012-07-26 00:12:17 +02:00
|
|
|
|
2012-01-21 21:57:41 +01:00
|
|
|
volume = 255;
|
|
|
|
SoundEffectLength = 0;
|
|
|
|
loop = false;
|
|
|
|
allocated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::FreeMemory()
|
|
|
|
{
|
|
|
|
Stop();
|
2012-11-07 23:27:41 +01:00
|
|
|
if(this->voice != -1)
|
|
|
|
SoundHandle.RemoveDecoder(this->voice);
|
2012-07-27 19:26:49 +02:00
|
|
|
if(allocated && sound != NULL)
|
2012-07-18 16:09:28 +02:00
|
|
|
free(sound);
|
2012-07-27 19:26:49 +02:00
|
|
|
allocated = false;
|
2012-11-04 15:13:42 +01:00
|
|
|
memset(this->filepath, 0, 256);
|
2012-07-18 16:09:28 +02:00
|
|
|
sound = NULL;
|
|
|
|
length = 0;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
SoundEffectLength = 0;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-11-04 15:13:42 +01:00
|
|
|
bool GuiSound::Load(const char *path)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-04-03 16:04:47 +02:00
|
|
|
FreeMemory();
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-11-04 15:13:42 +01:00
|
|
|
if(path == NULL || path[strlen(path)-1] == '/')
|
2012-01-21 21:57:41 +01:00
|
|
|
return false;
|
|
|
|
|
2013-09-02 00:31:46 +02:00
|
|
|
if(!fsop_FileExist(path))
|
2012-07-26 00:12:17 +02:00
|
|
|
{
|
2012-11-04 15:13:42 +01:00
|
|
|
gprintf("gui_sound.cpp: Failed to load file %s!!\n", path);
|
2012-04-03 16:04:47 +02:00
|
|
|
return false;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-11-04 15:13:42 +01:00
|
|
|
SoundHandle.AddDecoder(this->voice, path);
|
|
|
|
//gprintf("gui_sound.cpp: Loading %s using voice %d\n", path, this->voice);
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundDecoder *decoder = SoundHandle.Decoder(this->voice);
|
2012-04-03 16:04:47 +02:00
|
|
|
if(!decoder)
|
2012-07-26 00:12:17 +02:00
|
|
|
{
|
2012-07-27 19:26:49 +02:00
|
|
|
gprintf("gui_sound.cpp: No Decoder!\n");
|
2012-04-03 16:04:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-26 00:12:17 +02:00
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
if(!decoder->IsBufferReady())
|
2012-07-26 00:12:17 +02:00
|
|
|
{
|
2012-07-27 19:26:49 +02:00
|
|
|
gprintf("gui_sound.cpp: Buffer not ready!\n");
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundHandle.RemoveDecoder(this->voice);
|
2012-01-21 21:57:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-04 15:13:42 +01:00
|
|
|
strncpy(this->filepath, path, 255);
|
2013-06-29 18:54:21 +02:00
|
|
|
this->filepath[255] = '\0';
|
2012-04-03 16:04:47 +02:00
|
|
|
SetLoop(loop);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-04 15:13:42 +01:00
|
|
|
bool GuiSound::Load(const u8 *snd, u32 len, bool isallocated)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-04-03 16:04:47 +02:00
|
|
|
FreeMemory();
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-11-03 20:16:03 +01:00
|
|
|
if(snd == NULL || len == 0)
|
2012-07-26 00:12:17 +02:00
|
|
|
return false;
|
2012-04-03 16:04:47 +02:00
|
|
|
|
2017-09-22 00:03:41 +02:00
|
|
|
if(!isallocated && memcmp(snd, "RIFF", 4) == 0)// "RIFF" is in WAV files
|
2012-04-03 16:04:47 +02:00
|
|
|
return LoadSoundEffect(snd, len);
|
|
|
|
|
2012-07-27 19:26:49 +02:00
|
|
|
sound = (u8*)snd;
|
|
|
|
length = len;
|
|
|
|
allocated = isallocated;
|
2012-07-26 00:12:17 +02:00
|
|
|
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundHandle.AddDecoder(this->voice, sound, length);
|
|
|
|
SoundDecoder *decoder = SoundHandle.Decoder(this->voice);
|
2012-07-27 19:26:49 +02:00
|
|
|
if(!decoder || !decoder->IsBufferReady())
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundHandle.RemoveDecoder(this->voice);
|
2012-04-03 16:04:47 +02:00
|
|
|
return false;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
SetLoop(loop);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiSound::LoadSoundEffect(const u8 * snd, u32 len)
|
|
|
|
{
|
|
|
|
FreeMemory();
|
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
WavDecoder decoder(snd, len);
|
|
|
|
decoder.Rewind();
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
u32 done = 0;
|
2012-08-05 15:48:15 +02:00
|
|
|
sound = (u8 *)MEM2_memalign(32, 4096);
|
2012-04-03 16:04:47 +02:00
|
|
|
memset(sound, 0, 4096);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
while(1)
|
|
|
|
{
|
2012-08-05 15:48:15 +02:00
|
|
|
u8 * tmpsnd = (u8 *)MEM2_realloc(sound, done+4096);
|
2012-04-03 16:04:47 +02:00
|
|
|
if(!tmpsnd)
|
|
|
|
{
|
2012-07-18 16:09:28 +02:00
|
|
|
free(sound);
|
2012-04-03 16:04:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
sound = tmpsnd;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2017-09-22 00:03:41 +02:00
|
|
|
int read = decoder.Read(sound+done, 4096);
|
2012-04-03 16:04:47 +02:00
|
|
|
if(read <= 0)
|
2012-07-18 16:09:28 +02:00
|
|
|
break;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
done += read;
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-08-05 15:48:15 +02:00
|
|
|
sound = (u8 *)MEM2_realloc(sound, done);
|
2012-04-03 16:04:47 +02:00
|
|
|
SoundEffectLength = done;
|
|
|
|
allocated = true;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
return true;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::Play(int vol, bool restart)
|
|
|
|
{
|
2012-04-03 16:04:47 +02:00
|
|
|
if(SoundEffectLength > 0)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-07-27 19:26:49 +02:00
|
|
|
ASND_StopVoice(this->voice);
|
|
|
|
ASND_SetVoice(this->voice, VOICE_MONO_16BIT, 22050, 0, sound, SoundEffectLength, vol, vol, NULL);
|
2012-01-21 21:57:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-27 19:26:49 +02:00
|
|
|
if((IsPlaying() && !restart) || this->voice < 0 || this->voice >= 16)
|
2012-07-26 00:12:17 +02:00
|
|
|
return;
|
2012-04-03 16:04:47 +02:00
|
|
|
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundDecoder *decoder = SoundHandle.Decoder(this->voice);
|
2012-07-26 00:12:17 +02:00
|
|
|
if(!decoder)
|
|
|
|
return;
|
2012-04-03 16:04:47 +02:00
|
|
|
|
2012-07-27 19:26:49 +02:00
|
|
|
ASND_StopVoice(this->voice);
|
2012-04-03 16:04:47 +02:00
|
|
|
if(decoder->IsEOF())
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-04-03 16:04:47 +02:00
|
|
|
decoder->ClearBuffer();
|
|
|
|
decoder->Rewind();
|
|
|
|
decoder->Decode();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
u8 * curbuffer = decoder->GetBuffer();
|
|
|
|
int bufsize = decoder->GetBufferSize();
|
|
|
|
decoder->LoadNext();
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundHandle.ThreadSignal();
|
2012-04-03 16:04:47 +02:00
|
|
|
|
2012-07-27 19:26:49 +02:00
|
|
|
ASND_SetVoice(this->voice, decoder->GetFormat(), decoder->GetSampleRate(), 0, curbuffer, bufsize, vol, vol, SoundCallback);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::Play()
|
|
|
|
{
|
|
|
|
Play(volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::Stop()
|
|
|
|
{
|
2012-07-18 16:09:28 +02:00
|
|
|
volume = 0;
|
2012-07-27 19:26:49 +02:00
|
|
|
if(!IsPlaying() || this->voice < 0 || this->voice >= 16)
|
2012-01-21 21:57:41 +01:00
|
|
|
return;
|
|
|
|
|
2012-07-27 19:26:49 +02:00
|
|
|
ASND_StopVoice(this->voice);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundDecoder *decoder = SoundHandle.Decoder(this->voice);
|
2012-07-26 00:12:17 +02:00
|
|
|
if(!decoder)
|
|
|
|
return;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
decoder->ClearBuffer();
|
2012-01-21 21:57:41 +01:00
|
|
|
Rewind();
|
2012-07-26 00:12:17 +02:00
|
|
|
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundHandle.ThreadSignal();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::Pause()
|
|
|
|
{
|
2012-07-27 19:26:49 +02:00
|
|
|
if(this->voice < 0 || this->voice >= 16)
|
2012-01-21 21:57:41 +01:00
|
|
|
return;
|
|
|
|
|
2012-07-27 19:26:49 +02:00
|
|
|
ASND_StopVoice(this->voice);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::Resume()
|
|
|
|
{
|
2012-04-03 16:04:47 +02:00
|
|
|
Play();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiSound::IsPlaying()
|
|
|
|
{
|
2012-07-27 19:26:49 +02:00
|
|
|
if(this->voice < 0 || this->voice >= 16)
|
2012-01-21 21:57:41 +01:00
|
|
|
return false;
|
|
|
|
|
2012-07-27 19:26:49 +02:00
|
|
|
int result = ASND_StatusVoice(this->voice);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
if(result == SND_WORKING || result == SND_WAITING)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GuiSound::GetVolume()
|
|
|
|
{
|
|
|
|
return volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::SetVolume(int vol)
|
|
|
|
{
|
2012-07-27 19:26:49 +02:00
|
|
|
if(this->voice < 0 || this->voice >= 16 || vol < 0)
|
2012-01-21 21:57:41 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
volume = vol;
|
2012-07-27 19:26:49 +02:00
|
|
|
ASND_ChangeVolumeVoice(this->voice, volume, volume);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::SetLoop(u8 l)
|
|
|
|
{
|
|
|
|
loop = l;
|
|
|
|
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundDecoder *decoder = SoundHandle.Decoder(this->voice);
|
2012-07-26 00:12:17 +02:00
|
|
|
if(!decoder)
|
|
|
|
return;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
decoder->SetLoop(l == 1);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiSound::Rewind()
|
|
|
|
{
|
2012-10-15 21:16:14 +02:00
|
|
|
SoundDecoder *decoder = SoundHandle.Decoder(this->voice);
|
2012-07-26 00:12:17 +02:00
|
|
|
if(!decoder)
|
|
|
|
return;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-04-03 16:04:47 +02:00
|
|
|
decoder->Rewind();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-08-16 00:33:54 +02:00
|
|
|
void GuiSound::SetVoice(s8 v)
|
|
|
|
{
|
|
|
|
this->voice = v;
|
|
|
|
}
|
|
|
|
|
2012-01-21 21:57:41 +01:00
|
|
|
void soundInit(void)
|
|
|
|
{
|
|
|
|
ASND_Init();
|
|
|
|
ASND_Pause(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void soundDeinit(void)
|
|
|
|
{
|
|
|
|
ASND_Pause(1);
|
|
|
|
ASND_End();
|
|
|
|
}
|