summaryrefslogtreecommitdiffhomepage
path: root/MIDIPlayer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'MIDIPlayer.cpp')
-rw-r--r--MIDIPlayer.cpp55
1 files changed, 52 insertions, 3 deletions
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 <signal.h>
+
+#include <iostream>
+
+namespace bp = boost::process;
+namespace fs = std::filesystem;
+
+MIDIPlayer::MIDIPlayer(const std::filesystem::path& path):
+ m_child{},
+ m_dir{path},
+ m_file{}
{
-}
+ std::vector<std::string> 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<std::string> MIDIPlayer::get_filelist()
{
+ std::vector<std::string> 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;
}
+