ubuntu alsa
然而在换成了ubuntu-12.04 LST后,我发现/dev中根本找不到dsp,之前直接操作/dev/dsp的程序都无法正常运行,而是 can't find /dev/dsp.
经过我再三查找,发现现在基本上都不用dsp了,大多数人都在用ALSA音频编程(其实我也是个菜鸟)。首先介绍一下一些关于ALSA编程的知识:
http://www.op.net/~pbd/alsa-audio.html ]
我们从alsa在Linux中的设备文件结构开始我们的alsa之旅. 看看我的电脑中的alsa驱动的设备文件结构:
- $ cd /dev/snd
- $ ls -l
- crw-rw----+ root audio , -- : controlC0
- crw-rw----+ root audio , -- : midiC0D0
- crw-rw----+ root audio , -- : pcmC0D0c
- crw-rw----+ root audio , -- : pcmC0D0p
- crw-rw----+ root audio , -- : pcmC0D1p
- crw-rw----+ root audio , -- : seq
- crw-rw----+ root audio , -- : timer
- $
我们可以看到以下设备文件:
- controlC0 --> 用于声卡的控制,例如通道选择,混音,麦克风的控制等
- midiC0D0 --> 用于播放midi音频
- pcmC0D0c --〉 用于录音的pcm设备
- pcmC0D0p --〉 用于播放的pcm设备
- seq --〉 音序器
- timer --〉 定时器
其中,C0D0代表的是声卡0中的设备0,pcmC0D0c最后一个c代表capture,pcmC0D0p最后一个p代表playback,这些都是alsa-driver中的命名规则。从上面的列表可以看出,我的声卡下挂了6个设备,根据声卡的实际能力,驱动实际上可以挂上更多种类的设备,在include/sound/core.h中,定义了以下设备类型:
- #define SNDRV_DEV_TOPLEVEL ((__force snd_device_type_t) 0)
#define SNDRV_DEV_CONTROL ((__force snd_device_type_t) 1)
#define SNDRV_DEV_LOWLEVEL_PRE ((__force snd_device_type_t) 2)
#define SNDRV_DEV_LOWLEVEL_NORMAL ((__force snd_device_type_t) 0x1000)
#define SNDRV_DEV_PCM ((__force snd_device_type_t) 0x1001)
#define SNDRV_DEV_RAWMIDI ((__force snd_device_type_t) 0x1002)
#define SNDRV_DEV_TIMER ((__force snd_device_type_t) 0x1003)
#define SNDRV_DEV_SEQUENCER ((__force snd_device_type_t) 0x1004)
#define SNDRV_DEV_HWDEP ((__force snd_device_type_t) 0x1005)
#define SNDRV_DEV_INFO ((__force snd_device_type_t) 0x1006)
#define SNDRV_DEV_BUS ((__force snd_device_type_t) 0x1007)
#define SNDRV_DEV_CODEC ((__force snd_device_type_t) 0x1008)
#define SNDRV_DEV_JACK ((__force snd_device_type_t) 0x1009)
#define SNDRV_DEV_LOWLEVEL ((__force snd_device_type_t) 0x2000)
通常,我们更关心的是pcm和control这两种设备。
5.一些例子,这些例子在官方文档也有,请自行查阅
- #include <iostream>
- #include <alsa/asoundlib.h>
- int main()
- {
- std::cout << "ALSA library version: " << SND_LIB_VERSION_STR << std::endl;
- std::cout << "PCM stream types: " << std::endl;
- for (int val=; val <= SND_PCM_STREAM_LAST; ++val)
- std::cout << snd_pcm_stream_name((snd_pcm_stream_t)val) << std::endl;
- std::cout << std::endl;
- std::cout << "PCM access types: " << std::endl;
- for (int val=; val <= SND_PCM_ACCESS_LAST; ++val)
- std::cout << snd_pcm_access_name((snd_pcm_access_t)val) << std::endl;
- std::cout << std::endl;
- std::cout << "PCM subformats: " << std::endl;
- for (int val=; val <= SND_PCM_SUBFORMAT_LAST; ++val)
- std::cout << snd_pcm_subformat_name((snd_pcm_subformat_t)val) << " (" << snd_pcm_subformat_description((snd_pcm_subformat_t)val) << ")" << std::endl;
- std::cout << std::endl;
- std::cout << "PCM states: " << std::endl;
- for (int val=; val <= SND_PCM_STATE_LAST; ++val)
- std::cout << snd_pcm_state_name((snd_pcm_state_t)val) << std::endl;
- std::cout << std::endl;
- std::cout << "PCM formats: " << std::endl;
- for (int val=; val <= SND_PCM_FORMAT_LAST; ++val)
- std::cout << snd_pcm_format_name((snd_pcm_format_t)val) << " (" << snd_pcm_format_description((snd_pcm_format_t)val) << ")" << std::endl;
- std::cout << std::endl;
- }
- #include <iostream>
- #include <alsa/asoundlib.h>
- int main()
- {
- int rc;
- snd_pcm_t* handle;
- snd_pcm_hw_params_t* params;
- unsigned int val, val2;
- int dir;
- snd_pcm_uframes_t frames;
- if ( (rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, )) < )
- {
- std::cerr << "unable to open pcm devices: " << snd_strerror(rc) << std::endl;
- exit();
- }
- snd_pcm_hw_params_alloca(¶ms);
- snd_pcm_hw_params_any(handle, params);
- snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
- snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
- snd_pcm_hw_params_set_channels(handle, params, );
- val = ;
- snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
- if ( (rc = snd_pcm_hw_params(handle, params)) < )
- {
- std::cerr << "unable to set hw parameters: " << snd_strerror(rc) << std::endl;
- exit();
- }
- std::cout << "PCM handle name = " << snd_pcm_name(handle) << std::endl;
- std::cout << "PCM state = " << snd_pcm_state_name(snd_pcm_state(handle)) << std::endl;
- snd_pcm_hw_params_get_access(params, (snd_pcm_access_t *)&val);
- std::cout << "access type = " << snd_pcm_access_name((snd_pcm_access_t)val) << std::endl;
- snd_pcm_hw_params_get_format(params, (snd_pcm_format_t*)(&val));
- std::cout << "format = '" << snd_pcm_format_name((snd_pcm_format_t)val) << "' (" << snd_pcm_format_description((snd_pcm_format_t)val) << ")" << std::endl;
- snd_pcm_hw_params_get_subformat(params, (snd_pcm_subformat_t *)&val);
- std::cout << "subformat = '" <<
- snd_pcm_subformat_name((snd_pcm_subformat_t)val) << "' (" << snd_pcm_subformat_description((snd_pcm_subformat_t)val) << ")" << std::endl;
- snd_pcm_hw_params_get_channels(params, &val);
- std::cout << "channels = " << val << std::endl;
- snd_pcm_hw_params_get_rate(params, &val, &dir);
- std::cout << "rate = " << val << " bps" << std::endl;
- snd_pcm_hw_params_get_period_time(params, &val, &dir);
- std::cout << "period time = " << val << " us" << std::endl;
- snd_pcm_hw_params_get_period_size(params, &frames, &dir);
- std::cout << "period size = " << static_cast<int>(frames) << " frames" << std::endl;
- snd_pcm_hw_params_get_buffer_time(params, &val, &dir);
- std::cout << "buffer time = " << val << " us" << std::endl;
- snd_pcm_hw_params_get_buffer_size(params, (snd_pcm_uframes_t *) &val);
- std::cout << "buffer size = " << val << " frames" << std::endl;
- snd_pcm_hw_params_get_periods(params, &val, &dir);
- std::cout << "periods per buffer = " << val << " frames" << std::endl;
- snd_pcm_hw_params_get_rate_numden(params, &val, &val2);
- std::cout << "exact rate = " << val/val2 << " bps" << std::endl;
- val = snd_pcm_hw_params_get_sbits(params);
- std::cout << "significant bits = " << val << std::endl;
- snd_pcm_hw_params_get_tick_time(params, &val, &dir);
- std::cout << "tick time = " << val << " us" << std::endl;
- val = snd_pcm_hw_params_is_batch(params);
- std::cout << "is batch = " << val << std::endl;
- val = snd_pcm_hw_params_is_block_transfer(params);
- std::cout << "is block transfer = " << val << std::endl;
- val = snd_pcm_hw_params_is_double(params);
- std::cout << "is double = " << val << std::endl;
- val = snd_pcm_hw_params_is_half_duplex(params);
- std::cout << "is half duplex = " << val << std::endl;
- val = snd_pcm_hw_params_is_joint_duplex(params);
- std::cout << "is joint duplex = " << val << std::endl;
- val = snd_pcm_hw_params_can_overrange(params);
- std::cout << "can overrange = " << val << std::endl;
- val = snd_pcm_hw_params_can_mmap_sample_resolution(params);
- std::cout << "can mmap = " << val << std::endl;
- val = snd_pcm_hw_params_can_pause(params);
- std::cout << "can pause = " << val << std::endl;
- val = snd_pcm_hw_params_can_resume(params);
- std::cout << "can resume = " << val << std::endl;
- val = snd_pcm_hw_params_can_sync_start(params);
- std::cout << "can sync start = " << val << std::endl;
- snd_pcm_close(handle);
- return ;
- }
- #include <iostream>
- #include <alsa/asoundlib.h>
- int main()
- {
- long loops;
- int rc;
- int size;
- snd_pcm_t* handle;
- snd_pcm_hw_params_t* params;
- unsigned int val;
- int dir;
- snd_pcm_uframes_t frames;
- char* buffer;
- if ( (rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, )) < )
- {
- std::cerr << "unable to open pcm device: " << snd_strerror(rc) << std::endl;
- exit();
- }
- snd_pcm_hw_params_alloca(¶ms);
- snd_pcm_hw_params_any(handle, params);
- snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
- snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
- snd_pcm_hw_params_set_channels(handle, params, );
- val = ;
- snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
- frames = ;
- snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);
- if ( (rc = snd_pcm_hw_params(handle, params)) < )
- {
- std::cerr << "unable to set hw paramseters: " << snd_strerror(rc) << std::endl;
- exit();
- }
- snd_pcm_hw_params_get_period_size(params, &frames, &dir);
- size = frames * ;
- buffer = new char[size];
- snd_pcm_hw_params_get_period_time(params, &val, &dir);
- loops = / val;
- while (loops > ) {
- loops--;
- if ( (rc = read(, buffer, size)) == )
- {
- std::cerr << "end of file on input" << std::endl;
- break;
- }
- else if (rc != size)
- std::cerr << "short read: read " << rc << " bytes" << std::endl;
- if ( (rc = snd_pcm_writei(handle, buffer, frames)) == -EPIPE)
- {
- std::cerr << "underrun occurred" << std::endl;
- snd_pcm_prepare(handle);
- }
- else if (rc < )
- std::cerr << "error from writei: " << snd_strerror(rc) << std::endl;
- else if (rc != (int)frames)
- std::cerr << "short write, write " << rc << " frames" << std::endl;
- }
- snd_pcm_drain(handle);
- snd_pcm_close(handle);
- free(buffer);
- return ;
- }
- #include <iostream>
- #include <alsa/asoundlib.h>
- int main()
- {
- long loops;
- int rc;
- int size;
- snd_pcm_t* handle;
- snd_pcm_hw_params_t* params;
- unsigned int val;
- int dir;
- snd_pcm_uframes_t frames;
- char* buffer;
- if ( (rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_CAPTURE, )) < )
- {
- std::cerr << "unable to open pcm device: " << snd_strerror(rc) << std::endl;
- exit();
- }
- snd_pcm_hw_params_alloca(¶ms);
- snd_pcm_hw_params_any(handle, params);
- snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
- snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
- snd_pcm_hw_params_set_channels(handle, params, );
- val = ;
- snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);
- if ( (rc = snd_pcm_hw_params(handle, params)) < )
- {
- std::cerr << "unable to set hw parameters: " << snd_strerror(rc) << std::endl;
- exit();
- }
- snd_pcm_hw_params_get_period_size(params, &frames, &dir);
- size = frames * ;
- buffer = new char[size];
- snd_pcm_hw_params_get_period_time(params, &val, &dir);
- loops = / val;
- while (loops > )
- {
- loops --;
- rc = snd_pcm_readi(handle, buffer, frames);
- if (rc == -EPIPE)
- {
- std::cerr << "overrun occurred" << std::endl;
- snd_pcm_prepare(handle);
- }
- else if (rc < )
- std::cerr << "error from read: " << snd_strerror(rc) << std::endl;
- else if ( rc != (int)frames)
- std::cerr << "short read, read " << rc << " frames" << std::endl;
- rc = write(, buffer, size);
- if (rc != size)
- std::cerr << "short write: wrote " << rc << " bytes" << std::endl;
- }
- snd_pcm_drain(handle);
- snd_pcm_close(handle);
- free(buffer);
- return ;
- }
注意:编译的时候记得加上参数,g++ xxx.cpp -o xxx -lasound;如果编译时出现如下错误:alsa/asoundlib.h: No such file or directory
缺少一个库:
apt-get install libasound2-dev
OK!
ubuntu alsa的更多相关文章
- Ubuntu下声卡驱动解决方法alsa
一.首先介绍一下什么是ALSA : Advanced Linux Sound Architecture 的简称为 ALSA ,译成中文的意思是先进的Linux声音架构(这是google翻译的):一谈到 ...
- Ubuntu 杂音 alsa*
$ sudo alsactl init # 初始化 $ sudo alsactl store # 存储状态 会调的话可以 $ alsamixer
- ubuntu声音系统
查看声卡:cat /proc/asound/cards 显示所有ALSA的组件:cat /proc/asound/device aplay -l ubuntu使用pulseaudio,是ALSA(先进 ...
- sysv-rc-conf管理Ubuntu server开机启动服务
在RedHat中,都是使用chkconfig来管理服务的,但是在Ubuntu Server中,却有一个更好的工具,chkconfig也是可以使用的.今天来说一下sysv-rc-conf sysv-rc ...
- ubuntu入门
Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音.了解发音是有意义的,您不是第一个为此困惑的人,当然,也不会是最后一个:) 大多数的美国人读 ubun ...
- ubuntu下编译VLC
ubuntu下编译VLC 标签(空格分隔): ubuntu vlc 视频 编译 [TOC] 1.下载VLC源码包并解压 VLC的源码包在VLC的官网有,可以直接下载.也可以使用git来clone一个. ...
- ubuntu中的Wine详解
什么是wine?(转自百度百科,具体看百科) wine,是一款优秀的Linux系统平台下的模拟器软件,用来将Windows系统下的软件在Linux系统下稳定运行,该软件更新频繁,日臻完善,可以运行许多 ...
- Ubuntu升级没有声音的解决方法
自从安装U14.04LTS版本后,每次开机都会弹出update窗,以前因为网络速度慢没更新成功过,这回环境允许就尝试了下这个过程,很顺利,可更新后没声音了,找了N中方法来解决,像更改配置文件/etc/ ...
- Ubuntu下编译内核
一.下载源代码和编译软件的准备 下载内核源代码:http://www.kernel.org/ 注意,点击2.6.25内核的F版,即完整版. 如果你懒得去网站点联接,运行下列命令: 代码: $cd ~ ...
随机推荐
- .Net MVC 自定义Action类型,XmlAction,ImageAction等
MVC开发的时候,难免会用到XML格式数据,如果将XML数据当作字符串直接返回给前台,其实这不是真正意义上的xmL,你可以看到ContentType是text/html而非XML类型,这往往会造成前端 ...
- Redis PHP连接操作
安装 在PHP程序中使用Redis,需要确保我们有Redis的PHP驱动程序和PHP安装设置在机器上.可以查看PHP教程教你如何在机器上安装PHP.现在,让我们来看看一下如何设置Redis的PHP驱动 ...
- 【css】关于 hr 在各浏览器中的问题
在做页面是有时候会用到分割线 hr,但是在 ie6 和 ie7 下显示很蛋疼,本文将教你如何写出兼容各浏览器的 hr. 首页我们先了解下 hr 在各浏览器下的差异,如下表格: 正常浏览器 ie6. ...
- HTML(四):行级标签和块级标签
一.行级标签 行级标签又称为内联标签,行级标签不会单独占据一行,设置宽高无效,行内内部可以容纳其他行内元素,但不可以容纳块元素,不然会出现无法预知的效果. 常见行级标签: span.strong.em ...
- js dom添加回车事件
<!DOCTYPE html> <html lang="en" class="no-js"> <head> <meta ...
- Tornado + Celery + RabbitMQ
声明:代码是从项目中截取的, 为进行测试 使用Celery任务队列,Celery 只是一个任务队列,需要一个broker媒介,将耗时的任务传递给Celery任务队列执行,执行完毕将结果通过broker ...
- ERROR:tornado上传文件过大超出范围报错
该怎么解决呢? HTTPServer里面指定max_buffer_size就可以了 EXAMPLE # server = HTTPServer(application, max_buffer_size ...
- 图形界面至少要有一个顶级Swing容器
图形界面至少要有一个顶级Swing容器 顶级Swing容器为其它Swing组件在屏幕上的绘制和处理事件提供支持 常用的顶级容器: JFrame(框架):表示主程序窗口 JDialog(对话框):每个J ...
- 数据抓取的艺术(三):抓取Google数据之心得
本来是想把这部分内容放到前一篇<数据抓取的艺术(二):数据抓取程序优化>之中.但是随着任务的完成,我越来越感觉到其中深深的趣味,现总结如下: (1)时间 时间是一个与抓取规模相形而 ...
- LintCode #1 A + B 问题
分析:以3 + 5为例 3的二进制为 1 1,5的二进制为 1 0 1,可以这样做:1先给这两个数加起来不考虑进位,这样得到的结果为 1 1 0,会发现与^得到的结果相同,与是先给两个数 做^运算:2 ...