-
Notifications
You must be signed in to change notification settings - Fork 0
/
alsa_client.c
47 lines (42 loc) · 1.25 KB
/
alsa_client.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
#include <alsa/asoundlib.h>
#include <stdio.h>
#include "seq.h"
static snd_seq_t *handle;
int port_lower = -1;
int port_upper = -1;
int open_seq()
{
unsigned caps, type;
if (snd_seq_open(&handle, "default", SND_SEQ_OPEN_OUTPUT, 0) < 0) {
fprintf(stderr, "Error opening ALSA sequencer.\n");
return 0;
}
snd_seq_set_client_name(handle, "midiklav");
caps = SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ;
type = SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION;
port_lower = snd_seq_create_simple_port(handle, "lower keys",
caps, type);
if (port_lower < 0 || (port_upper = snd_seq_create_simple_port(
handle, "upper keys", caps, type)) < 0) {
fprintf(stderr, "Error creating sequencer port.\n");
return 0;
}
return 1;
}
int send_note(int port, int on, int ch, int note, int vel)
{
snd_seq_event_t ev;
snd_seq_ev_clear(&ev);
ev.dest.client = SND_SEQ_ADDRESS_SUBSCRIBERS;
ev.source.client = snd_seq_client_id(handle);
ev.source.port = port;
snd_seq_ev_set_direct(&ev);
ev.type = on ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
snd_seq_ev_set_fixed(&ev);
ev.data.note.channel = ch;
ev.data.note.note = note;
ev.data.note.velocity = vel;
snd_seq_event_output(handle, &ev);
snd_seq_drain_output(handle);
return 1;
}