audio::play
This commit is contained in:
+74
-9
@@ -6,35 +6,100 @@
|
||||
#include "NoAudio.h"
|
||||
|
||||
namespace audio {
|
||||
extern Backend* backend;
|
||||
static speakerid_t nextId = 1;
|
||||
static Backend* backend;
|
||||
static std::unordered_map<speakerid_t, std::unique_ptr<Speaker>> speakers;
|
||||
}
|
||||
|
||||
audio::Backend* audio::backend = nullptr;
|
||||
using namespace audio;
|
||||
|
||||
void audio::initialize(bool enabled) {
|
||||
if (enabled) {
|
||||
audio::backend = ALAudio::create();
|
||||
backend = ALAudio::create();
|
||||
}
|
||||
if (audio::backend == nullptr) {
|
||||
if (backend == nullptr) {
|
||||
std::cerr << "could not to initialize audio" << std::endl;
|
||||
audio::backend = NoAudio::create();
|
||||
backend = NoAudio::create();
|
||||
}
|
||||
}
|
||||
|
||||
Sound* audio::createSound(std::shared_ptr<PCM> pcm, bool keepPCM) {
|
||||
return backend->createSound(pcm, keepPCM);
|
||||
}
|
||||
|
||||
void audio::setListener(
|
||||
glm::vec3 position,
|
||||
glm::vec3 velocity,
|
||||
glm::vec3 lookAt,
|
||||
glm::vec3 up
|
||||
) {
|
||||
audio::backend->setListener(position, velocity, lookAt, up);
|
||||
backend->setListener(position, velocity, lookAt, up);
|
||||
}
|
||||
|
||||
void remove_lower_priority_speaker(int priority) {
|
||||
for (auto it = speakers.begin(); it != speakers.end();) {
|
||||
if (it->second->getPriority() < priority && it->second->isPaused()) {
|
||||
speakers.erase(it);
|
||||
return;
|
||||
}
|
||||
it++;
|
||||
}
|
||||
for (auto it = speakers.begin(); it != speakers.end();) {
|
||||
if (it->second->getPriority() < priority) {
|
||||
speakers.erase(it);
|
||||
return;
|
||||
}
|
||||
it++;
|
||||
}
|
||||
}
|
||||
|
||||
speakerid_t audio::play(
|
||||
Sound* sound,
|
||||
glm::vec3 position,
|
||||
float volume,
|
||||
float pitch,
|
||||
bool loop,
|
||||
int priority
|
||||
) {
|
||||
Speaker* speaker = sound->newInstance(priority);
|
||||
if (speaker == nullptr) {
|
||||
remove_lower_priority_speaker(priority);
|
||||
speaker = sound->newInstance(priority);
|
||||
}
|
||||
if (speaker == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
speakerid_t id = nextId++;
|
||||
speakers[id].reset(speaker);
|
||||
speaker->setPosition(position);
|
||||
speaker->setVolume(volume);
|
||||
speaker->setPitch(pitch);
|
||||
speaker->setLoop(loop);
|
||||
speaker->play();
|
||||
return id;
|
||||
}
|
||||
|
||||
Speaker* audio::get(speakerid_t id) {
|
||||
auto found = speakers.find(id);
|
||||
if (found == speakers.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void audio::update(double delta) {
|
||||
audio::backend->update(delta);
|
||||
backend->update(delta);
|
||||
|
||||
for (auto it = speakers.begin(); it != speakers.end();) {
|
||||
if (it->second->isStopped()) {
|
||||
speakers.erase(it);
|
||||
} else {
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void audio::close() {
|
||||
delete audio::backend;
|
||||
audio::backend = nullptr;
|
||||
delete backend;
|
||||
backend = nullptr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user