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 ~ ...
随机推荐
- CAS (8) —— Mac下配置CAS到JBoss EAP 6.4(6.x)的Standalone模式(服务端)
CAS (8) -- Mac下配置CAS到JBoss EAP 6.4(6.x)的Standalone模式(服务端) jboss版本: jboss-eap-6.4-CVE-2015-7501 jdk版本 ...
- [算法]谷歌笔试题:Beautiful Numbers
题目描述 思路 这道题就是在说,由多个1组成的数是beautiful的,现在想求出r进制中的r,使得给出的数字转换成r进制,得到beautiful的数字,如果有多种方式转换,那么取1的个数最多的那种情 ...
- SpringBoot2 时间类型自动格式化 自动转换
package com.archibladwitwicke.springboot2.chapter03.configurer; import com.archibladwitwicke.springb ...
- 微信小程序——自定义导航栏
微信头部导航栏可能通过json配置: 但是有时候我们项目需求可能需要自定义头部导航栏,如下图所示: 现在具体说一下实现步骤及方法: 步骤: 1.在 app.json 里面把 "navigat ...
- jq 获取当前屏幕高度
alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(document).height()); //浏览器时下窗口文档的高度 alert($(docum ...
- 在天河二号上对比Julia,Python和R语言
Julia是一款高级高效为技术计算(technical computing)而设计的编程语言,其语法与其他计算环境类似.其为分布式计算和并行所设计,最知名的地方在于其接近C语言的高效率. 按开发者的话 ...
- 浅谈跨域以WebService对跨域的支持
跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其他域下的资源. 在 ...
- Linux Makefile简单模板
########################################### #Makefile for simple programs ########################## ...
- USB学习笔记连载(十六):USB数字签名
转载:http://blog.chinaaet.com/crazybingo/p/34487 曾记得在最开始安装驱动程序的时候出现过这个错误....但是最近我在别的电脑安装的时候又不出现这个错误了.. ...
- TL431的几种常用用法
TL431的主要作用是使得电路获得更稳定的电压,TL431是一种较为精密的可控稳压源,有着较为特殊的动态阻抗.其动态响应速度快,输出噪声低,价格低廉. 注意上述一句话概括,就是便宜,精密可控稳压源TL ...