Working on audio in web part 1

This commit is contained in:
Vladislav Khorev 2026-04-17 17:13:55 +03:00
parent 3778a603f2
commit b6e0422d71
9 changed files with 65 additions and 13 deletions

3
.gitattributes vendored
View File

@ -2,3 +2,6 @@
*.png filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text
*.anim filter=lfs diff=lfs merge=lfs -text
*.wav filter=lfs diff=lfs merge=lfs -text
*.ogg filter=lfs diff=lfs merge=lfs -text
*.mp3 filter=lfs diff=lfs merge=lfs -text

View File

@ -149,6 +149,8 @@ set(EMSCRIPTEN_FLAGS
"-sUSE_LIBPNG=1"
"-sUSE_ZLIB=1"
"-sUSE_SDL_TTF=2"
"-sUSE_SDL_MIXER=2"
"-sSDL2_MIXER_FORMATS=[ogg,mp3]"
#"-pthread"
#"-sUSE_PTHREADS=1"
"-fexceptions"
@ -168,6 +170,7 @@ set(EMSCRIPTEN_LINK_FLAGS
"--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/../resources/loading.png@resources/loading.png"
"--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/../resources/shaders@resources/shaders"
"--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/../resources/start.lua@resources/start.lua"
"--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/../audio@audio"
)
# Применяем настройки линковки

View File

@ -173,6 +173,7 @@ endif()
# Какие папки с ресурсами нужно копировать
set(RUNTIME_RESOURCE_DIRS
"resources"
"audio"
)
# Копируем ресурсы и шейдеры в папку exe и в корень build/

Binary file not shown.

View File

@ -1,9 +1,14 @@
#include "AudioPlayerAsync.h"
#include "AudioPlayerAsync.h"
#include <iostream>
#ifdef __EMSCRIPTEN__
AudioPlayerAsync::AudioPlayerAsync() {}
#else
AudioPlayerAsync::AudioPlayerAsync() : worker(&AudioPlayerAsync::workerThread, this) {}
#endif
AudioPlayerAsync::~AudioPlayerAsync() {
#ifndef __EMSCRIPTEN__
{
std::unique_lock<std::mutex> lock(mtx);
stop = true;
@ -11,6 +16,7 @@ AudioPlayerAsync::~AudioPlayerAsync() {
}
if (worker.joinable())
worker.join();
#endif
shutdown();
}
@ -57,8 +63,7 @@ void AudioPlayerAsync::playSoundAsync(const std::string& filePath, int loops, in
return;
}
std::unique_lock<std::mutex> lock(mtx);
taskQueue.push([this, filePath, loops, channel]() {
auto task = [this, filePath, loops, channel]() {
Mix_Chunk* sound = nullptr;
{
std::lock_guard<std::mutex> cacheLock(soundCacheMutex);
@ -82,15 +87,21 @@ void AudioPlayerAsync::playSoundAsync(const std::string& filePath, int loops, in
if (result == -1) {
std::cerr << "Mix_PlayChannel failed: " << Mix_GetError() << std::endl;
}
});
};
#ifdef __EMSCRIPTEN__
task();
#else
std::unique_lock<std::mutex> lock(mtx);
taskQueue.push(std::move(task));
cv.notify_one();
#endif
}
void AudioPlayerAsync::playMusicAsync(const std::string& filePath, int loops) {
if (!initialized) return;
std::unique_lock<std::mutex> lock(mtx);
taskQueue.push([this, filePath, loops]() {
auto task = [filePath, loops]() {
Mix_Music* music = Mix_LoadMUS(filePath.c_str());
if (!music) {
std::cerr << "Failed to load music " << filePath << ": " << Mix_GetError() << std::endl;
@ -100,57 +111,85 @@ void AudioPlayerAsync::playMusicAsync(const std::string& filePath, int loops) {
std::cerr << "Mix_PlayMusic failed: " << Mix_GetError() << std::endl;
Mix_FreeMusic(music);
}
});
};
#ifdef __EMSCRIPTEN__
task();
#else
std::unique_lock<std::mutex> lock(mtx);
taskQueue.push(std::move(task));
cv.notify_one();
#endif
}
void AudioPlayerAsync::stopMusicAsync() {
if (!initialized) return;
#ifdef __EMSCRIPTEN__
Mix_HaltMusic();
#else
std::unique_lock<std::mutex> lock(mtx);
taskQueue.push([]() {
Mix_HaltMusic();
});
cv.notify_one();
#endif
}
void AudioPlayerAsync::pauseMusicAsync() {
if (!initialized) return;
#ifdef __EMSCRIPTEN__
Mix_PauseMusic();
#else
std::unique_lock<std::mutex> lock(mtx);
taskQueue.push([]() {
Mix_PauseMusic();
});
cv.notify_one();
#endif
}
void AudioPlayerAsync::resumeMusicAsync() {
if (!initialized) return;
#ifdef __EMSCRIPTEN__
Mix_ResumeMusic();
#else
std::unique_lock<std::mutex> lock(mtx);
taskQueue.push([]() {
Mix_ResumeMusic();
});
cv.notify_one();
#endif
}
void AudioPlayerAsync::setMusicVolume(int volume) {
if (!initialized) return;
volume = std::max(0, std::min(128, volume));
#ifdef __EMSCRIPTEN__
Mix_VolumeMusic(volume);
#else
std::unique_lock<std::mutex> lock(mtx);
taskQueue.push([volume]() {
Mix_VolumeMusic(volume);
});
cv.notify_one();
#endif
}
void AudioPlayerAsync::setSoundVolume(int volume) {
if (!initialized) return;
volume = std::max(0, std::min(128, volume));
#ifdef __EMSCRIPTEN__
Mix_Volume(-1, volume);
#else
std::unique_lock<std::mutex> lock(mtx);
taskQueue.push([volume]() {
Mix_Volume(-1, volume); // все каналы
});
cv.notify_one();
#endif
}
#ifndef __EMSCRIPTEN__
void AudioPlayerAsync::workerThread() {
while (true) {
std::function<void()> task;
@ -165,3 +204,4 @@ void AudioPlayerAsync::workerThread() {
task();
}
}
#endif

View File

@ -5,11 +5,14 @@
#include <unordered_map>
#include <memory>
#include <mutex>
#include <SDL2/SDL.h>
#ifndef __EMSCRIPTEN__
#include <queue>
#include <thread>
#include <condition_variable>
#include <functional>
#include <SDL2/SDL.h>
#endif
class AudioPlayerAsync {
public:
@ -30,12 +33,14 @@ public:
void exit() { stop = true; }
private:
#ifndef __EMSCRIPTEN__
void workerThread();
std::thread worker;
std::mutex mtx;
std::condition_variable cv;
std::queue<std::function<void()>> taskQueue;
#endif
bool stop = false;
std::unordered_map<std::string, Mix_Chunk*> soundCache;

View File

@ -487,10 +487,10 @@ namespace ZL
if (event.type == SDL_KEYDOWN && event.key.repeat == 0) {
switch (event.key.keysym.sym) {
case SDLK_1:
if (audioPlayer) audioPlayer->playSoundAsync("resources/sounds/background.wav");
if (audioPlayer) audioPlayer->playSoundAsync("audio/background.wav");
break;
case SDLK_2:
if (audioPlayer) audioPlayer->playMusicAsync("resources/sounds/lullaby-music-vol20-186394--online-audio-convert.com.ogg");
if (audioPlayer) audioPlayer->playMusicAsync("audio/lullaby-music-vol20-186394--online-audio-convert.com.ogg");
break;
case SDLK_3:
if (audioPlayer) audioPlayer->stopMusicAsync();

View File

@ -12,7 +12,7 @@ namespace ZL {
void InteractiveObject::draw(Renderer& renderer) const {
if (!isActive || !texture) return;
/*
std::cout << "[DRAW] InteractiveObject::draw() called" << std::endl;
std::cout << "[DRAW] Object: " << name << std::endl;
std::cout << "[DRAW] Position: (" << position.x() << ", " << position.y() << ", " << position.z() << ")" << std::endl;
@ -22,7 +22,7 @@ namespace ZL {
std::cout << "[DRAW] First vertex: (" << mesh.data.PositionData[0].x() << ", "
<< mesh.data.PositionData[0].y() << ", " << mesh.data.PositionData[0].z() << ")" << std::endl;
}
*/
// Apply position transformation
renderer.PushMatrix();
renderer.TranslateMatrix(position);