diff --git a/meson.build b/meson.build index 0d8b9e41370525a83418e3c77281a7dc9f99cbd1..3a02e8add8b01541d8cc7d9f5e7ce2155620eb80 100644 --- a/meson.build +++ b/meson.build @@ -2,9 +2,9 @@ project('q6voiced', 'c') pkg = import('pkgconfig') dbus = dependency('dbus-1') -tinyalsa = dependency('tinyalsa') +alsa = dependency('alsa') executable('q6voiced', 'q6voiced.c', - dependencies: [dbus, tinyalsa], + dependencies: [dbus, alsa], install: true) diff --git a/q6voiced.c b/q6voiced.c index c8d6e133e02a4b2401c242268cff80fbd65c4b98..072dd7fa479a01738e00aad10911b6ae942955b5 100644 --- a/q6voiced.c +++ b/q6voiced.c @@ -2,20 +2,12 @@ #include <stdbool.h> #include <stdio.h> #include <dbus/dbus.h> -#include <tinyalsa/asoundlib.h> - -/* Note: These parameters have little relevance (no audio data written) */ -struct pcm_config pcm_config_voice_call = { - .channels = 1, - .rate = 8000, - .period_size = 160, - .period_count = 2, - .format = PCM_FORMAT_S16_LE, -}; +#include <string.h> +#include <alsa/asoundlib.h> struct q6voiced { - unsigned int card, device; - struct pcm *tx, *rx; + char card[64]; + snd_pcm_t *tx, *rx; }; static void q6voiced_open(struct q6voiced *v) @@ -27,14 +19,31 @@ static void q6voiced_open(struct q6voiced *v) * Opening the PCM devices starts the stream. * This should be replaced by a codec2codec link probably. */ - v->tx = pcm_open(v->card, v->device, PCM_IN, &pcm_config_voice_call); - v->rx = pcm_open(v->card, v->device, PCM_OUT, &pcm_config_voice_call); - if (!pcm_is_ready(v->tx) || pcm_prepare(v->tx)) + if (snd_pcm_open(&v->tx, v->card, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK) < 0 || + snd_pcm_set_params(v->tx, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 8000, 0, 500000) < 0 || + snd_pcm_prepare(v->tx) < 0) { perror("Failed to open tx"); - if (!pcm_is_ready(v->rx) || pcm_prepare(v->rx)) + goto error; + } + if (snd_pcm_open(&v->rx, v->card, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0 || + snd_pcm_set_params(v->rx, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 8000, 0, 500000) < 0 || + snd_pcm_prepare(v->rx) < 0) { perror("Failed to open rx"); + goto error; + } printf("PCM devices were opened.\n"); + return; + +error: + if (v->tx) { + snd_pcm_close(v->tx); + v->tx = NULL; + } + if (v->rx) { + snd_pcm_close(v->rx); + v->rx = NULL; + } } static void q6voiced_close(struct q6voiced *v) @@ -42,8 +51,8 @@ static void q6voiced_close(struct q6voiced *v) if (!v->tx) return; /* Not active */ - pcm_close(v->rx); - pcm_close(v->tx); + snd_pcm_close(v->tx); + snd_pcm_close(v->rx); v->rx = v->tx = NULL; printf("PCM devices were closed.\n"); @@ -112,11 +121,13 @@ int main(int argc, char **argv) DBusConnection *conn; DBusError err; - if (argc != 2 || sscanf(argv[1], "hw:%u,%u", &v.card, &v.device) != 2) { + if (argc != 2) { fprintf(stderr, "Usage: q6voiced hw:<card>,<device>\n"); return 1; } + strlcpy(v.card, argv[1], sizeof(v.card) - 1); + // See: http://web.archive.org/web/20100309103206/http://dbus.freedesktop.org/doc/dbus/libdbus-tutorial.html // "Receiving a Signal"