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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 174 - (show annotations) (download)
Tue Jul 6 03:27:38 2004 UTC (19 years, 8 months ago) by senkov
File size: 5616 byte(s)
* Reworked the infrastructure to allow for parameter
registration and creation
* Changed alsa audio output and midi drivers
to work with new infrastructure

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * *
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 MidiInputDevice::MidiInputDevice(std::map<String,DeviceCreationParameter*> DriverParameters) {
28 this->Parameters = DriverParameters;
29 }
30
31 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 MidiInputDevice::MidiInputPort::~MidiInputPort() {
41 std::map<String,DeviceCreationParameter*>::iterator iter = Parameters.begin();
42 while (iter != Parameters.end()) {
43 Parameters.erase(iter);
44 delete iter->second;
45 iter++;
46 }
47 }
48
49 MidiInputDevice::MidiInputPort::MidiInputPort(MidiInputDevice* pDevice, int portNumber) {
50 this->pDevice = pDevice;
51 this->portNumber = portNumber;
52 Parameters["name"] = new ParameterName(this);
53 }
54
55 MidiInputDevice* MidiInputDevice::MidiInputPort::GetDevice() {
56 return pDevice;
57 }
58
59 uint MidiInputDevice::MidiInputPort::GetPortNumber() {
60 return portNumber;
61 }
62
63 std::map<String,DeviceCreationParameter*> MidiInputDevice::DeviceParameters() {
64 return Parameters;
65 }
66
67 std::map<String,DeviceCreationParameter*> MidiInputDevice::MidiInputPort::DeviceParameters() {
68 return Parameters;
69 }
70
71 void MidiInputDevice::MidiInputPort::DispatchNoteOn(uint8_t Key, uint8_t Velocity, uint MidiChannel) {
72 std::set<Engine*>::iterator engineiter = MidiChannelMap[MidiChannel].begin();
73 std::set<Engine*>::iterator end = MidiChannelMap[MidiChannel].end();
74 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOn(Key, Velocity);
75 }
76
77 void MidiInputDevice::MidiInputPort::DispatchNoteOff(uint8_t Key, uint8_t Velocity, uint MidiChannel) {
78 std::set<Engine*>::iterator engineiter = MidiChannelMap[MidiChannel].begin();
79 std::set<Engine*>::iterator end = MidiChannelMap[MidiChannel].end();
80 for (; engineiter != end; engineiter++) (*engineiter)->SendNoteOff(Key, Velocity);
81 }
82
83 void MidiInputDevice::MidiInputPort::DispatchPitchbend(int Pitch, uint MidiChannel) {
84 std::set<Engine*>::iterator engineiter = MidiChannelMap[MidiChannel].begin();
85 std::set<Engine*>::iterator end = MidiChannelMap[MidiChannel].end();
86 for (; engineiter != end; engineiter++) (*engineiter)->SendPitchbend(Pitch);
87 }
88
89 void MidiInputDevice::MidiInputPort::DispatchControlChange(uint8_t Controller, uint8_t Value, uint MidiChannel) {
90 std::set<Engine*>::iterator engineiter = MidiChannelMap[MidiChannel].begin();
91 std::set<Engine*>::iterator end = MidiChannelMap[MidiChannel].end();
92 for (; engineiter != end; engineiter++) (*engineiter)->SendControlChange(Controller, Value);
93 }
94
95 void MidiInputDevice::MidiInputPort::Connect(Engine* pEngine, midi_chan_t MidiChannel) {
96 if (MidiChannel < 0 || MidiChannel > 16)
97 throw MidiInputException("MIDI channel index out of bounds");
98 Disconnect(pEngine);
99 MidiChannelMap[MidiChannel].insert(pEngine);
100 }
101
102 void MidiInputDevice::MidiInputPort::Disconnect(Engine* pEngine) {
103 try { for (int i = 0; i <= 16; i++) MidiChannelMap[i].erase(pEngine); }
104 catch(...) { /* NOOP */ }
105 }
106
107 void MidiInputDevice::AcquirePorts(uint newPorts) {
108 int diff = this->Ports.size() - newPorts;
109 if (!diff)
110 return; //Number of ports matches already, nothing to do.
111
112 while (diff != 0) {
113 if (diff > 0) { //We've got too many ports, remove one
114 std::map<int,MidiInputPort*>::iterator portsIter = Ports.end();
115 --portsIter;
116 Ports.erase(portsIter);
117 delete portsIter->second;
118 diff--;
119 }
120 if (diff < 0) { //We don't have enough ports, create one
121 MidiInputPort* midiPort = this->CreateMidiPort();
122 Ports[midiPort->portNumber] = midiPort;
123 diff++;
124 }
125 }
126 }
127
128 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC