简体   繁体   English

Alsa 库 32 位

[英]Alsa lib 32 bits

I've been trying to use the ALSA lib for a while and I don't understand how I should use it.我一直在尝试使用 ALSA lib,但我不明白我应该如何使用它。

I took an example program and I've tried to modify it to use float (32bits) instead of unsigned char (8bits).我拿了一个示例程序,我试图修改它以使用float (32 位)而不是unsigned char (8 位)。 But now when I'm running it, I have a segmentation fault in the second loop.但是现在当我运行它时,我在第二个循环中出现了分段错误。

Here is my code :这是我的代码:

#include <alsa/asoundlib.h>




snd_pcm_t *create_pcm(const char* name, snd_pcm_stream_t mode, snd_pcm_format_t format, snd_pcm_access_t access, unsigned int nbChannel, unsigned int rate, int softSample, unsigned int latency)
{
    int err;
    snd_pcm_t *handle;

    if ((err = snd_pcm_open(&handle, name, mode, 0)) < 0) {
        printf("Playback open error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }

    if ((err = snd_pcm_set_params(handle,
                                  format,
                                  access,
                                  nbChannel,
                                  rate,
                                  softSample,
                                  latency)) < 0) {   /* 0.5sec */
        printf("Playback open error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }

    return handle;
}



int main(void)
{
    unsigned int i;
    snd_pcm_t *handle;
    snd_pcm_sframes_t frames;
    float buffer[16*1024];              /* some random data */


    handle = create_pcm("default", // name of the device used by the sound card
                        SND_PCM_STREAM_PLAYBACK, // to use the device in output
                        SND_PCM_FORMAT_FLOAT, // use the device with 32bit depth (float)
                        SND_PCM_ACCESS_RW_INTERLEAVED,
                        1, // use 1 channel
                        48000, // use 48000 Hz (dvd quality)
                        1, // soft resample ON
                        500000); // 0.5s of latency


    // building random data
    for(i = 0; i < sizeof(buffer); i++)
        buffer[i] = i % 255; // random();





    for (i = 0; i < 16; i++) {
        frames = snd_pcm_writei(handle, buffer, sizeof(buffer)); // segmentation fault
        if(frames < 0)
            frames = snd_pcm_recover(handle, frames, 0);
        if (frames < 0) {
            printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
            break;
        }
        if (frames > 0 && frames < (long)sizeof(buffer))
            printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
    }

    snd_pcm_close(handle);
    return 0;
}

How to use this lib with 32bits?如何将这个库与 32 位一起使用?

I've tried this format and others like little endian or big endian.. The only one that doesn't crash is SND_PCM_FORMAT_FLOAT but it's making the error :我已经尝试过这种格式和其他像 little endian 或 big endian .. 唯一不会崩溃的SND_PCM_FORMAT_FLOATSND_PCM_FORMAT_FLOAT但它正在犯错误:

ALSA lib pcm.c:8507:(snd_pcm_set_params) Sample format not available for PLAYBACK: Invalid argument
Playback open error: Invalid argument

Thanks in advance.提前致谢。

PS: Linux, Ubuntu 19.10 64bits PS:Linux,Ubuntu 19.10 64 位

The segmentation fault may already occur when you write into buffer :写入buffer时可能已经发生分段错误:

for(i = 0; i < sizeof(buffer); i++)
    buffer[i] = i % 255; // random();

sizeof(buffer) will give you the size in bytes not the number of elements. sizeof(buffer)会给你以字节为单位的大小而不是元素的数量。 They are only equal for char (and unsigned char ) since sizeof(char) is 1 .它们仅对char (和unsigned char )相等,因为sizeof(char)1 You most likely want to iterate over the elements:您最有可能想要遍历元素:

for(i = 0; i < sizeof buffer/sizeof *buffer; i++)
    buffer[i] = i % 255; // random();

It was indeed a problem of condition in my loop and my snd_pcm_writei()这确实是我的循环和我的snd_pcm_writei()的条件问题

Here is the code without errors thanks to @Osiris :由于@Osiris,这是没有错误的代码:

#include <alsa/asoundlib.h>




snd_pcm_t *create_pcm(const char* name, snd_pcm_stream_t mode, snd_pcm_format_t format, snd_pcm_access_t access, unsigned int nbChannel, unsigned int rate, int softSample, unsigned int latency)
{
    int err;
    snd_pcm_t *handle;

    if ((err = snd_pcm_open(&handle, name, mode, 0)) < 0) {
        printf("Playback open error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }

    if ((err = snd_pcm_set_params(handle,
                                  format,
                                  access,
                                  nbChannel,
                                  rate,
                                  softSample,
                                  latency)) < 0) {   /* 0.5sec */
        printf("Playback open error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }

    return handle;
}



int main(void)
{
    unsigned int i;
    snd_pcm_t *handle;
    snd_pcm_sframes_t frames;
    float buffer[16*1024];              /* some random data */


    handle = create_pcm("default", // name of the device used by the sound card
                        SND_PCM_STREAM_PLAYBACK, // to use the device in output
                        SND_PCM_FORMAT_FLOAT, // use the device with 32bit depth (float)
                        SND_PCM_ACCESS_RW_INTERLEAVED,
                        1, // use 1 channel
                        48000, // use 48000 Hz (dvd quality)
                        1, // soft resample ON
                        500000); // 0.5s of latency


    // building random data
    for(i = 0; i < sizeof(buffer) / sizeof(*buffer); i++)
        buffer[i] = i % 0xffffffff; // random();





    for (i = 0; i < 16; i++) {
        frames = snd_pcm_writei(handle, buffer, sizeof(buffer) / sizeof(*buffer)); // segmentation fault
        if(frames < 0)
            frames = snd_pcm_recover(handle, frames, 0);
        if (frames < 0) {
            printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
            break;
        }
        if (frames > 0 && frames < (long)(sizeof(buffer) / sizeof(*buffer)))
            printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
    }

    snd_pcm_close(handle);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM