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 |
|
36 |
int portid; |
37 |
|
38 |
if (snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_INPUT, 0) < 0) { |
39 |
fprintf(stderr, "Error opening ALSA sequencer.\n"); |
40 |
exit(1); |
41 |
} |
42 |
snd_seq_set_client_name(seq_handle, "LinuxSampler"); |
43 |
if ((portid = snd_seq_create_simple_port(seq_handle, "LinuxSampler", |
44 |
SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, |
45 |
SND_SEQ_PORT_TYPE_APPLICATION)) < 0) { |
46 |
fprintf(stderr, "Error creating sequencer port.\n"); |
47 |
exit(1); |
48 |
} |
49 |
return 0; |
50 |
} |
51 |
|
52 |
void MidiIn::close_alsa_midi_seq(void) { |
53 |
} |
54 |
|
55 |
int MidiIn::Main() { |
56 |
|
57 |
int res = open_alsa_midi_seq(); |
58 |
if (res < 0) { |
59 |
fprintf(stderr,"opening of MIDI in device failed, exiting.\n"); |
60 |
exit(1); |
61 |
} |
62 |
|
63 |
int npfd; |
64 |
struct pollfd *pfd; |
65 |
snd_seq_event_t *ev; |
66 |
|
67 |
npfd = snd_seq_poll_descriptors_count(seq_handle, POLLIN); |
68 |
pfd = (struct pollfd *)alloca(npfd * sizeof(struct pollfd)); |
69 |
snd_seq_poll_descriptors(seq_handle, pfd, npfd, POLLIN); |
70 |
while (true) { |
71 |
if (poll(pfd, npfd, 100000) > 0) { |
72 |
|
73 |
do { |
74 |
snd_seq_event_input(seq_handle, &ev); |
75 |
|
76 |
switch (ev->type) { |
77 |
case SND_SEQ_EVENT_CONTROLLER: |
78 |
// fprintf(stderr, "Control event on Channel %2d: num=%5d val=%5d \n", |
79 |
// ev->data.control.channel, ev->data.control.param, ev->data.control.value); |
80 |
|
81 |
pAudioThread->ProcessContinuousController(ev->data.control.channel, ev->data.control.param, ev->data.control.value); |
82 |
|
83 |
break; |
84 |
|
85 |
case SND_SEQ_EVENT_PITCHBEND: |
86 |
// fprintf(stderr, "Pitchbender event on Channel %2d: %5d \n", |
87 |
// ev->data.control.channel, ev->data.control.value); |
88 |
break; |
89 |
|
90 |
case SND_SEQ_EVENT_NOTEON: |
91 |
//fprintf(stderr, "Note On event on Channel %2d: note=%5d velocity=%d \n", |
92 |
// ev->data.control.channel, ev->data.note.note, ev->data.note.velocity); |
93 |
if (ev->data.note.velocity != 0) { |
94 |
pAudioThread->ProcessNoteOn(ev->data.note.note, ev->data.note.velocity); |
95 |
} |
96 |
else { |
97 |
pAudioThread->ProcessNoteOff(ev->data.note.note, 0); |
98 |
} |
99 |
break; |
100 |
|
101 |
case SND_SEQ_EVENT_NOTEOFF: |
102 |
//fprintf(stderr, "Note Off event on Channel %2d: note=%5d velocity=%d \n", |
103 |
// ev->data.control.channel, ev->data.note.note, ev->data.note.velocity); |
104 |
pAudioThread->ProcessNoteOff(ev->data.note.note, ev->data.note.velocity); |
105 |
break; |
106 |
} |
107 |
snd_seq_free_event(ev); |
108 |
} while (snd_seq_event_input_pending(seq_handle, 0) > 0); |
109 |
} // end of if(poll...) |
110 |
} // end of while |
111 |
} |
112 |
|