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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
#define _POSIX_C_SOURCE 200809L
#include "playback.h"
#include <errno.h>
#include <fcntl.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>
enum {
PLAYBACK_STEM_COUNT = 6,
PLAYBACK_EXEC_FAILURE = 127
};
typedef struct {
char letter;
const char *name;
} PlaybackStem;
static const PlaybackStem PLAYBACK_STEMS[PLAYBACK_STEM_COUNT] = {
{'g', "guitar"},
{'b', "bass"},
{'d', "drums"},
{'p', "piano"},
{'v', "vocals"},
{'o', "other"}
};
static const char PLAYBACK_INPUT_CONFIGURATION[] =
"> add speed 0.1\n"
"< add speed -0.1\n"
"} multiply pitch 1.0594630943592953\n"
"{ multiply pitch 0.9438743126816935\n";
static const char PLAYBACK_MESSAGE_LEVELS[] =
"--msg-level=cplayer=warn,ao=warn";
static const char PLAYBACK_STATUS_MESSAGE[] =
"--term-status-msg=${time-pos} / ${duration} (${percent-pos}%)";
static const char PLAYBACK_SHORTCUTS[] =
" keys action\n"
" > playback speed +0.1\n"
" < playback speed -0.1\n"
" } pitch up one semitone\n"
" { pitch down one semitone\n"
" q quit\n";
static void write_message(char *message, size_t message_size,
const char *format, ...)
{
va_list arguments;
if (message == NULL || message_size == 0U) {
return;
}
va_start(arguments, format);
(void)vsnprintf(message, message_size, format, arguments);
va_end(arguments);
}
bool stem_identifier_validate(const char *identifier, char *message,
size_t message_size)
{
const unsigned char *character;
if (identifier == NULL || identifier[0] == '\0') {
write_message(message, message_size, "music identifier cannot be empty");
return false;
}
for (character = (const unsigned char *)identifier;
*character != '\0'; ++character) {
bool valid = (*character >= (unsigned char)'a' &&
*character <= (unsigned char)'z') ||
(*character >= (unsigned char)'A' &&
*character <= (unsigned char)'Z') ||
(*character >= (unsigned char)'0' &&
*character <= (unsigned char)'9') ||
*character == (unsigned char)'_' ||
*character == (unsigned char)'-';
if (!valid) {
write_message(message, message_size,
"invalid music identifier '%s': use only letters, digits, '_' and '-'",
identifier);
return false;
}
}
return true;
}
static int stem_index(char letter)
{
int index;
for (index = 0; index < PLAYBACK_STEM_COUNT; ++index) {
if (PLAYBACK_STEMS[index].letter == letter) {
return index;
}
}
return -1;
}
static char *stem_path(const char *identifier, const char *stem_name)
{
static const char path_prefix[] = "./";
static const char directory_suffix[] = "_stems/";
static const char extension[] = ".wav";
size_t identifier_length = strlen(identifier);
size_t stem_length = strlen(stem_name);
size_t total;
char *path;
if (identifier_length > SIZE_MAX - identifier_length ||
identifier_length * 2U > SIZE_MAX - sizeof(directory_suffix) -
stem_length - sizeof(extension) -
sizeof(path_prefix)) {
errno = ENAMETOOLONG;
return NULL;
}
total = sizeof(path_prefix) - 1U + identifier_length * 2U +
sizeof(directory_suffix) - 1U + stem_length +
sizeof(extension) + 1U;
path = malloc(total);
if (path == NULL) {
return NULL;
}
(void)snprintf(path, total, "./%s_stems/%s_%s.wav", identifier,
identifier, stem_name);
return path;
}
static char *external_file_argument(const char *path)
{
static const char prefix[] = "--external-file=";
size_t path_length = strlen(path);
char *argument;
if (path_length > SIZE_MAX - sizeof(prefix)) {
errno = ENAMETOOLONG;
return NULL;
}
argument = malloc(sizeof(prefix) + path_length);
if (argument == NULL) {
return NULL;
}
memcpy(argument, prefix, sizeof(prefix) - 1U);
memcpy(argument + sizeof(prefix) - 1U, path, path_length + 1U);
return argument;
}
static bool write_input_configuration(int descriptor, const char *contents,
size_t length)
{
while (length > 0U) {
ssize_t written = write(descriptor, contents, length);
if (written < 0) {
if (errno == EINTR) {
continue;
}
return false;
}
contents += (size_t)written;
length -= (size_t)written;
}
return true;
}
static char *input_configuration_argument(void)
{
static const char prefix[] = "--input-conf=";
static const char file_name[] = "stemsep-mpv-input.conf";
static const char fallback_directory[] = "/tmp";
const char *directory = getenv("XDG_RUNTIME_DIR");
size_t directory_length;
size_t path_length;
char *path;
char *argument;
int descriptor;
int saved_errno;
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));
descriptor = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0600);
if (descriptor < 0) {
free(path);
return NULL;
}
if (!write_input_configuration(descriptor, PLAYBACK_INPUT_CONFIGURATION,
sizeof(PLAYBACK_INPUT_CONFIGURATION) - 1U)) {
saved_errno = errno;
(void)close(descriptor);
free(path);
errno = saved_errno;
return NULL;
}
if (close(descriptor) < 0) {
free(path);
return NULL;
}
if (path_length > SIZE_MAX - sizeof(prefix)) {
free(path);
errno = ENAMETOOLONG;
return NULL;
}
argument = malloc(sizeof(prefix) + path_length);
if (argument == NULL) {
free(path);
return NULL;
}
memcpy(argument, prefix, sizeof(prefix) - 1U);
memcpy(argument + sizeof(prefix) - 1U, path, path_length);
free(path);
return argument;
}
int playback_run(const char *identifier, const char *selection)
{
bool selected[PLAYBACK_STEM_COUNT] = {false};
char *paths[PLAYBACK_STEM_COUNT] = {NULL};
char *external_arguments[PLAYBACK_STEM_COUNT - 1] = {NULL};
char filter[128];
char filter_argument[sizeof(filter) + sizeof("--lavfi-complex=")];
char *arguments[7 + PLAYBACK_STEM_COUNT] = {NULL};
char *input_configuration = NULL;
size_t selected_count = 0U;
size_t argument_count = 0U;
size_t index;
int saved_errno;
if (selection == NULL || selection[0] == '\0') {
fprintf(stderr, "error: -p requires at least one lowercase stem letter\n");
return 2;
}
for (index = 0U; selection[index] != '\0'; ++index) {
int mapped_index = stem_index(selection[index]);
if (mapped_index < 0) {
fprintf(stderr,
"error: invalid -p selection '%s': use only lowercase letters g, b, d, p, v and o\n",
selection);
return 2;
}
selected[mapped_index] = true;
}
for (index = 0U; index < PLAYBACK_STEM_COUNT; ++index) {
if (selected[index]) {
paths[selected_count] = stem_path(identifier, PLAYBACK_STEMS[index].name);
if (paths[selected_count] == NULL) {
fprintf(stderr, "error: cannot allocate playback path: %s\n",
strerror(errno));
goto allocation_failure;
}
++selected_count;
}
}
arguments[argument_count++] = (char *)"mpv";
arguments[argument_count++] = (char *)"--no-video";
arguments[argument_count++] = (char *)PLAYBACK_MESSAGE_LEVELS;
arguments[argument_count++] = (char *)PLAYBACK_STATUS_MESSAGE;
input_configuration = input_configuration_argument();
if (input_configuration == NULL) {
fprintf(stderr, "warning: cannot write mpv key bindings: %s\n",
strerror(errno));
} else {
arguments[argument_count++] = input_configuration;
}
if (selected_count > 1U) {
size_t offset = 0U;
for (index = 0U; index < selected_count; ++index) {
int written = snprintf(filter + offset, sizeof(filter) - offset,
"[aid%zu]", index + 1U);
if (written < 0 || (size_t)written >= sizeof(filter) - offset) {
fprintf(stderr, "error: cannot construct mpv audio filter\n");
goto allocation_failure;
}
offset += (size_t)written;
}
(void)snprintf(filter + offset, sizeof(filter) - offset,
"amix=inputs=%zu[ao]", selected_count);
(void)snprintf(filter_argument, sizeof(filter_argument),
"--lavfi-complex=%s", filter);
arguments[argument_count++] = filter_argument;
}
arguments[argument_count++] = paths[0];
for (index = 1U; index < selected_count; ++index) {
external_arguments[index - 1U] = external_file_argument(paths[index]);
if (external_arguments[index - 1U] == NULL) {
fprintf(stderr, "error: cannot allocate mpv argument: %s\n",
strerror(errno));
goto allocation_failure;
}
arguments[argument_count++] = external_arguments[index - 1U];
}
arguments[argument_count] = NULL;
fputs(PLAYBACK_SHORTCUTS, stdout);
(void)fflush(stdout);
(void)setenv("PIPEWIRE_DEBUG", "0", 0);
execvp("mpv", arguments);
saved_errno = errno;
fprintf(stderr, "error: cannot execute mpv: %s\n", strerror(saved_errno));
free(input_configuration);
for (index = 0U; index < PLAYBACK_STEM_COUNT - 1U; ++index) {
free(external_arguments[index]);
}
for (index = 0U; index < PLAYBACK_STEM_COUNT; ++index) {
free(paths[index]);
}
return PLAYBACK_EXEC_FAILURE;
allocation_failure:
free(input_configuration);
for (index = 0U; index < PLAYBACK_STEM_COUNT - 1U; ++index) {
free(external_arguments[index]);
}
for (index = 0U; index < PLAYBACK_STEM_COUNT; ++index) {
free(paths[index]);
}
return 1;
}
|