From 472cbf21e567c0c65c124f96cebe717cdef901fb Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 12 Jan 2025 23:08:08 +0100 Subject: Play MIDI --- MIDIPlayer.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) (limited to 'MIDIPlayer.cpp') diff --git a/MIDIPlayer.cpp b/MIDIPlayer.cpp index 41f852e..c7945d6 100644 --- a/MIDIPlayer.cpp +++ b/MIDIPlayer.cpp @@ -1,27 +1,76 @@ #include "MIDIPlayer.h" -MIDIPlayer::MIDIPlayer() +#include + +#include + +namespace bp = boost::process; +namespace fs = std::filesystem; + +MIDIPlayer::MIDIPlayer(const std::filesystem::path& path): + m_child{}, + m_dir{path}, + m_file{} { -} + std::vector list = get_filelist(); + if (list.size() > 0) { + m_file = list[0]; + } +} void MIDIPlayer::start() { - "aplaymidi -p24 locked_out_of_heaven.midi" + if (m_child.valid() && m_child.running()) { + stop(); + } else { + m_child = bp::child("aplaymidi -p24 locked_out_of_heaven.midi");//, bp::std_out > bp::null); + } } void MIDIPlayer::stop() { + // note:: m_child.terminate() would kill via SIGKILL, preventing note offs + + if (m_child.valid()) { + int result = kill(m_child.native_handle(), SIGTERM); + if (result < 0) { + std::cerr << "Error in MIDIPlayer::stop(): kill() unsuccessful\n"; + } + } } bool MIDIPlayer::is_playing() { + if (!m_child.valid()) { + return false; + } + return m_child.running(); } void MIDIPlayer::set_file(const std::string& filename) { + m_file = filename; +} + +std::string MIDIPlayer::get_file() +{ + return m_file; } std::vector MIDIPlayer::get_filelist() { + std::vector result; + for (auto const& dir_entry: fs::directory_iterator{m_dir}) { + fs::path entry{dir_entry.path()}; + fs::path extension = entry.extension(); + if (extension == ".midi" || extension == ".mid") { + result.push_back(entry.filename()); + } + if (result.size() == 99) { + break; + } + } + return result; } + -- cgit v1.2.3