author | František Kučera <franta-hg@frantovo.cz> |
Mon, 04 Jan 2021 00:15:56 +0100 | |
branch | v_0 |
changeset 10 | 4d95b089457d |
parent 9 | ee976a1d1f0a |
child 11 | 5b351628a377 |
permissions | -rw-r--r-- |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* DJM-Fix |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#include <iostream> |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
18 |
#include <stdexcept> |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
19 |
#include <thread> |
9 | 20 |
#include <mutex> |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
21 |
#include <atomic> |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
|
3
e238528eb19c
link to ALSA (libasound)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
23 |
#include <alsa/asoundlib.h> |
e238528eb19c
link to ALSA (libasound)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
24 |
|
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
#include "AlsaBridge.h" |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
namespace djmfix { |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
namespace alsa { |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
class AlsaBridgeImpl : public AlsaBridge, private djmfix::MidiSender { |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
private: |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
djmfix::DJMFix* djmFix; |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
33 |
snd_rawmidi_t* input; |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
34 |
snd_rawmidi_t* output; |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
35 |
std::thread receivingThread; |
9 | 36 |
std::recursive_mutex midiMutex; |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
37 |
std::atomic<bool> stopped{false}; |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
38 |
|
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
39 |
void run() { |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
40 |
while (!stopped) { |
9 | 41 |
{ |
42 |
std::lock_guard<std::recursive_mutex> lock(midiMutex); |
|
43 |
// TODO: poll |
|
44 |
uint8_t buffer[256]; |
|
45 |
ssize_t length = snd_rawmidi_read(input, buffer, sizeof (buffer)); |
|
46 |
if (length > 0 && length <= sizeof (buffer)) { |
|
47 |
// TODO: multiple messages combined together? |
|
48 |
djmFix->receive(MidiMessage(buffer, buffer + length)); |
|
49 |
} |
|
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
50 |
} |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
51 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
52 |
} |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
53 |
} |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
public: |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
|
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
56 |
AlsaBridgeImpl(djmfix::DJMFix* djmFix, const std::string& deviceName) : djmFix(djmFix) { |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
57 |
if (djmFix == nullptr) throw std::invalid_argument("need a djmFix for AlsaBridge"); |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
58 |
|
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
59 |
int error = snd_rawmidi_open(&input, &output, deviceName.c_str(), SND_RAWMIDI_NONBLOCK); |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
60 |
if (error) throw std::invalid_argument("unable to open ALSA device"); |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
61 |
|
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
62 |
|
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
djmFix->setMidiSender(this); |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
virtual ~AlsaBridgeImpl() { |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
67 |
// TODO: do not use raw/exclusive access to the device |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
68 |
snd_rawmidi_close(input); |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
69 |
snd_rawmidi_close(output); |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
std::cerr << "~AlsaBridgeImpl()" << std::endl; // TODO: do not mess STDIO |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
virtual void start() override { |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
djmFix->start(); |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
75 |
receivingThread = std::thread(&AlsaBridgeImpl::run, this); |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
78 |
virtual void stop() override { |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
79 |
stopped = true; |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
80 |
receivingThread.join(); |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
81 |
djmFix->stop(); |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
82 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
83 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
84 |
virtual void send(MidiMessage midiMessage) override { |
9 | 85 |
std::lock_guard<std::recursive_mutex> lock(midiMutex); |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
86 |
ssize_t length = snd_rawmidi_write(output, midiMessage.data(), midiMessage.size()); |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
87 |
std::cerr << "AlsaBridgeImpl::send(): length = " << length << std::endl; // TODO: do not mess STDIO |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
88 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
89 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
}; |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
|
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
92 |
AlsaBridge* create(djmfix::DJMFix* djmFix, const std::string& deviceName) { |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
93 |
return new AlsaBridgeImpl(djmFix, deviceName); |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
97 |
} |