0

Preface: I have looked at the other answers, and I believe this is a different type of issue. It's probably pointer related, but none of the other answers helped. I also may just be missing something.

I managed to make a working program which streams raw audio data to stdout, using the ALSA library. I am now trying to generalize this code into a library, and the global variables in the .c file of the library are not updating. As far as I can tell, the setup is no different than the original program, so I think it should work... but it doesn't. I'd really appreciate someone explaining exactly how I'm messing this up. Thank you pre-emptively!

EDIT 1:

All global variables at the top of wm8782.c fail to set when wm8782_open_audio() is called. For example, when snd_pcm_hw_params_get_period_time(params, &val, &dir); is called in wm8782_us_to_loops() after wm8782_open_audio() has been called, it fails because params was not initialized. However, wm8782_open_audio() completes without error!

EDIT 2:

I have added extern declarations in my header, no dice. See below for the updated files.

EDIT 3:

I have reproduced the problem in a single C file with no header. Simply breaking out the code into functions is enough to break it. I have replaced the 2 library files with this monolithic C file, so as to eliminate linker errors and the like.

Here is the original program I am trying to generalize into a library. This does work as I want it to.

#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>

long loops;
int rc;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir;
snd_pcm_uframes_t frames;
char *buffer;

int main()
{
    /* Open PCM device for recording (capture). */
    rc = snd_pcm_open(&handle, "hw:1",
                      SND_PCM_STREAM_CAPTURE, 0);
    if (rc < 0)
    {
        fprintf(stderr,
                "unable to open pcm device: %s\n",
                snd_strerror(rc));
        exit(1);
    }

    /* Allocate a hardware parameters object. */
    snd_pcm_hw_params_alloca(&params);

    /* Fill it in with default values. */
    snd_pcm_hw_params_any(handle, params);

    /* Set the desired hardware parameters. */

    /* Interleaved mode */
    snd_pcm_hw_params_set_access(handle, params,
                                 SND_PCM_ACCESS_RW_INTERLEAVED);

    /* Signed 32-bit little-endian format */
    snd_pcm_hw_params_set_format(handle, params,
                                 SND_PCM_FORMAT_S32_LE);

    /* Two channels (stereo) */
    snd_pcm_hw_params_set_channels(handle, params, 2);

    /* 44100 bits/second sampling rate (CD quality) */
    val = 44100;
    snd_pcm_hw_params_set_rate_near(handle, params,
                                    &val, &dir);

    /* Set period size to 64 frames. */
    frames = 64;
    snd_pcm_hw_params_set_period_size_near(handle,
                                           params, &frames, &dir);

    /* Write the parameters to the driver */
    rc = snd_pcm_hw_params(handle, params);
    if (rc < 0)
    {
        fprintf(stderr,
                "unable to set hw parameters: %s\n",
                snd_strerror(rc));
        exit(1);
    }

    /* Use a buffer large enough to hold one period */
    snd_pcm_hw_params_get_period_size(params,
                                      &frames, &dir);
    size = frames * 8; /* 4 bytes/sample, 2 channels */
    buffer = (char *) malloc(size);

    /* We want to loop for 5 seconds */
    snd_pcm_hw_params_get_period_time(params,
                                      &val, &dir);
    //fprintf(stderr, "%u\n", val);
    loops = 5000000 / val;

    while (loops > 0)
    {
        loops--;
        rc = snd_pcm_readi(handle, buffer, frames);
        if (rc == -EPIPE)
        {
            /* EPIPE means overrun */
            fprintf(stderr, "overrun occurred\n");
            snd_pcm_prepare(handle);
        }
        else if (rc < 0)
        {
            fprintf(stderr,
                    "error from read: %s\n",
                    snd_strerror(rc));
        }
        else if (rc != (int)frames)
        {
            fprintf(stderr, "short read, read %d frames\n", rc);
        }
        rc = write(1, buffer, size);
        if (rc != size)
            fprintf(stderr,
                    "short write: wrote %d bytes\n", rc);
    }

    snd_pcm_drain(handle);
    snd_pcm_close(handle);
    free(buffer);

    return 0;
}

Now here is the modular version that will eventually be a library. This fails with ERROR: Failed to get number of loops.:

#define ALSA_PCM_NEW_HW_PARAMS_API

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <alsa/asoundlib.h>

char *wm8782_buffer;
int wm8782_buffer_size;

snd_pcm_t *wm8782_handle;
snd_pcm_hw_params_t *wm8782_params;
int wm8782_dir;
snd_pcm_uframes_t wm8782_frames;
unsigned int wm8782_val;
int wm8782_rc;

typedef enum
{
    WM8782_READ_OVERRUN,
    WM8782_READ_ERROR,
    WM8782_READ_SHORT,
    WM8782_READ_GOOD,
} wm8782read_t;

typedef enum
{
    WM8782_RATE_8K = 8000,
    WM8782_RATE_16K = 16000,
    WM8782_RATE_32K = 32000,
    WM8782_RATE_44_1K = 44100,
    WM8782_RATE_48K = 48000,
    WM8782_RATE_96K = 96000,
    WM8782_RATE_192K = 192000,
} wm8782rate_t;

bool wm8782_open_audio(wm8782rate_t rate_in, snd_pcm_uframes_t wm8782_frames_in)
{
    // Open PCM device for recording (capture).
    wm8782_rc = snd_pcm_open(&wm8782_handle, "hw:1",
                      SND_PCM_STREAM_CAPTURE, 0);
    if (wm8782_rc < 0)
    {
        fprintf(stderr,
                "ERROR: Unable to open pcm device: %s\n",
                snd_strerror(wm8782_rc));
        return false;
    }

    // Allocate a hardware parameters object.
    snd_pcm_hw_params_alloca(&wm8782_params);

    // Fill it in with default values
    snd_pcm_hw_params_any(wm8782_handle, wm8782_params);

    // Set the desired hardware parameters. */

    // Interleaved mode
    snd_pcm_hw_params_set_access(wm8782_handle, wm8782_params, SND_PCM_ACCESS_RW_INTERLEAVED);

    // Signed 32-bit little-endian format
    snd_pcm_hw_params_set_format(wm8782_handle, wm8782_params, SND_PCM_FORMAT_S32_LE);

    // Two channels (stereo)
    snd_pcm_hw_params_set_channels(wm8782_handle, wm8782_params, 2);

    // Set rate
    wm8782_val = rate_in;
    snd_pcm_hw_params_set_rate_near(wm8782_handle, wm8782_params, &wm8782_val, &wm8782_dir);

    // Set period size.
    wm8782_frames = wm8782_frames_in;
    snd_pcm_hw_params_set_period_size_near(wm8782_handle, wm8782_params, &wm8782_frames, &wm8782_dir);

    // Write the parameters to the driver
    wm8782_rc = snd_pcm_hw_params(wm8782_handle, wm8782_params);
    if (wm8782_rc < 0)
    {
        fprintf(stderr,
                "ERROR: Unable to set hw parameters: %s\n",
                snd_strerror(wm8782_rc));
        return false;
    }

    // Use a buffer large enough to hold one period
    snd_pcm_hw_params_get_period_size(wm8782_params, &wm8782_frames, &wm8782_dir);
    wm8782_buffer_size = wm8782_frames * 8; // 4 bytes/sample, 2 channels
    wm8782_buffer = (char *) malloc(wm8782_buffer_size);

    return true;
}

void wm8782_close_audio()
{
    snd_pcm_drain(wm8782_handle);
    snd_pcm_close(wm8782_handle);
    free(wm8782_buffer);
}

wm8782read_t wm8782_read_audio()
{
    wm8782_rc = snd_pcm_readi(wm8782_handle, wm8782_buffer, wm8782_frames);
    if (wm8782_rc == -EPIPE)
    {
        // EPIPE means overrun
        fprintf(stderr, "ERROR: Overrun occurred\n");
        snd_pcm_prepare(wm8782_handle);
        return WM8782_READ_OVERRUN;
    }
    else if (wm8782_rc < 0)
    {
        fprintf(stderr, "ERROR: From read: %s\n", snd_strerror(wm8782_rc));
        return WM8782_READ_ERROR;
    }
    else if (wm8782_rc != (int)wm8782_frames)
    {
        fprintf(stderr, "ERROR: Short read, read %d wm8782_frames\n", wm8782_rc);
        return WM8782_READ_SHORT;
    }

    return WM8782_READ_GOOD;
}

unsigned int wm8782_us_to_loops(unsigned int us_in)
{
    if(snd_pcm_hw_params_get_period_time(wm8782_params, &wm8782_val, &wm8782_dir) < 0)
    {
        fprintf(stderr, "ERROR: Failed to get number of loops.\n");
        return 0;
    }
    //fprintf(stderr, "%u\n", wm8782_val);
    return (us_in / wm8782_val);
}

int main()
{
    if(!wm8782_open_audio(44100, 64))
    {
        fprintf(stderr, "unable to open pcm device");
    }

    unsigned int loops =  wm8782_us_to_loops(5000000);

    while (loops > 0)
    {
        loops--;
        wm8782_read_audio();

        wm8782_rc = write(1, wm8782_buffer, wm8782_buffer_size);
        if (wm8782_rc != wm8782_buffer_size)
            fprintf(stderr,
                    "short write: wrote %d bytes\n", wm8782_rc);
    }

    wm8782_close_audio();

    return 0;
}
Ella Jameson
  • 408
  • 3
  • 8
  • Which global variables are you having trouble with? – Barmar May 22 '19 at 21:14
  • You need to use `extern ;` in the header file so that the application using the library will link to the variable in the library. – Barmar May 22 '19 at 21:16
  • You do need to declare the variables `extern` in the header, else you are declaring a separate variable in each source that includes the header, which produces undefined behavior. You should also ensure that exactly one source in your library either declares those same variables *without* `extern`, or declares them with initializers, or both. That's what will cause them to actually be part of the library, instead of being an external dependency. – John Bollinger May 22 '19 at 21:35

1 Answers1

1

Okay, so it turns out to NOT be an issue with globals at all... sorry all! It's some weird invalid argument being passed to snd_pcm_hw_params_get_period_size(), specifically. The code actually works with a manually defined number of loops, so I am closing this question.

Ella Jameson
  • 408
  • 3
  • 8