Skip to content

Commit

Permalink
IMusicDriver getDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
Raffaello committed Oct 30, 2023
1 parent 6688ff5 commit 2fc8450
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 49 deletions.
1 change: 1 addition & 0 deletions sdl2-hyper-sonic-drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
96 changes: 52 additions & 44 deletions sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/devices/IDevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,64 @@
#include <memory>
#include <optional>
#include <HyperSonicDrivers/audio/IMixer.hpp>
#include <HyperSonicDrivers/drivers/IMusicDriver.hpp>
//#include <HyperSonicDrivers/drivers/IMusicDriver.hpp>
#include <HyperSonicDrivers/drivers/midi/IMidiDriver.hpp>
#include <HyperSonicDrivers/hardware/IHardware.hpp>
#include <HyperSonicDrivers/devices/types.hpp>

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<audio::IMixer>& 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<uint8_t> 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<audio::IMixer> 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<audio::IMixer> m_mixer;
hardware::IHardware* m_hardware = nullptr;
private:
// TODO: remove the atomic when removing the thread in MIDDrv
std::atomic<bool> m_acquired = false;
std::atomic<drivers::IMusicDriver*> m_owner = nullptr;
};

template<class T, class To = devices::IDevice, typename... Args>
std::shared_ptr<To> make_device(Args... args)
class IMusicDriver;
}

namespace devices
{
return std::dynamic_pointer_cast<To>(std::make_shared<T>(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<audio::IMixer>& 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<uint8_t> 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<audio::IMixer> 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<audio::IMixer> m_mixer;
hardware::IHardware* m_hardware = nullptr;
private:
// TODO: remove the atomic when removing the thread in MIDDrv
std::atomic<bool> m_acquired = false;
std::atomic<drivers::IMusicDriver*> m_owner = nullptr;
};

template<class T, class To = devices::IDevice, typename... Args>
std::shared_ptr<To> make_device(Args... args)
{
return std::dynamic_pointer_cast<To>(std::make_shared<T>(args...));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <HyperSonicDrivers/drivers/IMusicDriver.hpp>
#include <HyperSonicDrivers/utils/ILogger.hpp>

namespace HyperSonicDrivers::drivers
{
IMusicDriver::IMusicDriver(const std::shared_ptr<devices::IDevice>& device) :
m_device(device)
{
if (m_device == nullptr)
{
utils::throwLogC<std::runtime_error>("device is null");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <memory>
#include <HyperSonicDrivers/devices/IDevice.hpp>

namespace HyperSonicDrivers::drivers
{
/**
Expand All @@ -16,7 +19,7 @@ namespace HyperSonicDrivers::drivers
IMusicDriver(IMusicDriver&&) = delete;
IMusicDriver& operator=(IMusicDriver&) = delete;

IMusicDriver() = default;
IMusicDriver(const std::shared_ptr<devices::IDevice>& device);
virtual ~IMusicDriver() = default;

virtual void play(const uint8_t track) noexcept = 0;
Expand All @@ -31,5 +34,10 @@ namespace HyperSonicDrivers::drivers

// TODO: it might not be required
//virtual bool isPaused() const noexcept = 0;

inline std::shared_ptr<devices::IDevice> 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<devices::IDevice> m_device;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<devices::IDevice> m_device;
//std::shared_ptr<devices::IDevice> m_device;
// this is to abstract the specific midi driver implementation
std::unique_ptr<drivers::midi::IMidiDriver> m_midiDriver;
std::shared_ptr<audio::MIDI> m_midi;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ namespace HyperSonicDrivers::drivers::westwood
uint8_t m_opExtraLevel1BD = 0;
uint8_t m_opExtraLevel2BD = 0;

std::shared_ptr<devices::Opl> m_device;
std::shared_ptr<hardware::opl::OPL> m_opl;

struct QueueEntry
Expand Down

0 comments on commit 2fc8450

Please sign in to comment.