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 ~ ...
随机推荐
- go语言简述
Go是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go语言被设计成一门应用于搭建web服务器,存储集群或类似用途的巨型中央服务器的系统编程语言.对于高性能分布式系统领域而言,Go语 ...
- SpringCloudConfig与SpringCloudEureka 注册中心与配置中心高可用的意义
所有的配置会缓存在本地,远程配置中心DOWN机,不影响本地使用,只是无法重新请求服务端获取配置的更新. 不管是注册中心的高可用,还是配置中心的高可用.本质上都是保证服务能注册上去或者能从配置中心获取配 ...
- springcloud服务已经关但是Eureka还是显示up
该状态持续很久,访问该服务也返回错误,但在注册中心界面,该服务却一直存在,且为UP状态,并且在大约十分钟后,出现一行红色大字:EMERGENCY! EUREKA MAY BE INCORRECTLY ...
- kindle书摘-围城-相爱勿相伤
https://github.com/starrtc/android-demo 围城(爱熄灭了灯,心围一座城.出版七十周年纪念版) (钱钟书) - 您在位置 #49-49的标注 | 添加于 2018年 ...
- 解决android studio上“com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65935”问题
我是在更换应用的一个jar包时发生的这个错误,网上查到说是因为同时在工程中引入了多个第三方jar包,导致调用的方法数超过了android设定的65935个(DEX 64K problem),进而导致d ...
- css3学习系列之初识 transform (一)
一.transform是哈? 从字面意思来看 transform的含义是:改变,使…变形:转换,对 没错 就是变形 变形当有 放大缩小 东倒 西歪,刚好css3中 transform 也出这个 放几个 ...
- javascript小技巧[转]
总的来说,如果你要找js 的东西,而不看这两篇的话,肯定要多花好多时间!!哈哈!! 如果你找的javascript的东西的话,建议你 ctrl+F 直接在这个页上找,因为这里80%有你要找的,但是要 ...
- C语言实现Linux下删除非空目录
#include <sys/stat.h> #include <dirent.h> #include <fcntl.h> /** * 递归删除目录(删除该目录以及该 ...
- JUnit4测试方法异常抛出实例
它用于测试由方法抛出的异常. import org.junit.*; /** * JUnit Expected Exception Test * @author yiibai * */ public ...
- Tomcat 部署一工程时Deploy Location 为什么 是 INVALID
1.eclipse项目: 在项目的.settings目录下,找到org.eclipse.wst.common.component文件: <?xml version="1.0" ...