/
lpmidi.c
163 lines (137 loc) · 4.74 KB
/
lpmidi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the University of California, Berkeley nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "liblaunchpad.h"
#include <alsa/asoundlib.h>
#include <pthread.h>
// globals
struct launchpad* lp;
snd_seq_t* midi_client;
int midi_in;
int midi_out;
void midi_register()
{
//open a new alsa midi client
if (snd_seq_open(&midi_client, "default", SND_SEQ_OPEN_DUPLEX, 0) < 0) {
fprintf(stderr,"coud not open a new midi client\n");
return;
}
//set the client's name
snd_seq_set_client_name(midi_client, "Launchpad");
//open a new midi output port
midi_out = snd_seq_create_simple_port(midi_client, "Out",
SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ,
SND_SEQ_PORT_TYPE_PORT);
if (midi_out < 0) {
fprintf(stderr,"could not open the midi out port\n");
return;
}
midi_in = snd_seq_create_simple_port(midi_client, "In",
SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE,
SND_SEQ_PORT_TYPE_PORT);
if (midi_in < 0) {
fprintf(stderr,"could not open the midi in port\n");
return;
}
}
void midi_deregister()
{
snd_seq_delete_simple_port(midi_client, midi_out);
snd_seq_delete_simple_port(midi_client, midi_in);
snd_seq_close(midi_client);
}
void* lp2midi(void* nothing)
{
printf("waiting for launchpad events\n");
snd_seq_event_t event;
while (1) {
// wait for an event
lp_receive(lp);
// setup
snd_seq_ev_clear(&event);
snd_seq_ev_set_source(&event, midi_out); // set the output port number
snd_seq_ev_set_subs(&event); // broadcast to subscribers
// fill the event
switch(lp->event[0]) {
case NOTE:
snd_seq_ev_set_noteon(&event, 0, lp->event[1], lp->event[2]);
break;
case CTRL:
snd_seq_ev_set_controller(&event, 0, lp->event[1], lp->event[2]);
break;
}
// send now
snd_seq_ev_set_direct(&event);
snd_seq_event_output(midi_client, &event);
snd_seq_drain_output(midi_client);
}
}
void* midi2lp(void* nothing)
{
printf("waiting for midi events\n");
snd_seq_event_t *ev;
while (1) {
// get a new event. if there aren't any, wait for one
snd_seq_event_input(midi_client, &ev);
// copy the data to send
switch (ev->type) {
case SND_SEQ_EVENT_NOTEON:
lp_send3(lp, NOTE_ON, ev->data.note.note, ev->data.note.velocity);
break;
case SND_SEQ_EVENT_NOTEOFF:
lp_send3(lp, NOTE_OFF, ev->data.note.note, ev->data.note.velocity);
break;
case SND_SEQ_EVENT_CONTROLLER:
lp_send3(lp, CTRL, ev->data.control.param, ev->data.control.value);
break;
}
// free the midi event
snd_seq_free_event(ev);
}
}
int main(int argc, char* argv[])
{
int err;
pthread_t lp2midi_thread, midi2lp_thread;
lp = lp_register();
midi_register();
// start listening to the launchpad
err = pthread_create(&lp2midi_thread, NULL, lp2midi, NULL);
if (err){
fprintf(stderr, "failed to start usb thread with error %d", err);
return 0;
}
// start listening to the midi port
err = pthread_create(&midi2lp_thread, NULL, midi2lp, NULL);
if (err){
fprintf(stderr, "failed to start midi thread with error %d", err);
return 0;
}
// wait for the threads to finish
pthread_join(lp2midi_thread, NULL);
pthread_join(midi2lp_thread, NULL);
midi_deregister();
lp_deregister(lp);
}