refactor: changes to internal structure

This commit is contained in:
Curve 2021-02-15 21:11:13 +01:00
parent 5c927a36a7
commit 0ed8e5ed9c
No known key found for this signature in database
GPG Key ID: 460F6C466BD35813
6 changed files with 125 additions and 12 deletions

View File

@ -16,10 +16,11 @@ namespace Soundux
inline Objects::Config gConfig;
inline Objects::Hotkeys gHotKeys;
inline Objects::Settings gSettings;
inline std::shared_ptr<Objects::Window> gGui;
inline std::unique_ptr<Objects::Window> gGui;
inline Objects::ProcessingQueue<std::uintptr_t> gQueue;
/* Allows for fast & easy sound access, is populated on start up */
inline std::map<std::uint32_t, std::reference_wrapper<Objects::Sound>> gSounds;
inline std::shared_mutex gSoundsMutex;
inline std::map<std::uint32_t, Objects::Sound> gSounds;
} // namespace Globals
} // namespace Soundux

View File

@ -0,0 +1,69 @@
#include "objects.hpp"
#include "globals.hpp"
#include <algorithm>
#include <fancy.hpp>
#include <functional>
#include <optional>
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<Tab> &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<Tab> Data::getTabs() const
{
return tabs;
}
std::optional<Sound> 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

View File

@ -1,4 +1,5 @@
#pragma once
#include <functional>
#include <map>
#include <string>
#include <vector>
@ -15,11 +16,13 @@ namespace Soundux
std::string name;
std::string path;
std::vector<int> hotkeys;
std::uint64_t modifiedDate;
std::vector<std::size_t> 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<AudioDevice> deviceSettings;
std::vector<std::uint32_t> stopHotkey;
std::vector<int> stopHotkey;
bool allowOverlapping;
bool tabHotKeysOnly;
bool tabHotkeysOnly;
bool darkTheme;
};
struct Data
class Data
{
int width, height;
std::uint32_t output;
private:
std::vector<Tab> tabs;
public:
int width, height;
std::string output; // TODO(curve): Make use of `output`
std::uint32_t soundIdCounter;
std::vector<Tab> getTabs() const;
void setTabs(const std::vector<Tab> &);
void addTab(Tab);
void removeTabById(const std::uint32_t &);
std::optional<Sound> getSound(const std::uint32_t &);
};
} // namespace Objects
} // namespace Soundux

View File

@ -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;

View File

@ -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<PlayingSound> Audio::play(const Objects::Sound &sound,
const std::optional<Objects::AudioDevice> &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<int>(pSound.length / config.sampleRate);
pSound.id = ++Globals::gData.soundIdCounter;
if (playbackDevice)
{
@ -306,4 +319,15 @@ namespace Soundux::Objects
return playBackDevices;
}
std::vector<PlayingSound> Audio::getPlayingSounds()
{
std::shared_lock lock(soundsMutex);
std::vector<PlayingSound> rtn;
for (const auto &sound : playingSounds)
{
rtn.push_back(sound.second);
}
return rtn;
}
} // namespace Soundux::Objects

View File

@ -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<ma_device *, PlayingSound> playingSounds;
@ -63,7 +63,7 @@ namespace Soundux
float getVolume(const std::string &);
std::vector<AudioDevice> getAudioDevices();
// std::vector<Objects::PlayingSound> getPlayingSounds();
std::vector<Objects::PlayingSound> getPlayingSounds();
Audio();
~Audio();