summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Bezerra <amelco.herman@gmail.com>2026-10-28 17:16:27 -0300
committerAndre Bezerra <amelco.herman@gmail.com>2026-11-17 11:59:21 -0300
commit186196a1ad7d2170208dbc3a7548a3159fe4a171 (patch)
treec73b0f89701445377d63c2ebc39385afbe238401
parent89cb9eb4c72755befddfb4830e2e7e8b097134de (diff)
add pitch and speed modulation on playback
-rw-r--r--README.md14
-rw-r--r--playback.c105
2 files changed, 118 insertions, 1 deletions
diff --git a/README.md b/README.md
index 4235b5e..b948abc 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,20 @@ and remains attached to the terminal so its normal playback controls work.
`stemsep` assumes `mpv` is installed and available in `PATH`; requested files
are passed directly to mpv, which reports any missing-file errors.
+While a stem is playing, these keys adjust speed and pitch independently:
+
+- `>`: increase playback speed by 0.1
+- `<`: decrease playback speed by 0.1
+- `}`: raise the pitch by one semitone
+- `{`: lower the pitch by one semitone
+
+Speed changes keep the pitch and pitch changes keep the speed. The bindings are
+written to `$XDG_RUNTIME_DIR/stemsep-mpv-input.conf` (`/tmp` when that variable
+is unset) and handed to mpv with `--input-conf`, which replaces the keys mpv
+binds to these four characters by default and means the user's own
+`input.conf` is not loaded during stem playback. Pitch control uses mpv's
+`pitch` property and requires mpv 0.40 or newer.
+
With missing parameters, a wrong flag, or incompatible modes, the program
prints an error followed by:
diff --git a/playback.c b/playback.c
index 39d6e49..8a45a3b 100644
--- a/playback.c
+++ b/playback.c
@@ -3,6 +3,7 @@
#include "playback.h"
#include <errno.h>
+#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
@@ -31,6 +32,12 @@ static const PlaybackStem PLAYBACK_STEMS[PLAYBACK_STEM_COUNT] = {
{'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 void write_message(char *message, size_t message_size,
const char *format, ...)
{
@@ -133,6 +140,92 @@ static char *external_file_argument(const char *path)
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};
@@ -140,7 +233,8 @@ int playback_run(const char *identifier, const char *selection)
char *external_arguments[PLAYBACK_STEM_COUNT - 1] = {NULL};
char filter[128];
char filter_argument[sizeof(filter) + sizeof("--lavfi-complex=")];
- char *arguments[4 + PLAYBACK_STEM_COUNT] = {NULL};
+ char *arguments[5 + PLAYBACK_STEM_COUNT] = {NULL};
+ char *input_configuration = NULL;
size_t selected_count = 0U;
size_t argument_count = 0U;
size_t index;
@@ -175,6 +269,13 @@ int playback_run(const char *identifier, const char *selection)
arguments[argument_count++] = (char *)"mpv";
arguments[argument_count++] = (char *)"--no-video";
+ 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) {
@@ -207,6 +308,7 @@ int playback_run(const char *identifier, const char *selection)
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]);
}
@@ -216,6 +318,7 @@ int playback_run(const char *identifier, const char *selection)
return PLAYBACK_EXEC_FAILURE;
allocation_failure:
+ free(input_configuration);
for (index = 0U; index < PLAYBACK_STEM_COUNT - 1U; ++index) {
free(external_arguments[index]);
}