Skip to content

Commit

Permalink
Sndio: Make Library a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebeatrici committed Aug 11, 2024
1 parent 63ea861 commit e2f1b28
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
17 changes: 13 additions & 4 deletions src/backends/Sndio/Library.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ struct sio_par;
namespace sndio {
class Library {
public:
Library();
~Library();
static Library &instance() {
static Library instance;
return instance;
}

explicit operator bool() const { return m_handle; }

ErrorCode load(const std::string_view libraryName);
void unload();

void *m_handle;

sio_hdl *(*open)(const char *name, unsigned int mode, int nbio_flag);
void (*close)(sio_hdl *hdl);

Expand All @@ -49,9 +49,18 @@ class Library {
int (*revents)(sio_hdl *hdl, PollFD *pfd);

private:
Library();
~Library();

Library(const Library &) = delete;
Library &operator=(const Library &) = delete;

void *m_handle;
};

static inline auto &lib() {
return Library::instance();
}
} // namespace sndio

#endif
42 changes: 20 additions & 22 deletions src/backends/Sndio/Sndio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ static constexpr auto DEFAULT_QUANTUM = 1024;

using FluxData = CrossAudio_FluxData;

static Library lib;

static auto toImpl(BE_Engine *engine) {
return reinterpret_cast< Engine * >(engine);
}
Expand Down Expand Up @@ -53,7 +51,7 @@ static ErrorCode init() {
ErrorCode ret;

for (const auto name : names) {
if ((ret = lib.load(name)) != CROSSAUDIO_EC_LIBRARY) {
if ((ret = lib().load(name)) != CROSSAUDIO_EC_LIBRARY) {
break;
}
}
Expand All @@ -62,7 +60,7 @@ static ErrorCode init() {
}

static ErrorCode deinit() {
lib.unload();
lib().unload();

return CROSSAUDIO_EC_OK;
}
Expand Down Expand Up @@ -186,24 +184,24 @@ ErrorCode Flux::start(FluxConfig &config, const FluxFeedback &feedback) {
}

if (config.node && strcmp(config.node, CROSSAUDIO_FLUX_DEFAULT_NODE) != 0) {
m_handle = lib.open(config.node, mode, 1);
m_handle = lib().open(config.node, mode, 1);
} else {
m_handle = lib.open(DEFAULT_NODE, mode, 1);
m_handle = lib().open(DEFAULT_NODE, mode, 1);
}

if (!m_handle) {
return CROSSAUDIO_EC_GENERIC;
}

sio_par par;
if (!configToPar(par, config) || !lib.setpar(m_handle, &par) || !lib.getpar(m_handle, &par)) {
if (!configToPar(par, config) || !lib().setpar(m_handle, &par) || !lib().getpar(m_handle, &par)) {
stop();
return CROSSAUDIO_EC_GENERIC;
}

m_quantum = par.appbufsz / config.channels;

if (!lib.start(m_handle)) {
if (!lib().start(m_handle)) {
stop();
return CROSSAUDIO_EC_GENERIC;
}
Expand All @@ -222,7 +220,7 @@ ErrorCode Flux::stop() {
}

if (m_handle) {
lib.close(m_handle);
lib().close(m_handle);
m_handle = nullptr;
}

Expand Down Expand Up @@ -255,19 +253,19 @@ void Flux::processInput() {
const uint32_t frameSize = (std::bit_ceil(m_config.sampleBits) / 8) * m_config.channels;

std::vector< std::byte > buffer(frameSize * m_quantum);
std::vector< pollfd > fds(lib.nfds(m_handle));
std::vector< pollfd > fds(lib().nfds(m_handle));

while (!m_halt) {
const int numFds = lib.pollfd(m_handle, fds.data(), POLLIN);
const int numFds = lib().pollfd(m_handle, fds.data(), POLLIN);
if (numFds > 0 && poll(fds.data(), numFds, -1) < 0) {
continue;
}

if (lib.revents(m_handle, fds.data()) & POLLHUP) {
if (lib().revents(m_handle, fds.data()) & POLLHUP) {
return;
}

const auto bytes = lib.read(m_handle, buffer.data(), buffer.size());
const auto bytes = lib().read(m_handle, buffer.data(), buffer.size());
if (bytes != buffer.size()) {
return;
}
Expand All @@ -276,9 +274,9 @@ void Flux::processInput() {
m_feedback.process(m_feedback.userData, &fluxData);

if (m_pause.test()) {
lib.stop(m_handle);
lib().stop(m_handle);
m_pause.wait(true);
lib.start(m_handle);
lib().start(m_handle);
}
}
}
Expand All @@ -287,36 +285,36 @@ void Flux::processOutput() {
const uint32_t frameSize = (std::bit_ceil(m_config.sampleBits) / 8) * m_config.channels;

std::vector< std::byte > buffer(frameSize * m_quantum);
std::vector< pollfd > fds(lib.nfds(m_handle));
std::vector< pollfd > fds(lib().nfds(m_handle));

while (!m_halt) {
const int numFds = lib.pollfd(m_handle, fds.data(), POLLOUT);
const int numFds = lib().pollfd(m_handle, fds.data(), POLLOUT);
if (numFds > 0 && poll(fds.data(), numFds, -1) < 0) {
continue;
}

if (lib.revents(m_handle, fds.data()) & POLLHUP) {
if (lib().revents(m_handle, fds.data()) & POLLHUP) {
return;
}

FluxData fluxData = { buffer.data(), m_quantum };
m_feedback.process(m_feedback.userData, &fluxData);

const auto bytes = lib.write(m_handle, buffer.data(), buffer.size());
const auto bytes = lib().write(m_handle, buffer.data(), buffer.size());
if (bytes != buffer.size()) {
return;
}

if (m_pause.test()) {
lib.stop(m_handle);
lib().stop(m_handle);
m_pause.wait(true);
lib.start(m_handle);
lib().start(m_handle);
}
}
}

bool Flux::configToPar(sio_par &par, const FluxConfig &config) {
lib.initpar(&par);
lib().initpar(&par);

switch (config.bitFormat) {
default:
Expand Down

0 comments on commit e2f1b28

Please sign in to comment.