16 |
|
|
17 |
namespace LinuxSampler { |
namespace LinuxSampler { |
18 |
|
|
19 |
struct _private_data_t { |
struct VirtualMidiDevice::private_data_t { |
20 |
atomic_t notesChanged; // whether some key changed at all |
atomic_t notesChanged; // whether some key changed at all |
21 |
atomic_t pNoteChanged[MIDI_KEYS]; // which key(s) changed |
atomic_t pNoteChanged[MIDI_KEYS]; // which key(s) changed |
22 |
atomic_t pNoteIsActive[MIDI_KEYS]; // status of each key (either active or inactive) |
atomic_t pNoteIsActive[MIDI_KEYS]; // status of each key (either active or inactive) |
24 |
atomic_t pNoteOffVelocity[MIDI_KEYS]; |
atomic_t pNoteOffVelocity[MIDI_KEYS]; |
25 |
RingBuffer<VirtualMidiDevice::event_t,false> events; |
RingBuffer<VirtualMidiDevice::event_t,false> events; |
26 |
|
|
27 |
_private_data_t() : events(MAX_EVENTS, 0) {} |
private_data_t() : events(MAX_EVENTS, 0) {} |
28 |
}; |
}; |
29 |
|
|
30 |
VirtualMidiDevice::VirtualMidiDevice() { |
VirtualMidiDevice::VirtualMidiDevice() : p(new private_data_t) { |
|
_private_data_t* p = new _private_data_t; |
|
|
pPrivateData = p; |
|
31 |
atomic_t zero = ATOMIC_INIT(0); |
atomic_t zero = ATOMIC_INIT(0); |
32 |
atomic_t defaultVelocity = ATOMIC_INIT(127); |
atomic_t defaultVelocity = ATOMIC_INIT(127); |
33 |
p->notesChanged = zero; |
p->notesChanged = zero; |
40 |
} |
} |
41 |
|
|
42 |
VirtualMidiDevice::~VirtualMidiDevice() { |
VirtualMidiDevice::~VirtualMidiDevice() { |
43 |
if (pPrivateData) delete (_private_data_t*)pPrivateData; |
delete p; |
44 |
} |
} |
45 |
|
|
46 |
bool VirtualMidiDevice::SendNoteOnToSampler(uint8_t Key, uint8_t Velocity) { |
bool VirtualMidiDevice::SendNoteOnToSampler(uint8_t Key, uint8_t Velocity) { |
47 |
if (Key >= MIDI_KEYS || Velocity > 127) return false; |
if (Key >= MIDI_KEYS || Velocity > 127) return false; |
|
_private_data_t* p = (_private_data_t*)pPrivateData; |
|
48 |
event_t ev = { EVENT_TYPE_NOTEON, Key, Velocity }; |
event_t ev = { EVENT_TYPE_NOTEON, Key, Velocity }; |
49 |
if (p->events.write_space() <= 0) return false; |
if (p->events.write_space() <= 0) return false; |
50 |
p->events.push(&ev); |
p->events.push(&ev); |
53 |
|
|
54 |
bool VirtualMidiDevice::SendNoteOffToSampler(uint8_t Key, uint8_t Velocity) { |
bool VirtualMidiDevice::SendNoteOffToSampler(uint8_t Key, uint8_t Velocity) { |
55 |
if (Key >= MIDI_KEYS || Velocity > 127) return false; |
if (Key >= MIDI_KEYS || Velocity > 127) return false; |
|
_private_data_t* p = (_private_data_t*)pPrivateData; |
|
56 |
event_t ev = { EVENT_TYPE_NOTEOFF, Key, Velocity }; |
event_t ev = { EVENT_TYPE_NOTEOFF, Key, Velocity }; |
57 |
if (p->events.write_space() <= 0) return false; |
if (p->events.write_space() <= 0) return false; |
58 |
p->events.push(&ev); |
p->events.push(&ev); |
60 |
} |
} |
61 |
|
|
62 |
bool VirtualMidiDevice::GetMidiEventFromDevice(event_t& Event) { |
bool VirtualMidiDevice::GetMidiEventFromDevice(event_t& Event) { |
|
_private_data_t* p = (_private_data_t*)pPrivateData; |
|
63 |
return (p->events.pop(&Event) > 0); |
return (p->events.pop(&Event) > 0); |
64 |
} |
} |
65 |
|
|
66 |
bool VirtualMidiDevice::NotesChanged() { |
bool VirtualMidiDevice::NotesChanged() { |
|
_private_data_t* p = (_private_data_t*)pPrivateData; |
|
67 |
int c = atomic_read( &p->notesChanged ); |
int c = atomic_read( &p->notesChanged ); |
68 |
atomic_sub(c, &p->notesChanged ); |
atomic_sub(c, &p->notesChanged ); |
69 |
return c; |
return c; |
70 |
} |
} |
71 |
|
|
72 |
bool VirtualMidiDevice::NoteChanged(uint8_t Key) { |
bool VirtualMidiDevice::NoteChanged(uint8_t Key) { |
|
_private_data_t* p = (_private_data_t*)pPrivateData; |
|
73 |
int c = atomic_read( &(p->pNoteChanged)[Key] ); |
int c = atomic_read( &(p->pNoteChanged)[Key] ); |
74 |
atomic_sub(c, &(p->pNoteChanged)[Key] ); |
atomic_sub(c, &(p->pNoteChanged)[Key] ); |
75 |
return c; |
return c; |
76 |
} |
} |
77 |
|
|
78 |
bool VirtualMidiDevice::NoteIsActive(uint8_t Key) { |
bool VirtualMidiDevice::NoteIsActive(uint8_t Key) { |
|
_private_data_t* p = (_private_data_t*)pPrivateData; |
|
79 |
return atomic_read( &(p->pNoteIsActive)[Key] ); |
return atomic_read( &(p->pNoteIsActive)[Key] ); |
80 |
} |
} |
81 |
|
|
82 |
uint8_t VirtualMidiDevice::NoteOnVelocity(uint8_t Key) { |
uint8_t VirtualMidiDevice::NoteOnVelocity(uint8_t Key) { |
|
_private_data_t* p = (_private_data_t*)pPrivateData; |
|
83 |
return atomic_read( &(p->pNoteOnVelocity)[Key] ); |
return atomic_read( &(p->pNoteOnVelocity)[Key] ); |
84 |
} |
} |
85 |
|
|
86 |
uint8_t VirtualMidiDevice::NoteOffVelocity(uint8_t Key) { |
uint8_t VirtualMidiDevice::NoteOffVelocity(uint8_t Key) { |
|
_private_data_t* p = (_private_data_t*)pPrivateData; |
|
87 |
return atomic_read( &(p->pNoteOffVelocity)[Key] ); |
return atomic_read( &(p->pNoteOffVelocity)[Key] ); |
88 |
} |
} |
89 |
|
|
90 |
void VirtualMidiDevice::SendNoteOnToDevice(uint8_t Key, uint8_t Velocity) { |
void VirtualMidiDevice::SendNoteOnToDevice(uint8_t Key, uint8_t Velocity) { |
91 |
if (Key >= MIDI_KEYS) return; |
if (Key >= MIDI_KEYS) return; |
|
_private_data_t* p = (_private_data_t*)pPrivateData; |
|
92 |
atomic_set( &(p->pNoteOnVelocity)[Key], Velocity ); |
atomic_set( &(p->pNoteOnVelocity)[Key], Velocity ); |
93 |
atomic_inc( &(p->pNoteIsActive)[Key] ); |
atomic_inc( &(p->pNoteIsActive)[Key] ); |
94 |
atomic_inc( &(p->pNoteChanged)[Key] ); |
atomic_inc( &(p->pNoteChanged)[Key] ); |
97 |
|
|
98 |
void VirtualMidiDevice::SendNoteOffToDevice(uint8_t Key, uint8_t Velocity) { |
void VirtualMidiDevice::SendNoteOffToDevice(uint8_t Key, uint8_t Velocity) { |
99 |
if (Key >= MIDI_KEYS) return; |
if (Key >= MIDI_KEYS) return; |
|
_private_data_t* p = (_private_data_t*)pPrivateData; |
|
100 |
atomic_set( &(p->pNoteOffVelocity)[Key], Velocity ); |
atomic_set( &(p->pNoteOffVelocity)[Key], Velocity ); |
101 |
atomic_dec( &(p->pNoteIsActive)[Key] ); |
atomic_dec( &(p->pNoteIsActive)[Key] ); |
102 |
atomic_inc( &(p->pNoteChanged)[Key] ); |
atomic_inc( &(p->pNoteChanged)[Key] ); |