linux - 不能用 libasound 播放 USB 声卡中的声音。 (C++)

标签 linux audio alsa soundcard libasound

我目前正在学习使用 alsa API (libasound) 进行开发。 我想将 PCM 声音发送到我的 USB 声卡。

我运行这段代码:

#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>

#include "helloPi.h"

int main(int argc, char *argv[]) {
    int i;
    int err;

    snd_pcm_t *playback_handle;
    snd_pcm_hw_params_t *hw_params;

    if ((err = snd_pcm_open(&playback_handle, "hw:1,0", SND_PCM_STREAM_PLAYBACK,
        0)) < 0) {
        fprintf(stderr, "cannot open audio device %s (%s)\n", argv[1],
            snd_strerror(err));
        exit(1);
    }

    if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
        fprintf(stderr, "cannot allocate hardware parameter structure (%s)\n",
            snd_strerror(err));
        exit(1);
    }

    if ((err = snd_pcm_hw_params_any(playback_handle, hw_params)) < 0){
        fprintf(stderr, "cannot initialize hardware parameter structure (%s)\n",
            snd_strerror(err));
        exit(1);
    }

    if ((err = snd_pcm_hw_params_set_access(playback_handle, hw_params,
        SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
        fprintf(stderr, "cannot set access type (%s)\n", snd_strerror(err));
        exit(1);
    }

    if ((err = snd_pcm_hw_params_set_format(playback_handle, hw_params,
        SND_PCM_FORMAT_S16_LE)) < 0) {
        fprintf(stderr, "cannot set sample format (%s)\n", snd_strerror(err));
        exit(1);
    }

    unsigned int freq = 44100;

    if ((err = snd_pcm_hw_params_set_rate_near(playback_handle, hw_params,
        &freq, 0)) < 0) {
        fprintf(stderr, "cannot set sample rate (%s)\n", snd_strerror(err));
        exit(1);
    }

    if ((err = snd_pcm_hw_params_set_channels(playback_handle, hw_params, 2))
        < 0) {
        fprintf(stderr, "cannot set channel count (%s)\n", snd_strerror(err));
        exit(1);
    }

    if ((err = snd_pcm_hw_params(playback_handle, hw_params)) < 0) {
        fprintf(stderr, "cannot set parameters (%s)\n", snd_strerror(err));
        exit(1);
    }

    snd_pcm_hw_params_free(hw_params);

    if ((err = snd_pcm_prepare(playback_handle)) < 0) {
        fprintf(stderr, "cannot prepare audio interface for use (%s)\n",
            snd_strerror(err));
        exit(1);
    }

    for (i = 0; i < 100; ++i) {
        if ((err = snd_pcm_writei(playback_handle, buf, 2048)) < 0) {
            fprintf(stderr, "write to audio interface failed (%s)\n",
                snd_strerror(err));
            exit(1);
        }
    }

    snd_pcm_close(playback_handle);
    exit(0);
  }

注意:buf 是在 HelloPi.h 中声明的包含声波的数组。

当我将 snd_pcm_open(&playback_handle, "hw:1,0", SND_PCM_STREAM_PLAYBACK,0)hw:0,0 一起使用时,可以很好地处理内部声音我笔记本电脑的卡。但是,如果我使用 hw:1,0 来使用 USB 声卡,则什么也没有发生(甚至没有错误!)。

如果我在终端中运行:

aplay -l

我有以下结果:

carte 0: PCH [HDA Intel PCH], périphérique 0: ALC283 Analog [ALC283 Analog]
  Sous-périphériques: 1/1
  Sous-périphérique #0: subdevice #0
carte 0: PCH [HDA Intel PCH], périphérique 3: HDMI 0 [HDMI 0]
  Sous-périphériques: 1/1
  Sous-périphérique #0: subdevice #0
carte 1: Set [C-Media USB Headphone Set], périphérique 0: USB Audio [USB Audio]
  Sous-périphériques: 1/1
  Sous-périphérique #0: subdevice #0

你好

我知道 usb 卡没问题,因为我有声音:

speaker-test -Dhw:1,0 -c2 -twav

我不知道为什么我的代码是用 hw:1,0...产生声音的音符

我希望你们中的一些人能帮助我! 谢谢,

马克西姆。

最佳答案

我找到了一个解决方案,我将示例中提供的代码与 asoundlib 一起使用。

/*
 *  This extra small demo sends a random samples to your speakers.
 */

#include <alsa/asoundlib.h>
static char *device = "hw:1,0";         /* playback device */

snd_output_t *output = NULL;
unsigned char buffer[16*1024];              /* some random data */

int main(void)
{
        int err;
        unsigned int i;
        snd_pcm_t *handle;
        snd_pcm_sframes_t frames;

        for (i = 0; i < sizeof(buffer); i++)
                buffer[i] = random() & 0xff;

    if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
        printf("Playback1 open error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }
    if ((err = snd_pcm_set_params(handle,
                                  SND_PCM_FORMAT_S16,
                                  SND_PCM_ACCESS_RW_INTERLEAVED,
                                  2,
                                  44100,
                                  0,
                                  50000)) < 0) {    /* 0.5sec */
        printf("Playback2 open error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }

        for (i = 0; i < 16; i++) {
                frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
                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;
}

它可以工作,但我仍然需要找出第一个代码不好的原因。

再见, 马克西姆。

关于linux - 不能用 libasound 播放 USB 声卡中的声音。 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38672296/

相关文章:

audio - 如何配置 JACK 音频服务器以自动使用特定的卡?

c - c中带有malloc的字符串数组的数组

c - socket 。设置 sockaddr_in 结构的 s_addr 字段

c++ - 我在注册表中的哪里可以找到音频设备名称的不同部分

audio - 在使用Windows 10双启动的全新Ubuntu 18.04.5系统上没有声音

linux - 树莓派 gstreamer1.0 alsasrc0 : Internal data flow error

json - 将 JSONL 转换为 JSON

Android 和内核模块

java - MIDI 轨道不改变音高或乐器

audio - 如何将PCM音频样本流转换为音量?