low latency linux audio i/o library

Programming applications for making music on Linux.

Moderators: MattKingUSA, khz

Post Reply
zerocool
Posts: 1
Joined: Thu Sep 12, 2019 5:50 am

low latency linux audio i/o library

Post 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!
User avatar
sadko4u
Established Member
Posts: 986
Joined: Mon Sep 28, 2015 9:03 pm
Has thanked: 2 times
Been thanked: 359 times

Re: low latency linux audio i/o library

Post 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.
LSP (Linux Studio Plugins) Developer and Maintainer.
Basslint
Established Member
Posts: 1511
Joined: Sun Jan 27, 2019 2:25 pm
Location: Italy
Has thanked: 382 times
Been thanked: 298 times

Re: low latency linux audio i/o library

Post 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.
The community of believers was of one heart and mind, and no one claimed that any of his possessions was his own, but they had everything in common. [Acts 4:32]

Please donate time (even bug reports) or money to libre software 🎁

Jam on openSUSE + GeekosDAW!
Post Reply