Linux ALSA音频PCM播放编程
使用ALSA播放两个频率的单音,并使用GNU Radio中的Audio Source和FFT来观测声音的频谱。
#include <alsa/asoundlib.h>
#include <math.h>
#include <inttypes.h> int main(int argc, char **argv)
{
long loops;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
unsigned int val;
int rc;
int size;
int dir;
char *buffer; /* Open PCM device for playback. */
rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, );
if (rc < ) {
fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc));
exit();
} /* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms); /* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params); /* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit format */
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, );
/* 16000 samples/second sampling rate */
val = ;
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
/* Set period size to 32 frames. */
frames = ;
snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); /* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < ) {
fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc));
exit();
} /* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params, &frames, &dir); size = frames * * ; /* 2 bytes/sample, 2 channels */ buffer = (char *) malloc(size);
if(buffer == NULL) {
fprintf(stderr, "Not enough Memory!\n");
exit();
} /* We want to loop for 15 seconds */
snd_pcm_hw_params_get_period_time(params, &val, &dir); /* 15 seconds in microseconds divided by period time */
loops = / val; for (size_t i = ; i < size; i += ) {
// Generate a 500Hz tone
*((int16_t *)(buffer + i)) = (uint16_t)(16384.0*sin(i*2.0*M_PI/size));
*((int16_t *)(buffer + i + )) = (uint16_t)(16384.0*sin(i*2.0*M_PI/size));
// Generate a 2kHz tone
*((int16_t *)(buffer + i)) += (uint16_t)(16384.0*sin(i*8.0*M_PI/size));
*((int16_t *)(buffer + i + )) += (uint16_t)(16384.0*sin(i*8.0*M_PI/size));
} while (loops > ) {
loops--;
//rc = read(0, 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, frames);
if (rc == -EPIPE) {
/* EPIPE means underrun */
fprintf(stderr, "underrun occurred\n");
snd_pcm_prepare(handle);
} else if (rc < ) {
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_drop(handle);
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
return ;
}

Linux ALSA音频PCM播放编程的更多相关文章
- 基于Linux ALSA音频驱动的wav文件解析及播放程序 2012
本设计思路:先打开一个普通wav音频文件,从定义的文件头前面的44个字节中,取出文件头的定义消息,置于一个文件头的结构体中.然后打开alsa音频驱动,从文件头结构体取出采样精度,声道数,采样频率三个重 ...
- 嵌入式驱动开发之---Linux ALSA音频驱动(一)
本文的部分内容参考来自DroidPhone的博客(http://blog.csdn.net/droidphone/article/details/6271122),关于ALSA写得很不错的文章,只是少 ...
- Linux ALSA 音频库 配置和使用
ALSA应用库是核心功能,而alsa-utils是一些工具功能集合库.单纯地播放一个wav文件,使用alsa-utils即可,如果还需要合成音频.调试音频质量,那么就需要ALSA应用库. 欲安装使用A ...
- Linux ALSA音频库(二) 环境测试+音频合成+语音切换 项目代码分享
1. 环境测试 alsa_test.c #include <alsa/asoundlib.h> #include <stdio.h> // 官方测试代码, 运行后只要有一堆信息 ...
- 嵌入式开发之davinci--- 8148/8168/8127 中的alsa音频pcm g711 和aac 音频格式
(1)alsa pcm (2)g711 (3)aac (4) --------------author:pkf -------------------time:2-4 ---------------- ...
- linux alsa音频中采样率fs、比特率BCLK 、主时钟MCLK关系
转:https://blog.csdn.net/lugandong/article/details/72468831 一.拿512fs说话: 看图知道采样的位深是32bit(位),左右声道各占了8*3 ...
- 基于Orangpi Zero和Linux ALSA实现WIFI无线音箱(三)
作品已经完成,先上源码: https://files.cnblogs.com/files/qzrzq1/WIFISpeaker.zip 全文包含三篇,这是第三篇,主要讲述接收端程序的原理和过程. 第一 ...
- 内核Alsa之pcm
pcm用来描述alsa中数字音频流.Alsa音频的播放/录制就是通过pcm来实现 的. 名词解释 声音是连续模拟量,计算机将它离散化之后用数字表示,就有了以下几个名词术语. Frame. 帧是音频流中 ...
- 【转】Alsa音频编程【精华】
一.前序 这里了解一下各个参数的含义以及一些基本概念. 声音是连续模拟量,计算机将它离散化之后用数字表示,就有了以下几个名词术语. 样本长度(sample):样本是记录音频数据最基本的单位,计算机对每 ...
随机推荐
- webpack 入门总结和实践(按需异步加载,css单独打包,生成多个入口文件)
为什么是webpack webpack一下自己就
- OOD沉思录 --- 导引
一个对象一定会有如下4个属性: 1,它的身份标示,可能只是它在内存中的地址; 2,它的类的属性(通常是静态属性)和这些属性的值(通常是动态的); 3,它的类的行为(从实现者的角度看); 3,它的公开接 ...
- CodeForces 785C Anton and Fairy Tale
二分. 如果$n≤m$,显然只能$n$天. 如果$n>m$,至少可以$m$天,剩余还可以支撑多少天,可以二分计算得到,也可以推公式.二分计算的话可能爆$long$ $long$,上了个$Java ...
- python爬虫(1)——BeautifulSoup库函数find_all() (转)
原文地址:http://blog.csdn.net/depers15/article/details/51934210 python--BeautifulSoup库函数find_all() 一.语法介 ...
- bound和unbound方法,类的绑定和非绑定是什么
作者:灵剑链接:https://www.zhihu.com/question/41006598/answer/148994582来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- Redis学习篇(一)之String类型及其操作
SET 作用: 设置key对应的值, 返回ok 语法: SET key value [EX seconds] [PX milliseconds] [NX] [XX] 如果key已经存在,同名会产生覆盖 ...
- HDU 6118 度度熊的交易计划(费用流)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6118 [题目大意] 给出一张无向边权图,每个点最多可以生产b[i]商品,每件代价为a[i], 每个 ...
- Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test
POJ 1811 Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 32534 Accepted: 8 ...
- bzoj 2038 小Z的袜子 莫队算法
题意 给你一个长度序列,有多组询问,每次询问(l,r)任选两个数相同的概率.n <= 50000,数小于等于n. 莫队算法裸题. 莫队算法:将序列分为根号n段,将询问排序,以L所在的块为第一关键 ...
- BZOJ 2818: Gcd 筛法
2818: Gcd 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2818 Description 给定整数N,求1<=x,y< ...