MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ refresh_devices()

size_t MayaFlux::Core::MIDIBackend::refresh_devices ( )
overridevirtual

Refresh the device list.

Returns
Number of devices found

Re-enumerates available devices. May trigger device callbacks for newly connected or disconnected devices.

Implements MayaFlux::Core::IInputBackend.

Definition at line 148 of file MIDIBackend.cpp.

149{
150 if (!m_initialized.load()) {
151 return 0;
152 }
153
154 std::vector<InputDeviceInfo> newly_added_devices;
155
156 try {
157 RtMidiIn midi_in;
158 unsigned int port_count = midi_in.getPortCount();
159
160 {
161 std::lock_guard lock(m_devices_mutex);
162
163 for (unsigned int i = 0; i < port_count; ++i) {
164 std::string port_name = midi_in.getPortName(i);
165
166 if (!port_matches_filter(port_name)) {
167 continue;
168 }
169
170 uint32_t dev_id = find_or_assign_device_id(i);
171
172 MIDIPortInfo info {};
173 info.id = dev_id;
174 info.name = port_name;
175 info.backend_type = InputType::MIDI;
176 info.is_connected = true;
177 info.is_input = true;
178 info.is_output = false;
179 info.port_number = static_cast<uint8_t>(i);
180 info.rtmidi_port_number = i;
181
182 bool is_new = (m_enumerated_devices.find(dev_id) == m_enumerated_devices.end());
183
184 m_enumerated_devices[dev_id] = info;
185
186 if (is_new) {
187 newly_added_devices.push_back(info);
188 }
189 }
190 }
191
192 for (const auto& info : newly_added_devices) {
194 "MIDI port found: {}", info.name);
195 notify_device_change(info, true);
196 }
197
198 {
199 std::lock_guard lock(m_devices_mutex);
200 return m_enumerated_devices.size();
201 }
202 } catch (const RtMidiError& error) {
204 "Error enumerating MIDI ports: {}", error.getMessage());
205 return 0;
206 }
207}
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
uint32_t find_or_assign_device_id(unsigned int rtmidi_port)
std::unordered_map< uint32_t, MIDIPortInfo > m_enumerated_devices
bool port_matches_filter(const std::string &port_name) const
std::atomic< bool > m_initialized
void notify_device_change(const InputDeviceInfo &info, bool connected)
@ MIDI
MIDI controllers and instruments.
@ InputBackend
Input device backend (HID, MIDI, OSC)
void error(Component component, Context context, std::source_location location, std::string_view message)
Log an error message and optionally throw an exception.
@ Core
Core engine, backend, subsystems.

References MayaFlux::Journal::Core, find_or_assign_device_id(), MayaFlux::Core::InputDeviceInfo::id, MayaFlux::Journal::InputBackend, m_devices_mutex, m_enumerated_devices, m_initialized, MF_ERROR, MF_WARN, MayaFlux::Core::MIDI, notify_device_change(), and port_matches_filter().

Referenced by initialize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: