Hook up the first UI sound, make it play.

This commit is contained in:
Henrik Rydgård 2020-08-02 23:02:30 +02:00
parent b30be913c0
commit 5423f76e25
5 changed files with 89 additions and 20 deletions

View file

@ -1882,11 +1882,11 @@ static u32 sceMpegAvcCopyYCbCr(u32 mpeg, u32 sourceAddr, u32 YCbCrAddr)
MpegContext *ctx = getMpegCtx(mpeg); MpegContext *ctx = getMpegCtx(mpeg);
if (!ctx) { if (!ctx) {
WARN_LOG(ME, "UNIMPL sceMpegAvcCopyYCbCr(%08x, %08x, %08x): bad mpeg handle", mpeg, sourceAddr, YCbCrAddr); ERROR_LOG(ME, "UNIMPL sceMpegAvcCopyYCbCr(%08x, %08x, %08x): bad mpeg handle", mpeg, sourceAddr, YCbCrAddr);
return -1; return -1;
} }
ERROR_LOG(ME, "UNIMPL sceMpegAvcCopyYCbCr(%08x, %08x, %08x)", mpeg, sourceAddr, YCbCrAddr); WARN_LOG(ME, "UNIMPL sceMpegAvcCopyYCbCr(%08x, %08x, %08x)", mpeg, sourceAddr, YCbCrAddr);
return 0; return 0;
} }

View file

@ -5,6 +5,7 @@
#include "base/timeutil.h" #include "base/timeutil.h"
#include "file/chunk_file.h" #include "file/chunk_file.h"
#include "file/vfs.h" #include "file/vfs.h"
#include "ui/root.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Core/HW/SimpleAudioDec.h" #include "Core/HW/SimpleAudioDec.h"
@ -260,10 +261,12 @@ BackgroundAudio::Sample *BackgroundAudio::LoadSample(const std::string &path) {
return nullptr; return nullptr;
} }
WavData wave; RIFFReader reader(data, (int)bytes);
wave.Read(RIFFReader(data, (int)bytes));
if (wave.num_channels != 2) { WavData wave;
wave.Read(reader);
if (wave.num_channels != 2 || wave.sample_rate != 44100 || wave.raw_bytes_per_frame_ != 4) {
ELOG("Wave format not supported for mixer playback. Must be 16-bit raw stereo. '%s'", path.c_str()); ELOG("Wave format not supported for mixer playback. Must be 16-bit raw stereo. '%s'", path.c_str());
return nullptr; return nullptr;
} }
@ -279,10 +282,23 @@ void BackgroundAudio::LoadSamples() {
samples_[(size_t)MenuSFX::BACK] = std::unique_ptr<Sample>(LoadSample("sfx_back.wav")); samples_[(size_t)MenuSFX::BACK] = std::unique_ptr<Sample>(LoadSample("sfx_back.wav"));
samples_[(size_t)MenuSFX::SELECT] = std::unique_ptr<Sample>(LoadSample("sfx_select.wav")); samples_[(size_t)MenuSFX::SELECT] = std::unique_ptr<Sample>(LoadSample("sfx_select.wav"));
samples_[(size_t)MenuSFX::CONFIRM] = std::unique_ptr<Sample>(LoadSample("sfx_confirm.wav")); samples_[(size_t)MenuSFX::CONFIRM] = std::unique_ptr<Sample>(LoadSample("sfx_confirm.wav"));
UI::SetSoundCallback([](UI::UISound sound) {
MenuSFX sfx;
switch (sound) {
case UI::UISound::BACK: sfx = MenuSFX::BACK; break;
case UI::UISound::CONFIRM: sfx = MenuSFX::CONFIRM; break;
case UI::UISound::SELECT: sfx = MenuSFX::SELECT; break;
default: return;
}
g_BackgroundAudio.PlaySFX(sfx);
});
} }
void BackgroundAudio::PlaySFX(MenuSFX sfx) { void BackgroundAudio::PlaySFX(MenuSFX sfx) {
plays_.push_back(PlayInstance{ sfx, 0 }); std::lock_guard<std::mutex> lock(g_bgMutex);
plays_.push_back(PlayInstance{ sfx, 0, 64, false });
} }
void BackgroundAudio::Clear(bool hard) { void BackgroundAudio::Clear(bool hard) {
@ -332,33 +348,58 @@ int BackgroundAudio::Play() {
} }
double now = time_now_d(); double now = time_now_d();
if (at3Reader) {
int sz = lastPlaybackTime <= 0.0 ? 44100 / 60 : (int)((now - lastPlaybackTime) * 44100); int sz = lastPlaybackTime <= 0.0 ? 44100 / 60 : (int)((now - lastPlaybackTime) * 44100);
sz = std::min(BUFSIZE / 2, sz); sz = std::min(BUFSIZE / 2, sz);
if (at3Reader) {
if (sz >= 16) { if (sz >= 16) {
if (at3Reader->Read(buffer, sz)) { if (at3Reader->Read(buffer, sz)) {
if (!fadingOut) { if (fadingOut) {
__PushExternalAudio(buffer, sz);
} else {
for (int i = 0; i < sz*2; i += 2) { for (int i = 0; i < sz*2; i += 2) {
buffer[i] *= volume; buffer[i] *= volume;
buffer[i + 1] *= volume; buffer[i + 1] *= volume;
volume += delta; volume += delta;
} }
}
}
}
} else {
for (int i = 0; i < sz * 2; i += 2) {
buffer[i] = 0;
buffer[i + 1] = 0;
}
}
// Mix in menu sound effects
if (!plays_.empty()) {
for (int i = 0; i < sz * 2; i += 2) {
std::vector<PlayInstance>::iterator iter = plays_.begin();
while (iter != plays_.end()) {
PlayInstance inst = *iter;
auto sample = samples_[(int)inst.sound].get();
if (iter->offset >= sample->length_) {
iter->done = true;
iter = plays_.erase(iter);
} else {
if (!iter->done) {
buffer[i] += sample->data_[inst.offset * 2] * inst.volume >> 8;
buffer[i + 1] += sample->data_[inst.offset * 2 + 1] * inst.volume >> 8;
}
iter->offset++;
iter++;
}
}
}
}
__PushExternalAudio(buffer, sz); __PushExternalAudio(buffer, sz);
if (volume <= 0.0f) {
if (at3Reader && fadingOut && volume <= 0.0f) {
Clear(true); Clear(true);
fadingOut = false; fadingOut = false;
gameLastChanged = 0; gameLastChanged = 0;
} }
}
}
lastPlaybackTime = now; lastPlaybackTime = now;
}
} else {
__PushExternalAudio(0, 0);
lastPlaybackTime = now;
}
return 0; return 0;
} }

View file

@ -45,6 +45,8 @@ private:
struct PlayInstance { struct PlayInstance {
MenuSFX sound; MenuSFX sound;
int offset; int offset;
int volume; // 0..255
bool done;
}; };
struct Sample { struct Sample {

View file

@ -17,6 +17,9 @@ static bool focusMovementEnabled;
bool focusForced; bool focusForced;
static std::mutex eventMutex_; static std::mutex eventMutex_;
static std::function<void(UISound)> soundCallback;
struct DispatchQueueItem { struct DispatchQueueItem {
Event *e; Event *e;
EventParams params; EventParams params;
@ -133,6 +136,18 @@ void MoveFocus(ViewGroup *root, FocusDirection direction) {
if (neigh.view) { if (neigh.view) {
neigh.view->SetFocus(); neigh.view->SetFocus();
root->SubviewFocused(neigh.view); root->SubviewFocused(neigh.view);
PlayUISound(UISound::SELECT);
}
}
void SetSoundCallback(std::function<void(UISound)> func) {
soundCallback = func;
}
void PlayUISound(UISound sound) {
if (soundCallback) {
soundCallback(UISound::SELECT);
} }
} }

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <functional>
#include "ui/ui_context.h" #include "ui/ui_context.h"
#include "input/input_state.h" #include "input/input_state.h"
@ -26,4 +27,14 @@ bool KeyEvent(const KeyInput &key, ViewGroup *root);
bool TouchEvent(const TouchInput &touch, ViewGroup *root); bool TouchEvent(const TouchInput &touch, ViewGroup *root);
bool AxisEvent(const AxisInput &axis, ViewGroup *root); bool AxisEvent(const AxisInput &axis, ViewGroup *root);
enum class UISound {
SELECT,
BACK,
CONFIRM,
};
void SetSoundCallback(std::function<void(UISound)> func);
void PlayUISound(UISound sound);
} // namespace UI } // namespace UI