added music
This commit is contained in:
1 parent
a1dda3fd50
commit
b5bcd3a812
9 files changed
+306
-92
No files matched your search
+131
-57
@@ -1,7 +1,5 @@
|
||||
#ifdef AUDIO
|
||||
|
||||
#include "AudioPlayerAsync.h"
|
||||
|
||||
#include "AudioPlayerAsync.h"
|
||||
#include <iostream>
|
||||
|
||||
AudioPlayerAsync::AudioPlayerAsync() : worker(&AudioPlayerAsync::workerThread, this) {}
|
||||
|
||||
@@ -11,62 +9,144 @@ AudioPlayerAsync::~AudioPlayerAsync() {
|
||||
stop = true;
|
||||
cv.notify_all();
|
||||
}
|
||||
worker.join();
|
||||
if (worker.joinable())
|
||||
worker.join();
|
||||
shutdown();
|
||||
}
|
||||
|
||||
void AudioPlayerAsync::stopAsync() {
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
taskQueue.push([this]() {
|
||||
//audioPlayerMutex.lock();
|
||||
audioPlayer->stop();
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
//audioPlayerMutex.unlock();
|
||||
});
|
||||
cv.notify_one();
|
||||
bool AudioPlayerAsync::init() {
|
||||
if (initialized) return true;
|
||||
|
||||
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
|
||||
std::cerr << "SDL_InitSubSystem(SDL_INIT_AUDIO) failed: " << SDL_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
|
||||
std::cerr << "Mix_OpenAudio failed: " << Mix_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
Mix_AllocateChannels(16);
|
||||
|
||||
initialized = true;
|
||||
std::cout << "AudioPlayerAsync initialized with SDL2_mixer" << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
void AudioPlayerAsync::resetAsync() {
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
taskQueue.push([this]() {
|
||||
//audioPlayerMutex.lock();
|
||||
audioPlayer.reset();
|
||||
audioPlayer = std::make_unique<AudioPlayer>();
|
||||
|
||||
//audioPlayerMutex.unlock();
|
||||
});
|
||||
cv.notify_one();
|
||||
}
|
||||
void AudioPlayerAsync::shutdown() {
|
||||
if (!initialized) return;
|
||||
|
||||
void AudioPlayerAsync::playSoundAsync(std::string soundName) {
|
||||
|
||||
soundNameMutex.lock();
|
||||
latestSoundName = soundName;
|
||||
soundNameMutex.unlock();
|
||||
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
taskQueue.push([this]() {
|
||||
//audioPlayerMutex.lock();
|
||||
if (audioPlayer) {
|
||||
audioPlayer->playSound(latestSoundName);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(soundCacheMutex);
|
||||
for (auto& pair : soundCache) {
|
||||
Mix_FreeChunk(pair.second);
|
||||
}
|
||||
soundCache.clear();
|
||||
}
|
||||
|
||||
Mix_CloseAudio();
|
||||
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
||||
initialized = false;
|
||||
std::cout << "AudioPlayerAsync shutdown" << std::endl;
|
||||
}
|
||||
|
||||
void AudioPlayerAsync::playSoundAsync(const std::string& filePath, int loops, int channel) {
|
||||
if (!initialized) {
|
||||
std::cerr << "AudioPlayerAsync not initialized" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
taskQueue.push([this, filePath, loops, channel]() {
|
||||
Mix_Chunk* sound = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> cacheLock(soundCacheMutex);
|
||||
auto it = soundCache.find(filePath);
|
||||
if (it != soundCache.end()) {
|
||||
sound = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
if (!sound) {
|
||||
sound = Mix_LoadWAV(filePath.c_str());
|
||||
if (!sound) {
|
||||
std::cerr << "Failed to load sound " << filePath << ": " << Mix_GetError() << std::endl;
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> cacheLock(soundCacheMutex);
|
||||
soundCache[filePath] = sound;
|
||||
}
|
||||
|
||||
int result = Mix_PlayChannel(channel, sound, loops);
|
||||
if (result == -1) {
|
||||
std::cerr << "Mix_PlayChannel failed: " << Mix_GetError() << std::endl;
|
||||
}
|
||||
//audioPlayerMutex.unlock();
|
||||
});
|
||||
cv.notify_one();
|
||||
}
|
||||
|
||||
void AudioPlayerAsync::playMusicAsync(std::string musicName) {
|
||||
|
||||
musicNameMutex.lock();
|
||||
latestMusicName = musicName;
|
||||
musicNameMutex.unlock();
|
||||
void AudioPlayerAsync::playMusicAsync(const std::string& filePath, int loops) {
|
||||
if (!initialized) return;
|
||||
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
taskQueue.push([this]() {
|
||||
//audioPlayerMutex.lock();
|
||||
if (audioPlayer) {
|
||||
audioPlayer->playMusic(latestMusicName);
|
||||
taskQueue.push([this, filePath, loops]() {
|
||||
Mix_Music* music = Mix_LoadMUS(filePath.c_str());
|
||||
if (!music) {
|
||||
std::cerr << "Failed to load music " << filePath << ": " << Mix_GetError() << std::endl;
|
||||
return;
|
||||
}
|
||||
//audioPlayerMutex.unlock();
|
||||
if (Mix_PlayMusic(music, loops) == -1) {
|
||||
std::cerr << "Mix_PlayMusic failed: " << Mix_GetError() << std::endl;
|
||||
Mix_FreeMusic(music);
|
||||
}
|
||||
});
|
||||
cv.notify_one();
|
||||
}
|
||||
|
||||
void AudioPlayerAsync::stopMusicAsync() {
|
||||
if (!initialized) return;
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
taskQueue.push([]() {
|
||||
Mix_HaltMusic();
|
||||
});
|
||||
cv.notify_one();
|
||||
}
|
||||
|
||||
void AudioPlayerAsync::pauseMusicAsync() {
|
||||
if (!initialized) return;
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
taskQueue.push([]() {
|
||||
Mix_PauseMusic();
|
||||
});
|
||||
cv.notify_one();
|
||||
}
|
||||
|
||||
void AudioPlayerAsync::resumeMusicAsync() {
|
||||
if (!initialized) return;
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
taskQueue.push([]() {
|
||||
Mix_ResumeMusic();
|
||||
});
|
||||
cv.notify_one();
|
||||
}
|
||||
|
||||
void AudioPlayerAsync::setMusicVolume(int volume) {
|
||||
if (!initialized) return;
|
||||
volume = std::max(0, std::min(128, volume));
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
taskQueue.push([volume]() {
|
||||
Mix_VolumeMusic(volume);
|
||||
});
|
||||
cv.notify_one();
|
||||
}
|
||||
|
||||
void AudioPlayerAsync::setSoundVolume(int volume) {
|
||||
if (!initialized) return;
|
||||
volume = std::max(0, std::min(128, volume));
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
taskQueue.push([volume]() {
|
||||
Mix_Volume(-1, volume); // все каналы
|
||||
});
|
||||
cv.notify_one();
|
||||
}
|
||||
@@ -76,18 +156,12 @@ void AudioPlayerAsync::workerThread() {
|
||||
std::function<void()> task;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
cv.wait(lock, [this]() { return !taskQueue.empty() || stop; });
|
||||
|
||||
if (stop && taskQueue.empty()) {
|
||||
cv.wait(lock, [this] { return !taskQueue.empty() || stop; });
|
||||
if (stop && taskQueue.empty())
|
||||
break;
|
||||
}
|
||||
|
||||
task = taskQueue.front();
|
||||
task = std::move(taskQueue.front());
|
||||
taskQueue.pop();
|
||||
}
|
||||
|
||||
task();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
Reference in new issue
Block a user