~vilhalmer/pamd

13f6187a1a93e06131cd74feb303562c6e395f9a — Bill Doyle 3 years ago
Initial commit
4 files changed, 223 insertions(+), 0 deletions(-)

A LICENSE
A README.md
A meson.build
A pamd.c
A  => LICENSE +9 -0
@@ 1,9 @@
MIT License

Copyright (c) 2020 Bill Doyle

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

A  => README.md +9 -0
@@ 1,9 @@
# pamd
## PulseAudio Mute Daemon

Listens to stdin and mutes a pulse source whenever input has been happening for
some amount of time. Unmutes again when input stops for some other length of
time.

Designed to be used with `intercept` from interception-tools to mute your mic
while you type.

A  => meson.build +23 -0
@@ 1,23 @@
project(
	'pamd',
	'c',
	version: '1.0.0',
	license: 'MIT',
	meson_version: '>=0.46.0',
	default_options: [
		'c_std=c11',
		'warning_level=2',
		'werror=true',
	],
)

executable(
	'pamd',
	files([
		'./pamd.c',
	]),
	dependencies: [
		dependency('libpulse'),
	],
	install: true,
)

A  => pamd.c +182 -0
@@ 1,182 @@
// PulseAudio Mute Daemon
// Mute or duck audio sources whenever the rate of input on stdin exceeds
// a threshold.

#include <errno.h>
#include <pulse/timeval.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <pulse/pulseaudio.h>

struct pamd {
    pa_context * ctx;
	pa_mainloop * loop;
	char * source;
    pa_usec_t timeout;
    pa_time_event * timer;
    struct timeval first_input;
    pa_usec_t input_threshold;
    bool muted;
    bool exit;
};

static void state_changed(pa_context * ctx, void * userdata) {
	struct pamd * pamd = userdata;

	switch (pa_context_get_state(ctx)) {
	case PA_CONTEXT_FAILED:
		fprintf(stderr, "Unable to connect to pulseaudio: %s\n", pa_strerror(pa_context_errno(ctx)));
		pa_mainloop_quit(pamd->loop, 2);
		break;
	case PA_CONTEXT_READY:
	case PA_CONTEXT_UNCONNECTED:
	case PA_CONTEXT_AUTHORIZING:
	case PA_CONTEXT_CONNECTING:
	case PA_CONTEXT_SETTING_NAME:
	case PA_CONTEXT_TERMINATED:
		break;
	}
}


static void stdin_callback(
        pa_mainloop_api * mainloop_api,
        pa_io_event *event __attribute__((unused)),
        int fd __attribute__((unused)),
        pa_io_event_flags_t flags __attribute__((unused)),
        void * userdata) {
	struct pamd * pamd = userdata;

    while (getchar()) {}
    if (pamd->exit) {
        return;
    }

    struct timeval now;
    gettimeofday(&now, NULL);

    if (pamd->first_input.tv_sec == 0) {
        pamd->first_input = now;
    }

    pa_timeval_add(&now, pamd->timeout);
    mainloop_api->time_restart(pamd->timer, &now);

    if (!pamd->muted && pa_timeval_age(&pamd->first_input) >= pamd->input_threshold) {
        pa_context_set_source_mute_by_name(pamd->ctx, pamd->source, true, NULL, NULL);
        pamd->muted = true;
    }
}


static void timer_expired_callback(
        pa_mainloop_api * mainloop_api __attribute__((unused)),
        pa_time_event * event __attribute__((unused)),
        const struct timeval * tv __attribute__((unused)),
        void * userdata) {
    struct pamd * pamd = userdata;
    if (pamd->exit) {
        return;
    }

    pa_context_set_source_mute_by_name(pamd->ctx, pamd->source, false, NULL, NULL);
    pamd->muted = false;
    pamd->first_input.tv_sec = 0;  // Make the time invalid
}

static void exit_signal_callback(
        pa_mainloop_api * api,
        pa_signal_event * event __attribute__((unused)),
        int signal __attribute__((unused)),
        void * userdata) {
    struct pamd * pamd = userdata;
    if (pamd->exit) {
        return;
    }

    pa_context_set_source_mute_by_name(pamd->ctx, pamd->source, false, NULL, NULL);
    pamd->exit = true;
    api->quit(api, 0);
}


const char * usage =
	"Usage: pamd [-h] [-s source_id]\n"
	"  -h        Show this help\n"
	"  -s        Operate on a specific source instead of the default; may be\n"
	"            either a name or index\n"
	"\n"
	"";


int main(int argc, char * argv[]) {
	pa_mainloop * loop = pa_mainloop_new();
	assert(loop);

	pa_mainloop_api * api = pa_mainloop_get_api(loop);
	assert(api);

	pa_proplist * properties = pa_proplist_new();
	pa_context * ctx = pa_context_new_with_proplist(api, "pamd", properties);
	assert(ctx);

	assert(pa_context_connect(ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) >= 0);

	struct pamd pamd = {
        .ctx = ctx,
		.loop = loop,
		.source = "@DEFAULT_SOURCE@",
        .timeout = 300000,  // 300ms
        .input_threshold = 1000000,
	};

	int opt;
	while ((opt = getopt(argc, argv, "hs:")) != -1) {
		switch (opt) {
		case 's':
			pamd.source = optarg;
			break;
		case 'h':
			printf("%s", usage);
			return 0;
		default:
			printf("%s", usage);
			return 1;
		}
	}

    pa_io_event * stdin_ev = api->io_new(
            api, STDIN_FILENO, PA_IO_EVENT_INPUT, stdin_callback, &pamd);
    assert(stdin_ev);

    struct timeval now;
    gettimeofday(&now, NULL);
    pa_timeval_add(&now, pamd.timeout);
    pamd.timer = api->time_new(api, &now, timer_expired_callback, &pamd);
    assert(pamd.timer);

	pa_context_set_state_callback(ctx, state_changed, &pamd);

    assert(pa_signal_init(api) == 0);
    pa_signal_new(SIGINT, exit_signal_callback, &pamd);
    pa_signal_new(SIGTERM, exit_signal_callback, &pamd);

	int exit = 0;
	if (pa_mainloop_run(loop, &exit) < 0) {
		fprintf(stderr, "Unable to communicate with pulseaudio\n");
		exit = 1;
	}

    api->io_free(stdin_ev);
    api->time_free(pamd.timer);
    pa_signal_done();
	pa_context_disconnect(ctx);
	pa_context_unref(ctx);
	pa_proplist_free(properties);
	pa_mainloop_free(loop);

	return exit;
}