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>
|
2019-08-14 23:24:55 +02:00
|
|
|
#include <gui/sounds/SoundHandler.hpp>
|
2022-02-05 14:28:08 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <string>
|
2018-06-21 20:44:58 +02:00
|
|
|
|
2020-08-13 12:38:07 +02:00
|
|
|
GuiSound::GuiSound(const char *filepath) {
|
2018-06-21 20:44:58 +02:00
|
|
|
voice = -1;
|
|
|
|
Load(filepath);
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
2020-08-13 12:38:07 +02:00
|
|
|
GuiSound::GuiSound(const uint8_t *snd, int32_t length) {
|
2018-06-21 20:44:58 +02:00
|
|
|
voice = -1;
|
|
|
|
Load(snd, length);
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
GuiSound::~GuiSound() {
|
2020-08-13 12:38:07 +02:00
|
|
|
if (voice >= 0) {
|
2017-10-29 10:28:14 +01:00
|
|
|
SoundHandler::instance()->RemoveDecoder(voice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-13 12:38:07 +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
|
2020-08-13 12:38:07 +02:00
|
|
|
for (int32_t i = 0; i < MAX_DECODERS; i++) {
|
|
|
|
SoundDecoder *decoder = SoundHandler::instance()->getDecoder(i);
|
|
|
|
if (decoder == NULL) {
|
2017-10-29 10:28:14 +01:00
|
|
|
SoundHandler::instance()->AddDecoder(i, filepath);
|
|
|
|
decoder = SoundHandler::instance()->getDecoder(i);
|
2020-08-13 12:38:07 +02:00
|
|
|
if (decoder) {
|
2017-10-29 10:28:14 +01:00
|
|
|
voice = i;
|
|
|
|
SoundHandler::instance()->ThreadSignal();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 12:38:07 +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
|
|
|
}
|
|
|
|
|
2020-08-13 12:38:07 +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;
|
|
|
|
}
|
|
|
|
|
2020-08-13 12:58:19 +02:00
|
|
|
if (!snd) {
|
2017-10-29 10:28:14 +01:00
|
|
|
return false;
|
2020-08-13 12:58:19 +02:00
|
|
|
}
|
2017-10-29 10:28:14 +01:00
|
|
|
|
|
|
|
//! find next free decoder
|
2020-08-13 12:38:07 +02:00
|
|
|
for (int32_t i = 0; i < MAX_DECODERS; i++) {
|
|
|
|
SoundDecoder *decoder = SoundHandler::instance()->getDecoder(i);
|
|
|
|
if (decoder == NULL) {
|
2017-10-29 10:28:14 +01:00
|
|
|
SoundHandler::instance()->AddDecoder(i, snd, len);
|
|
|
|
decoder = SoundHandler::instance()->getDecoder(i);
|
2020-08-13 12:38:07 +02:00
|
|
|
if (decoder) {
|
2017-10-29 10:28:14 +01:00
|
|
|
voice = i;
|
|
|
|
SoundHandler::instance()->ThreadSignal();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 12:38:07 +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();
|
|
|
|
|
2020-08-13 12:38:07 +02:00
|
|
|
Voice *v = SoundHandler::instance()->getVoice(voice);
|
2020-08-13 12:58:19 +02:00
|
|
|
if (v) {
|
2017-10-29 10:28:14 +01:00
|
|
|
v->setState(Voice::STATE_START);
|
2020-08-13 12:58:19 +02:00
|
|
|
}
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
void GuiSound::Stop() {
|
2020-08-13 12:38:07 +02:00
|
|
|
Voice *v = SoundHandler::instance()->getVoice(voice);
|
|
|
|
if (v) {
|
2020-08-13 12:58:19 +02:00
|
|
|
if ((v->getState() != Voice::STATE_STOP) && (v->getState() != Voice::STATE_STOPPED)) {
|
2017-10-29 10:28:14 +01:00
|
|
|
v->setState(Voice::STATE_STOP);
|
2020-08-13 12:58:19 +02:00
|
|
|
}
|
2017-10-29 10:28:14 +01:00
|
|
|
|
2020-08-13 12:38:07 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-13 12:38:07 +02:00
|
|
|
SoundDecoder *decoder = SoundHandler::instance()->getDecoder(voice);
|
|
|
|
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() {
|
2020-08-13 12:58:19 +02:00
|
|
|
if (!IsPlaying()) {
|
2017-10-29 10:28:14 +01:00
|
|
|
return;
|
2020-08-13 12:58:19 +02:00
|
|
|
}
|
2017-10-29 10:28:14 +01:00
|
|
|
|
2020-08-13 12:38:07 +02:00
|
|
|
Voice *v = SoundHandler::instance()->getVoice(voice);
|
2020-08-13 12:58:19 +02:00
|
|
|
if (v) {
|
2017-10-29 10:28:14 +01:00
|
|
|
v->setState(Voice::STATE_STOP);
|
2020-08-13 12:58:19 +02:00
|
|
|
}
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
void GuiSound::Resume() {
|
2020-08-13 12:58:19 +02:00
|
|
|
if (IsPlaying()) {
|
2017-10-29 10:28:14 +01:00
|
|
|
return;
|
2020-08-13 12:58:19 +02:00
|
|
|
}
|
2017-10-29 10:28:14 +01:00
|
|
|
|
2020-08-13 12:38:07 +02:00
|
|
|
Voice *v = SoundHandler::instance()->getVoice(voice);
|
2020-08-13 12:58:19 +02:00
|
|
|
if (v) {
|
2017-10-29 10:28:14 +01:00
|
|
|
v->setState(Voice::STATE_START);
|
2020-08-13 12:58:19 +02:00
|
|
|
}
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
bool GuiSound::IsPlaying() {
|
2020-08-13 12:38:07 +02:00
|
|
|
Voice *v = SoundHandler::instance()->getVoice(voice);
|
|
|
|
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) {
|
2020-08-13 12:58:19 +02:00
|
|
|
if (vol > 100) {
|
2017-10-29 10:28:14 +01:00
|
|
|
vol = 100;
|
2020-08-13 12:58:19 +02:00
|
|
|
}
|
2017-10-29 10:28:14 +01:00
|
|
|
|
2020-08-13 12:38:07 +02:00
|
|
|
uint32_t volumeConv = ((0x8000 * vol) / 100) << 16;
|
2017-10-29 10:28:14 +01:00
|
|
|
|
2020-08-13 12:38:07 +02:00
|
|
|
Voice *v = SoundHandler::instance()->getVoice(voice);
|
2020-08-13 12:58:19 +02:00
|
|
|
if (v) {
|
2017-10-29 10:28:14 +01:00
|
|
|
v->setVolume(volumeConv);
|
2020-08-13 12:58:19 +02:00
|
|
|
}
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
void GuiSound::SetLoop(bool l) {
|
2020-08-13 12:38:07 +02:00
|
|
|
SoundDecoder *decoder = SoundHandler::instance()->getDecoder(voice);
|
2020-08-13 12:58:19 +02:00
|
|
|
if (decoder) {
|
2017-10-29 10:28:14 +01:00
|
|
|
decoder->SetLoop(l);
|
2020-08-13 12:58:19 +02:00
|
|
|
}
|
2017-10-29 10:28:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:44:58 +02:00
|
|
|
void GuiSound::Rewind() {
|
2017-10-29 10:28:14 +01:00
|
|
|
Stop();
|
|
|
|
}
|