/[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 1896 - (show annotations) (download)
Mon May 4 18:34:44 2009 UTC (13 years, 9 months ago) by persson
File size: 3526 byte(s)
* bugfix: two private structs had the same name, which could cause
  problems if the linker chose the wrong constructor

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

  ViewVC Help
Powered by ViewVC