Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
rtmidi_device.h
1// SPDX-FileCopyrightText: © 2020, 2024 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#ifndef __AUDIO_RTMIDI_DEVICE_H__
5#define __AUDIO_RTMIDI_DEVICE_H__
6
7#include "zrythm-config.h"
8
10#include "utils/monotonic_time_provider.h"
11#include "utils/ring_buffer.h"
12
13#if HAVE_RTMIDI
14
15# include <rtmidi_c.h>
16
17class MidiPort;
18
24
25constexpr size_t MIDI_BUFFER_SIZE = 32768;
26
31enum RtMidiDeviceFlow
32{
33 RTMIDI_DEVICE_FLOW_OUTPUT,
34 RTMIDI_DEVICE_FLOW_INPUT,
35};
36
37class RtMidiDevice
38{
39public:
40 using TimestampGenerator = std::function<utils::MonotonicTime ()>;
41 using DeviceNameProvider = std::function<std::string ()>;
42
48 RtMidiDevice (
49 bool is_input,
50 unsigned int device_id,
51 DeviceNameProvider device_name_provider,
52 std::string name = "");
53
54 ~RtMidiDevice ()
55 {
56 if (started_)
57 {
58 stop ();
59 }
60 if (opened_)
61 {
62 close ();
63 }
64 if (is_input_)
65 {
66 rtmidi_in_free (in_handle_);
67 }
68 else
69 {
70 rtmidi_out_free (out_handle_);
71 }
72 }
73
79 void open ();
80
84 void close ();
85
91 void start (TimestampGenerator timestamp_generator);
92
96 void stop ();
97
98public:
100 bool is_input_{};
101
103 unsigned int id_{};
104
105 // std::string name_;
106
108 bool opened_{};
109
111 bool started_{};
112
113 /* ---- INPUT ---- */
114 RtMidiInPtr in_handle_{};
115
116 /* ---- OUTPUT ---- */
117 RtMidiOutPtr out_handle_{};
118
120 // MidiPort * port_{};
121
123 RingBuffer<MidiEvent> midi_ring_{ 1024 };
124
127 MidiEventVector events_;
128
130 std::binary_semaphore midi_ring_sem_{ 1 };
131
132 TimestampGenerator timestamp_generator_;
133
134 DeviceNameProvider device_name_provider_;
135};
136
140
141#endif // HAVE_RTMIDI
142#endif // __AUDIO_RTMIDI_DEVICE_H__
MIDI port specifics.
Definition midi_port.h:19
MIDI events.