diff options
author | Roland Reichwein <mail@reichwein.it> | 2025-01-19 21:03:06 +0000 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2025-01-19 21:03:06 +0000 |
commit | 892888da03028cbf67bf005747902b9e3e5b9773 (patch) | |
tree | a5fd97f9c07c4d08a343e47d0ba2e68f4b5d296e /MIDIPlayer.cpp | |
parent | 7f54b359f0261ae5b70f303a2080b13dd70cd0c2 (diff) |
Play WAV
Diffstat (limited to 'MIDIPlayer.cpp')
-rw-r--r-- | MIDIPlayer.cpp | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/MIDIPlayer.cpp b/MIDIPlayer.cpp index cb6a6bf..6b07105 100644 --- a/MIDIPlayer.cpp +++ b/MIDIPlayer.cpp @@ -20,13 +20,23 @@ using namespace std::chrono_literals; namespace { std::unordered_set<std::string> supported_devices{"AudioBox 22 VSL", "CH345", "M2"}; + + fs::path wav_from_midi(const fs::path& midipath) + { + fs::path result{midipath}; + + result.replace_extension("wav"); + + return result; + } } MIDIPlayer::MIDIPlayer(Config& config): m_config(config), - m_child{}, + m_child_midiplayer{}, m_dir{m_config.get_file_path()}, - m_file{} + m_file{}, + m_audio_player{m_dir, m_file} { std::vector<std::string> list = get_filelist(); @@ -41,34 +51,37 @@ MIDIPlayer::MIDIPlayer(Config& config): void MIDIPlayer::start() { - if (m_child.valid() && m_child.running()) { + if (m_child_midiplayer.valid() && m_child_midiplayer.running()) { stop(); - } else { - m_child = bp::child(fmt::format("aplaymidi-mp -c -p{} \"{}\"", m_client, (m_dir / m_file).string()).c_str());//, bp::std_out > bp::null); - if (!m_child.valid() || !m_child.running()) { - throw std::runtime_error("aplaymidi not started"); - } } + m_child_midiplayer = bp::child(fmt::format("aplaymidi-mp -c -p{} \"{}\"", m_client, (m_dir / m_file).string()).c_str());//, bp::std_out > bp::null); + if (!m_child_midiplayer.valid() || !m_child_midiplayer.running()) { + throw std::runtime_error("aplaymidi not started"); + } + + m_audio_player.start(); } void MIDIPlayer::stop() { - // note:: m_child.terminate() would kill via SIGKILL, preventing note offs + // note:: m_child_midiplayer.terminate() would kill via SIGKILL - if (m_child.valid()) { - int result = kill(m_child.native_handle(), SIGTERM); + if (m_child_midiplayer.valid()) { + int result = kill(m_child_midiplayer.native_handle(), SIGTERM); if (result < 0) { std::cerr << "Error in MIDIPlayer::stop(): kill() unsuccessful\n"; } } + + m_audio_player.stop(); } bool MIDIPlayer::is_playing() { - if (!m_child.valid()) { + if (!m_child_midiplayer.valid()) { return false; } - return m_child.running(); + return m_child_midiplayer.running(); } void MIDIPlayer::set_file(const std::string& filename) @@ -80,6 +93,8 @@ void MIDIPlayer::set_file(const std::string& filename) std::this_thread::sleep_for(100ms); start(); } + + m_audio_player.set_file(wav_from_midi(filename)); } std::string MIDIPlayer::get_file() |