/[svn]/linuxsampler/trunk/src/drivers/midi/VirtualMidiDevice.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/drivers/midi/VirtualMidiDevice.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1663 - (show annotations) (download)
Mon Feb 4 00:38:08 2008 UTC (16 years, 2 months ago) by schoenebeck
File size: 4218 byte(s)
* provide velocity information to instrument
  editors now as well

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 atomic_t pNoteOnVelocity[MIDI_KEYS];
24 atomic_t pNoteOffVelocity[MIDI_KEYS];
25 RingBuffer<VirtualMidiDevice::event_t,false> events;
26
27 _private_data_t() : events(MAX_EVENTS, 0) {}
28 };
29
30 VirtualMidiDevice::VirtualMidiDevice() {
31 _private_data_t* p = new _private_data_t;
32 pPrivateData = p;
33 atomic_t zero = ATOMIC_INIT(0);
34 atomic_t defaultVelocity = ATOMIC_INIT(127);
35 p->notesChanged = zero;
36 for (int i = 0; i < MIDI_KEYS; i++) {
37 p->pNoteChanged[i] = zero;
38 p->pNoteIsActive[i] = zero;
39 p->pNoteOnVelocity[i] = defaultVelocity;
40 p->pNoteOffVelocity[i] = defaultVelocity;
41 }
42 }
43
44 VirtualMidiDevice::~VirtualMidiDevice() {
45 if (pPrivateData) delete (_private_data_t*)pPrivateData;
46 }
47
48 bool VirtualMidiDevice::SendNoteOnToSampler(uint8_t Key, uint8_t Velocity) {
49 if (Key >= MIDI_KEYS || Velocity > 127) return false;
50 _private_data_t* p = (_private_data_t*)pPrivateData;
51 event_t ev = { EVENT_TYPE_NOTEON, Key, Velocity };
52 if (p->events.write_space() <= 0) return false;
53 p->events.push(&ev);
54 return true;
55 }
56
57 bool VirtualMidiDevice::SendNoteOffToSampler(uint8_t Key, uint8_t Velocity) {
58 if (Key >= MIDI_KEYS || Velocity > 127) return false;
59 _private_data_t* p = (_private_data_t*)pPrivateData;
60 event_t ev = { EVENT_TYPE_NOTEOFF, Key, Velocity };
61 if (p->events.write_space() <= 0) return false;
62 p->events.push(&ev);
63 return true;
64 }
65
66 bool VirtualMidiDevice::GetMidiEventFromDevice(event_t& Event) {
67 _private_data_t* p = (_private_data_t*)pPrivateData;
68 return (p->events.pop(&Event) > 0);
69 }
70
71 bool VirtualMidiDevice::NotesChanged() {
72 _private_data_t* p = (_private_data_t*)pPrivateData;
73 int c = atomic_read( &p->notesChanged );
74 atomic_sub(c, &p->notesChanged );
75 return c;
76 }
77
78 bool VirtualMidiDevice::NoteChanged(uint8_t Key) {
79 _private_data_t* p = (_private_data_t*)pPrivateData;
80 int c = atomic_read( &(p->pNoteChanged)[Key] );
81 atomic_sub(c, &(p->pNoteChanged)[Key] );
82 return c;
83 }
84
85 bool VirtualMidiDevice::NoteIsActive(uint8_t Key) {
86 _private_data_t* p = (_private_data_t*)pPrivateData;
87 return atomic_read( &(p->pNoteIsActive)[Key] );
88 }
89
90 uint8_t VirtualMidiDevice::NoteOnVelocity(uint8_t Key) {
91 _private_data_t* p = (_private_data_t*)pPrivateData;
92 return atomic_read( &(p->pNoteOnVelocity)[Key] );
93 }
94
95 uint8_t VirtualMidiDevice::NoteOffVelocity(uint8_t Key) {
96 _private_data_t* p = (_private_data_t*)pPrivateData;
97 return atomic_read( &(p->pNoteOffVelocity)[Key] );
98 }
99
100 void VirtualMidiDevice::SendNoteOnToDevice(uint8_t Key, uint8_t Velocity) {
101 if (Key >= MIDI_KEYS) return;
102 _private_data_t* p = (_private_data_t*)pPrivateData;
103 atomic_set( &(p->pNoteOnVelocity)[Key], Velocity );
104 atomic_inc( &(p->pNoteIsActive)[Key] );
105 atomic_inc( &(p->pNoteChanged)[Key] );
106 atomic_inc( &p->notesChanged );
107 }
108
109 void VirtualMidiDevice::SendNoteOffToDevice(uint8_t Key, uint8_t Velocity) {
110 if (Key >= MIDI_KEYS) return;
111 _private_data_t* p = (_private_data_t*)pPrivateData;
112 atomic_set( &(p->pNoteOffVelocity)[Key], Velocity );
113 atomic_dec( &(p->pNoteIsActive)[Key] );
114 atomic_inc( &(p->pNoteChanged)[Key] );
115 atomic_inc( &p->notesChanged );
116 }
117
118 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC