#ALSA 播放代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <alsa/asoundlib.h>#define SAMPLE_RATE (44100)
#define CHANNELS (2)
#define FRAME_SIZE (2 * CHANNELS)
// snd_pcm_hw_params_alloca snd_pcm_hw_params_set_format
int audio_play(const char *filename)
{int rc;int size;unsigned int val;int dir = 0;snd_pcm_uframes_t frames;char *buffer;int fd;int err;fd = open(filename, O_RDONLY);snd_pcm_t *handle;//以播放模式打开设备rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);if (rc < 0){fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc));exit(1);}//配置硬件参数结构体snd_pcm_hw_params_t *params;//params申请内存snd_pcm_hw_params_malloc(&params);//使用pcm设备初始化hwparamserr = snd_pcm_hw_params_any(handle, params);if (err < 0){fprintf(stderr, "Can not configure this PCM device: %s\n", snd_strerror(err));exit(1);}//设置多路数据在buffer中的存储方式//SND_PCM_ACCESS_RW_INTERLEAVED每个周期(period)左右声道的数据交叉存放err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);if (err < 0){fprintf(stderr, "Failed to set PCM device to interleaved: %s\n", snd_strerror(err));exit(1);}//设置16位采样格式,S16为有符号16位,LE是小端模式snd_pcm_format_t format = SND_PCM_FORMAT_S16_LEerr = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);if (err < 0){fprintf(stderr, "Failed to set PCM device to 16-bit signed PCM: %s\n", snd_strerror(err));exit(1);}//设置声道数,双声道err = snd_pcm_hw_params_set_channels(handle, params, CHANNELS);if (err < 0){fprintf(stderr, "Failed to set PCM device to mono: %s\n", snd_strerror(err));exit(1);}//采样率44.1KHZval = SAMPLE_RATE;//设置采样率,如果采样率不支持,会用硬件支持最接近的采样率err = snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);if (err < 0){fprintf(stderr, "Failed to set PCM device to sample rate =%d: %s\n", val, snd_strerror(err));exit(1);}unsigned int buffer_time, period_time;//获取最大的缓冲时间,buffer_time单位为us,500000us=0.5ssnd_pcm_hw_params_get_buffer_time_max(params, &buffer_time, 0);if (buffer_time > 500000)buffer_time = 500000;//设置缓冲时间err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, 0);if (err < 0){fprintf(stderr, "Failed to set PCM device to buffer time =%d: %s\n", buffer_time, snd_strerror(err));exit(1);}period_time = buffer_time / 2;//设置周期时间err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, 0);if (err < 0){fprintf(stderr, "Failed to set PCM device to period time =%d: %s\n", period_time, snd_strerror(err));exit(1);}snd_pcm_uframes_t buffer_size;snd_pcm_hw_params_get_buffer_size_max(params, &buffer_size);snd_pcm_hw_params_set_buffer_size_near(handle, params, &buffer_size);snd_pcm_uframes_t period_size = buffer_size / 4;snd_pcm_hw_params_set_period_size_near(handle, params, &period_size, 0);//让这些参数作用于PCM设备rc = snd_pcm_hw_params(handle, params);if (rc < 0){fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc));exit(1);}int bit = snd_pcm_format_physical_width(format);snd_pcm_hw_params_get_period_size(params, &frames, &dir);// 1 frame = channels * sample_size.size = frames * FRAME_SIZE; /* 2 bytes/sample, 1 channels */buffer = (char *)malloc(size);while (1){rc = read(fd, buffer, size);if (rc == 0){fprintf(stderr, "end of file on input\n");break;}else if (rc != size){fprintf(stderr, "short read: read %d bytes\n", rc);}rc = snd_pcm_writei(handle, buffer, rc / FRAME_SIZE);if (rc == -EPIPE){fprintf(stderr, "underrun occurred\n");err = snd_pcm_prepare(handle);if (err < 0){fprintf(stderr, "can not recover from underrun: %s\n", snd_strerror(err));}}else if (rc < 0){fprintf(stderr, "error from writei: %s\n", snd_strerror(rc));}else if (rc != (int)frames){fprintf(stderr, "short write, write %d frames\n", rc);}}snd_pcm_drain(handle);snd_pcm_close(handle);free(buffer);close(fd);return 0;
}

#ALSA 录音代码

ALSA C语言使用相关推荐

  1. c语言实现alsa播放

    c语言实现alsa播放 c语言实现alsa录音 这个比较简单直接上代码 #include <alsa/asoundlib.h> #include <math.h>#define ...

  2. ALSA(二), makefile, Autotools, premake

    http://antkillerfarm.github.io/ 从Gstreamer到ALSA(续) 4.SOC_SINGLE类宏 这里对SOC_SINGLE类的宏,详细说明一下,因为只有这些宏才是真 ...

  3. 扯淡!C语言怎么可能被淘汰呢?

    点击上方"大鱼机器人",选择"置顶/星标公众号" 福利干货,第一时间送达 我想通过这篇短文向你展示C伟大的一面. 作者 | Jakub Lukasiewicz, ...

  4. Alsa里面恶心的DAPM

    相关文章 音频系统,Alsa 里面的buff 是怎么计算的? 为什么需要超过48k的采样音频? 我在MTK平台下调试音频ALSA 音频几个重要的参数 openwrt 音频开发 (干货)Ai音箱和Lin ...

  5. 音频系统,Alsa 里面的buff 是怎么计算的?

    相关文章 (干货)Ai音箱和Linux音频驱动小谈 Linux ALSA 图解 我在MTK平台下调试音频ALSA 我们知道声音是模拟信号,模拟信号转成数字信号就一定有大小,既然有大小,那我们就需要开辟 ...

  6. Linux ALSA 图解

    最近在解决一个音频的问题,所以正好借这个机会来把音频的东西重新梳理一下,总结是一个很好的习惯,能方便自以后遇到问题快速排查问题. 平台「MT8167」 内核版本「kernel 4.4」 音频读数据函数 ...

  7. C 语言怎么可能被淘汰呢?

    我想通过这篇短文向你展示C伟大的一面. 作者 | Jakub Lukasiewicz,已获作者翻译授权 译者 | 弯月,责编 | 张文 头图 | CSDN 下载自东方 IC 出品 | CSDN(ID: ...

  8. Writing an ALSA Driver(二)

    前言 本文档介绍了如何编写ALSA(高级Linux声音体系结构)驱动程序.该文档主要关注PCI声卡.对于其他设备类型,API也可能不同.但是,至少ALSA内核API是一致的,因此编写它们仍然会有所帮助 ...

  9. 【QT】linux下alsa库的移植和QT中音视频的处理笔记

    一.音频的输入 linux下有一个开源的音频库----alsa库,实现了录音的功能,alsa库包含如下内容: alsa-lib-1.0.22.tar.bz2 ------- alsa的核心支持库 al ...

最新文章

  1. 使程序在后台执行,并将日志输出至文件
  2. selenium 无法定位打开a链接_测试干货 :Selenium8种元素定位法
  3. 使用Kali官网提供的虚拟机系统
  4. python手机版编程-手机编python
  5. 事关每个程序员的职业规划与履历
  6. atom配置python环境_Python编程:用VScode配置Python开发环境
  7. 【精品分享】决定边缘计算未来形态的五大需求
  8. 16_多易教育之《yiee数据运营系统》用户画像-标签体系设计篇
  9. 原子哥家的 SIM800L透传模式配置
  10. linux挂载卸载美色商城,索尼爱立信LT18i
  11. Apple watch无法登陆网易云音乐
  12. Latex插入文献--利用谷歌学术
  13. Educational Codeforces Round 91 (Rated for Div. 2) D. Berserk And Fireball
  14. java将字符转换成拼音_java中将汉字转换成拼音的实现代码
  15. 常用ES6、ES7、ES8、ES9、ES10、ES11、ES12新特性归纳
  16. 开源与标准协同发展研究报告(2022)
  17. BGB:GameBoy模拟器
  18. Java微信手气红包实现
  19. 电路方案分析(三)两轮自平衡小车
  20. 我的健身目标是增肌。身高167cm,体重大约53kg,给出一个身体各部位的训练计划...

热门文章

  1. 经典PID控制器的缺陷
  2. brpc源码分析——数据报处理过程
  3. 型、T型、K型 热电偶的区别和特点
  4. 配置 WinHTTP 的代理设置
  5. 【TeXstudio】【2】一般的图片和表格的表现形式
  6. vuex:状态管理库,分5大模块:
  7. HTML 标签的 coords 属性
  8. c语言8行7列星号矩形,C语言程序设计课件_完整版.ppt
  9. C++ 文件查找 _findfirst、_findnext和_fineclose的使用
  10. 摄像头工作原理及结构介绍(一)