format: reformat project

Signed-off-by: Vyacheslav Ivanov <islavaivanov76@gmail.com>
This commit is contained in:
Vyacheslav Ivanov
2024-08-03 19:53:48 +03:00
committed by Pugemon
parent 736cd175d5
commit bbf33e8e4d
202 changed files with 7389 additions and 5609 deletions
+77 -58
View File
@@ -1,18 +1,19 @@
#include "ALAudio.hpp"
#include "alutil.hpp"
#include "../../debug/Logger.hpp"
#include <string>
#include <utility>
#include "../../debug/Logger.hpp"
#include "alutil.hpp"
static debug::Logger logger("al-audio");
using namespace audio;
ALSound::ALSound(ALAudio* al, uint buffer, const std::shared_ptr<PCM>& pcm, bool keepPCM)
: al(al), buffer(buffer)
{
ALSound::ALSound(
ALAudio* al, uint buffer, const std::shared_ptr<PCM>& pcm, bool keepPCM
)
: al(al), buffer(buffer) {
duration = pcm->getDuration();
if (keepPCM) {
this->pcm = pcm;
@@ -36,8 +37,10 @@ std::unique_ptr<Speaker> ALSound::newInstance(int priority, int channel) const {
return speaker;
}
ALStream::ALStream(ALAudio* al, std::shared_ptr<PCMStream> source, bool keepSource)
: al(al), source(std::move(source)), keepSource(keepSource) {
ALStream::ALStream(
ALAudio* al, std::shared_ptr<PCMStream> source, bool keepSource
)
: al(al), source(std::move(source)), keepSource(keepSource) {
}
ALStream::~ALStream() {
@@ -60,10 +63,12 @@ std::shared_ptr<PCMStream> ALStream::getSource() const {
bool ALStream::preloadBuffer(uint buffer, bool loop) {
size_t read = source->readFully(this->buffer, BUFFER_SIZE, loop);
if (!read)
return false;
ALenum format = AL::to_al_format(source->getChannels(), source->getBitsPerSample());
AL_CHECK(alBufferData(buffer, format, this->buffer, read, source->getSampleRate()));
if (!read) return false;
ALenum format =
AL::to_al_format(source->getChannels(), source->getBitsPerSample());
AL_CHECK(alBufferData(
buffer, format, this->buffer, read, source->getSampleRate()
));
return true;
}
@@ -83,7 +88,6 @@ std::unique_ptr<Speaker> ALStream::createSpeaker(bool loop, int channel) {
return std::make_unique<ALSpeaker>(al, source, PRIORITY_HIGH, channel);
}
void ALStream::bindSpeaker(speakerid_t speaker) {
auto sp = audio::get_speaker(this->speaker);
if (sp) {
@@ -110,7 +114,7 @@ void ALStream::unqueueBuffers(uint alsource) {
AL_CHECK(alSourceUnqueueBuffers(alsource, 1, &buffer));
unusedBuffers.push(buffer);
uint bps = source->getBitsPerSample()/8;
uint bps = source->getBitsPerSample() / 8;
uint channels = source->getChannels();
ALint bufferSize;
@@ -151,10 +155,10 @@ void ALStream::update(double delta) {
}
uint alsource = alspeaker->source;
unqueueBuffers(alsource);
uint preloaded = enqueueBuffers(alsource);
if (speaker->isStopped() && !alspeaker->stopped) {
if (preloaded) {
speaker->play();
@@ -166,10 +170,12 @@ void ALStream::update(double delta) {
duration_t ALStream::getTime() const {
uint total = totalPlayedSamples;
auto alspeaker = dynamic_cast<ALSpeaker*>(audio::get_speaker(this->speaker));
auto alspeaker =
dynamic_cast<ALSpeaker*>(audio::get_speaker(this->speaker));
if (alspeaker) {
uint alsource = alspeaker->source;
total += static_cast<duration_t>(AL::getSourcef(alsource, AL_SAMPLE_OFFSET));
total +=
static_cast<duration_t>(AL::getSourcef(alsource, AL_SAMPLE_OFFSET));
if (source->isSeekable()) {
total %= source->getTotalSamples();
}
@@ -178,11 +184,11 @@ duration_t ALStream::getTime() const {
}
void ALStream::setTime(duration_t time) {
if (!source->isSeekable())
return;
if (!source->isSeekable()) return;
uint sample = time * source->getSampleRate();
source->seek(sample);
auto alspeaker = dynamic_cast<ALSpeaker*>(audio::get_speaker(this->speaker));
auto alspeaker =
dynamic_cast<ALSpeaker*>(audio::get_speaker(this->speaker));
if (alspeaker) {
bool paused = alspeaker->isPaused();
AL_CHECK(alSourceStop(alspeaker->source));
@@ -198,8 +204,8 @@ void ALStream::setTime(duration_t time) {
}
}
ALSpeaker::ALSpeaker(ALAudio* al, uint source, int priority, int channel)
: al(al), priority(priority), channel(channel), source(source) {
ALSpeaker::ALSpeaker(ALAudio* al, uint source, int priority, int channel)
: al(al), priority(priority), channel(channel), source(source) {
}
ALSpeaker::~ALSpeaker() {
@@ -209,11 +215,10 @@ ALSpeaker::~ALSpeaker() {
}
void ALSpeaker::update(const Channel* channel) {
if (source == 0)
return;
if (source == 0) return;
float gain = this->volume * channel->getVolume();
AL_CHECK(alSourcef(source, AL_GAIN, gain));
if (!paused) {
if (isPaused() && !channel->isPaused()) {
play();
@@ -230,9 +235,12 @@ int ALSpeaker::getChannel() const {
State ALSpeaker::getState() const {
int state = AL::getSourcei(source, AL_SOURCE_STATE, AL_STOPPED);
switch (state) {
case AL_PLAYING: return State::playing;
case AL_PAUSED: return State::paused;
default: return State::stopped;
case AL_PLAYING:
return State::playing;
case AL_PAUSED:
return State::paused;
default:
return State::stopped;
}
}
@@ -264,7 +272,11 @@ void ALSpeaker::play() {
paused = false;
stopped = false;
auto channel = get_channel(this->channel);
AL_CHECK(alSourcef(source, AL_GAIN, volume * channel->getVolume() * get_channel(0)->getVolume()));
AL_CHECK(alSourcef(
source,
AL_GAIN,
volume * channel->getVolume() * get_channel(0)->getVolume()
));
AL_CHECK(alSourcePlay(source));
}
@@ -325,7 +337,9 @@ glm::vec3 ALSpeaker::getVelocity() const {
}
void ALSpeaker::setRelative(bool relative) {
AL_CHECK(alSourcei(source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE));
AL_CHECK(
alSourcei(source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE)
);
}
bool ALSpeaker::isRelative() const {
@@ -336,19 +350,17 @@ int ALSpeaker::getPriority() const {
return priority;
}
ALAudio::ALAudio(ALCdevice* device, ALCcontext* context)
: device(device), context(context)
{
: device(device), context(context) {
ALCint size;
alcGetIntegerv(device, ALC_ATTRIBUTES_SIZE, 1, &size);
std::vector<ALCint> attrs(size);
alcGetIntegerv(device, ALC_ALL_ATTRIBUTES, size, &attrs[0]);
for (size_t i = 0; i < attrs.size(); ++i){
if (attrs[i] == ALC_MONO_SOURCES) {
logger.info() << "max mono sources: " << attrs[i+1];
maxSources = attrs[i+1];
}
for (size_t i = 0; i < attrs.size(); ++i) {
if (attrs[i] == ALC_MONO_SOURCES) {
logger.info() << "max mono sources: " << attrs[i + 1];
maxSources = attrs[i + 1];
}
}
auto devices = getAvailableDevices();
logger.info() << "devices:";
@@ -366,7 +378,7 @@ ALAudio::~ALAudio() {
AL_CHECK(alDeleteSources(1, &source));
}
for (uint buffer : allbuffers){
for (uint buffer : allbuffers) {
AL_CHECK(alDeleteBuffers(1, &buffer));
}
@@ -379,23 +391,28 @@ ALAudio::~ALAudio() {
context = nullptr;
}
std::unique_ptr<Sound> ALAudio::createSound(std::shared_ptr<PCM> pcm, bool keepPCM) {
std::unique_ptr<Sound> ALAudio::createSound(
std::shared_ptr<PCM> pcm, bool keepPCM
) {
auto format = AL::to_al_format(pcm->channels, pcm->bitsPerSample);
uint buffer = getFreeBuffer();
AL_CHECK(alBufferData(buffer, format, pcm->data.data(), pcm->data.size(), pcm->sampleRate));
AL_CHECK(alBufferData(
buffer, format, pcm->data.data(), pcm->data.size(), pcm->sampleRate
));
return std::make_unique<ALSound>(this, buffer, pcm, keepPCM);
}
std::unique_ptr<Stream> ALAudio::openStream(std::shared_ptr<PCMStream> stream, bool keepSource) {
std::unique_ptr<Stream> ALAudio::openStream(
std::shared_ptr<PCMStream> stream, bool keepSource
) {
return std::make_unique<ALStream>(this, stream, keepSource);
}
std::unique_ptr<ALAudio> ALAudio::create() {
ALCdevice* device = alcOpenDevice(nullptr);
if (device == nullptr)
return nullptr;
if (device == nullptr) return nullptr;
ALCcontext* context = alcCreateContext(device, nullptr);
if (!alcMakeContextCurrent(context)){
if (!alcMakeContextCurrent(context)) {
alcCloseDevice(device);
return nullptr;
}
@@ -404,14 +421,15 @@ std::unique_ptr<ALAudio> ALAudio::create() {
return std::make_unique<ALAudio>(device, context);
}
uint ALAudio::getFreeSource(){
if (!freesources.empty()){
uint ALAudio::getFreeSource() {
if (!freesources.empty()) {
uint source = freesources.back();
freesources.pop_back();
return source;
}
if (allsources.size() == maxSources){
logger.error() << "attempted to create new source, but limit is " << maxSources;
if (allsources.size() == maxSources) {
logger.error() << "attempted to create new source, but limit is "
<< maxSources;
return 0;
}
ALuint id;
@@ -423,8 +441,8 @@ uint ALAudio::getFreeSource(){
return id;
}
uint ALAudio::getFreeBuffer(){
if (!freebuffers.empty()){
uint ALAudio::getFreeBuffer() {
if (!freebuffers.empty()) {
uint buffer = freebuffers.back();
freebuffers.pop_back();
return buffer;
@@ -439,11 +457,11 @@ uint ALAudio::getFreeBuffer(){
return id;
}
void ALAudio::freeSource(uint source){
void ALAudio::freeSource(uint source) {
freesources.push_back(source);
}
void ALAudio::freeBuffer(uint buffer){
void ALAudio::freeBuffer(uint buffer) {
freebuffers.push_back(buffer);
}
@@ -460,14 +478,15 @@ std::vector<std::string> ALAudio::getAvailableDevices() const {
do {
devicesVec.emplace_back(ptr);
ptr += devicesVec.back().size() + 1;
}
while (ptr[0]);
} while (ptr[0]);
return devicesVec;
}
void ALAudio::setListener(glm::vec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up){
ALfloat listenerOri[] = { at.x, at.y, at.z, up.x, up.y, up.z };
void ALAudio::setListener(
glm::vec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up
) {
ALfloat listenerOri[] = {at.x, at.y, at.z, up.x, up.y, up.z};
AL_CHECK(alListener3f(AL_POSITION, position.x, position.y, position.z));
AL_CHECK(alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z));
+30 -18
View File
@@ -1,14 +1,14 @@
#ifndef SRC_AUDIO_AUDIO_HPP_
#define SRC_AUDIO_AUDIO_HPP_
#include "../audio.hpp"
#include "../../typedefs.hpp"
#include <queue>
#include <vector>
#include <string>
#include <glm/glm.hpp>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#include "../../typedefs.hpp"
#include "../audio.hpp"
#ifdef __APPLE__
#include <OpenAL/al.h>
@@ -29,7 +29,12 @@ namespace audio {
std::shared_ptr<PCM> pcm;
duration_t duration;
public:
ALSound(ALAudio* al, uint buffer, const std::shared_ptr<PCM>& pcm, bool keepPCM);
ALSound(
ALAudio* al,
uint buffer,
const std::shared_ptr<PCM>& pcm,
bool keepPCM
);
~ALSound();
duration_t getDuration() const override {
@@ -40,12 +45,13 @@ namespace audio {
return pcm;
}
std::unique_ptr<Speaker> newInstance(int priority, int channel) const override;
std::unique_ptr<Speaker> newInstance(int priority, int channel)
const override;
};
class ALStream : public Stream {
static inline constexpr size_t BUFFER_SIZE = 44100;
ALAudio* al;
std::shared_ptr<PCMStream> source;
std::queue<uint> unusedBuffers;
@@ -60,7 +66,9 @@ namespace audio {
public:
size_t totalPlayedSamples = 0;
ALStream(ALAudio* al, std::shared_ptr<PCMStream> source, bool keepSource);
ALStream(
ALAudio* al, std::shared_ptr<PCMStream> source, bool keepSource
);
~ALStream();
std::shared_ptr<PCMStream> getSource() const override;
@@ -68,11 +76,11 @@ namespace audio {
std::unique_ptr<Speaker> createSpeaker(bool loop, int channel) override;
speakerid_t getSpeaker() const override;
void update(double delta) override;
duration_t getTime() const override;
void setTime(duration_t time) override;
static inline constexpr uint STREAM_BUFFERS = 3;
duration_t getTime() const override;
void setTime(duration_t time) override;
static inline constexpr uint STREAM_BUFFERS = 3;
};
/// @brief AL source adapter
@@ -147,8 +155,12 @@ namespace audio {
std::vector<std::string> getAvailableDevices() const;
std::unique_ptr<Sound> createSound(std::shared_ptr<PCM> pcm, bool keepPCM) override;
std::unique_ptr<Stream> openStream(std::shared_ptr<PCMStream> stream, bool keepSource) override;
std::unique_ptr<Sound> createSound(
std::shared_ptr<PCM> pcm, bool keepPCM
) override;
std::unique_ptr<Stream> openStream(
std::shared_ptr<PCMStream> stream, bool keepSource
) override;
void setListener(
glm::vec3 position,
@@ -158,7 +170,7 @@ namespace audio {
) override;
void update(double delta) override;
bool isDummy() const override {
return false;
}
@@ -167,4 +179,4 @@ namespace audio {
};
}
#endif // SRC_AUDIO_AUDIO_HPP_
#endif // SRC_AUDIO_AUDIO_HPP_
+29 -23
View File
@@ -1,12 +1,12 @@
#include "alutil.hpp"
#include "../../debug/Logger.hpp"
#include <fstream>
#include <cstring>
#include <fstream>
#include <memory>
#include <type_traits>
#include "../../debug/Logger.hpp"
#ifdef __APPLE__
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
@@ -17,28 +17,34 @@
static debug::Logger logger("open-al");
bool AL::check_errors(const std::string& filename, const std::uint_fast32_t line){
bool AL::check_errors(
const std::string& filename, const std::uint_fast32_t line
) {
ALenum error = alGetError();
if(error != AL_NO_ERROR){
if (error != AL_NO_ERROR) {
logger.error() << filename << ": " << line;
switch(error){
case AL_INVALID_NAME:
logger.error() << "a bad name (ID) was passed to an OpenAL function";
break;
case AL_INVALID_ENUM:
logger.error() << "an invalid enum value was passed to an OpenAL function";
break;
case AL_INVALID_VALUE:
logger.error() << "an invalid value was passed to an OpenAL function";
break;
case AL_INVALID_OPERATION:
logger.error() << "the requested operation is not valid";
break;
case AL_OUT_OF_MEMORY:
logger.error() << "the requested operation resulted in OpenAL running out of memory";
break;
default:
logger.error() << "UNKNOWN AL ERROR: " << error;
switch (error) {
case AL_INVALID_NAME:
logger.error()
<< "a bad name (ID) was passed to an OpenAL function";
break;
case AL_INVALID_ENUM:
logger.error()
<< "an invalid enum value was passed to an OpenAL function";
break;
case AL_INVALID_VALUE:
logger.error()
<< "an invalid value was passed to an OpenAL function";
break;
case AL_INVALID_OPERATION:
logger.error() << "the requested operation is not valid";
break;
case AL_OUT_OF_MEMORY:
logger.error() << "the requested operation resulted in OpenAL "
"running out of memory";
break;
default:
logger.error() << "UNKNOWN AL ERROR: " << error;
}
return false;
}
+33 -30
View File
@@ -1,11 +1,11 @@
#ifndef AUDIO_AUDIOUTIL_HPP_
#define AUDIO_AUDIOUTIL_HPP_
#include "../../typedefs.hpp"
#include <cstdint>
#include <string>
#include <type_traits>
#include <cstdint>
#include "../../typedefs.hpp"
#ifdef __APPLE__
#include <OpenAL/al.h>
@@ -15,22 +15,25 @@
#include <glm/glm.hpp>
#define AL_CHECK(STATEMENT) STATEMENT; AL::check_errors(__FILE__, __LINE__)
#define AL_CHECK(STATEMENT) \
STATEMENT; \
AL::check_errors(__FILE__, __LINE__)
#define AL_GET_ERROR() AL::check_errors(__FILE__, __LINE__)
namespace AL {
/// @return true if no errors
bool check_errors(const std::string& filename, const std::uint_fast32_t line);
namespace AL {
/// @return true if no errors
bool check_errors(
const std::string& filename, const std::uint_fast32_t line
);
/// @brief alGetSourcef wrapper
/// @param source target source
/// @param field enum value
/// @param def default value will be returned in case of error
/// @return field value or default
inline float getSourcef(uint source, ALenum field, float def=0.0f) {
inline float getSourcef(uint source, ALenum field, float def = 0.0f) {
float value = def;
if (source == 0)
return def;
if (source == 0) return def;
AL_CHECK(alGetSourcef(source, field, &value));
return value;
}
@@ -40,10 +43,11 @@ namespace AL {
/// @param field enum value
/// @param def default value will be returned in case of error
/// @return field value or default
inline glm::vec3 getSource3f(uint source, ALenum field, glm::vec3 def={}) {
inline glm::vec3 getSource3f(
uint source, ALenum field, glm::vec3 def = {}
) {
glm::vec3 value = def;
if (source == 0)
return def;
if (source == 0) return def;
AL_CHECK(alGetSource3f(source, field, &value.x, &value.y, &value.z));
return value;
}
@@ -53,32 +57,31 @@ namespace AL {
/// @param field enum value
/// @param def default value will be returned in case of error
/// @return field value or default
inline float getSourcei(uint source, ALenum field, int def=0) {
inline float getSourcei(uint source, ALenum field, int def = 0) {
int value = def;
if (source == 0)
return def;
if (source == 0) return def;
AL_CHECK(alGetSourcei(source, field, &value));
return value;
}
static inline ALenum to_al_format(short channels, short bitsPerSample){
static inline ALenum to_al_format(short channels, short bitsPerSample) {
bool stereo = (channels > 1);
switch (bitsPerSample) {
case 16:
if (stereo)
return AL_FORMAT_STEREO16;
else
return AL_FORMAT_MONO16;
case 8:
if (stereo)
return AL_FORMAT_STEREO8;
else
return AL_FORMAT_MONO8;
default:
return -1;
case 16:
if (stereo)
return AL_FORMAT_STEREO16;
else
return AL_FORMAT_MONO16;
case 8:
if (stereo)
return AL_FORMAT_STEREO8;
else
return AL_FORMAT_MONO8;
default:
return -1;
}
}
}
#endif // AUDIO_AUDIOUTIL_HPP_
#endif // AUDIO_AUDIOUTIL_HPP_
+6 -2
View File
@@ -9,11 +9,15 @@ NoSound::NoSound(const std::shared_ptr<PCM>& pcm, bool keepPCM) {
}
}
std::unique_ptr<Sound> NoAudio::createSound(std::shared_ptr<PCM> pcm, bool keepPCM) {
std::unique_ptr<Sound> NoAudio::createSound(
std::shared_ptr<PCM> pcm, bool keepPCM
) {
return std::make_unique<NoSound>(pcm, keepPCM);
}
std::unique_ptr<Stream> NoAudio::openStream(std::shared_ptr<PCMStream> stream, bool keepSource) {
std::unique_ptr<Stream> NoAudio::openStream(
std::shared_ptr<PCMStream> stream, bool keepSource
) {
return std::make_unique<NoStream>(stream, keepSource);
}
+20 -13
View File
@@ -9,7 +9,8 @@ namespace audio {
duration_t duration;
public:
NoSound(const std::shared_ptr<PCM>& pcm, bool keepPCM);
~NoSound() {}
~NoSound() {
}
duration_t getDuration() const override {
return duration;
@@ -19,7 +20,8 @@ namespace audio {
return pcm;
}
std::unique_ptr<Speaker> newInstance(int priority, int channel) const override {
std::unique_ptr<Speaker> newInstance(int priority, int channel)
const override {
return nullptr;
}
};
@@ -42,7 +44,8 @@ namespace audio {
void bindSpeaker(speakerid_t speaker) override {
}
std::unique_ptr<Speaker> createSpeaker(bool loop, int channel) override {
std::unique_ptr<Speaker> createSpeaker(bool loop, int channel)
override {
return nullptr;
}
@@ -63,19 +66,23 @@ namespace audio {
class NoAudio : public Backend {
public:
~NoAudio() {}
~NoAudio() {
}
std::unique_ptr<Sound> createSound(std::shared_ptr<PCM> pcm, bool keepPCM) override;
std::unique_ptr<Stream> openStream(std::shared_ptr<PCMStream> stream, bool keepSource) override;
std::unique_ptr<Sound> createSound(
std::shared_ptr<PCM> pcm, bool keepPCM
) override;
std::unique_ptr<Stream> openStream(
std::shared_ptr<PCMStream> stream, bool keepSource
) override;
void setListener(
glm::vec3 position,
glm::vec3 velocity,
glm::vec3 at,
glm::vec3 up
) override {}
glm::vec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up
) override {
}
void update(double delta) override {}
void update(double delta) override {
}
bool isDummy() const override {
return true;
@@ -85,4 +92,4 @@ namespace audio {
};
}
#endif // AUDIO_NOAUDIO_HPP_
#endif // AUDIO_NOAUDIO_HPP_
+33 -29
View File
@@ -1,15 +1,14 @@
#include "audio.hpp"
#include "NoAudio.hpp"
#include "AL/ALAudio.hpp"
#include "../coders/wav.hpp"
#include "../coders/ogg.hpp"
#include <iostream>
#include <stdexcept>
#include <utility>
#include "../coders/ogg.hpp"
#include "../coders/wav.hpp"
#include "AL/ALAudio.hpp"
#include "NoAudio.hpp"
namespace audio {
static speakerid_t nextId = 1;
static Backend* backend;
@@ -85,12 +84,12 @@ class PCMVoidSource : public PCMStream {
bool seekable;
bool closed = false;
public:
PCMVoidSource(size_t totalSamples, uint sampleRate, bool seekable)
: totalSamples(totalSamples),
remain(totalSamples),
sampleRate(sampleRate),
seekable(seekable)
{}
PCMVoidSource(size_t totalSamples, uint sampleRate, bool seekable)
: totalSamples(totalSamples),
remain(totalSamples),
sampleRate(sampleRate),
seekable(seekable) {
}
size_t read(char*, size_t bufferSize) override {
if (closed) {
@@ -117,7 +116,7 @@ public:
}
duration_t getTotalDuration() const override {
return static_cast<duration_t>(totalSamples) /
return static_cast<duration_t>(totalSamples) /
static_cast<duration_t>(sampleRate);
}
@@ -159,7 +158,7 @@ void audio::initialize(bool enabled) {
std::unique_ptr<PCM> audio::load_PCM(const fs::path& file, bool headerOnly) {
if (!fs::exists(file)) {
throw std::runtime_error("file not found '"+file.u8string()+"'");
throw std::runtime_error("file not found '" + file.u8string() + "'");
}
std::string ext = file.extension().u8string();
if (ext == ".wav" || ext == ".WAV") {
@@ -171,11 +170,15 @@ std::unique_ptr<PCM> audio::load_PCM(const fs::path& file, bool headerOnly) {
}
std::unique_ptr<Sound> audio::load_sound(const fs::path& file, bool keepPCM) {
std::shared_ptr<PCM> pcm(load_PCM(file, !keepPCM && backend->isDummy()).release());
std::shared_ptr<PCM> pcm(
load_PCM(file, !keepPCM && backend->isDummy()).release()
);
return create_sound(pcm, keepPCM);
}
std::unique_ptr<Sound> audio::create_sound(std::shared_ptr<PCM> pcm, bool keepPCM) {
std::unique_ptr<Sound> audio::create_sound(
std::shared_ptr<PCM> pcm, bool keepPCM
) {
return backend->createSound(std::move(pcm), keepPCM);
}
@@ -189,31 +192,32 @@ std::unique_ptr<PCMStream> audio::open_PCM_stream(const fs::path& file) {
throw std::runtime_error("unsupported audio stream format");
}
std::unique_ptr<Stream> audio::open_stream(const fs::path& file, bool keepSource) {
std::unique_ptr<Stream> audio::open_stream(
const fs::path& file, bool keepSource
) {
if (!keepSource && backend->isDummy()) {
auto header = load_PCM(file, true);
// using void source sized as audio instead of actual audio file
return open_stream(
std::make_shared<PCMVoidSource>(header->totalSamples, header->sampleRate, header->seekable),
std::make_shared<PCMVoidSource>(
header->totalSamples, header->sampleRate, header->seekable
),
keepSource
);
}
return open_stream(
std::shared_ptr<PCMStream>(open_PCM_stream(file)),
keepSource
std::shared_ptr<PCMStream>(open_PCM_stream(file)), keepSource
);
}
std::unique_ptr<Stream> audio::open_stream(std::shared_ptr<PCMStream> stream, bool keepSource) {
std::unique_ptr<Stream> audio::open_stream(
std::shared_ptr<PCMStream> stream, bool keepSource
) {
return backend->openStream(std::move(stream), keepSource);
}
void audio::set_listener(
glm::vec3 position,
glm::vec3 velocity,
glm::vec3 lookAt,
glm::vec3 up
glm::vec3 position, glm::vec3 velocity, glm::vec3 lookAt, glm::vec3 up
) {
backend->setListener(position, velocity, lookAt, up);
}
@@ -317,7 +321,7 @@ speakerid_t audio::play_stream(
bool loop,
int channel
) {
std::shared_ptr<Stream> stream (open_stream(file, false));
std::shared_ptr<Stream> stream(open_stream(file, false));
return play(stream, position, relative, volume, pitch, loop, channel);
}
@@ -335,14 +339,14 @@ int audio::create_channel(const std::string& name) {
return index;
}
channels.emplace_back(std::make_unique<Channel>(name));
return channels.size()-1;
return channels.size() - 1;
}
int audio::get_channel_index(const std::string& name) {
int index = 0;
for (auto& channel : channels) {
if (channel->getName() == name) {
return index;
return index;
}
index++;
}
+76 -68
View File
@@ -1,12 +1,12 @@
#ifndef AUDIO_AUDIO_HPP_
#define AUDIO_AUDIO_HPP_
#include "../typedefs.hpp"
#include <vector>
#include <memory>
#include <filesystem>
#include <glm/glm.hpp>
#include <memory>
#include <vector>
#include "../typedefs.hpp"
namespace fs = std::filesystem;
@@ -29,14 +29,11 @@ namespace audio {
class Speaker;
/// @brief Audio speaker states
enum class State {
playing,
paused,
stopped
};
enum class State { playing, paused, stopped };
/// @brief Mixer channel controls speakers volume and effects
/// There is main channel 'master' and sub-channels like 'regular', 'music', 'ambient'...
/// There is main channel 'master' and sub-channels like 'regular', 'music',
/// 'ambient'...
class Channel {
/// @brief Channel name
std::string name;
@@ -46,7 +43,7 @@ namespace audio {
public:
Channel(std::string name);
/// @brief Get channel volume
/// @brief Get channel volume
float getVolume() const;
/// @brief Set channel volume
@@ -57,8 +54,7 @@ namespace audio {
const std::string& getName() const;
inline void setPaused(bool flag) {
if (flag == paused)
return;
if (flag == paused) return;
if (flag) {
pause();
} else {
@@ -92,24 +88,24 @@ namespace audio {
/// @brief Audio source is seekable
bool seekable;
PCM(
std::vector<char> data,
PCM(std::vector<char> data,
size_t totalSamples,
uint8_t channels,
uint8_t bitsPerSample,
uint sampleRate,
bool seekable
) : data(std::move(data)),
totalSamples(totalSamples),
channels(channels),
bitsPerSample(bitsPerSample),
sampleRate(sampleRate),
seekable(seekable) {}
bool seekable)
: data(std::move(data)),
totalSamples(totalSamples),
channels(channels),
bitsPerSample(bitsPerSample),
sampleRate(sampleRate),
seekable(seekable) {
}
/// @brief Get total audio duration
/// @return duration in seconds
/// @return duration in seconds
inline duration_t getDuration() const {
return static_cast<duration_t>(totalSamples) /
return static_cast<duration_t>(totalSamples) /
static_cast<duration_t>(sampleRate);
}
};
@@ -123,38 +119,38 @@ namespace audio {
/// @param buffer destination buffer
/// @param bufferSize destination buffer size
/// @param loop loop stream (seek to start when end reached)
/// @return size of data received
/// @return size of data received
/// (always equals bufferSize if seekable and looped)
virtual size_t readFully(char* buffer, size_t bufferSize, bool loop);
virtual size_t read(char* buffer, size_t bufferSize) = 0;
/// @brief Close stream
virtual void close()=0;
virtual void close() = 0;
/// @brief Check if stream is open
virtual bool isOpen() const=0;
virtual bool isOpen() const = 0;
/// @brief Get total samples number if seekable or 0
virtual size_t getTotalSamples() const=0;
virtual size_t getTotalSamples() const = 0;
/// @brief Get total audio track duration if seekable or 0.0
virtual duration_t getTotalDuration() const=0;
virtual duration_t getTotalDuration() const = 0;
/// @brief Get number of audio channels
/// @return 1 if mono, 2 if stereo
virtual uint getChannels() const=0;
virtual uint getChannels() const = 0;
/// @brief Get audio sampling frequency
/// @return number of mono samples per second
virtual uint getSampleRate() const=0;
virtual uint getSampleRate() const = 0;
/// @brief Get number of bits per mono sample
/// @return 8 or 16
virtual uint getBitsPerSample() const=0;
virtual uint getBitsPerSample() const = 0;
/// @brief Check if the stream does support seek feature
virtual bool isSeekable() const=0;
virtual bool isSeekable() const = 0;
/// @brief Move playhead to the selected sample number
/// @param position selected sample number
@@ -170,16 +166,18 @@ namespace audio {
virtual ~Stream() {};
/// @brief Get pcm data source
/// @return PCM stream or nullptr if audio::openStream
/// @return PCM stream or nullptr if audio::openStream
/// keepSource argument is set to false
virtual std::shared_ptr<PCMStream> getSource() const = 0;
/// @brief Create new speaker bound to the Stream
/// @brief Create new speaker bound to the Stream
/// and having high priority
/// @param loop is stream looped (required for correct buffers preload)
/// @param channel channel index
/// @return speaker id or 0
virtual std::unique_ptr<Speaker> createSpeaker(bool loop, int channel) = 0;
virtual std::unique_ptr<Speaker> createSpeaker(
bool loop, int channel
) = 0;
/// @brief Unbind previous speaker and bind new speaker to the stream
/// @param speaker speaker id or 0 if all you need is unbind speaker
@@ -201,7 +199,7 @@ namespace audio {
virtual void setTime(duration_t time) = 0;
};
/// @brief Sound is an audio asset that supposed to support many
/// @brief Sound is an audio asset that supposed to support many
/// simultaneously playing instances (speakers).
/// So it's audio data is stored in memory.
class Sound {
@@ -209,7 +207,8 @@ namespace audio {
/// @brief Sound variants will be chosen randomly to play
std::vector<std::shared_ptr<Sound>> variants;
virtual ~Sound() {}
virtual ~Sound() {
}
/// @brief Get sound duration
/// @return duration in seconds (>= 0.0)
@@ -220,19 +219,21 @@ namespace audio {
virtual std::shared_ptr<PCM> getPCM() const = 0;
/// @brief Create new sound instance
/// @param priority instance priority. High priority instance can
/// @param priority instance priority. High priority instance can
/// take out speaker from low priority instance
/// @param channel channel index
/// @return new speaker with sound bound or nullptr
/// @return new speaker with sound bound or nullptr
/// if all speakers are in use
virtual std::unique_ptr<Speaker> newInstance(int priority, int channel) const = 0;
virtual std::unique_ptr<Speaker> newInstance(int priority, int channel)
const = 0;
};
/// @brief Audio source controller interface.
/// @attention Speaker is not supposed to be reused
class Speaker {
public:
virtual ~Speaker() {}
virtual ~Speaker() {
}
/// @brief Synchronize the speaker with channel settings
/// @param channel speaker channel
@@ -277,7 +278,7 @@ namespace audio {
/// @brief Stop and destroy speaker
virtual void stop() = 0;
/// @brief Get current time position of playing audio
/// @return time position in seconds
virtual duration_t getTime() const = 0;
@@ -316,17 +317,17 @@ namespace audio {
/// @brief Determines if the position is relative to the listener
virtual bool isRelative() const = 0;
/// @brief Check if speaker is playing
/// @brief Check if speaker is playing
inline bool isPlaying() const {
return getState() == State::playing;
}
/// @brief Check if speaker is paused
/// @brief Check if speaker is paused
inline bool isPaused() const {
return getState() == State::paused;
}
/// @brief Check if speaker is stopped
/// @brief Check if speaker is stopped
inline bool isStopped() const {
return getState() == State::stopped;
}
@@ -336,12 +337,16 @@ namespace audio {
public:
virtual ~Backend() {};
virtual std::unique_ptr<Sound> createSound(std::shared_ptr<PCM> pcm, bool keepPCM) = 0;
virtual std::unique_ptr<Stream> openStream(std::shared_ptr<PCMStream> stream, bool keepSource) = 0;
virtual std::unique_ptr<Sound> createSound(
std::shared_ptr<PCM> pcm, bool keepPCM
) = 0;
virtual std::unique_ptr<Stream> openStream(
std::shared_ptr<PCMStream> stream, bool keepSource
) = 0;
virtual void setListener(
glm::vec3 position,
glm::vec3 velocity,
glm::vec3 lookAt,
glm::vec3 position,
glm::vec3 velocity,
glm::vec3 lookAt,
glm::vec3 up
) = 0;
virtual void update(double delta) = 0;
@@ -358,21 +363,23 @@ namespace audio {
/// @brief Load audio file info and PCM data
/// @param file audio file
/// @param headerOnly read header only
/// @throws std::runtime_error if I/O error ocurred or format is unknown
/// @throws std::runtime_error if I/O error ocurred or format is unknown
/// @return PCM audio data
std::unique_ptr<PCM> load_PCM(const fs::path& file, bool headerOnly);
/// @brief Load sound from file
/// @param file audio file path
/// @param keepPCM store PCM data in sound to make it accessible with Sound::getPCM
/// @throws std::runtime_error if I/O error ocurred or format is unknown
/// @param keepPCM store PCM data in sound to make it accessible with
/// Sound::getPCM
/// @throws std::runtime_error if I/O error ocurred or format is unknown
/// @return new Sound instance
std::unique_ptr<Sound> load_sound(const fs::path& file, bool keepPCM);
/// @brief Create new sound from PCM data
/// @param pcm PCM data
/// @param keepPCM store PCM data in sound to make it accessible with Sound::getPCM
/// @return new Sound instance
/// @param keepPCM store PCM data in sound to make it accessible with
/// Sound::getPCM
/// @return new Sound instance
std::unique_ptr<Sound> create_sound(std::shared_ptr<PCM> pcm, bool keepPCM);
/// @brief Open new PCM stream from file
@@ -383,15 +390,19 @@ namespace audio {
/// @brief Open new audio stream from file
/// @param file audio file path
/// @param keepSource store PCMStream in stream to make it accessible with Stream::getSource
/// @param keepSource store PCMStream in stream to make it accessible with
/// Stream::getSource
/// @return new Stream instance
std::unique_ptr<Stream> open_stream(const fs::path& file, bool keepSource);
/// @brief Open new audio stream from source
/// @param stream PCM data source
/// @param keepSource store PCMStream in stream to make it accessible with Stream::getSource
/// @param keepSource store PCMStream in stream to make it accessible with
/// Stream::getSource
/// @return new Stream instance
std::unique_ptr<Stream> open_stream(std::shared_ptr<PCMStream> stream, bool keepSource);
std::unique_ptr<Stream> open_stream(
std::shared_ptr<PCMStream> stream, bool keepSource
);
/// @brief Configure 3D listener
/// @param position listener position
@@ -399,10 +410,7 @@ namespace audio {
/// @param lookAt point the listener look at
/// @param up camera up vector
void set_listener(
glm::vec3 position,
glm::vec3 velocity,
glm::vec3 lookAt,
glm::vec3 up
glm::vec3 position, glm::vec3 velocity, glm::vec3 lookAt, glm::vec3 up
);
/// @brief Play 3D sound in the world
@@ -412,7 +420,7 @@ namespace audio {
/// @param volume sound volume [0.0-1.0]
/// @param pitch sound pitch multiplier [0.0-...]
/// @param loop loop sound
/// @param priority sound priority
/// @param priority sound priority
/// (PRIORITY_LOW, PRIORITY_NORMAL, PRIORITY_HIGH)
/// @param channel channel index
/// @return speaker id or 0
@@ -470,7 +478,7 @@ namespace audio {
/// @return speaker or nullptr
Speaker* get_speaker(speakerid_t id);
/// @brief Create new channel.
/// @brief Create new channel.
/// All non-builtin channels will be destroyed on audio::reset() call
/// @param name channel name
/// @return new channel index
@@ -499,7 +507,7 @@ namespace audio {
/// @brief Get alive speakers number (including paused)
size_t count_speakers();
/// @brief Get playing streams number (including paused)
/// @brief Get playing streams number (including paused)
size_t count_streams();
/// @brief Update audio streams and sound instanced
@@ -508,9 +516,9 @@ namespace audio {
/// @brief Stop all playing audio in channel, reset channel state
void reset_channel(int channel);
/// @brief Finalize audio system
void close();
};
#endif // AUDIO_AUDIO_HPP_
#endif // AUDIO_AUDIO_HPP_