使用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(&params); /* 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 ;
}

最新文章

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

热门文章

  1. volatile 的一个经典例子
  2. Java 性能优化手册 — 提高 Java 代码性能的各种技巧
  3. EOJ 3246 实验室传染病
  4. Python类总结-析构函数和__call__方法
  5. Eclipse中Tomcat 修改后台代码即时生效
  6. java 接口 继承 接口 抽象类 继承 实体类
  7. 【UOJ #105】【APIO2014】Beads and wires
  8. python3-开发进阶Flask的基础(3)
  9. Java 基础总结--反射的基本操作
  10. Spring整合Mybatis案例,献给初学的朋友