forked from bsl/send-sds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.c
100 lines (80 loc) · 1.94 KB
/
midi.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
#define _XOPEN_SOURCE 500 /* string.h strdup */
#include <stdio.h>
#include <string.h>
#include <alsa/asoundlib.h>
#include "midi.h"
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
struct midi {
char *device;
snd_rawmidi_t *handle_in;
snd_rawmidi_t *handle_out;
};
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
int
midi_open_interface(
const char *device,
midi_t *midi_r,
err_t err
) {
int r;
snd_rawmidi_t *handle_in, *handle_out;
midi_t midi;
r = snd_rawmidi_open(&handle_in, &handle_out, device, 0);
if (r) {
err_set2(err, "snd_rawmidi_open \"%s\": error %d", device, r);
return 0;
}
midi = malloc(sizeof(*midi));
midi->device = strdup(device);
midi->handle_in = handle_in;
midi->handle_out = handle_out;
*midi_r = midi;
return 1;
}
void
midi_close_interface(
midi_t midi
) {
if (midi == NULL) {
return;
}
free(midi->device);
free(midi->handle_in);
free(midi->handle_out);
free(midi);
}
/* int */
/* midi_flush(midi_t midi) { */
/* return (snd_rawmidi_drop(midi->handle_out) == 0) */
/* && (snd_rawmidi_drop(midi->handle_in) == 0); */
/* } */
int
midi_send(
midi_t midi,
const unsigned char *data,
size_t data_length,
err_t err
) {
ssize_t r;
r = snd_rawmidi_write(midi->handle_out, data, data_length);
if ((size_t)r != data_length) {
err_set2(err, "snd_rawmidi_write: tried to write %d bytes, wrote %d", data_length, r);
return 0;
}
return 1;
}
int
midi_read(
midi_t midi,
unsigned char *byte
) {
ssize_t r;
r = snd_rawmidi_read(midi->handle_in, byte, 1);
if ((size_t)r != 1) {
/* err_set2(err, "snd_rawmidi_read: tried to read 1 byte, read %d", r); */
fprintf(stderr,
"snd_rawmidi_read: tried to read 1 byte, read %d\n", (int) r);
return 0;
}
return 1;
}