/[svn]/linuxsampler/trunk/src/mididriver/MidiInputDevice.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/mididriver/MidiInputDevice.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 153 - (hide annotations) (download)
Mon Jun 28 04:21:11 2004 UTC (19 years, 10 months ago) by senkov
File size: 6830 byte(s)
* Updated MIDI infrastructure similar to what was previously
done with the AUDIO
* Implemented Alsa driver using new infrastructure
* TODO: MacOS drivers!

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 53 * *
7     * This program is free software; you can redistribute it and/or modify *
8     * it under the terms of the GNU General Public License as published by *
9     * the Free Software Foundation; either version 2 of the License, or *
10     * (at your option) any later version. *
11     * *
12     * This program is distributed in the hope that it will be useful, *
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15     * GNU General Public License for more details. *
16     * *
17     * You should have received a copy of the GNU General Public License *
18     * along with this program; if not, write to the Free Software *
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20     * MA 02111-1307 USA *
21     ***************************************************************************/
22    
23     #include "MidiInputDevice.h"
24    
25     namespace LinuxSampler {
26    
27 senkov 153 MidiInputDevice::MidiInputDevice(std::map<String,DeviceCreationParameter*> DriverParameters) {
28     this->Parameters = DriverParameters;
29 schoenebeck 64 }
30    
31 senkov 153 MidiInputDevice::~MidiInputDevice() {
32     std::map<String,DeviceCreationParameter*>::iterator iter = Parameters.begin();
33     while (iter != Parameters.end()) {
34     Parameters.erase(iter);
35     delete iter->second;
36     iter++;
37     }
38     }
39    
40     std::map<String,DeviceCreationParameter*> MidiInputDevice::AvailableParameters() {
41     static const std::map<String,DeviceCreationParameter*> available_parameters = CreateAvailableParameters();
42     return available_parameters;
43     }
44    
45     std::map<String,DeviceCreationParameter*> MidiInputDevice::CreateAvailableParameters() {
46     static ParameterActive param_active(NULL);
47     static ParameterPorts param_ports(NULL);
48     std::map<String,DeviceCreationParameter*> result;
49     result["active"] = &param_active;
50     result["ports"] = &param_ports;
51     return result;
52     }
53    
54     MidiInputDevice::MidiInputPort::~MidiInputPort() {
55     std::map<String,DeviceCreationParameter*>::iterator iter = Parameters.begin();
56     while (iter != Parameters.end()) {
57     Parameters.erase(iter);
58     delete iter->second;
59     iter++;
60     }
61     }
62    
63     MidiInputDevice* MidiInputDevice::MidiInputPort::GetDevice() {
64     return pDevice;
65     }
66    
67     uint MidiInputDevice::MidiInputPort::GetPortNumber() {
68     return portNumber;
69     }
70    
71     std::map<String,DeviceCreationParameter*> MidiInputDevice::MidiInputPort::AvailableParameters() {
72     static const std::map<String,DeviceCreationParameter*> available_parameters = CreateAvailableParameters();
73     return available_parameters;
74     }
75    
76     std::map<String,DeviceCreationParameter*> MidiInputDevice::MidiInputPort::CreateAvailableParameters() {
77     static ParameterName param_name(NULL);
78     std::map<String,DeviceCreationParameter*> result;
79     result["name"] = &param_name;
80     return result;
81     }
82    
83     std::map<String,DeviceCreationParameter*> MidiInputDevice::DeviceParameters() {
84     return Parameters;
85     }
86    
87     std::map<String,DeviceCreationParameter*> MidiInputDevice::MidiInputPort::DeviceParameters() {
88     return Parameters;
89     }
90    
91     void MidiInputDevice::MidiInputPort::DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel) {
92 schoenebeck 53 std::set<Engine*>::iterator engineiter = MidiChannelMap[MidiChannel].begin();
93     std::set<Engine*>::iterator end = MidiChannelMap[MidiChannel].end();
94     for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity);
95     }
96    
97 senkov 153 void MidiInputDevice::MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel) {
98 schoenebeck 53 std::set<Engine*>::iterator engineiter = MidiChannelMap[MidiChannel].begin();
99     std::set<Engine*>::iterator end = MidiChannelMap[MidiChannel].end();
100     for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity);
101     }
102    
103 senkov 153 void MidiInputDevice::MidiInputPort::DispatchPitchbend(int Pitch, uint MidiChannel) {
104 schoenebeck 53 std::set<Engine*>::iterator engineiter = MidiChannelMap[MidiChannel].begin();
105     std::set<Engine*>::iterator end = MidiChannelMap[MidiChannel].end();
106     for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch);
107     }
108    
109 senkov 153 void MidiInputDevice::MidiInputPort::DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel) {
110 schoenebeck 53 std::set<Engine*>::iterator engineiter = MidiChannelMap[MidiChannel].begin();
111     std::set<Engine*>::iterator end = MidiChannelMap[MidiChannel].end();
112     for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value);
113     }
114    
115 senkov 153 void MidiInputDevice::MidiInputPort::Connect(Engine* pEngine, midi_chan_t MidiChannel) {
116 schoenebeck 53 if (MidiChannel < 0 || MidiChannel > 16)
117     throw MidiInputException("MIDI channel index out of bounds");
118     Disconnect(pEngine);
119     MidiChannelMap[MidiChannel].insert(pEngine);
120     }
121    
122 senkov 153 void MidiInputDevice::MidiInputPort::Disconnect(Engine* pEngine) {
123 schoenebeck 53 try { for (int i = 0; i <= 16; i++) MidiChannelMap[i].erase(pEngine); }
124     catch(...) { /* NOOP */ }
125     }
126    
127 senkov 153 void MidiInputDevice::AcquirePorts(uint newPorts) {
128     int diff = this->Ports.size() - newPorts;
129     if (!diff)
130     return; //Number of ports matches already, nothing to do.
131    
132     while (diff != 0) {
133     if (diff > 0) { //We've got too many ports, remove one
134     std::map<int,MidiInputPort*>::iterator portsIter = Ports.end();
135     Ports.erase(--portsIter);
136     delete portsIter->second;
137     diff--;
138     }
139     if (diff < 0) { //We don't have enough ports, create one
140     MidiInputPort* midiPort = this->CreateMidiPort();
141     Ports[midiPort->portNumber] = midiPort;
142     diff++;
143     }
144     }
145 schoenebeck 64 }
146    
147 schoenebeck 53 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC