mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-04-27 05:23:51 +00:00
Replace process-spawning audio with miniaudio for non-blocking playback
Eliminates severe stuttering from fork/exec + disk I/O by streaming audio directly from memory using miniaudio library.
This commit is contained in:
parent
c047446fb7
commit
bd3f1921d1
11 changed files with 96445 additions and 133 deletions
|
|
@ -1,16 +1,14 @@
|
|||
#include "audio/music_manager.hpp"
|
||||
#include "audio/audio_engine.hpp"
|
||||
#include "pipeline/asset_manager.hpp"
|
||||
#include "core/logger.hpp"
|
||||
#include "platform/process.hpp"
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
namespace wowee {
|
||||
namespace audio {
|
||||
|
||||
MusicManager::MusicManager() {
|
||||
tempFilePath = platform::getTempFilePath("wowee_music.mp3");
|
||||
}
|
||||
MusicManager::MusicManager() = default;
|
||||
|
||||
MusicManager::~MusicManager() {
|
||||
shutdown();
|
||||
|
|
@ -23,15 +21,21 @@ bool MusicManager::initialize(pipeline::AssetManager* assets) {
|
|||
}
|
||||
|
||||
void MusicManager::shutdown() {
|
||||
stopCurrentProcess();
|
||||
// Clean up temp file
|
||||
std::remove(tempFilePath.c_str());
|
||||
AudioEngine::instance().stopMusic();
|
||||
playing = false;
|
||||
currentTrack.clear();
|
||||
}
|
||||
|
||||
void MusicManager::playMusic(const std::string& mpqPath, bool loop) {
|
||||
if (!assetManager) return;
|
||||
if (mpqPath == currentTrack && playing) return;
|
||||
|
||||
// Check if AudioEngine is ready
|
||||
if (!AudioEngine::instance().isInitialized()) {
|
||||
LOG_WARNING("Music: AudioEngine not initialized");
|
||||
return;
|
||||
}
|
||||
|
||||
// Read music file from MPQ
|
||||
auto data = assetManager->readFile(mpqPath);
|
||||
if (data.empty()) {
|
||||
|
|
@ -40,37 +44,17 @@ void MusicManager::playMusic(const std::string& mpqPath, bool loop) {
|
|||
}
|
||||
|
||||
// Stop current playback
|
||||
stopCurrentProcess();
|
||||
AudioEngine::instance().stopMusic();
|
||||
|
||||
// Write to temp file
|
||||
std::ofstream out(tempFilePath, std::ios::binary);
|
||||
if (!out) {
|
||||
LOG_ERROR("Music: Could not write temp file");
|
||||
return;
|
||||
}
|
||||
out.write(reinterpret_cast<const char*>(data.data()), data.size());
|
||||
out.close();
|
||||
|
||||
// Play with ffplay in background
|
||||
std::vector<std::string> args;
|
||||
args.push_back("-nodisp");
|
||||
args.push_back("-autoexit");
|
||||
if (loop) {
|
||||
args.push_back("-loop");
|
||||
args.push_back("0");
|
||||
}
|
||||
args.push_back("-volume");
|
||||
args.push_back(std::to_string(volumePercent));
|
||||
args.push_back(tempFilePath);
|
||||
|
||||
playerPid = platform::spawnProcess(args);
|
||||
if (playerPid != INVALID_PROCESS) {
|
||||
// Play with AudioEngine (non-blocking, streams from memory)
|
||||
float volume = volumePercent / 100.0f;
|
||||
if (AudioEngine::instance().playMusic(data, volume, loop)) {
|
||||
playing = true;
|
||||
currentTrack = mpqPath;
|
||||
currentTrackIsFile = false;
|
||||
LOG_INFO("Music: Playing ", mpqPath);
|
||||
} else {
|
||||
LOG_ERROR("Music: Failed to spawn ffplay process");
|
||||
LOG_ERROR("Music: Failed to play music via AudioEngine");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -82,33 +66,46 @@ void MusicManager::playFilePath(const std::string& filePath, bool loop) {
|
|||
return;
|
||||
}
|
||||
|
||||
stopCurrentProcess();
|
||||
|
||||
std::vector<std::string> args;
|
||||
args.push_back("-nodisp");
|
||||
args.push_back("-autoexit");
|
||||
if (loop) {
|
||||
args.push_back("-loop");
|
||||
args.push_back("0");
|
||||
// Check if AudioEngine is ready
|
||||
if (!AudioEngine::instance().isInitialized()) {
|
||||
LOG_WARNING("Music: AudioEngine not initialized");
|
||||
return;
|
||||
}
|
||||
args.push_back("-volume");
|
||||
args.push_back(std::to_string(volumePercent));
|
||||
args.push_back(filePath);
|
||||
|
||||
playerPid = platform::spawnProcess(args);
|
||||
if (playerPid != INVALID_PROCESS) {
|
||||
// Read file into memory
|
||||
std::ifstream file(filePath, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
LOG_ERROR("Music: Could not open file: ", filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
std::streamsize size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<uint8_t> data(static_cast<size_t>(size));
|
||||
if (!file.read(reinterpret_cast<char*>(data.data()), size)) {
|
||||
LOG_ERROR("Music: Could not read file: ", filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
// Stop current playback
|
||||
AudioEngine::instance().stopMusic();
|
||||
|
||||
// Play with AudioEngine
|
||||
float volume = volumePercent / 100.0f;
|
||||
if (AudioEngine::instance().playMusic(data, volume, loop)) {
|
||||
playing = true;
|
||||
currentTrack = filePath;
|
||||
currentTrackIsFile = true;
|
||||
LOG_INFO("Music: Playing file ", filePath);
|
||||
} else {
|
||||
LOG_ERROR("Music: Failed to spawn ffplay process");
|
||||
LOG_ERROR("Music: Failed to play music via AudioEngine");
|
||||
}
|
||||
}
|
||||
|
||||
void MusicManager::stopMusic(float fadeMs) {
|
||||
(void)fadeMs; // ffplay doesn't support fade easily
|
||||
stopCurrentProcess();
|
||||
(void)fadeMs; // Fade not implemented yet
|
||||
AudioEngine::instance().stopMusic();
|
||||
playing = false;
|
||||
currentTrack.clear();
|
||||
currentTrackIsFile = false;
|
||||
|
|
@ -119,42 +116,31 @@ void MusicManager::setVolume(int volume) {
|
|||
if (volume > 100) volume = 100;
|
||||
if (volumePercent == volume) return;
|
||||
volumePercent = volume;
|
||||
if (playing && !currentTrack.empty()) {
|
||||
std::string track = currentTrack;
|
||||
bool isFile = currentTrackIsFile;
|
||||
stopCurrentProcess();
|
||||
playing = false;
|
||||
currentTrack.clear();
|
||||
currentTrackIsFile = false;
|
||||
if (isFile) {
|
||||
playFilePath(track, true);
|
||||
} else {
|
||||
playMusic(track, true);
|
||||
}
|
||||
}
|
||||
|
||||
// Update AudioEngine music volume directly (no restart needed!)
|
||||
float vol = volumePercent / 100.0f;
|
||||
AudioEngine::instance().setMusicVolume(vol);
|
||||
}
|
||||
|
||||
void MusicManager::crossfadeTo(const std::string& mpqPath, float fadeMs) {
|
||||
if (mpqPath == currentTrack && playing) return;
|
||||
|
||||
// Simple implementation: stop and start (no actual crossfade with subprocess)
|
||||
// Simple implementation: stop and start (no actual crossfade yet)
|
||||
if (fadeMs > 0 && playing) {
|
||||
crossfading = true;
|
||||
pendingTrack = mpqPath;
|
||||
fadeTimer = 0.0f;
|
||||
fadeDuration = fadeMs / 1000.0f;
|
||||
stopCurrentProcess();
|
||||
AudioEngine::instance().stopMusic();
|
||||
} else {
|
||||
playMusic(mpqPath);
|
||||
}
|
||||
}
|
||||
|
||||
void MusicManager::update(float deltaTime) {
|
||||
// Check if player process is still running
|
||||
if (playerPid != INVALID_PROCESS) {
|
||||
if (!platform::isProcessRunning(playerPid)) {
|
||||
playing = false;
|
||||
}
|
||||
// Check if music is still playing
|
||||
if (playing && !AudioEngine::instance().isMusicPlaying()) {
|
||||
playing = false;
|
||||
}
|
||||
|
||||
// Handle crossfade
|
||||
|
|
@ -169,12 +155,5 @@ void MusicManager::update(float deltaTime) {
|
|||
}
|
||||
}
|
||||
|
||||
void MusicManager::stopCurrentProcess() {
|
||||
if (playerPid != INVALID_PROCESS) {
|
||||
platform::killProcess(playerPid);
|
||||
playing = false;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace audio
|
||||
} // namespace wowee
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue