Skip to content

Commit

Permalink
obs-event-sounds: don't overlap sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
XPhyro committed Jan 17, 2024
1 parent 3c4a2c0 commit 874d30c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/cpp/shared/obs/obs-event-sounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// @LDFLAGS -I/usr/include/obs/ -lobs

#include <cerrno>
#include <csignal>
#include <cstdlib>
#include <optional>
#include <sstream>
#include <unordered_map>

Expand All @@ -25,7 +27,7 @@ std::unordered_map<enum obs_frontend_event, const char*> event_sounds = {
{ OBS_FRONTEND_EVENT_REPLAY_BUFFER_SAVED, "replay-saved.mp3" },
};

void play_sound(const char* filename)
std::optional<decltype(fork())> play_sound(const char* filename)
{
static std::stringstream ss;
ss.str("");
Expand All @@ -36,16 +38,26 @@ void play_sound(const char* filename)
errno = 0;
if (auto pid = fork(); pid < 0 || errno) {
perror("fork");
return {};
} else if (pid == 0) {
execlp("play", "play", "-q", "--", ss.str().c_str(), NULL);
std::exit(EXIT_SUCCESS);
} else {
return { pid };
}
}

void event_callback(enum obs_frontend_event event, [[maybe_unused]] void* private_data)
{
if (event_sounds.contains(event))
play_sound(event_sounds.at(event));
if (!event_sounds.contains(event))
return;

static decltype(play_sound("")) last_pid;
if (last_pid) {
kill(*last_pid, SIGTERM);
last_pid.reset();
}
last_pid = play_sound(event_sounds.at(event));
}

const char* obs_module_author(void)
Expand Down

0 comments on commit 874d30c

Please sign in to comment.