1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003 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 "midiin.h" |
24 |
|
25 |
MidiIn::MidiIn(AudioThread* pAudioThread) : Thread(true, 1, -1) { |
26 |
this->pAudioThread = pAudioThread; |
27 |
memset(MIDIControllerTable, 0x00, 128); // set all controller values to zero |
28 |
} |
29 |
|
30 |
MidiIn::~MidiIn() { |
31 |
close_alsa_midi_seq(); |
32 |
} |
33 |
|
34 |
int MidiIn::open_alsa_midi_seq(void) { |
35 |
int portid; |
36 |
if (snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_INPUT, 0) < 0) { |
37 |
fprintf(stderr, "Error opening ALSA sequencer.\n"); |
38 |
exit(1); |
39 |
} |
40 |
snd_seq_set_client_name(seq_handle, "LinuxSampler"); |
41 |
if ((portid = snd_seq_create_simple_port(seq_handle, "LinuxSampler", |
42 |
SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, |
43 |
SND_SEQ_PORT_TYPE_APPLICATION)) < 0) { |
44 |
fprintf(stderr, "Error creating sequencer port.\n"); |
45 |
exit(1); |
46 |
} |
47 |
return 0; |
48 |
} |
49 |
|
50 |
void MidiIn::close_alsa_midi_seq(void) { |
51 |
} |
52 |
|
53 |
int MidiIn::Main() { |
54 |
int res = open_alsa_midi_seq(); |
55 |
if (res < 0) { |
56 |
fprintf(stderr,"opening of MIDI in device failed, exiting.\n"); |
57 |
exit(1); |
58 |
} |
59 |
|
60 |
int npfd; |
61 |
struct pollfd *pfd; |
62 |
snd_seq_event_t *ev; |
63 |
|
64 |
npfd = snd_seq_poll_descriptors_count(seq_handle, POLLIN); |
65 |
pfd = (struct pollfd *)alloca(npfd * sizeof(struct pollfd)); |
66 |
snd_seq_poll_descriptors(seq_handle, pfd, npfd, POLLIN); |
67 |
while (true) { |
68 |
if (poll(pfd, npfd, 100000) > 0) { |
69 |
do { |
70 |
snd_seq_event_input(seq_handle, &ev); |
71 |
switch (ev->type) { |
72 |
case SND_SEQ_EVENT_CONTROLLER: |
73 |
pAudioThread->SendControlChange(ev->data.control.channel, ev->data.control.param, ev->data.control.value); |
74 |
break; |
75 |
|
76 |
case SND_SEQ_EVENT_PITCHBEND: |
77 |
// fprintf(stderr, "Pitchbender event on Channel %2d: %5d \n", |
78 |
// ev->data.control.channel, ev->data.control.value); |
79 |
break; |
80 |
|
81 |
case SND_SEQ_EVENT_NOTEON: |
82 |
if (ev->data.note.velocity != 0) { |
83 |
pAudioThread->SendNoteOn(ev->data.note.note, ev->data.note.velocity); |
84 |
} |
85 |
else { |
86 |
pAudioThread->SendNoteOff(ev->data.note.note, 0); |
87 |
} |
88 |
break; |
89 |
|
90 |
case SND_SEQ_EVENT_NOTEOFF: |
91 |
pAudioThread->SendNoteOff(ev->data.note.note, ev->data.note.velocity); |
92 |
break; |
93 |
} |
94 |
snd_seq_free_event(ev); |
95 |
} while (snd_seq_event_input_pending(seq_handle, 0) > 0); |
96 |
} |
97 |
} |
98 |
} |
99 |
|