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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1295 - (show annotations) (download)
Mon Aug 13 20:05:56 2007 UTC (16 years, 7 months ago) by iliev
File size: 6794 byte(s)
* fixed a crash which occurs when changing the number of ports of a MIDI
  device connected to a sampler channel to number less then or equal
  to the index of the port to which the sampler channel is connected

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005, 2006 Christian Schoenebeck *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21 * MA 02111-1307 USA *
22 ***************************************************************************/
23
24 #include "MidiInputDevice.h"
25
26 #include "../../Sampler.h"
27
28 namespace LinuxSampler {
29
30 // *************** ParameterActive ***************
31 // *
32
33 MidiInputDevice::ParameterActive::ParameterActive() : DeviceCreationParameterBool() {
34 InitWithDefault();
35 }
36
37 MidiInputDevice::ParameterActive::ParameterActive(String active) : DeviceCreationParameterBool(active) {
38 }
39
40 String MidiInputDevice::ParameterActive::Description() {
41 return "Enable / disable device";
42 }
43
44 bool MidiInputDevice::ParameterActive::Fix() {
45 return false;
46 }
47
48 bool MidiInputDevice::ParameterActive::Mandatory() {
49 return false;
50 }
51
52 std::map<String,DeviceCreationParameter*> MidiInputDevice::ParameterActive::DependsAsParameters() {
53 return std::map<String,DeviceCreationParameter*>();
54 }
55
56 optional<bool> MidiInputDevice::ParameterActive::DefaultAsBool(std::map<String,String> Parameters) {
57 return true;
58 }
59
60 void MidiInputDevice::ParameterActive::OnSetValue(bool b) throw (Exception) {
61 if (b) ((MidiInputDevice*)pDevice)->Listen();
62 else ((MidiInputDevice*)pDevice)->StopListen();
63 }
64
65 String MidiInputDevice::ParameterActive::Name() {
66 return "ACTIVE";
67 }
68
69
70
71 // *************** ParameterPorts ***************
72 // *
73
74 MidiInputDevice::ParameterPorts::ParameterPorts() : DeviceCreationParameterInt() {
75 InitWithDefault();
76 }
77
78 MidiInputDevice::ParameterPorts::ParameterPorts(String val) : DeviceCreationParameterInt(val) {
79 }
80
81 String MidiInputDevice::ParameterPorts::Description() {
82 return "Number of ports";
83 }
84
85 bool MidiInputDevice::ParameterPorts::Fix() {
86 return false;
87 }
88
89 bool MidiInputDevice::ParameterPorts::Mandatory() {
90 return false;
91 }
92
93 std::map<String,DeviceCreationParameter*> MidiInputDevice::ParameterPorts::DependsAsParameters() {
94 return std::map<String,DeviceCreationParameter*>();
95 }
96
97 optional<int> MidiInputDevice::ParameterPorts::DefaultAsInt(std::map<String,String> Parameters) {
98 return 1;
99 }
100
101 optional<int> MidiInputDevice::ParameterPorts::RangeMinAsInt(std::map<String,String> Parameters) {
102 return optional<int>::nothing;
103 }
104
105 optional<int> MidiInputDevice::ParameterPorts::RangeMaxAsInt(std::map<String,String> Parameters) {
106 return optional<int>::nothing;
107 }
108
109 std::vector<int> MidiInputDevice::ParameterPorts::PossibilitiesAsInt(std::map<String,String> Parameters) {
110 return std::vector<int>();
111 }
112
113 void MidiInputDevice::ParameterPorts::OnSetValue(int i) throw (Exception) {
114 MidiInputDevice* dev = static_cast<MidiInputDevice*> (pDevice);
115 Sampler* s = static_cast<Sampler*> (dev->pSampler);
116 std::map<uint, SamplerChannel*> channels = s->GetSamplerChannels();
117 std::map<uint, SamplerChannel*>::iterator iter = channels.begin();
118 for (; iter != channels.end(); iter++) {
119 SamplerChannel* chn = iter->second;
120 if (chn->GetMidiInputDevice() == NULL || chn->GetMidiInputDevice() != pDevice) {
121 continue;
122 }
123
124 int port = chn->GetMidiInputPort();
125 if (port >= i) {
126 String err = "Sampler channel " + ToString(iter->first);
127 err += " is still connected to MIDI port " + ToString(port);
128 throw Exception(err);
129 }
130 }
131
132 ((MidiInputDevice*)pDevice)->AcquirePorts(i);
133 }
134
135 String MidiInputDevice::ParameterPorts::Name() {
136 return "PORTS";
137 }
138
139
140
141 // *************** MidiInputDevice ***************
142 // *
143
144 MidiInputDevice::MidiInputDevice(std::map<String,DeviceCreationParameter*> DriverParameters, void* pSampler) {
145 this->Parameters = DriverParameters;
146 this->pSampler = pSampler;
147 }
148
149 MidiInputDevice::~MidiInputDevice() {
150 std::map<String,DeviceCreationParameter*>::iterator iter = Parameters.begin();
151 while (iter != Parameters.end()) {
152 delete iter->second;
153 iter++;
154 }
155 Parameters.clear();
156 }
157
158 MidiInputPort* MidiInputDevice::GetPort(uint iPort) throw (MidiInputException) {
159 if (iPort >= Ports.size()) throw MidiInputException("There is no port " + ToString(iPort));
160 return Ports[iPort];
161 }
162
163 std::map<String,DeviceCreationParameter*> MidiInputDevice::DeviceParameters() {
164 return Parameters;
165 }
166
167 void MidiInputDevice::AcquirePorts(uint newPorts) {
168 int diff = this->Ports.size() - newPorts;
169 if (!diff)
170 return; //Number of ports matches already, nothing to do.
171
172 while (diff != 0) {
173 if (diff > 0) { //We've got too many ports, remove one
174 std::map<int,MidiInputPort*>::iterator portsIter = Ports.end();
175 --portsIter;
176 delete portsIter->second;
177 Ports.erase(portsIter);
178 diff--;
179 }
180 if (diff < 0) { //We don't have enough ports, create one
181 MidiInputPort* midiPort = this->CreateMidiPort();
182 Ports[midiPort->portNumber] = midiPort;
183 diff++;
184 }
185 }
186 }
187
188 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC