1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
#include "MIDIPlayer.h"
#include "config.h"
#include <signal.h>
#include <fmt/format.h>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <unordered_set>
#include <stdexcept>
namespace bp = boost::process;
namespace fs = std::filesystem;
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_midiplayer{},
m_dir{m_config.get_file_path()},
m_file{},
m_audio_player{m_dir, m_file}
{
std::vector<std::string> list = get_filelist();
if (list.size() > 0) {
m_file = list[0];
}
init_seq();
iterate_ports();
close_seq();
}
void MIDIPlayer::start()
{
if (m_child_midiplayer.valid() && m_child_midiplayer.running()) {
stop();
}
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_midiplayer.terminate() would kill via SIGKILL
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_midiplayer.valid()) {
return false;
}
return m_child_midiplayer.running();
}
void MIDIPlayer::set_file(const std::string& filename)
{
m_file = filename;
if (is_playing()) {
stop();
std::this_thread::sleep_for(100ms);
start();
}
m_audio_player.set_file(wav_from_midi(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;
}
}
std::sort(result.begin(), result.end());
return result;
}
void MIDIPlayer::init_seq(void)
{
/* open sequencer */
int err = snd_seq_open(&m_seq, "default", SND_SEQ_OPEN_DUPLEX, 0);
if (err < 0)
{
throw std::runtime_error("snd_seq_open()");
}
}
void MIDIPlayer::close_seq(void)
{
int err = snd_seq_close(m_seq);
if (err < 0)
{
throw std::runtime_error("snd_seq_close()");
}
}
void MIDIPlayer::iterate_ports(void)
{
bool found{};
snd_seq_client_info_t *cinfo;
snd_seq_port_info_t *pinfo;
snd_seq_client_info_alloca(&cinfo);
snd_seq_port_info_alloca(&pinfo);
snd_seq_client_info_set_client(cinfo, -1);
while (snd_seq_query_next_client(m_seq, cinfo) >= 0) {
int client = snd_seq_client_info_get_client(cinfo);
snd_seq_port_info_set_client(pinfo, client);
snd_seq_port_info_set_port(pinfo, -1);
while (snd_seq_query_next_port(m_seq, pinfo) >= 0) {
/* port must understand MIDI messages */
if (!(snd_seq_port_info_get_type(pinfo)
& SND_SEQ_PORT_TYPE_MIDI_GENERIC))
continue;
/* we need both WRITE and SUBS_WRITE */
if ((snd_seq_port_info_get_capability(pinfo)
& (SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE))
!= (SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE))
continue;
if (supported_devices.contains(std::string{snd_seq_client_info_get_name(cinfo)})) {
m_client = snd_seq_port_info_get_client(pinfo);
found = true;
}
//std::cout << fmt::format("DEBUG: {}", get_midi_port()) << std::endl;
//printf("%3d:%-3d %-32.32s %s\n",
// snd_seq_port_info_get_client(pinfo),
// snd_seq_port_info_get_port(pinfo),
// snd_seq_client_info_get_name(cinfo),
// snd_seq_port_info_get_name(pinfo));
}
}
if (!found) {
throw std::runtime_error("No MIDI device found");
}
}
int MIDIPlayer::get_midi_port()
{
return m_client;
}
|