Working on audio in web part 1
This commit is contained in:
1 parent
3778a603f2
commit
b6e0422d71
9 files changed
+65
-13
No files matched your search
@@ -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;
|
||||
@@ -164,4 +203,5 @@ void AudioPlayerAsync::workerThread() {
|
||||
}
|
||||
task();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user