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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1663 by schoenebeck, Mon Feb 4 00:38:08 2008 UTC revision 2384 by schoenebeck, Fri Dec 14 16:04:49 2012 UTC
# Line 1  Line 1 
1  /*  /*
2      Copyright (C) 2008 Christian Schoenebeck      Copyright (C) 2008 - 2012 Christian Schoenebeck
3   */   */
4    
5  #include "VirtualMidiDevice.h"  #include "VirtualMidiDevice.h"
# Line 8  Line 8 
8  #include "../../common/atomic.h"  #include "../../common/atomic.h"
9  #include "../../common/RingBuffer.h"  #include "../../common/RingBuffer.h"
10    
11  #define MIDI_KEYS       128  #define MIDI_KEYS               128
12    #define MIDI_CONTROLLERS        128
13    
14  // assuming VirtualMidiDevice implementation is only controlled  // assuming VirtualMidiDevice implementation is only controlled
15  // by mouse (and the user not being Billy the Kid)  // by mouse (and the user not being Billy the Kid)
# Line 16  Line 17 
17    
18  namespace LinuxSampler {  namespace LinuxSampler {
19    
20      struct _private_data_t {      struct VirtualMidiDevice::private_data_t {
21          atomic_t notesChanged; // whether some key changed at all          atomic_t notesChanged; // whether some key changed at all
22          atomic_t pNoteChanged[MIDI_KEYS]; // which key(s) changed          atomic_t pNoteChanged[MIDI_KEYS]; // which key(s) changed
23          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 pNoteOnVelocity[MIDI_KEYS];          atomic_t pNoteOnVelocity[MIDI_KEYS];
25          atomic_t pNoteOffVelocity[MIDI_KEYS];          atomic_t pNoteOffVelocity[MIDI_KEYS];
26            atomic_t ccsChanged; // whether some controller changed at all
27            atomic_t pCCChanged[MIDI_CONTROLLERS]; // which controller(s) changed
28            atomic_t pCCValue[MIDI_CONTROLLERS]; // current value of each controller
29          RingBuffer<VirtualMidiDevice::event_t,false> events;          RingBuffer<VirtualMidiDevice::event_t,false> events;
30    
31          _private_data_t() : events(MAX_EVENTS, 0) {}          private_data_t() : events(MAX_EVENTS, 0) {}
32      };      };
33    
34      VirtualMidiDevice::VirtualMidiDevice() {      VirtualMidiDevice::VirtualMidiDevice() : p(new private_data_t) {
         _private_data_t* p = new _private_data_t;  
         pPrivateData = p;  
35          atomic_t zero = ATOMIC_INIT(0);          atomic_t zero = ATOMIC_INIT(0);
36          atomic_t defaultVelocity = ATOMIC_INIT(127);          atomic_t defaultVelocity = ATOMIC_INIT(127);
37            atomic_t defaultCCValue = ATOMIC_INIT(0);
38          p->notesChanged = zero;          p->notesChanged = zero;
39            p->ccsChanged   = zero;
40          for (int i = 0; i < MIDI_KEYS; i++) {          for (int i = 0; i < MIDI_KEYS; i++) {
41              p->pNoteChanged[i]  = zero;              p->pNoteChanged[i]  = zero;
42              p->pNoteIsActive[i] = zero;              p->pNoteIsActive[i] = zero;
43              p->pNoteOnVelocity[i] = defaultVelocity;              p->pNoteOnVelocity[i] = defaultVelocity;
44              p->pNoteOffVelocity[i] = defaultVelocity;              p->pNoteOffVelocity[i] = defaultVelocity;
45                p->pCCChanged[i] = zero;
46                p->pCCValue[i]   = defaultCCValue;
47          }          }
48      }      }
49    
50      VirtualMidiDevice::~VirtualMidiDevice() {      VirtualMidiDevice::~VirtualMidiDevice() {
51          if (pPrivateData) delete (_private_data_t*)pPrivateData;          delete p;
52        }
53        
54        void VirtualMidiDevice::SetMaxEvents(int n) {
55            p->events.resize(n);
56      }      }
57    
58      bool VirtualMidiDevice::SendNoteOnToSampler(uint8_t Key, uint8_t Velocity) {      bool VirtualMidiDevice::SendNoteOnToSampler(uint8_t Key, uint8_t Velocity) {
59          if (Key >= MIDI_KEYS || Velocity > 127) return false;          if (Key >= MIDI_KEYS || Velocity > 127) return false;
         _private_data_t* p = (_private_data_t*)pPrivateData;  
60          event_t ev = { EVENT_TYPE_NOTEON, Key, Velocity };          event_t ev = { EVENT_TYPE_NOTEON, Key, Velocity };
61          if (p->events.write_space() <= 0) return false;          if (p->events.write_space() <= 0) return false;
62          p->events.push(&ev);          p->events.push(&ev);
# Line 56  namespace LinuxSampler { Line 65  namespace LinuxSampler {
65    
66      bool VirtualMidiDevice::SendNoteOffToSampler(uint8_t Key, uint8_t Velocity) {      bool VirtualMidiDevice::SendNoteOffToSampler(uint8_t Key, uint8_t Velocity) {
67          if (Key >= MIDI_KEYS || Velocity > 127) return false;          if (Key >= MIDI_KEYS || Velocity > 127) return false;
         _private_data_t* p = (_private_data_t*)pPrivateData;  
68          event_t ev = { EVENT_TYPE_NOTEOFF, Key, Velocity };          event_t ev = { EVENT_TYPE_NOTEOFF, Key, Velocity };
69          if (p->events.write_space() <= 0) return false;          if (p->events.write_space() <= 0) return false;
70          p->events.push(&ev);          p->events.push(&ev);
71          return true;          return true;
72      }      }
73        
74        bool VirtualMidiDevice::SendCCToSampler(uint8_t Controller, uint8_t Value) {
75            if (Controller >= MIDI_CONTROLLERS || Value > 127) return false;
76            event_t ev = { EVENT_TYPE_CC, Controller, Value };
77            if (p->events.write_space() <= 0) return false;
78            p->events.push(&ev);
79            return true;
80        }
81    
82      bool VirtualMidiDevice::GetMidiEventFromDevice(event_t& Event) {      bool VirtualMidiDevice::GetMidiEventFromDevice(event_t& Event) {
         _private_data_t* p = (_private_data_t*)pPrivateData;  
83          return (p->events.pop(&Event) > 0);          return (p->events.pop(&Event) > 0);
84      }      }
85    
86      bool VirtualMidiDevice::NotesChanged() {      bool VirtualMidiDevice::NotesChanged() {
         _private_data_t* p = (_private_data_t*)pPrivateData;  
87          int c = atomic_read( &p->notesChanged );          int c = atomic_read( &p->notesChanged );
88          atomic_sub(c, &p->notesChanged );          atomic_sub(c, &p->notesChanged );
89          return c;          return c;
90      }      }
91        
92        bool VirtualMidiDevice::ControllersChanged() {
93            int c = atomic_read( &p->ccsChanged );
94            atomic_sub(c, &p->ccsChanged );
95            return c;
96        }
97    
98      bool VirtualMidiDevice::NoteChanged(uint8_t Key) {      bool VirtualMidiDevice::NoteChanged(uint8_t Key) {
         _private_data_t* p = (_private_data_t*)pPrivateData;  
99          int c = atomic_read( &(p->pNoteChanged)[Key] );          int c = atomic_read( &(p->pNoteChanged)[Key] );
100          atomic_sub(c, &(p->pNoteChanged)[Key] );          atomic_sub(c, &(p->pNoteChanged)[Key] );
101          return c;          return c;
102      }      }
103        
104        bool VirtualMidiDevice::ControllerChanged(uint8_t Controller) {
105            int c = atomic_read( &(p->pCCChanged)[Controller] );
106            atomic_sub(c, &(p->pCCChanged)[Controller] );
107            return c;
108        }
109    
110      bool VirtualMidiDevice::NoteIsActive(uint8_t Key) {      bool VirtualMidiDevice::NoteIsActive(uint8_t Key) {
         _private_data_t* p = (_private_data_t*)pPrivateData;  
111          return atomic_read( &(p->pNoteIsActive)[Key] );          return atomic_read( &(p->pNoteIsActive)[Key] );
112      }      }
113    
114      uint8_t VirtualMidiDevice::NoteOnVelocity(uint8_t Key) {      uint8_t VirtualMidiDevice::NoteOnVelocity(uint8_t Key) {
         _private_data_t* p = (_private_data_t*)pPrivateData;  
115          return atomic_read( &(p->pNoteOnVelocity)[Key] );          return atomic_read( &(p->pNoteOnVelocity)[Key] );
116      }      }
117    
118      uint8_t VirtualMidiDevice::NoteOffVelocity(uint8_t Key) {      uint8_t VirtualMidiDevice::NoteOffVelocity(uint8_t Key) {
         _private_data_t* p = (_private_data_t*)pPrivateData;  
119          return atomic_read( &(p->pNoteOffVelocity)[Key] );          return atomic_read( &(p->pNoteOffVelocity)[Key] );
120      }      }
121        
122        uint8_t VirtualMidiDevice::ControllerValue(uint8_t Controller) {
123            return atomic_read( &(p->pCCValue)[Controller] );
124        }
125    
126      void VirtualMidiDevice::SendNoteOnToDevice(uint8_t Key, uint8_t Velocity) {      void VirtualMidiDevice::SendNoteOnToDevice(uint8_t Key, uint8_t Velocity) {
127          if (Key >= MIDI_KEYS) return;          if (Key >= MIDI_KEYS) return;
         _private_data_t* p = (_private_data_t*)pPrivateData;  
128          atomic_set( &(p->pNoteOnVelocity)[Key], Velocity );          atomic_set( &(p->pNoteOnVelocity)[Key], Velocity );
129          atomic_inc( &(p->pNoteIsActive)[Key] );          atomic_inc( &(p->pNoteIsActive)[Key] );
130          atomic_inc( &(p->pNoteChanged)[Key] );          atomic_inc( &(p->pNoteChanged)[Key] );
# Line 108  namespace LinuxSampler { Line 133  namespace LinuxSampler {
133    
134      void VirtualMidiDevice::SendNoteOffToDevice(uint8_t Key, uint8_t Velocity) {      void VirtualMidiDevice::SendNoteOffToDevice(uint8_t Key, uint8_t Velocity) {
135          if (Key >= MIDI_KEYS) return;          if (Key >= MIDI_KEYS) return;
         _private_data_t* p = (_private_data_t*)pPrivateData;  
136          atomic_set( &(p->pNoteOffVelocity)[Key], Velocity );          atomic_set( &(p->pNoteOffVelocity)[Key], Velocity );
137          atomic_dec( &(p->pNoteIsActive)[Key] );          if (atomic_read( &(p->pNoteIsActive)[Key] )) // only decrement if not zero
138                atomic_dec( &(p->pNoteIsActive)[Key] );
139          atomic_inc( &(p->pNoteChanged)[Key] );          atomic_inc( &(p->pNoteChanged)[Key] );
140          atomic_inc( &p->notesChanged );          atomic_inc( &p->notesChanged );
141      }      }
142        
143        void VirtualMidiDevice::SendCCToDevice(uint8_t Controller, uint8_t Value) {
144            if (Controller >= MIDI_CONTROLLERS) return;
145            atomic_set( &(p->pCCValue)[Controller], Value );
146            atomic_inc( &(p->pCCChanged)[Controller] );
147            atomic_inc( &p->ccsChanged );
148        }
149    
150  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.1663  
changed lines
  Added in v.2384

  ViewVC Help
Powered by ViewVC