diff --git a/src/core/global/globals.hpp b/src/core/global/globals.hpp index 0b701e5..9cbb90e 100644 --- a/src/core/global/globals.hpp +++ b/src/core/global/globals.hpp @@ -16,10 +16,11 @@ namespace Soundux inline Objects::Config gConfig; inline Objects::Hotkeys gHotKeys; inline Objects::Settings gSettings; - inline std::shared_ptr gGui; + inline std::unique_ptr gGui; inline Objects::ProcessingQueue gQueue; /* Allows for fast & easy sound access, is populated on start up */ - inline std::map> gSounds; + inline std::shared_mutex gSoundsMutex; + inline std::map gSounds; } // namespace Globals } // namespace Soundux \ No newline at end of file diff --git a/src/core/global/objects.cpp b/src/core/global/objects.cpp new file mode 100644 index 0000000..fdbb9d3 --- /dev/null +++ b/src/core/global/objects.cpp @@ -0,0 +1,69 @@ +#include "objects.hpp" +#include "globals.hpp" +#include +#include +#include +#include + +namespace Soundux::Objects +{ + void Data::addTab(Tab tab) + { + tab.id = tabs.size(); + tabs.push_back(tab); + std::unique_lock lock(Globals::gSoundsMutex); + + for (const auto &sound : tab.sounds) + { + Globals::gSounds.insert({sound.id, sound}); + } + } + void Data::removeTabById(const std::uint32_t &index) + { + std::unique_lock lock(Globals::gSoundsMutex); + if (tabs.size() > index) + { + auto &tab = tabs.at(index); + for (auto &sound : tab.sounds) + { + Globals::gSounds.erase(sound.id); + } + + tabs.erase(tabs.begin() + index); + } + else + { + Fancy::fancy.logTime().warning() << "Tried to remove non existant tab" << std::endl; + } + } + void Data::setTabs(const std::vector &newTabs) + { + tabs = newTabs; + std::unique_lock lock(Globals::gSoundsMutex); + + Globals::gSounds.clear(); + for (const auto &tab : tabs) + { + for (const auto &sound : tab.sounds) + { + Globals::gSounds.insert({sound.id, sound}); + } + } + } + std::vector Data::getTabs() const + { + return tabs; + } + std::optional Data::getSound(const std::uint32_t &id) + { + std::shared_lock lock(Globals::gSoundsMutex); + + if (Globals::gSounds.find(id) != Globals::gSounds.end()) + { + return Globals::gSounds.at(id); + } + + Fancy::fancy.logTime().warning() << "Tried to access non existant sound " << id << std::endl; + return std::nullopt; + } +} // namespace Soundux::Objects \ No newline at end of file diff --git a/src/core/global/objects.hpp b/src/core/global/objects.hpp index 2b69560..841da9f 100644 --- a/src/core/global/objects.hpp +++ b/src/core/global/objects.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -15,11 +16,13 @@ namespace Soundux std::string name; std::string path; + std::vector hotkeys; std::uint64_t modifiedDate; - std::vector hotKeys; }; struct Tab { + // TODO(curve) will be used later to move tabs + std::uint32_t id; //* Equal to index std::string name; std::string path; @@ -28,18 +31,30 @@ namespace Soundux struct Settings { + // TODO(curve): Adjust deviceSettings & Audio::Devices when UI changes some settings std::vector deviceSettings; - std::vector stopHotkey; + std::vector stopHotkey; bool allowOverlapping; - bool tabHotKeysOnly; + bool tabHotkeysOnly; bool darkTheme; }; - struct Data + class Data { - int width, height; - std::uint32_t output; + private: std::vector tabs; + + public: + int width, height; + std::string output; // TODO(curve): Make use of `output` std::uint32_t soundIdCounter; + + std::vector getTabs() const; + void setTabs(const std::vector &); + + void addTab(Tab); + void removeTabById(const std::uint32_t &); + + std::optional getSound(const std::uint32_t &); }; } // namespace Objects } // namespace Soundux \ No newline at end of file diff --git a/src/core/hotkeys/linux/x11.cpp b/src/core/hotkeys/linux/x11.cpp index a7e12b3..12f29f3 100644 --- a/src/core/hotkeys/linux/x11.cpp +++ b/src/core/hotkeys/linux/x11.cpp @@ -28,6 +28,10 @@ namespace Soundux::Objects return; } } + else + { + Fancy::fancy.logTime() << "Using DISPLAY " << displayenv; + } display = x11Display; int major_op = 0, event_rtn = 0, ext_rtn = 0; diff --git a/src/helper/audio/audio.cpp b/src/helper/audio/audio.cpp index fb69b02..1aa7bba 100644 --- a/src/helper/audio/audio.cpp +++ b/src/helper/audio/audio.cpp @@ -23,6 +23,14 @@ namespace Soundux::Objects defaultOutputDevice = &devices.at(device.name); } } + + for (const auto &device : Globals::gSettings.deviceSettings) + { + if (devices.find(device.name) != devices.end()) + { + devices.at(device.name).volume = device.volume; + } + } } Audio::~Audio() { @@ -31,6 +39,11 @@ namespace Soundux::Objects std::optional Audio::play(const Objects::Sound &sound, const std::optional &playbackDevice) { + if (!Globals::gSettings.allowOverlapping) + { + stopAll(); + } + auto *decoder = new ma_decoder; auto res = ma_decoder_init_file(sound.path.c_str(), nullptr, decoder); @@ -69,9 +82,9 @@ namespace Soundux::Objects pSound.sound = sound; pSound.rawDevice = device; pSound.rawDecoder = decoder; - // TODO(curve): This may only work on mp3s pSound.length = ma_decoder_get_length_in_pcm_frames(decoder); - pSound.id = ++idCounter; + pSound.lengthInSeconds = static_cast(pSound.length / config.sampleRate); + pSound.id = ++Globals::gData.soundIdCounter; if (playbackDevice) { @@ -306,4 +319,15 @@ namespace Soundux::Objects return playBackDevices; } + std::vector Audio::getPlayingSounds() + { + std::shared_lock lock(soundsMutex); + std::vector rtn; + for (const auto &sound : playingSounds) + { + rtn.push_back(sound.second); + } + + return rtn; + } } // namespace Soundux::Objects \ No newline at end of file diff --git a/src/helper/audio/audio.hpp b/src/helper/audio/audio.hpp index d70a597..8ca8e22 100644 --- a/src/helper/audio/audio.hpp +++ b/src/helper/audio/audio.hpp @@ -28,6 +28,7 @@ namespace Soundux ma_decoder *rawDecoder; long long length = 0; long long seekTo = 0; + int lengthInSeconds = 0; long long readFrames = 0; AudioDevice device; @@ -38,7 +39,6 @@ namespace Soundux }; class Audio { - std::uint32_t idCounter; std::shared_mutex soundsMutex; std::map playingSounds; @@ -63,7 +63,7 @@ namespace Soundux float getVolume(const std::string &); std::vector getAudioDevices(); - // std::vector getPlayingSounds(); + std::vector getPlayingSounds(); Audio(); ~Audio();