75 lines
1.9 KiB
C++
75 lines
1.9 KiB
C++
#pragma once
|
|
#ifdef __ANDROID__
|
|
#include <SDL_mixer.h>
|
|
#include <SDL.h>
|
|
#else
|
|
#include <SDL2/SDL_mixer.h>
|
|
#include <SDL2/SDL.h>
|
|
#endif
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <vector>
|
|
|
|
#ifndef __EMSCRIPTEN__
|
|
#include <queue>
|
|
#include <thread>
|
|
#include <condition_variable>
|
|
#include <functional>
|
|
#endif
|
|
|
|
class AudioPlayerAsync {
|
|
public:
|
|
AudioPlayerAsync();
|
|
~AudioPlayerAsync();
|
|
|
|
bool init();
|
|
void shutdown();
|
|
|
|
void playSoundAsync(const std::string& filePath, int loops = 0, int channel = -1);
|
|
void playMusicAsync(const std::string& filePath, int loops = -1);
|
|
void crossFadeMusicAsync(const std::string& filePath, int fadeDurationMs = 1000, int loops = -1);
|
|
void crossFadeMusicFromPositionAsync(const std::string& filePath, int fadeDurationMs = 1000, int loops = -1);
|
|
void stopMusicAsync();
|
|
void pauseMusicAsync();
|
|
void resumeMusicAsync();
|
|
void setMusicVolume(int volume); // 0..128
|
|
void setSoundVolume(int volume); // 0..128
|
|
int getMusicVolume() const { return musicVolume_; }
|
|
int getSoundVolume() const { return soundVolume_; }
|
|
|
|
void setMusicEnabled(bool enabled);
|
|
void setSoundEnabled(bool enabled);
|
|
bool isMusicEnabled() const { return musicEnabled_; }
|
|
bool isSoundEnabled() const { return soundEnabled_; }
|
|
|
|
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;
|
|
std::mutex soundCacheMutex;
|
|
|
|
std::string currentTrack;
|
|
|
|
Mix_Music* currentMusic_ = nullptr;
|
|
std::vector<char> currentMusicData_;
|
|
|
|
int musicVolume_ = 128;
|
|
int soundVolume_ = 128;
|
|
bool musicEnabled_ = true;
|
|
bool soundEnabled_ = true;
|
|
|
|
bool initialized = false;
|
|
};
|