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 "MidiInputDeviceAlsa.h" |
24 |
|
|
|
25 |
|
|
namespace LinuxSampler { |
26 |
|
|
|
27 |
|
|
/** |
28 |
|
|
* Create Alsa MIDI input device for LinuxSampler. Opens and initializes |
29 |
|
|
* Alsa sequencer client and creates one Alsa MIDI input port for |
30 |
|
|
* LinuxSampler. The optional argument allows to auto connect to a Alsa |
31 |
|
|
* MIDI source (e.g. a software sequencer or a hardware MIDI input |
32 |
|
|
* port). |
33 |
|
|
* |
34 |
|
|
* @param AutoConnectPortID - (optional) Alsa client and port ID of a |
35 |
|
|
* MIDI source we should auto connect to |
36 |
|
|
* (e.g. "64:0") |
37 |
|
|
* @throws MidiInputException if initialization failed |
38 |
|
|
*/ |
39 |
|
|
MidiInputDeviceAlsa::MidiInputDeviceAlsa(char* AutoConnectPortID) : Thread(true, 1, -1) { |
40 |
|
|
if (snd_seq_open(&hAlsaSeq, "default", SND_SEQ_OPEN_INPUT, 0) < 0) { |
41 |
|
|
throw MidiInputException("Error opening ALSA sequencer"); |
42 |
|
|
} |
43 |
|
|
this->hAlsaSeqClient = snd_seq_client_id(hAlsaSeq); |
44 |
|
|
snd_seq_set_client_name(hAlsaSeq, "LinuxSampler"); |
45 |
|
|
if ((this->hAlsaSeqPort = snd_seq_create_simple_port(hAlsaSeq, "LinuxSampler", |
46 |
|
|
SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, |
47 |
|
|
SND_SEQ_PORT_TYPE_APPLICATION)) < 0) { |
48 |
|
|
throw MidiInputException("Error creating sequencer port"); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
if (AutoConnectPortID) ConnectToAlsaMidiSource(AutoConnectPortID); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
MidiInputDeviceAlsa::~MidiInputDeviceAlsa() { |
55 |
|
|
//TODO: close Alsa seq |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
void MidiInputDeviceAlsa::Listen() { |
59 |
|
|
StartThread(); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
void MidiInputDeviceAlsa::StopListen() { |
63 |
|
|
StopThread(); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
/** |
67 |
|
|
* Connects this Alsa midi input device with an Alsa MIDI source. |
68 |
|
|
* |
69 |
|
|
* @param Client - Alsa sequencer client and port ID of a MIDI source |
70 |
|
|
* (e.g. "64:0") |
71 |
|
|
* @throws MidiInputException if connection cannot be established |
72 |
|
|
*/ |
73 |
|
|
void MidiInputDeviceAlsa::ConnectToAlsaMidiSource(const char* MidiSource) { |
74 |
|
|
snd_seq_addr_t sender, dest; |
75 |
|
|
snd_seq_port_subscribe_t* subs; |
76 |
|
|
int hExtClient, hExtPort; |
77 |
|
|
|
78 |
|
|
sscanf(MidiSource, "%d:%d", &hExtClient, &hExtPort); |
79 |
|
|
sender.client = (char) hExtClient; |
80 |
|
|
sender.port = (char) hExtPort; |
81 |
|
|
dest.client = (char) this->hAlsaSeqClient; |
82 |
|
|
dest.port = (char) this->hAlsaSeqPort; |
83 |
|
|
snd_seq_port_subscribe_alloca(&subs); |
84 |
|
|
snd_seq_port_subscribe_set_sender(subs, &sender); |
85 |
|
|
snd_seq_port_subscribe_set_dest(subs, &dest); |
86 |
|
|
snd_seq_port_subscribe_set_queue(subs, 1); |
87 |
|
|
snd_seq_port_subscribe_set_time_update(subs, 1); |
88 |
|
|
snd_seq_port_subscribe_set_time_real(subs, 1); |
89 |
|
|
if (snd_seq_subscribe_port(this->hAlsaSeq, subs) < 0) |
90 |
|
|
throw MidiInputException(String("Unable to connect to Alsa seq client \'") + MidiSource + "\' (" + snd_strerror(errno) + ")"); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
int MidiInputDeviceAlsa::Main() { |
94 |
|
|
int npfd; |
95 |
|
|
struct pollfd* pfd; |
96 |
|
|
snd_seq_event_t* ev; |
97 |
|
|
|
98 |
|
|
npfd = snd_seq_poll_descriptors_count(hAlsaSeq, POLLIN); |
99 |
|
|
pfd = (struct pollfd*) alloca(npfd * sizeof(struct pollfd)); |
100 |
|
|
snd_seq_poll_descriptors(hAlsaSeq, pfd, npfd, POLLIN); |
101 |
|
|
while (true) { |
102 |
|
|
if (poll(pfd, npfd, 100000) > 0) { |
103 |
|
|
do { |
104 |
|
|
snd_seq_event_input(hAlsaSeq, &ev); |
105 |
|
|
switch (ev->type) { |
106 |
|
|
case SND_SEQ_EVENT_CONTROLLER: |
107 |
|
|
DispatchControlChange(ev->data.control.param, ev->data.control.value, ev->data.control.channel); |
108 |
|
|
break; |
109 |
|
|
|
110 |
|
|
case SND_SEQ_EVENT_PITCHBEND: |
111 |
|
|
// fprintf(stderr, "Pitchbender event on Channel %2d: %5d \n", |
112 |
|
|
// ev->data.control.channel, ev->data.control.value); |
113 |
|
|
DispatchPitchbend(ev->data.control.value, ev->data.control.channel); |
114 |
|
|
break; |
115 |
|
|
|
116 |
|
|
case SND_SEQ_EVENT_NOTEON: |
117 |
|
|
if (ev->data.note.velocity != 0) { |
118 |
|
|
DispatchNoteOn(ev->data.note.note, ev->data.note.velocity, ev->data.control.channel); |
119 |
|
|
} |
120 |
|
|
else { |
121 |
|
|
DispatchNoteOff(ev->data.note.note, 0, ev->data.control.channel); |
122 |
|
|
} |
123 |
|
|
break; |
124 |
|
|
|
125 |
|
|
case SND_SEQ_EVENT_NOTEOFF: |
126 |
|
|
DispatchNoteOff(ev->data.note.note, ev->data.note.velocity, ev->data.control.channel); |
127 |
|
|
break; |
128 |
|
|
} |
129 |
|
|
snd_seq_free_event(ev); |
130 |
|
|
} while (snd_seq_event_input_pending(hAlsaSeq, 0) > 0); |
131 |
|
|
} |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
} // namespace LinuxSampler |