-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcspkr-midi.c
207 lines (170 loc) · 5.47 KB
/
pcspkr-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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* pcspkr-midi.c - Turn your pc speaker into an ALSA MIDI device.
*
* Copyright (C) 2013-2022 Jakob Flierl <jakob.flierl@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation:
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* ## Usage:
*
* $ cat /usr/lib/udev/rules.d/70-pcspkr-beep.rules
ACTION=="add", SUBSYSTEM=="input", ATTRS{name}=="PC Speaker", ENV{DEVNAME}!="", GROUP="audio", MODE="0620"
$ sudo rmmod pcspkr
$ sudo modprobe pcspkr
$ sudo udevadm control --reload
$ make LDLIBS="-lasound -lm" pcspkr-midi
$ ./pcspkr-midi
$ aconnect -o
$ aconnect -i
$ aconnect <input> <output>
$ # Next, play some notes on your MIDI keybard.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/kd.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <poll.h>
#include <alsa/asoundlib.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof *(a))
static snd_seq_t *seq;
static volatile sig_atomic_t signal_received = 0;
/* prints an error message to stderr, and dies */
static void fatal(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
vfprintf(stderr, msg, ap);
va_end(ap);
fputc('\n', stderr);
exit(EXIT_FAILURE);
}
/* memory allocation error handling */
static void check_mem(void *p)
{
if (!p)
fatal("out of memory");
}
/* error handling for ALSA functions */
static void check_snd(const char *operation, int err)
{
if (err < 0)
fatal("cannot %s - %s", operation, snd_strerror(err));
}
static void sighandler(int sig)
{
signal_received = 1;
}
snd_seq_addr_t input_addr;
static void wait_ms(double t) {
struct timespec ts;
ts.tv_sec = t / 1000;
ts.tv_nsec = (t - ts.tv_sec * 1000) * 1000000;
nanosleep(&ts, NULL);
}
void beep(int fd, int frq) {
struct input_event ev;
ev.type = EV_SND;
ev.code = SND_TONE;
ev.value = frq;
int result;
result = write(fd, (const void *)&ev, sizeof(ev));
if (result < 0) {
fprintf(stderr, "error writing in beep=%s\n", strerror(errno));
}
}
int main(int argc, char** argv) {
int err;
err = snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0);
check_snd("open sequencer", err);
err = snd_seq_set_client_name(seq, "pcspkr-midi");
check_snd("set client name", err);
int client = snd_seq_client_id(seq);
check_snd("get client id", client);
int port = snd_seq_create_simple_port(seq, "pcspkr-midi",
SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE | SND_SEQ_PORT_CAP_SYNC_WRITE,
SND_SEQ_PORT_TYPE_APPLICATION);
check_snd("create port", port);
printf("Opened ALSA Midi client:port %d:%d\n", client, port);
int fd = open("/dev/input/by-path/platform-pcspkr-event-spkr", O_WRONLY, 0);
if (fd == -1) {
fprintf(stderr, "%d: could not open speaker device. Did you \"sudo modprobe pcspkr\"?\n", fd);
}
char namebuf[128];
if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) < 0) {
return 1;
}
struct input_id inpid;
if (ioctl(fd, EVIOCGID, &inpid) < 0) {
return 1;
}
fprintf(stderr, "Found \"%s\": bustype = %d, vendor = 0x%.4x, product = 0x%.4x, version = %d\n",
namebuf, inpid.bustype, inpid.vendor, inpid.product, inpid.version);
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
#define c 262*2
int pollfds_count = snd_seq_poll_descriptors_count(seq, POLLIN);
struct pollfd *pollfds = alloca(pollfds_count * sizeof *pollfds);
err = snd_seq_poll_descriptors(seq, pollfds, pollfds_count, POLLIN);
check_snd("get poll descriptors", err);
pollfds_count = err;
while(1) {
snd_seq_event_t *rec_ev;
rec_ev = NULL;
retry:
err = poll(pollfds, pollfds_count, 1000);
if (signal_received)
break;
if (err == 0)
goto retry; // timeout
if (err < 0)
fatal("poll error: %s", strerror(errno));
unsigned short revents;
err = snd_seq_poll_descriptors_revents(seq, pollfds, pollfds_count, &revents);
check_snd("get poll events", err);
if (revents & (POLLERR | POLLNVAL))
break;
if (!(revents & POLLIN))
continue;
err = snd_seq_event_input(seq, &rec_ev);
check_snd("input MIDI event", err);
if (rec_ev->type == SND_SEQ_EVENT_NOTEON) {
printf("NOTE on %d\n", rec_ev->data.note.note);
double f = 440.0*exp(((rec_ev->data.note.note-69.0)/12.0)*log(2.0));
beep(fd, f);
} else if (rec_ev->type == SND_SEQ_EVENT_NOTEOFF) {
printf("NOTE off\n");
beep(fd, 0);
} else if (rec_ev->type == SND_SEQ_EVENT_PORT_SUBSCRIBED) {
printf("SND_SEQ_EVENT_PORT_SUBSCRIBED\n");
} else if (rec_ev->type == SND_SEQ_EVENT_PORT_UNSUBSCRIBED) {
printf("SND_SEQ_EVENT_PORT_UNSUBSCRIBED\n");
beep(fd, 0);
}
snd_seq_free_event(rec_ev);
}
beep(fd, 0);
close(fd);
return 0;
}