Linux ALSA音频PCM播放编程
2024-09-02 05:56:39
使用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 ;
}
最新文章
- Lua热更系统
- My安卓知识5--百度地图api的使用,周边信息检索
- iOS10 UI教程视图的生命周期
- 一个android的各种控件库
- Struct2、Hibernate3、Spring3框架搭建实战(转)
- Redis系列(1)之安装
- Intellij IDEA 建立文件夹目录问题
- get和post与服务端的交互方式
- LeetCode算法题-Rotate String(Java实现)
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Saratov
- Apache的commons工具类
- mysql 知识
- SparkStreaming--reduceByKeyAndWindow
- 测试webservice的时候,如果出现这个错误:";The test form is only available for requests from the local machine";
- 使用Spring+Junit4进行测试
- 解题:POI 2014 Ant colony
- (三)宏 __cplusplus C/C++混合编程
- 免费一年MAP2014+6个月免费MIS2014
- JSP学习笔记(五):日期处理、页面重定向、点击量统计、自动刷新和发送邮件
- 【转】线程池体系介绍及从阿里Java开发手册学习线程池的正确创建方法