23 |
#include "midiin.h" |
#include "midiin.h" |
24 |
|
|
25 |
MidiIn::MidiIn(AudioThread* pAudioThread) : Thread(true, 1, -1) { |
MidiIn::MidiIn(AudioThread* pAudioThread) : Thread(true, 1, -1) { |
26 |
|
this->seq_handle = NULL; |
27 |
this->pAudioThread = pAudioThread; |
this->pAudioThread = pAudioThread; |
28 |
memset(MIDIControllerTable, 0x00, 128); // set all controller values to zero |
memset(MIDIControllerTable, 0x00, 128); // set all controller values to zero |
29 |
} |
} |
33 |
} |
} |
34 |
|
|
35 |
int MidiIn::open_alsa_midi_seq(void) { |
int MidiIn::open_alsa_midi_seq(void) { |
|
|
|
|
int portid; |
|
|
|
|
36 |
if (snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_INPUT, 0) < 0) { |
if (snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_INPUT, 0) < 0) { |
37 |
fprintf(stderr, "Error opening ALSA sequencer.\n"); |
fprintf(stderr, "Error opening ALSA sequencer.\n"); |
38 |
exit(1); |
exit(1); |
39 |
} |
} |
40 |
|
this->AlsaID = snd_seq_client_id(seq_handle); |
41 |
snd_seq_set_client_name(seq_handle, "LinuxSampler"); |
snd_seq_set_client_name(seq_handle, "LinuxSampler"); |
42 |
if ((portid = snd_seq_create_simple_port(seq_handle, "LinuxSampler", |
if ((this->AlsaPort = snd_seq_create_simple_port(seq_handle, "LinuxSampler", |
43 |
SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, |
SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, |
44 |
SND_SEQ_PORT_TYPE_APPLICATION)) < 0) { |
SND_SEQ_PORT_TYPE_APPLICATION)) < 0) { |
45 |
fprintf(stderr, "Error creating sequencer port.\n"); |
fprintf(stderr, "Error creating sequencer port.\n"); |
46 |
exit(1); |
exit(1); |
47 |
} |
} |
51 |
void MidiIn::close_alsa_midi_seq(void) { |
void MidiIn::close_alsa_midi_seq(void) { |
52 |
} |
} |
53 |
|
|
54 |
int MidiIn::Main() { |
/** |
55 |
|
* Makes a connection to another Alsa sequencer client, so that all MIDI |
56 |
|
* events from e.g. a keyboard are always delivered to us. |
57 |
|
* |
58 |
|
* @param Client - Alsa sequencer client ID and port string to connect to |
59 |
|
* (e.g. "64:0") |
60 |
|
*/ |
61 |
|
void MidiIn::SubscribeToClient(const char* Client) { |
62 |
|
if (!this->seq_handle) { // if we haven't registered our seq client yet |
63 |
|
int res = open_alsa_midi_seq(); |
64 |
|
if (res < 0) { |
65 |
|
fprintf(stderr,"Opening of MIDI in device failed, exiting.\n"); |
66 |
|
exit(EXIT_FAILURE); |
67 |
|
} |
68 |
|
} |
69 |
|
|
70 |
int res = open_alsa_midi_seq(); |
snd_seq_addr_t sender, dest; |
71 |
if (res < 0) { |
snd_seq_port_subscribe_t* subs; |
72 |
fprintf(stderr,"opening of MIDI in device failed, exiting.\n"); |
int extClientID, extPortID; |
73 |
exit(1); |
|
74 |
|
sscanf(Client, "%d:%d", &extClientID, &extPortID); |
75 |
|
sender.client = (char) extClientID; |
76 |
|
sender.port = (char) extPortID; |
77 |
|
dest.client = (char) this->AlsaID; |
78 |
|
dest.port = (char) this->AlsaPort; |
79 |
|
snd_seq_port_subscribe_alloca(&subs); |
80 |
|
snd_seq_port_subscribe_set_sender(subs, &sender); |
81 |
|
snd_seq_port_subscribe_set_dest(subs, &dest); |
82 |
|
snd_seq_port_subscribe_set_queue(subs, 1); |
83 |
|
snd_seq_port_subscribe_set_time_update(subs, 1); |
84 |
|
snd_seq_port_subscribe_set_time_real(subs, 1); |
85 |
|
if (snd_seq_subscribe_port(this->seq_handle, subs) < 0) { |
86 |
|
fprintf(stderr, "Unable to subscribe to client \'%s\' (%s)\n", Client, snd_strerror(errno)); |
87 |
|
} |
88 |
|
} |
89 |
|
|
90 |
|
int MidiIn::Main() { |
91 |
|
if (!this->seq_handle) { // if we haven't registered our seq client yet |
92 |
|
int res = open_alsa_midi_seq(); |
93 |
|
if (res < 0) { |
94 |
|
fprintf(stderr,"Opening of MIDI in device failed, exiting.\n"); |
95 |
|
exit(EXIT_FAILURE); |
96 |
|
} |
97 |
} |
} |
98 |
|
|
99 |
int npfd; |
int npfd; |
105 |
snd_seq_poll_descriptors(seq_handle, pfd, npfd, POLLIN); |
snd_seq_poll_descriptors(seq_handle, pfd, npfd, POLLIN); |
106 |
while (true) { |
while (true) { |
107 |
if (poll(pfd, npfd, 100000) > 0) { |
if (poll(pfd, npfd, 100000) > 0) { |
|
|
|
108 |
do { |
do { |
109 |
snd_seq_event_input(seq_handle, &ev); |
snd_seq_event_input(seq_handle, &ev); |
|
|
|
110 |
switch (ev->type) { |
switch (ev->type) { |
111 |
case SND_SEQ_EVENT_CONTROLLER: |
case SND_SEQ_EVENT_CONTROLLER: |
112 |
// fprintf(stderr, "Control event on Channel %2d: num=%5d val=%5d \n", |
pAudioThread->SendControlChange(ev->data.control.channel, ev->data.control.param, ev->data.control.value); |
|
// ev->data.control.channel, ev->data.control.param, ev->data.control.value); |
|
|
|
|
|
pAudioThread->ProcessContinuousController(ev->data.control.channel, ev->data.control.param, ev->data.control.value); |
|
|
|
|
113 |
break; |
break; |
114 |
|
|
115 |
case SND_SEQ_EVENT_PITCHBEND: |
case SND_SEQ_EVENT_PITCHBEND: |
118 |
break; |
break; |
119 |
|
|
120 |
case SND_SEQ_EVENT_NOTEON: |
case SND_SEQ_EVENT_NOTEON: |
|
//fprintf(stderr, "Note On event on Channel %2d: note=%5d velocity=%d \n", |
|
|
// ev->data.control.channel, ev->data.note.note, ev->data.note.velocity); |
|
121 |
if (ev->data.note.velocity != 0) { |
if (ev->data.note.velocity != 0) { |
122 |
pAudioThread->ProcessNoteOn(ev->data.note.note, ev->data.note.velocity); |
pAudioThread->SendNoteOn(ev->data.note.note, ev->data.note.velocity); |
123 |
} |
} |
124 |
else { |
else { |
125 |
pAudioThread->ProcessNoteOff(ev->data.note.note, 0); |
pAudioThread->SendNoteOff(ev->data.note.note, 0); |
126 |
} |
} |
127 |
break; |
break; |
128 |
|
|
129 |
case SND_SEQ_EVENT_NOTEOFF: |
case SND_SEQ_EVENT_NOTEOFF: |
130 |
//fprintf(stderr, "Note Off event on Channel %2d: note=%5d velocity=%d \n", |
pAudioThread->SendNoteOff(ev->data.note.note, ev->data.note.velocity); |
|
// ev->data.control.channel, ev->data.note.note, ev->data.note.velocity); |
|
|
pAudioThread->ProcessNoteOff(ev->data.note.note, ev->data.note.velocity); |
|
131 |
break; |
break; |
132 |
} |
} |
133 |
snd_seq_free_event(ev); |
snd_seq_free_event(ev); |
134 |
} while (snd_seq_event_input_pending(seq_handle, 0) > 0); |
} while (snd_seq_event_input_pending(seq_handle, 0) > 0); |
135 |
} // end of if(poll...) |
} |
136 |
} // end of while |
} |
137 |
} |
} |
138 |
|
|