Linux平台音频测试程序
来源:互联网 发布:交付软件需提供 编辑:程序博客网 时间:2024/05/16 16:16
一个音频测试程序,录音并播放。代码如下:
#include <unistd.h>#include <fcntl.h>#include <sys/types.h>#include <sys/ioctl.h>#include <stdlib.h>#include <stdio.h>#include <signal.h>#include <linux/soundcard.h>#define LENGTH 3 /* 存储秒数 */#define RATE 8000 /* 采样频率 */#define SIZE 8 /* 量化位数 */#define CHANNELS 1 /* 声道数目 *//* 用于保存数字音频数据的内存缓冲区 */unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];int fd = -1;/* 声音设备的文件描述符 */static void sig_int(int signum){ printf("\ncatch a SIGINT signal, you may be press Ctrl+C.\n"); printf("ready to quit\n"); if (fd > 0) { close(fd); fd = -1; } exit(0);}int main(int argc, char* argv[]){ int arg;/* 用于ioctl调用的参数 */ int status; /* 系统调用的返回值 */ signal(SIGINT, sig_int); /* 打开声音设备 */ fd = open("/dev/dsp", O_RDWR); if (fd < 0) { perror("open of /dev/dsp failed"); exit(1); } /* 设置采样时的量化位数 */ arg = SIZE; status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg); if (status == -1) { perror("SOUND_PCM_WRITE_BITS ioctl failed"); exit(1); } if (arg != SIZE) { perror("unable to set sample size"); exit(1); } /* 设置采样时的声道数目 */ arg = CHANNELS; status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg); if (status == -1) { perror("SOUND_PCM_WRITE_CHANNELS ioctl failed"); exit(1); } if (arg != CHANNELS) { perror("unable to set number of channels"); exit(1); } /* 设置采样时的采样频率 */ arg = RATE; status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg); if (status == -1) { perror("SOUND_PCM_WRITE_WRITE ioctl failed"); exit(1); } /* 循环,直到按下Control-C */ while (1) { printf("Say something:\n"); status = read(fd, buf, sizeof(buf)); /* 录音 */ if (status != sizeof(buf)) perror("read wrong number of bytes"); printf("You said:\n"); status = write(fd, buf, sizeof(buf)); /* 回放 */ if (status != sizeof(buf)) perror("wrote wrong number of bytes"); /* 在继续录音前等待回放结束 */ status = ioctl(fd, SOUND_PCM_SYNC, 0); if (status == -1) perror("SOUND_PCM_SYNC ioctl failed"); }}
如果不想编写代码,可以在终端输入以下命令进行测试:
cat /dev/dsp > /dev/dsp
PS:设备需要有麦克风和音频接口。实测中发现一直在“沙沙”的声音。待研究。
李迟 2017.5.31 周三阅读全文
0 0
- Linux平台音频测试程序
- linux 音频测试
- BACnet协议栈工具测试演示程序(Linux平台
- 【hi3516a 调试笔记】 海思hi3516a平台音频aac编码测试
- OSS linux下的音频程序
- Linux平台下从零开始写一个C语言访问MySQL的测试程序
- sock_ev——linux平台socket事件框架(基于字节流的测试程序)
- sock_ev——linux平台socket事件框架(基于数据报的测试程序)
- 基于Linux的S3C6410嵌入式平台的外围设备驱动程序、Makefile及测试程序的实现
- Linux Proactor测试程序
- Linux Proactor测试程序
- Linux: Framebuffer测试程序
- linux测试程序
- linux蜂鸣器测试程序
- 纯linux测试程序
- Linux串口测试程序
- Linux: Framebuffer测试程序
- Linux串口测试程序
- Java的四种引用
- 常用命令
- mysql中的if函数小总
- python之SMTP邮件的发送与接收
- DateTime.Compare(t1,t2)比较两个日期大小
- Linux平台音频测试程序
- Unity3D游戏开发之虚拟现实项目开发流程
- 探索EDA工具
- Intent那些事
- JSON与MAP之间的转换
- spring请求中数据绑定到控制器的方式探究
- Yum命令
- Python2中的urllib、urllib2与Python3中的urllib以及第三方模块requests
- TFS使用心得