#define _POSIX_C_SOURCE 200809L #include "playback.h" #include #include #include #include #include #include #include #include #include #include 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; }