From 2fc8450a9ed3bb9aca2aa063c1d0c1c37f0211a6 Mon Sep 17 00:00:00 2001 From: Raffaello Bertini Date: Mon, 30 Oct 2023 23:17:58 +0000 Subject: [PATCH] IMusicDriver getDevice --- sdl2-hyper-sonic-drivers/CMakeLists.txt | 1 + .../src/HyperSonicDrivers/devices/IDevice.hpp | 96 ++++++++++--------- .../drivers/IMusicDriver.cpp | 14 +++ .../drivers/IMusicDriver.hpp | 10 +- .../HyperSonicDrivers/drivers/MIDDriver.cpp | 3 +- .../HyperSonicDrivers/drivers/MIDDriver.hpp | 2 +- .../drivers/westwood/ADLDriver.cpp | 3 +- .../drivers/westwood/ADLDriver.hpp | 1 - 8 files changed, 81 insertions(+), 49 deletions(-) create mode 100644 sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/IMusicDriver.cpp diff --git a/sdl2-hyper-sonic-drivers/CMakeLists.txt b/sdl2-hyper-sonic-drivers/CMakeLists.txt index e3f305a7..692571eb 100644 --- a/sdl2-hyper-sonic-drivers/CMakeLists.txt +++ b/sdl2-hyper-sonic-drivers/CMakeLists.txt @@ -100,6 +100,7 @@ target_sources(${LIB_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/devices/MT32.cpp # --- # ${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/drivers/PCMDriver.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/drivers/IMusicDriver.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/drivers/MIDDriver.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/drivers/midi/IMidiDriver.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/drivers/midi/IMidiChannel.cpp diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/devices/IDevice.hpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/devices/IDevice.hpp index 2f088bb1..b13519e7 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/devices/IDevice.hpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/devices/IDevice.hpp @@ -4,56 +4,64 @@ #include #include #include -#include +//#include #include #include #include -namespace HyperSonicDrivers::devices +namespace HyperSonicDrivers { - /** - * general interface for sound cards used by the drivers - * that is bound to a specific hardware - **/ - class IDevice + namespace drivers { - public: - IDevice(const std::shared_ptr& mixer, const eDeviceType type); - virtual ~IDevice() = default; - - virtual bool init() noexcept = 0; - virtual bool shutdown() noexcept = 0; - inline bool isInit() const noexcept { return m_init; }; - - std::optional getChannelId() const noexcept { return m_hardware->getChannelId(); }; - - inline bool isAcquired() const noexcept { return m_acquired; } - inline bool isOwned(const drivers::IMusicDriver* owner) const noexcept { return m_owner == owner; } - - bool acquire(drivers::IMusicDriver* owner); - bool release(const drivers::IMusicDriver* owner); - - // helpers methods - void setVolume(const uint8_t volume); - void setPan(const uint8_t pan); - void setVolumePan(const uint8_t volume, const uint8_t pan); - - inline std::shared_ptr getMixer() const noexcept { return m_mixer; }; - virtual hardware::IHardware* getHardware() const noexcept { return m_hardware; }; - const eDeviceType type; - protected: - bool m_init = false; - std::shared_ptr m_mixer; - hardware::IHardware* m_hardware = nullptr; - private: - // TODO: remove the atomic when removing the thread in MIDDrv - std::atomic m_acquired = false; - std::atomic m_owner = nullptr; - }; - - template - std::shared_ptr make_device(Args... args) + class IMusicDriver; + } + + namespace devices { - return std::dynamic_pointer_cast(std::make_shared(args...)); + /** + * general interface for sound cards used by the drivers + * that is bound to a specific hardware + **/ + class IDevice + { + public: + IDevice(const std::shared_ptr& mixer, const eDeviceType type); + virtual ~IDevice() = default; + + virtual bool init() noexcept = 0; + virtual bool shutdown() noexcept = 0; + inline bool isInit() const noexcept { return m_init; }; + + std::optional getChannelId() const noexcept { return m_hardware->getChannelId(); }; + + inline bool isAcquired() const noexcept { return m_acquired; } + inline bool isOwned(const drivers::IMusicDriver* owner) const noexcept { return m_owner == owner; } + + bool acquire(drivers::IMusicDriver* owner); + bool release(const drivers::IMusicDriver* owner); + + // helpers methods + void setVolume(const uint8_t volume); + void setPan(const uint8_t pan); + void setVolumePan(const uint8_t volume, const uint8_t pan); + + inline std::shared_ptr getMixer() const noexcept { return m_mixer; }; + virtual hardware::IHardware* getHardware() const noexcept { return m_hardware; }; + const eDeviceType type; + protected: + bool m_init = false; + std::shared_ptr m_mixer; + hardware::IHardware* m_hardware = nullptr; + private: + // TODO: remove the atomic when removing the thread in MIDDrv + std::atomic m_acquired = false; + std::atomic m_owner = nullptr; + }; + + template + std::shared_ptr make_device(Args... args) + { + return std::dynamic_pointer_cast(std::make_shared(args...)); + } } } diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/IMusicDriver.cpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/IMusicDriver.cpp new file mode 100644 index 00000000..95c82099 --- /dev/null +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/IMusicDriver.cpp @@ -0,0 +1,14 @@ +#include +#include + +namespace HyperSonicDrivers::drivers +{ + IMusicDriver::IMusicDriver(const std::shared_ptr& device) : + m_device(device) + { + if (m_device == nullptr) + { + utils::throwLogC("device is null"); + } + } +} diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/IMusicDriver.hpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/IMusicDriver.hpp index d8b38b61..743c70f0 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/IMusicDriver.hpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/IMusicDriver.hpp @@ -1,5 +1,8 @@ #pragma once +#include +#include + namespace HyperSonicDrivers::drivers { /** @@ -16,7 +19,7 @@ namespace HyperSonicDrivers::drivers IMusicDriver(IMusicDriver&&) = delete; IMusicDriver& operator=(IMusicDriver&) = delete; - IMusicDriver() = default; + IMusicDriver(const std::shared_ptr& device); virtual ~IMusicDriver() = default; virtual void play(const uint8_t track) noexcept = 0; @@ -31,5 +34,10 @@ namespace HyperSonicDrivers::drivers // TODO: it might not be required //virtual bool isPaused() const noexcept = 0; + + inline std::shared_ptr getDevice() const noexcept { return m_device; }; + protected: + // this is used to "lock" the device to a specific driver output and passed to IMidiDriver + std::shared_ptr m_device; }; } diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/MIDDriver.cpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/MIDDriver.cpp index f2dec378..6eb6e6c8 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/MIDDriver.cpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/MIDDriver.cpp @@ -38,7 +38,8 @@ namespace HyperSonicDrivers::drivers const audio::mixer::eChannelGroup group, const uint8_t volume, const uint8_t pan - ) : m_device(device), m_group(group), m_volume(volume), m_pan(pan) + ) : IMusicDriver(device), + m_group(group), m_volume(volume), m_pan(pan) { // TODO: move the acquire logic where the callback is set // NOTE/TODO: this brings up the acquire should set up the callback too? diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/MIDDriver.hpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/MIDDriver.hpp index 50390305..5cff599a 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/MIDDriver.hpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/MIDDriver.hpp @@ -57,7 +57,7 @@ namespace HyperSonicDrivers::drivers bool open_() noexcept; private: // this is used to "lock" the device to a specific driver output and passed to IMidiDriver - std::shared_ptr m_device; + //std::shared_ptr m_device; // this is to abstract the specific midi driver implementation std::unique_ptr m_midiDriver; std::shared_ptr m_midi; diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/westwood/ADLDriver.cpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/westwood/ADLDriver.cpp index cb1ec3d8..49e5af33 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/westwood/ADLDriver.cpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/westwood/ADLDriver.cpp @@ -28,7 +28,8 @@ namespace HyperSonicDrivers::drivers::westwood const audio::mixer::eChannelGroup group, const uint8_t volume, const uint8_t pan) : - m_device(opl), m_opl(opl->getOpl()) + IMusicDriver(opl), + m_opl(opl->getOpl()) { memset(m_channels.data(), 0, sizeof(m_channels)); hardware::TimerCallBack cb = std::bind(&ADLDriver::onCallback, this); diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/westwood/ADLDriver.hpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/westwood/ADLDriver.hpp index bc57f6e2..3ac691fe 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/westwood/ADLDriver.hpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/drivers/westwood/ADLDriver.hpp @@ -255,7 +255,6 @@ namespace HyperSonicDrivers::drivers::westwood uint8_t m_opExtraLevel1BD = 0; uint8_t m_opExtraLevel2BD = 0; - std::shared_ptr m_device; std::shared_ptr m_opl; struct QueueEntry