Skip to content

Commit

Permalink
init (#254)
Browse files Browse the repository at this point in the history
* init

* code rev

* IMusicDriver common interface

* [WIP] merging Device and MidiDevice...

* code rev

* code rev

* major refactor, remove MidiDevice(s)
integrate to IDevice,
Driver are responsible to acquire and release device.
Only driver send instruction to devices.
Devices can't be used directly to send MIDI messages

* fix test suite

* code rev

* code rev

* sonarcloud code rev

* code rev
  • Loading branch information
Raffaello authored Sep 23, 2023
1 parent d25caf3 commit ca147cb
Show file tree
Hide file tree
Showing 64 changed files with 803 additions and 1,359 deletions.
14 changes: 4 additions & 10 deletions sdl2-hyper-sonic-drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
cmake_minimum_required (VERSION 3.21)

find_package(SDL2 CONFIG REQUIRED)
find_package(rtmidi CONFIG REQUIRED)
find_package(MT32emu CONFIG REQUIRED)

set(CMAKE_CXX_STANDARD 20)
Expand Down Expand Up @@ -96,14 +95,10 @@ target_sources(${LIB_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/devices/IDevice.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/devices/Opl.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/devices/Adlib.cpp

${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/devices/midi/MidiNative.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/devices/midi/MidiOpl.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/devices/midi/MidiAdlib.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/devices/midi/MidiSbPro.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/devices/midi/MidiSbPro2.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/devices/midi/MidiMT32.cpp
# --- #
${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/devices/SbPro.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/devices/SbPro2.cpp
${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/MIDDriver.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/HyperSonicDrivers/drivers/westwood/ADLDriver.cpp
Expand Down Expand Up @@ -177,7 +172,6 @@ target_sources(${LIB_NAME} PRIVATE
target_link_libraries(${LIB_NAME}
PUBLIC
${LIB_SDL2}
RtMidi::rtmidi
MT32Emu::mt32emu
)
if(NOT (WITH_SDL2_STATIC))
Expand Down
10 changes: 0 additions & 10 deletions sdl2-hyper-sonic-drivers/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,6 @@ macro_example(
NONE
)

macro_example(
EXE RtMidi_example
FILE "rtmidi-example.cpp"
DEPS hyper-sonic-drivers-static
LINKS ${LIB_SDL2main} hyper-sonic-drivers-static spdlog::spdlog
FIXTURES
"../test/fixtures/midifile_sample.mid"
NONE
)

macro_example(
EXE MIDexample
FILE "mid-example.cpp"
Expand Down
30 changes: 24 additions & 6 deletions sdl2-hyper-sonic-drivers/examples/adl-example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <HyperSonicDrivers/files/westwood/ADLFile.hpp>
#include <HyperSonicDrivers/drivers/westwood/ADLDriver.hpp>
#include <HyperSonicDrivers/utils/ILogger.hpp>
#include <HyperSonicDrivers/devices/Adlib.hpp>
#include <HyperSonicDrivers/devices/SbPro.hpp>
#include <HyperSonicDrivers/devices/SbPro2.hpp>

#include <spdlog/spdlog.h>
#include <fmt/color.h>
Expand All @@ -24,16 +27,31 @@ using files::westwood::ADLFile;
using drivers::westwood::ADLDriver;


void adl_test(const OplEmulator emu, const OplType type, std::shared_ptr<audio::IMixer> mixer, const std::string& filename, const int track)
void adl_test(const OplEmulator emu, const OplType type, std::shared_ptr<audio::IMixer> mixer, const std::string& filename, const uint8_t track)
{
auto opl = OPLFactory::create(emu, type, mixer);
if (opl == nullptr)
return;
using devices::make_device;

auto adlFile = std::make_shared<ADLFile>(filename);
ADLDriver adlDrv(opl, audio::mixer::eChannelGroup::Music);
std::shared_ptr<devices::Opl> device;
switch (type)
{
using enum OplType;

case OPL2:
device = make_device<devices::Adlib, devices::Opl>(mixer, emu);
break;
case DUAL_OPL2:
device = make_device<devices::SbPro, devices::Opl>(mixer, emu);
break;
case OPL3:
device = make_device<devices::SbPro2, devices::Opl>(mixer, emu);
break;

}

ADLDriver adlDrv(device, audio::mixer::eChannelGroup::Music);
adlDrv.setADLFile(adlFile);
adlDrv.play(track, 0xFF);
adlDrv.play(track);

if(!mixer->isReady()) {
spdlog::error("mixer not ready yet..");
Expand Down
62 changes: 26 additions & 36 deletions sdl2-hyper-sonic-drivers/examples/mid-example.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
#include <HyperSonicDrivers/audio/sdl2/Mixer.hpp>

#include <HyperSonicDrivers/drivers/MIDDriver.hpp>
#include <HyperSonicDrivers/devices/midi/MidiNative.hpp>
#include <HyperSonicDrivers/devices/midi/MidiAdlib.hpp>
#include <HyperSonicDrivers/devices/midi/MidiSbPro.hpp>
#include <HyperSonicDrivers/devices/midi/MidiSbPro2.hpp>
#include <HyperSonicDrivers/devices/IMidiDevice.hpp>
#include <HyperSonicDrivers/devices/makers.hpp>

#include <HyperSonicDrivers/devices/Adlib.hpp>
#include <HyperSonicDrivers/devices/SbPro.hpp>
#include <HyperSonicDrivers/devices/SbPro2.hpp>
#include <HyperSonicDrivers/devices/IDevice.hpp>
#include <HyperSonicDrivers/files/dmx/OP2File.hpp>

#include <HyperSonicDrivers/hardware/opl/OPLFactory.hpp>
Expand All @@ -24,14 +21,17 @@

using namespace HyperSonicDrivers;

using audio::mixer::eChannelGroup;
using hardware::opl::OPLFactory;
using hardware::opl::OplEmulator;
using hardware::opl::OplType;


void mid_test_run(drivers::MIDDriver& midDrv, const std::shared_ptr<audio::MIDI>& midi)
{
auto start_time = std::chrono::system_clock::now();
midDrv.play(midi);
midDrv.setMidi(midi);
midDrv.play(0);
while (midDrv.isPlaying()) {
utils::delayMillis(1000);
}
Expand All @@ -44,26 +44,27 @@ void mid_test_run(drivers::MIDDriver& midDrv, const std::shared_ptr<audio::MIDI>
void scummvm_mid_test(const OplEmulator emu, const OplType type, const std::shared_ptr<audio::IMixer>& mixer,
const std::shared_ptr<audio::MIDI> midi)
{
std::shared_ptr<devices::IMidiDevice> midi_device;

std::shared_ptr<devices::IDevice> device;
switch (type)
{
using enum OplType;
using namespace devices;

case OPL2:
midi_device = make_midi_device<midi::MidiAdlib>(mixer, audio::mixer::eChannelGroup::Music, emu);
device = make_device<devices::Adlib>(mixer, emu);
break;
case DUAL_OPL2:
midi_device = make_midi_device<midi::MidiSbPro>(mixer, audio::mixer::eChannelGroup::Music, emu);
device = make_device<devices::SbPro>(mixer, emu);
break;
case OPL3:
midi_device = make_midi_device<midi::MidiSbPro2>(mixer, audio::mixer::eChannelGroup::Music, emu);
device = make_device<devices::SbPro2>(mixer, emu);
break;
default:
throw std::runtime_error("?");
}

drivers::MIDDriver midDrv(/*mixer,*/ midi_device);
drivers::MIDDriver midDrv(device, eChannelGroup::Music);
spdlog::info(std::format("playing midi (OPL type={})...", type));
mid_test_run(midDrv, midi);
}
Expand All @@ -72,47 +73,42 @@ void mid_test(const OplEmulator emu, const OplType type, const std::shared_ptr<a
const std::shared_ptr<audio::MIDI> midi)
{
auto op2file = files::dmx::OP2File("GENMIDI.OP2");
std::shared_ptr<devices::IMidiDevice> midi_device;
std::shared_ptr<devices::IDevice> device;
switch (type)
{
using enum OplType;
using namespace devices;

case OPL2:
midi_device = make_midi_device<midi::MidiAdlib>(mixer, op2file.getBank(), audio::mixer::eChannelGroup::Music, emu);
device = make_device<Adlib>(mixer, emu);
break;
case DUAL_OPL2:
midi_device = make_midi_device<midi::MidiSbPro>(mixer, op2file.getBank(), audio::mixer::eChannelGroup::Music, emu);
device = make_device<SbPro>(mixer, emu);
break;
case OPL3:
midi_device = make_midi_device<midi::MidiSbPro2>(mixer, op2file.getBank(), audio::mixer::eChannelGroup::Music, emu);
device = make_device<SbPro2>(mixer, emu);
break;
default:
throw std::runtime_error("?");
}

drivers::MIDDriver midDrv(/*mixer,*/ midi_device);
drivers::MIDDriver midDrv(/*mixer,*/ device, eChannelGroup::Music);
if (!midDrv.loadBankOP2(op2file.getBank()))
{
spdlog::error("can't load BankOP2");
return;
}

spdlog::info(std::format("playing midi (OPL type={})...", type));
mid_test_run(midDrv, midi);
}

void mid_test_native(/*const std::shared_ptr<audio::IMixer>& mixer,*/
const std::shared_ptr<audio::MIDI>& midi)
{
auto nativeMidi = std::make_shared<devices::midi::MidiNative>();

drivers::MIDDriver mid_drv(/*mixer,*/ nativeMidi);

spdlog::info("playing midi...");
mid_test_run(mid_drv, midi);
}

int run(const std::shared_ptr<audio::MIDI>& midi, const bool use_opldrv)
{
auto mixer = audio::make_mixer<audio::sdl2::Mixer>(8, 44100, 1024);
if (!mixer->init())
{
std::cerr << "can't init the mixer" << std::endl;
spdlog::error("can't init the mixer");
return 1;
}

Expand Down Expand Up @@ -160,11 +156,5 @@ int run(const std::shared_ptr<audio::MIDI>& midi, const bool use_opldrv)
}
}

// Native Midi
for (const auto& c : colors) {
spdlog::info(fmt::format(fg(c), m, "Native", "MIDI"));
}
mid_test_native(/*mixer,*/ midi);

return 0;
}
89 changes: 0 additions & 89 deletions sdl2-hyper-sonic-drivers/examples/rtmidi-example.cpp

This file was deleted.

Loading

0 comments on commit ca147cb

Please sign in to comment.