已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Linux下ALSA声卡编程 一 . 介绍 ALSA 标准是一个先进的 linux 声音体系。它包含内核驱动集合, API 库和工具对 Linux 声音进行支持。 ALSA 包含一系列内核驱动对不同的声卡进行支持,还提供了 libasound 的 API 库。用这些进行写程序不需要打开设备等操作,所以编程人员在写程序的时候不会被底层的东西困扰。与此相反 OSS/Free 驱动在内核层次调用,需要指定设备名和调用 ioctl 。为提供向后兼容, ALSA 提供内核模块模仿 OSS/Free 驱动,所以大多数的程序不需要改动。 ALSA 拥有调用插件的能力对新设备提供扩展,包括那些用软件模拟出来的虚拟设备。 ALSA 还提供一组命令行工具包括 mixer, sound file player 和工具控制一些特别的声卡的特别的作用。 二 .ALSA 体系: ALSA API 被主要分为以下几种接口: l 控制接口:提供灵活的方式管理注册的声卡和对存在的声卡进行查询。 l PCM 接口:提供管理数字音频的捕捉和回放。 l 原始 MIDI 接口 : 支持 MIDI (Musical Instrument Digital Interface), 一种标准电子音乐指令集。 这些 API 提供访问声卡上的 MIDI 总线。这些原始借口直接工作在 The MIDI 事件上,程序员只需要管理协议和时间。 l 记时接口 : 为支持声音的同步事件提供访问声卡上的定时器。 l 音序器接口:一个比原始 MIDI 接口高级的 MIDI 编程和声音同步高层接口。它可以处理很多的 MIDI 协议和定时器。 l 混音器接口:控制发送信号和控制声音大小的声卡上的设备。 三 . 声卡的缓存和数据的传输: 一块声卡有一个声卡内存用来存储记录的样本。当它被写满时就产生中断。内核驱动就使用 DMA 将数据传输到内存中。同样地,当在播放时就将内存中的声音样本使用 DMA 传到声卡的内存中! 声卡的缓存是环状的,这里只讨论应用程序中的内存结构: ALSA 将数据分成连续的片段然后传到按单元片段传输。 四:典型的声音程序结构: open interface for capture or playback set hardware parameters (access mode, data format, channels, rate, etc.) while there is data to be processed: read PCM data (capture) or write PCM data (playback) close interface 五 . 一些例子: 1. 显示一些 PCM 的类型和格式 : #include #include int main() std:cout ALSA library version: SND_LIB_VERSION_STR std:endl; std:cout PCM stream types: std:endl; for (int val=0; 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=0; 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=0; 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=0; 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=0; 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; 2. 打开 PCM 设备和设置参数 #include #include 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, 0) 0) std:cerr unable to open pcm devices: snd_strerror(rc) std:endl; exit(1); 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, 2); val = 44100; snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); if ( (rc = snd_pcm_hw_params(handle, params) 0) std:cerr unable to set hw parameters: snd_strerror(rc) std:endl; exit(1); 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(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 0; 3. 一个简单的声音播放程序 #include #include 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, 0) 0) std:cerr unable to open pcm device: snd_strerror(rc) std:endl; exit(1); 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, 2); val = 44100; snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); frames = 32; snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); if ( (rc = snd_pcm_hw_params(handle, params) 0) std:cerr unable to set hw paramseters: snd_strerror(rc) 0) loops-; if ( (rc = read(0, buffer, size) = 0) 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 0) 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 0; 4. 一个简单的记录声音的程序 #include #include 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, 0) 0) std:cerr unable to open pcm device: snd_strerror(rc) std:endl; exit(1); snd_pcm_hw_params_alloca(¶ms); snd_pcm_hw_params_any(handle, params); snd_pcm_hw_params_set_access(handle, params, SN
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 加强自身学习与技能提升实脸室负责人培训材料
- 全流程保险理赔服务质量提升计划
- 原材料采购计划及供应商管理方案-场景目标
- 企业项目管理计划及实施方案
- 医药行业专业论坛的组织与安排指南
- 综合门诊医生工作计划及安排
- 渔船伏季休渔期船员安全管理工作方案
- 加油加气站消防安全及事故判定标准相关知识测试试题附答案
- 2024年互联网应用发展现状及特征知识测试试题附答案
- 碳指数设计师高级参与企业ESG报告碳相关内容设计
- 构建“五育融合”的小学体育活动体系
- 医院网络安全培训课件
- 教师资格证初中英语教学知识与能力试卷(含解析)(2025年)
- DB11-T 1382-2022 空气源热泵系统应用技术规程
- 民法典合同编案例课件
- 化工冬季四防安全培训课件
- 银行贷后培训课件
- 2025甘肃张掖山丹县公安局招聘留置看护警务辅助人员40人笔试备考试题及答案解析
- 国家公园考试题型及答案
- 社区干部面试题目及答案
- 初中英语人称代词专项练习题
评论
0/150
提交评论