1 |
/* |
2 |
Copyright (C) 2008 Christian Schoenebeck |
3 |
*/ |
4 |
|
5 |
#include "VirtualMidiDevice.h" |
6 |
|
7 |
#include "../../common/global_private.h" |
8 |
#include "../../common/atomic.h" |
9 |
#include "../../common/RingBuffer.h" |
10 |
|
11 |
#define MIDI_KEYS 128 |
12 |
|
13 |
// assuming VirtualMidiDevice implementation is only controlled |
14 |
// by mouse (and the user not being Billy the Kid) |
15 |
#define MAX_EVENTS 12 |
16 |
|
17 |
namespace LinuxSampler { |
18 |
|
19 |
struct _private_data_t { |
20 |
atomic_t notesChanged; // whether some key changed at all |
21 |
atomic_t pNoteChanged[MIDI_KEYS]; // which key(s) changed |
22 |
atomic_t pNoteIsActive[MIDI_KEYS]; // status of each key (either active or inactive) |
23 |
RingBuffer<VirtualMidiDevice::event_t,false> events; |
24 |
|
25 |
_private_data_t() : events(MAX_EVENTS, 0) {} |
26 |
}; |
27 |
|
28 |
VirtualMidiDevice::VirtualMidiDevice() { |
29 |
_private_data_t* p = new _private_data_t; |
30 |
pPrivateData = p; |
31 |
atomic_t zero = ATOMIC_INIT(0); |
32 |
p->notesChanged = zero; |
33 |
for (int i = 0; i < MIDI_KEYS; i++) { |
34 |
p->pNoteChanged[i] = zero; |
35 |
p->pNoteIsActive[i] = zero; |
36 |
} |
37 |
} |
38 |
|
39 |
VirtualMidiDevice::~VirtualMidiDevice() { |
40 |
if (pPrivateData) delete (_private_data_t*)pPrivateData; |
41 |
} |
42 |
|
43 |
bool VirtualMidiDevice::SendNoteOnToSampler(uint8_t Key, uint8_t Velocity) { |
44 |
if (Key >= MIDI_KEYS || Velocity > 127) return false; |
45 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
46 |
event_t ev = { EVENT_TYPE_NOTEON, Key, Velocity }; |
47 |
if (p->events.write_space() <= 0) return false; |
48 |
p->events.push(&ev); |
49 |
return true; |
50 |
} |
51 |
|
52 |
bool VirtualMidiDevice::SendNoteOffToSampler(uint8_t Key, uint8_t Velocity) { |
53 |
if (Key >= MIDI_KEYS || Velocity > 127) return false; |
54 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
55 |
event_t ev = { EVENT_TYPE_NOTEOFF, Key, Velocity }; |
56 |
if (p->events.write_space() <= 0) return false; |
57 |
p->events.push(&ev); |
58 |
return true; |
59 |
} |
60 |
|
61 |
bool VirtualMidiDevice::GetMidiEventFromDevice(event_t& Event) { |
62 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
63 |
return (p->events.pop(&Event) > 0); |
64 |
} |
65 |
|
66 |
bool VirtualMidiDevice::NotesChanged() { |
67 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
68 |
int c = atomic_read( &p->notesChanged ); |
69 |
atomic_sub(c, &p->notesChanged ); |
70 |
return c; |
71 |
} |
72 |
|
73 |
bool VirtualMidiDevice::NoteChanged(uint8_t Key) { |
74 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
75 |
int c = atomic_read( &(p->pNoteChanged)[Key] ); |
76 |
atomic_sub(c, &(p->pNoteChanged)[Key] ); |
77 |
return c; |
78 |
} |
79 |
|
80 |
bool VirtualMidiDevice::NoteIsActive(uint8_t Key) { |
81 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
82 |
return atomic_read( &(p->pNoteIsActive)[Key] ); |
83 |
} |
84 |
|
85 |
void VirtualMidiDevice::SendNoteOnToDevice(uint8_t Key, uint8_t /*Velocity*/) { |
86 |
if (Key >= MIDI_KEYS) return; |
87 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
88 |
atomic_inc( &(p->pNoteIsActive)[Key] ); |
89 |
atomic_inc( &(p->pNoteChanged)[Key] ); |
90 |
atomic_inc( &p->notesChanged ); |
91 |
} |
92 |
|
93 |
void VirtualMidiDevice::SendNoteOffToDevice(uint8_t Key, uint8_t /*Velocity*/) { |
94 |
if (Key >= MIDI_KEYS) return; |
95 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
96 |
atomic_dec( &(p->pNoteIsActive)[Key] ); |
97 |
atomic_inc( &(p->pNoteChanged)[Key] ); |
98 |
atomic_inc( &p->notesChanged ); |
99 |
} |
100 |
|
101 |
} // namespace LinuxSampler |