Page 1 of 1

low latency linux audio i/o library

Posted: Thu Sep 12, 2019 5:57 am
by zerocool
Hi. I've tried PulseAudio with poor results given what I need. I'm trying to make my audio output match the input as fast as possible. The situation involves me connecting my electric guitar via USB cable.. There's a delay however. I know it's possible to achieve near real-time latency because I've used a program on Windows called Rocksmith that has a built-in amp and does such a thing. I haven't managed to get guitarix working so I can't say how reliable that is for my situation. If anyone could help me out I'd appreciate it. Here's my PulseAudio example:

Code: Select all

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#define BUFSIZE 8
/* A simple routine calling UNIX write() in a loop */

int main(int argc, char*argv[]) {
    /* The sample type to use */
    static const pa_sample_spec ss = {
        .format = PA_SAMPLE_S16LE,
        .rate = 44100,
        .channels = 2
    };
    pa_simple *s = NULL;
    pa_simple *x = NULL;
    int ret = 1;
    int error;
    /* Create the recording stream */
    if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
        fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
        goto finish;
    }

    if (!(x = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) {
    fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
    goto finish;
}


    for (;;) {
        uint8_t buf[BUFSIZE];
        /* Record some data ... */
        if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {
            fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
            goto finish;
        }
        /* And write it to STDOUT */


        if (pa_simple_write(x, buf, sizeof(buf), &error) < 0) {
            fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
            goto finish;
        }


    }
    ret = 0;
finish:
    if (s)
        pa_simple_free(s);
    return ret;
}
If someone can help me rid the delay and lower the latency as much as possible I'd appreciate it.

Otherwise, please suggest libraries that I can use for this project!

Thank you!

Re: low latency linux audio i/o library

Posted: Thu Sep 12, 2019 4:17 pm
by sadko4u
AFAIR PulseAudio is not about the real-time performance and low latency.
If you want to write low-latency program, consider using JACK or ALSA.

Re: low latency linux audio i/o library

Posted: Sun Sep 15, 2019 1:01 pm
by Basslint
zerocool wrote:Hi. I've tried PulseAudio with poor results given what I need. I'm trying to make my audio output match the input as fast as possible.
Hello,

aside from using JACK, what you want to do is called "pitch tracking" and there are several libraries that do it already, most notably aubio which implements several algorithms. For playing more than one note together, however, there are no easy solutions at the moment. This is some code you can use to begin.