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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1248 - (show annotations) (download)
Fri Jun 22 10:10:06 2007 UTC (16 years, 10 months ago) by persson
File size: 12711 byte(s)
* fixed some minor memory leaks

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 "MidiInputDeviceAlsa.h"
25 #include "MidiInputDeviceFactory.h"
26
27 #define perm_ok(pinfo,bits) ((snd_seq_port_info_get_capability(pinfo) & (bits)) == (bits))
28
29 namespace LinuxSampler {
30
31 // *************** ParameterName ***************
32 // *
33
34 MidiInputDeviceAlsa::MidiInputPortAlsa::ParameterName::ParameterName(MidiInputPort* pPort) throw (Exception) : MidiInputPort::ParameterName(pPort, "Port " + ToString(pPort->GetPortNumber())) {
35 OnSetValue(ValueAsString()); // initialize port name
36 }
37
38 void MidiInputDeviceAlsa::MidiInputPortAlsa::ParameterName::OnSetValue(String s) throw (Exception) {
39 if (s.size() > 16) throw Exception("Name too long for ALSA MIDI input port (max. 16 characters)");
40 snd_seq_port_info_t* hInfo;
41 snd_seq_port_info_malloc(&hInfo);
42 snd_seq_get_port_info(((MidiInputDeviceAlsa*)pPort->GetDevice())->hAlsaSeq, pPort->GetPortNumber(), hInfo);
43 snd_seq_port_info_set_name(hInfo, s.c_str());
44 snd_seq_set_port_info(((MidiInputDeviceAlsa*)pPort->GetDevice())->hAlsaSeq, pPort->GetPortNumber(), hInfo);
45 snd_seq_port_info_free(hInfo);
46 }
47
48
49
50 // *************** ParameterAlsaSeqBindings ***************
51 // *
52
53
54 MidiInputDeviceAlsa::MidiInputPortAlsa::ParameterAlsaSeqBindings::ParameterAlsaSeqBindings(MidiInputPortAlsa* pPort) : DeviceRuntimeParameterStrings( std::vector<String>() ) {
55 this->pPort = pPort;
56 }
57
58 String MidiInputDeviceAlsa::MidiInputPortAlsa::ParameterAlsaSeqBindings::Description() {
59 return "Bindings to other Alsa sequencer clients";
60 }
61 bool MidiInputDeviceAlsa::MidiInputPortAlsa::ParameterAlsaSeqBindings::Fix() {
62 return false;
63 }
64
65 std::vector<String> MidiInputDeviceAlsa::MidiInputPortAlsa::ParameterAlsaSeqBindings::PossibilitiesAsString() {
66 std::vector<String> res;
67 snd_seq_client_info_t* cinfo;
68 snd_seq_port_info_t* pinfo;
69
70 snd_seq_client_info_alloca(&cinfo);
71 snd_seq_port_info_alloca(&pinfo);
72 snd_seq_client_info_set_client(cinfo, -1);
73 while (snd_seq_query_next_client(pPort->pDevice->hAlsaSeq, cinfo) >= 0) {
74 snd_seq_port_info_set_client(pinfo, snd_seq_client_info_get_client(cinfo));
75 snd_seq_port_info_set_port(pinfo, -1);
76 while (snd_seq_query_next_port(pPort->pDevice->hAlsaSeq, pinfo) >= 0) {
77 if (perm_ok(pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ)) {
78 String seq_id = ToString(snd_seq_client_info_get_client(cinfo)) + ":" +
79 ToString(snd_seq_port_info_get_port(pinfo));
80 res.push_back(seq_id);
81 }
82 }
83 }
84
85 return res;
86 }
87
88 void MidiInputDeviceAlsa::MidiInputPortAlsa::ParameterAlsaSeqBindings::OnSetValue(std::vector<String> vS) throw (Exception) {
89 std::vector<String>::iterator iter = vS.begin();
90 for (; iter != vS.end(); iter++) pPort->ConnectToAlsaMidiSource((*iter).c_str());
91 }
92
93
94
95 // *************** ParameterAlsaSeqId ***************
96 // *
97
98 MidiInputDeviceAlsa::MidiInputPortAlsa::ParameterAlsaSeqId::ParameterAlsaSeqId(MidiInputPortAlsa* pPort)
99 : DeviceRuntimeParameterString(ToString(pPort->pDevice->hAlsaSeqClient) + ":" + ToString(pPort->portNumber)) {
100 }
101
102 String MidiInputDeviceAlsa::MidiInputPortAlsa::ParameterAlsaSeqId::Description() {
103 return "ALSA Sequencer ID";
104 }
105
106 bool MidiInputDeviceAlsa::MidiInputPortAlsa::ParameterAlsaSeqId::Fix() {
107 return true;
108 }
109
110 std::vector<String> MidiInputDeviceAlsa::MidiInputPortAlsa::ParameterAlsaSeqId::PossibilitiesAsString() {
111 return std::vector<String>(); // nothing
112 }
113
114 void MidiInputDeviceAlsa::MidiInputPortAlsa::ParameterAlsaSeqId::OnSetValue(String s) {
115 // not possible as parameter is 'fix'
116 }
117
118
119
120 // *************** MidiInputPortAlsa ***************
121 // *
122
123 MidiInputDeviceAlsa::MidiInputPortAlsa::MidiInputPortAlsa(MidiInputDeviceAlsa* pDevice) throw (MidiInputException) : MidiInputPort(pDevice, -1) {
124 this->pDevice = pDevice;
125
126 // create Alsa sequencer port
127 int alsaPort = snd_seq_create_simple_port(pDevice->hAlsaSeq, "unnamed port",
128 SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE,
129 SND_SEQ_PORT_TYPE_APPLICATION);
130 if (alsaPort < 0) throw MidiInputException("Error creating sequencer port");
131 this->portNumber = alsaPort;
132
133 delete Parameters["NAME"];
134 Parameters["NAME"] = new ParameterName(this);
135 Parameters["ALSA_SEQ_BINDINGS"] = new ParameterAlsaSeqBindings(this);
136 Parameters["ALSA_SEQ_ID"] = new ParameterAlsaSeqId(this);
137 }
138
139 MidiInputDeviceAlsa::MidiInputPortAlsa::~MidiInputPortAlsa() {
140 snd_seq_delete_simple_port(pDevice->hAlsaSeq, portNumber);
141 }
142
143 /**
144 * Connects this Alsa midi input device with an Alsa MIDI source.
145 *
146 * @param Client - Alsa sequencer client and port ID of a MIDI source
147 * (e.g. "64:0")
148 * @throws MidiInputException if connection cannot be established
149 */
150 void MidiInputDeviceAlsa::MidiInputPortAlsa::ConnectToAlsaMidiSource(const char* MidiSource) {
151 snd_seq_addr_t sender, dest;
152 snd_seq_port_subscribe_t* subs;
153 int hExtClient, hExtPort;
154
155 sscanf(MidiSource, "%d:%d", &hExtClient, &hExtPort);
156 sender.client = (char) hExtClient;
157 sender.port = (char) hExtPort;
158 dest.client = (char) pDevice->hAlsaSeqClient;
159 dest.port = (char) portNumber;
160 snd_seq_port_subscribe_alloca(&subs);
161 snd_seq_port_subscribe_set_sender(subs, &sender);
162 snd_seq_port_subscribe_set_dest(subs, &dest);
163 snd_seq_port_subscribe_set_queue(subs, 1);
164 snd_seq_port_subscribe_set_time_update(subs, 1);
165 snd_seq_port_subscribe_set_time_real(subs, 1);
166 if (snd_seq_subscribe_port(pDevice->hAlsaSeq, subs) < 0)
167 throw MidiInputException(String("Unable to connect to Alsa seq client \'") + MidiSource + "\' (" + snd_strerror(errno) + ")");
168 }
169
170
171
172 // *************** MidiInputDeviceAlsa ***************
173 // *
174
175 MidiInputDeviceAlsa::MidiInputDeviceAlsa(std::map<String,DeviceCreationParameter*> Parameters, void* pSampler) : MidiInputDevice(Parameters, pSampler), Thread(true, true, 1, -1) {
176 if (snd_seq_open(&hAlsaSeq, "default", SND_SEQ_OPEN_INPUT, 0) < 0) {
177 throw MidiInputException("Error opening ALSA sequencer");
178 }
179 this->hAlsaSeqClient = snd_seq_client_id(hAlsaSeq);
180 snd_seq_set_client_name(hAlsaSeq, "LinuxSampler");
181 AcquirePorts(((DeviceCreationParameterInt*)Parameters["PORTS"])->ValueAsInt());
182 if (((DeviceCreationParameterBool*)Parameters["ACTIVE"])->ValueAsBool()) {
183 Listen();
184 }
185 }
186
187 MidiInputDeviceAlsa::~MidiInputDeviceAlsa() {
188 // free the midi ports (we can't let the base class do this,
189 // as the MidiInputPortAlsa destructors need access to
190 // hAlsaSeq)
191 for (std::map<int,MidiInputPort*>::iterator iter = Ports.begin(); iter != Ports.end() ; iter++) {
192 delete static_cast<MidiInputPortAlsa*>(iter->second);
193 }
194 Ports.clear();
195
196 snd_seq_close(hAlsaSeq);
197 }
198
199 MidiInputDeviceAlsa::MidiInputPortAlsa* MidiInputDeviceAlsa::CreateMidiPort() {
200 return new MidiInputPortAlsa(this);
201 }
202
203 String MidiInputDeviceAlsa::Name() {
204 return "ALSA";
205 }
206
207 String MidiInputDeviceAlsa::Driver() {
208 return Name();
209 }
210
211 void MidiInputDeviceAlsa::Listen() {
212 StartThread();
213 }
214
215 void MidiInputDeviceAlsa::StopListen() {
216 StopThread();
217 }
218
219 String MidiInputDeviceAlsa::Description() {
220 return "Advanced Linux Sound Architecture";
221 }
222
223 String MidiInputDeviceAlsa::Version() {
224 String s = "$Revision: 1.19 $";
225 return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
226 }
227
228 int MidiInputDeviceAlsa::Main() {
229 int npfd;
230 struct pollfd* pfd;
231 snd_seq_event_t* ev;
232
233 npfd = snd_seq_poll_descriptors_count(hAlsaSeq, POLLIN);
234 pfd = (struct pollfd*) alloca(npfd * sizeof(struct pollfd));
235 snd_seq_poll_descriptors(hAlsaSeq, pfd, npfd, POLLIN);
236 while (true) {
237 if (poll(pfd, npfd, 100000) > 0) {
238 do {
239 snd_seq_event_input(hAlsaSeq, &ev);
240 int port = (int) ev->dest.port;
241 MidiInputPort* pMidiInputPort = Ports[port];
242
243 switch (ev->type) {
244 case SND_SEQ_EVENT_CONTROLLER:
245 if (ev->data.control.param == 0)
246 pMidiInputPort->DispatchBankSelectMsb(ev->data.control.value, ev->data.control.channel);
247 else if (ev->data.control.param == 32)
248 pMidiInputPort->DispatchBankSelectLsb(ev->data.control.value, ev->data.control.channel);
249 else
250 pMidiInputPort->DispatchControlChange(ev->data.control.param, ev->data.control.value, ev->data.control.channel);
251 break;
252
253 case SND_SEQ_EVENT_CHANPRESS:
254 pMidiInputPort->DispatchControlChange(128, ev->data.control.value, ev->data.control.channel);
255 break;
256
257 case SND_SEQ_EVENT_PITCHBEND:
258 pMidiInputPort->DispatchPitchbend(ev->data.control.value, ev->data.control.channel);
259 break;
260
261 case SND_SEQ_EVENT_NOTEON:
262 if (ev->data.note.velocity != 0) {
263 pMidiInputPort->DispatchNoteOn(ev->data.note.note, ev->data.note.velocity, ev->data.control.channel);
264 }
265 else {
266 pMidiInputPort->DispatchNoteOff(ev->data.note.note, 0, ev->data.control.channel);
267 }
268 break;
269
270 case SND_SEQ_EVENT_NOTEOFF:
271 pMidiInputPort->DispatchNoteOff(ev->data.note.note, ev->data.note.velocity, ev->data.control.channel);
272 break;
273
274 case SND_SEQ_EVENT_SYSEX:
275 pMidiInputPort->DispatchSysex(ev->data.ext.ptr, ev->data.ext.len);
276 break;
277
278 case SND_SEQ_EVENT_PGMCHANGE:
279 pMidiInputPort->DispatchProgramChange(ev->data.control.value, ev->data.control.channel);
280 break;
281 }
282 snd_seq_free_event(ev);
283 } while (snd_seq_event_input_pending(hAlsaSeq, 0) > 0);
284 }
285 }
286 }
287
288 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC