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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
#define _POSIX_C_SOURCE 200809L
#include "metronome.h"
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "wav_writer.h"
#define METRONOME_MIN_BPM 1.0
#define METRONOME_MAX_BPM 1000.0
#define METRONOME_MIN_BEATS 2U
#define METRONOME_MAX_BEATS 16U
#define METRONOME_SAMPLE_RATE 44100U
#define METRONOME_WRITE_BLOCK_FRAMES 4096U
#define METRONOME_ACCENT_HZ 1760.0
#define METRONOME_BEAT_HZ 880.0
#define METRONOME_ACCENT_AMPLITUDE 0.85
#define METRONOME_BEAT_AMPLITUDE 0.55
#define METRONOME_CLICK_SECONDS 0.05
#define METRONOME_ATTACK_SECONDS 0.002
#define METRONOME_DECAY 100.0
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif
enum {
METRONOME_EXEC_FAILURE = 127
};
static bool write_message(char *message, size_t message_size,
const char *format, ...)
{
va_list arguments;
if (message != NULL && message_size > 0U) {
va_start(arguments, format);
(void)vsnprintf(message, message_size, format, arguments);
va_end(arguments);
}
return false;
}
bool metronome_bpm_validate(const char *text, double *bpm, char *message,
size_t message_size)
{
char *end = NULL;
double value;
if (text == NULL || text[0] == '\0') {
return write_message(message, message_size,
"option '--bpm' requires a tempo");
}
errno = 0;
value = strtod(text, &end);
if (end == text || *end != '\0' || errno == ERANGE || !isfinite(value)) {
return write_message(message, message_size,
"invalid tempo '%s': expected a number of beats per minute",
text);
}
if (value < METRONOME_MIN_BPM || value > METRONOME_MAX_BPM) {
return write_message(message, message_size,
"invalid tempo '%s': use a value between %g and %g",
text, METRONOME_MIN_BPM, METRONOME_MAX_BPM);
}
*bpm = value;
return true;
}
bool metronome_beats_validate(const char *text, unsigned *beats, char *message,
size_t message_size)
{
char *end = NULL;
long value;
if (text == NULL || text[0] == '\0') {
return write_message(message, message_size,
"option '-t' requires a time signature");
}
errno = 0;
value = strtol(text, &end, 10);
if (end == text || *end != '\0' || errno == ERANGE) {
return write_message(message, message_size,
"invalid time signature '%s': expected a number of beats",
text);
}
if (value < (long)METRONOME_MIN_BEATS || value > (long)METRONOME_MAX_BEATS) {
return write_message(message, message_size,
"invalid time signature '%s': use between %u and %u beats per bar",
text, METRONOME_MIN_BEATS, METRONOME_MAX_BEATS);
}
*beats = (unsigned)value;
return true;
}
static char *metronome_file_path(void)
{
static const char file_name[] = "stemsep-metronome.wav";
static const char fallback_directory[] = "/tmp";
const char *directory = getenv("XDG_RUNTIME_DIR");
size_t directory_length;
size_t path_length;
char *path;
if (directory == NULL || directory[0] == '\0') {
directory = fallback_directory;
}
directory_length = strlen(directory);
while (directory_length > 0U && directory[directory_length - 1U] == '/') {
--directory_length;
}
if (directory_length == 0U ||
directory_length > SIZE_MAX - sizeof(file_name) - 1U) {
errno = ENAMETOOLONG;
return NULL;
}
path_length = directory_length + sizeof(file_name) + 1U;
path = malloc(path_length);
if (path == NULL) {
return NULL;
}
memcpy(path, directory, directory_length);
path[directory_length] = '/';
memcpy(path + directory_length + 1U, file_name, sizeof(file_name));
return path;
}
static double click_sample(size_t index, double frequency, double amplitude)
{
double seconds = (double)index / (double)METRONOME_SAMPLE_RATE;
double attack_frames = METRONOME_ATTACK_SECONDS * (double)METRONOME_SAMPLE_RATE;
double envelope = exp(-seconds * METRONOME_DECAY);
double value;
if ((double)index < attack_frames) {
envelope *= (double)index / attack_frames;
}
value = amplitude * envelope *
sin(2.0 * M_PI * frequency * seconds);
return value;
}
static bool write_click(WavWriter *writer, uint64_t frames, double frequency,
double amplitude, char *message, size_t message_size)
{
float block[METRONOME_WRITE_BLOCK_FRAMES];
uint64_t offset = 0U;
while (offset < frames) {
uint64_t remaining = frames - offset;
size_t count = remaining > METRONOME_WRITE_BLOCK_FRAMES
? METRONOME_WRITE_BLOCK_FRAMES
: (size_t)remaining;
size_t index;
for (index = 0U; index < count; ++index) {
block[index] = (float)click_sample((size_t)offset + index, frequency,
amplitude);
}
if (!wav_writer_write(writer, block, count, message, message_size)) {
return false;
}
offset += count;
}
return true;
}
static bool write_silence(WavWriter *writer, uint64_t frames, char *message,
size_t message_size)
{
static const float zeros[METRONOME_WRITE_BLOCK_FRAMES];
while (frames > 0U) {
size_t count = frames > METRONOME_WRITE_BLOCK_FRAMES
? METRONOME_WRITE_BLOCK_FRAMES
: (size_t)frames;
if (!wav_writer_write(writer, zeros, count, message, message_size)) {
return false;
}
frames -= count;
}
return true;
}
static bool write_bar(WavWriter *writer, uint64_t frames_per_beat, unsigned beats,
char *message, size_t message_size)
{
uint64_t click_frames =
(uint64_t)llround(METRONOME_CLICK_SECONDS * (double)METRONOME_SAMPLE_RATE);
unsigned beat;
if (click_frames > frames_per_beat) {
click_frames = frames_per_beat;
}
for (beat = 0U; beat < beats; ++beat) {
bool accent = beat == 0U;
if (!write_click(writer, click_frames,
accent ? METRONOME_ACCENT_HZ : METRONOME_BEAT_HZ,
accent ? METRONOME_ACCENT_AMPLITUDE
: METRONOME_BEAT_AMPLITUDE,
message, message_size)) {
return false;
}
if (!write_silence(writer, frames_per_beat - click_frames, message,
message_size)) {
return false;
}
}
return true;
}
int metronome_run(double bpm, unsigned beats_per_bar)
{
char *path;
char *arguments[6];
FILE *file;
WavWriter *writer;
char message[768];
int descriptor;
int flags = O_WRONLY | O_CREAT | O_TRUNC;
uint64_t frames_per_beat;
uint64_t bar_frames;
int saved_errno;
frames_per_beat = (uint64_t)llround(60.0 * (double)METRONOME_SAMPLE_RATE / bpm);
if (frames_per_beat == 0U) {
frames_per_beat = 1U;
}
if (frames_per_beat > UINT64_MAX / beats_per_bar) {
fprintf(stderr, "error: tempo %g is too slow for a single metronome bar\n",
bpm);
return 1;
}
bar_frames = frames_per_beat * beats_per_bar;
path = metronome_file_path();
if (path == NULL) {
fprintf(stderr, "error: cannot build the metronome file path: %s\n",
strerror(errno));
return 1;
}
#ifdef O_CLOEXEC
flags |= O_CLOEXEC;
#endif
#ifdef O_NOFOLLOW
flags |= O_NOFOLLOW;
#endif
descriptor = open(path, flags, 0600);
if (descriptor < 0) {
fprintf(stderr, "error: cannot create '%s': %s\n", path, strerror(errno));
free(path);
return 1;
}
file = fdopen(descriptor, "wb");
if (file == NULL) {
saved_errno = errno;
(void)close(descriptor);
fprintf(stderr, "error: cannot open '%s' for writing: %s\n", path,
strerror(saved_errno));
free(path);
return 1;
}
message[0] = '\0';
writer = wav_writer_open(file, path, METRONOME_SAMPLE_RATE, 1U, bar_frames,
message, sizeof(message));
if (writer == NULL) {
fprintf(stderr, "error: %s\n",
message[0] != '\0' ? message : "cannot create the metronome WAV");
free(path);
return 1;
}
if (!write_bar(writer, frames_per_beat, beats_per_bar, message,
sizeof(message))) {
fprintf(stderr, "error: %s\n",
message[0] != '\0' ? message : "cannot write the metronome WAV");
wav_writer_abort(writer);
free(path);
return 1;
}
message[0] = '\0';
if (!wav_writer_finish(writer, message, sizeof(message))) {
fprintf(stderr, "error: %s\n",
message[0] != '\0' ? message : "cannot finish the metronome WAV");
free(path);
return 1;
}
fprintf(stderr, "Metronome: %g BPM, %u beat%s per bar\n", bpm, beats_per_bar,
beats_per_bar == 1U ? "" : "s");
arguments[0] = (char *)"mpv";
arguments[1] = (char *)"--no-video";
arguments[2] = (char *)"--loop-file=inf";
arguments[3] = (char *)"--msg-level=cplayer=warn,ao=warn";
arguments[4] = path;
arguments[5] = NULL;
(void)setenv("PIPEWIRE_DEBUG", "0", 0);
execvp("mpv", arguments);
saved_errno = errno;
fprintf(stderr, "error: cannot execute mpv: %s\n", strerror(saved_errno));
free(path);
return METRONOME_EXEC_FAILURE;
}
|