2017-10-29 10:28:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2015 Dimok
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
2018-06-21 20:44:58 +02:00
|
|
|
#include <gui/GuiSound.h>
|
|
|
|
#include <sounds/SoundHandler.hpp>
|
|
|
|
|
|
|
|
GuiSound::GuiSound(const char * filepath) {
|
|
|
|
voice = -1;
|
|
|
|
Load(filepath);
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
GuiSound::GuiSound(const uint8_t * snd, int32_t length) {
|
|
|
|
voice = -1;
|
|
|
|
Load(snd, length);
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
GuiSound::~GuiSound() {
|
|
|
|
if(voice >= 0) {
|
2017-10-29 10:28:14 +01:00
|
|
|
SoundHandler::instance()->RemoveDecoder(voice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
bool GuiSound::Load(const char * filepath) {
|
|
|
|
if(voice >= 0) {
|
2017-10-29 10:28:14 +01:00
|
|
|
SoundHandler::instance()->RemoveDecoder(voice);
|
|
|
|
voice = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! find next free decoder
|
2018-06-21 20:44:58 +02:00
|
|
|
for(int32_t i = 0; i < MAX_DECODERS; i++) {
|
2017-10-29 10:28:14 +01:00
|
|
|
SoundDecoder * decoder = SoundHandler::instance()->getDecoder(i);
|
2018-06-21 20:44:58 +02:00
|
|
|
if(decoder == NULL) {
|
2017-10-29 10:28:14 +01:00
|
|
|
SoundHandler::instance()->AddDecoder(i, filepath);
|
|
|
|
decoder = SoundHandler::instance()->getDecoder(i);
|
2018-06-21 20:44:58 +02:00
|
|
|
if(decoder) {
|
2017-10-29 10:28:14 +01:00
|
|
|
voice = i;
|
|
|
|
SoundHandler::instance()->ThreadSignal();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
if(voice < 0) {
|
2017-10-29 10:28:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
return true;
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
bool GuiSound::Load(const uint8_t * snd, int32_t len) {
|
|
|
|
if(voice >= 0) {
|
2017-10-29 10:28:14 +01:00
|
|
|
SoundHandler::instance()->RemoveDecoder(voice);
|
|
|
|
voice = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!snd)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//! find next free decoder
|
2018-06-21 20:44:58 +02:00
|
|
|
for(int32_t i = 0; i < MAX_DECODERS; i++) {
|
2017-10-29 10:28:14 +01:00
|
|
|
SoundDecoder * decoder = SoundHandler::instance()->getDecoder(i);
|
2018-06-21 20:44:58 +02:00
|
|
|
if(decoder == NULL) {
|
2017-10-29 10:28:14 +01:00
|
|
|
SoundHandler::instance()->AddDecoder(i, snd, len);
|
|
|
|
decoder = SoundHandler::instance()->getDecoder(i);
|
2018-06-21 20:44:58 +02:00
|
|
|
if(decoder) {
|
2017-10-29 10:28:14 +01:00
|
|
|
voice = i;
|
|
|
|
SoundHandler::instance()->ThreadSignal();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
if(voice < 0) {
|
2017-10-29 10:28:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
return true;
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
void GuiSound::Play() {
|
2017-10-29 10:28:14 +01:00
|
|
|
Stop();
|
|
|
|
|
|
|
|
Voice * v = SoundHandler::instance()->getVoice(voice);
|
|
|
|
if(v)
|
|
|
|
v->setState(Voice::STATE_START);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
void GuiSound::Stop() {
|
2017-10-29 10:28:14 +01:00
|
|
|
Voice * v = SoundHandler::instance()->getVoice(voice);
|
2018-06-21 20:44:58 +02:00
|
|
|
if(v) {
|
2017-10-29 10:28:14 +01:00
|
|
|
if((v->getState() != Voice::STATE_STOP) && (v->getState() != Voice::STATE_STOPPED))
|
|
|
|
v->setState(Voice::STATE_STOP);
|
|
|
|
|
|
|
|
while(v->getState() != Voice::STATE_STOPPED)
|
2018-06-21 20:44:58 +02:00
|
|
|
OSSleepTicks(OSMicrosecondsToTicks(1000));
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SoundDecoder * decoder = SoundHandler::instance()->getDecoder(voice);
|
2018-06-21 20:44:58 +02:00
|
|
|
if(decoder) {
|
2017-10-29 10:28:14 +01:00
|
|
|
decoder->Lock();
|
|
|
|
decoder->Rewind();
|
|
|
|
decoder->ClearBuffer();
|
|
|
|
SoundHandler::instance()->ThreadSignal();
|
|
|
|
decoder->Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
void GuiSound::Pause() {
|
2017-10-29 10:28:14 +01:00
|
|
|
if(!IsPlaying())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Voice * v = SoundHandler::instance()->getVoice(voice);
|
|
|
|
if(v)
|
|
|
|
v->setState(Voice::STATE_STOP);
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
void GuiSound::Resume() {
|
2017-10-29 10:28:14 +01:00
|
|
|
if(IsPlaying())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Voice * v = SoundHandler::instance()->getVoice(voice);
|
|
|
|
if(v)
|
|
|
|
v->setState(Voice::STATE_START);
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
bool GuiSound::IsPlaying() {
|
2017-10-29 10:28:14 +01:00
|
|
|
Voice * v = SoundHandler::instance()->getVoice(voice);
|
2018-06-21 20:44:58 +02:00
|
|
|
if(v) {
|
2017-10-29 10:28:14 +01:00
|
|
|
return v->getState() == Voice::STATE_PLAYING;
|
|
|
|
}
|
2018-06-21 20:44:58 +02:00
|
|
|
return false;
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
void GuiSound::SetVolume(uint32_t vol) {
|
2017-10-29 10:28:14 +01:00
|
|
|
if(vol > 100)
|
|
|
|
vol = 100;
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
uint32_t volumeConv = ( (0x8000 * vol) / 100 ) << 16;
|
2017-10-29 10:28:14 +01:00
|
|
|
|
|
|
|
Voice * v = SoundHandler::instance()->getVoice(voice);
|
|
|
|
if(v)
|
|
|
|
v->setVolume(volumeConv);
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
void GuiSound::SetLoop(bool l) {
|
2017-10-29 10:28:14 +01:00
|
|
|
SoundDecoder * decoder = SoundHandler::instance()->getDecoder(voice);
|
|
|
|
if(decoder)
|
|
|
|
decoder->SetLoop(l);
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
void GuiSound::Rewind() {
|
2017-10-29 10:28:14 +01:00
|
|
|
Stop();
|
|
|
|
}
|