嵌入式爱好者

查看: 1313|回复: 2

[Linux] 调用ALSA音频采集无法识别库问题

[复制链接]

10

主题

38

帖子

86

积分

A40i/T3/T507/T527通行证FCU2401通行证

扫一扫,手机访问本帖
发表于 2023-4-15 15:19:39 | 显示全部楼层 |阅读模式
我的开发环境是全志A40i,用的配套的底板和核心板,按照手册录音放音没有问题。
之后我想实现的功能是读取录音的数据,并将其转换为指定格式文件输出,同时将数据显示在屏幕上,以波形图的形式,就像录音实时跟踪那种效果
在已经搭建好源码和交叉编译环境下,我编译一个文件在终端提示找不到#include <alsa/asoundlib.h>文件,是因为这句话没有识别,但是系统实际是已经安装了ALSA库的
我的源码如下
  1. #define ALSA_PCM_NEW_HW_PARAMS_API
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>

  10. #include <alsa/asoundlib.h>

  11. int main() {
  12.     long loops;
  13.     int rc;
  14.     int size;
  15.     unsigned int val;
  16.     int dir;
  17.     char *buffer;
  18.     snd_pcm_t *handle;
  19.     snd_pcm_hw_params_t *params;
  20.     snd_pcm_uframes_t frames;

  21.     /*以录制模式打开*/
  22.     /* Open PCM device for recording (capture). */
  23.     rc = snd_pcm_open( &handle, "default", SND_PCM_STREAM_CAPTURE, 0);
  24.     if (rc < 0) {
  25.         fprintf(stderr, "unable to open pcm device");
  26.         exit(EXIT_FAILURE);
  27.     }

  28.     /*分配一个参数对象*/
  29.     /* Allocate a hardware parameters object. */
  30.     snd_pcm_hw_params_alloca(&#182;ms);
  31.     /*初始化参数对象*/
  32.     /* Fill it in with default values. */
  33.     rc = snd_pcm_hw_params_any(handle, params);
  34.     if (rc < 0) {
  35.         printf("Err\n");
  36.     }
  37.     /* Set the desired hardware parameters. */

  38.     /*交错模式*/
  39.     /* Interleaved mode */
  40.     rc = snd_pcm_hw_params_set_access(handle, params,
  41.                           SND_PCM_ACCESS_RW_INTERLEAVED);
  42.     if (rc < 0) {
  43.         printf("Err\n");
  44.     }
  45.     /*PCM格式*/
  46.     /* Signed 16-bit little-endian format */
  47.     rc = snd_pcm_hw_params_set_format(handle, params,
  48.                                   SND_PCM_FORMAT_S16_LE);
  49.     if (rc < 0) {
  50.         printf("Err\n");
  51.     }
  52.     /*设置通道数*/
  53.     /* Two channels (stereo) */
  54.     rc = snd_pcm_hw_params_set_channels(handle, params, 2);
  55.     if (rc < 0) {
  56.         printf("Err\n");
  57.     }
  58.     /*设置采样率*/
  59.     /* 44100 bits/second sampling rate (CD quality) */
  60.     val = 44100;
  61.     rc = snd_pcm_hw_params_set_rate_near(handle, params,
  62.                                 &val, &dir);
  63.     if (rc < 0) {
  64.         printf("Err\n");
  65.     }
  66.     /*没周期的帧数*/
  67.     /* Set period size to 32 frames. */
  68.     frames = 32;
  69.     rc = snd_pcm_hw_params_set_period_size_near(handle,
  70.                             params, &frames, &dir);
  71.     if (rc < 0) {
  72.         printf("Err\n");
  73.     }
  74.     /* Write the parameters to the driver */
  75.     rc = snd_pcm_hw_params(handle, params);
  76.     if (rc < 0) {
  77.         fprintf(stderr,
  78.                 "unable to set hw parameters: %s/n",
  79.                 snd_strerror(rc));
  80.         exit(1);
  81.     }

  82.     /* Use a buffer large enough to hold one period */
  83.     rc = snd_pcm_hw_params_get_period_size(params,
  84.                                           &frames, &dir);
  85.     if (rc < 0) {
  86.         printf("Err\n");
  87.     }
  88.     size = frames * 4; /* 2 bytes/sample, 2 channels */
  89.     buffer = (char *) malloc(size);

  90.     /* We want to loop for 5 seconds */
  91.     rc = snd_pcm_hw_params_get_period_time(params, &val, &dir);
  92.     loops = 5000000 / val;

  93.     while (loops > 0) {
  94.         loops--;
  95.         rc = snd_pcm_readi(handle, buffer, frames);
  96.         if (rc == -EPIPE) {
  97.           /* EPIPE means overrun */
  98.           fprintf(stderr, "overrun occurred/n");
  99.           //把PCM流置于PREPARED状态,这样下次我们向该PCM流中数据时,它就能重新开始处理数据。
  100.           snd_pcm_prepare(handle);
  101.         } else if (rc < 0) {
  102.           fprintf(stderr,
  103.                   "error from read: %s/n",
  104.                   snd_strerror(rc));
  105.         } else if (rc != (int)frames) {
  106.           fprintf(stderr, "short read, read %d frames/n", rc);
  107.         }
  108.         rc = write(1, buffer, size);
  109.         if (rc != size)
  110.           fprintf(stderr,
  111.                   "short write: wrote %d bytes/n", rc);
  112.     }

  113.     //调用snd_pcm_drain把所有挂起没有传输完的声音样本传输完全
  114.     rc = snd_pcm_drain(handle);
  115.     //关闭该音频流,释放之前动态分配的缓冲区,退出
  116.     rc = snd_pcm_close(handle);
  117.     free(buffer);

  118.     return 0;
  119. }
复制代码
测试文件引用代码如下
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <sys/ioctl.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
复制代码
测试程序说明都没有执行,只是单纯的引用alsa库,都无法编译通过,报错信息如下


这里想请问一下大佬,如何正确的识别ALSA库



回复

使用道具 举报

0

主题

1173

帖子

2195

积分

发表于 2023-4-17 13:32:58 | 显示全部楼层
buildroot-201611/target/user_rootfs_misc/OKA40i_C/sdk_lib/cedarx/include/external/include/alsa/asoundlib.h您看看您的include路径有没有问题

点评

LIBS += -L/root/workspace/allwinner/A40i/bsp/lichee/out/sun8iw11p1/linux/common/buildroot/host/usr/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/ -lasound 在pro文件种添加这句好使了  详情 回复 发表于 2023-4-23 09:43
点评回复 支持 反对

使用道具 举报

10

主题

38

帖子

86

积分

A40i/T3/T507/T527通行证FCU2401通行证

 楼主| 发表于 2023-4-23 09:43:46 | 显示全部楼层
zhichao 发表于 2023-4-17 13:32
buildroot-201611/target/user_rootfs_misc/OKA40i_C/sdk_lib/cedarx/include/external/include/alsa/asoun ...

LIBS +=   -L/root/workspace/allwinner/A40i/bsp/lichee/out/sun8iw11p1/linux/common/buildroot/host/usr/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/ -lasound
在pro文件种添加这句好使了:lol
点评回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋| 飞凌嵌入式 ( 冀ICP备12004394号-1 )

GMT+8, 2024-5-19 08:58

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表