【问题标题】:Alsa lib 32 bitsAlsa lib 32 位
【发布时间】:2020-04-14 04:40:48
【问题描述】:

我一直在尝试使用 ALSA 库,但我不明白我应该如何使用它。

我使用了一个示例程序,并尝试将其修改为使用float(32 位)而不是unsigned char(8 位)。但是现在当我运行它时,我在第二个循环中出现了分段错误。

这是我的代码:

#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;
}

如何使用这个 32 位的库?

我已经尝试过这种格式以及其他格式,例如小端或大端。唯一不会崩溃的是SND_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

提前致谢。

P.S.:Linux,Ubuntu 19.10 64bits

【问题讨论】:

    标签: c linux audio alsa


    【解决方案1】:

    这确实是我的循环和我的snd_pcm_writei()中的条件问题

    感谢@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;
    }
    

    【讨论】:

      【解决方案2】:

      写入buffer时可能已经出现分段错误:

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

      sizeof(buffer) 会给你以字节为单位的大小,而不是元素的数量。它们仅对char(和unsigned char)相等,因为sizeof(char) 是1。您很可能想要遍历元素:

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

      【讨论】:

      • 谢谢,确实。我测试过了,没变,还是有分段错误。
      猜你喜欢
      • 2017-01-27
      • 2017-07-07
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 2021-05-12
      • 2022-11-12
      相关资源
      最近更新 更多