ALSA API 被主要分为以下几种接口: l 控制接口:提供灵活的方式管理注册的声卡和对存在的声卡进行查询。 l PCM接口:提供管理数字音频的捕捉和回放。 l 原始 MIDI 接口: 支持 MIDI (Musical Instrument Digital Interface), 一种标准电子音乐指令集。 这些 API 提供访问声卡上的 MIDI 总线。这些原始借口直接工作在 The MIDI 事件上,程序员只需要管理协议和时间。 l 记时接口: 为支持声音的同步事件提供访问声卡上的定时器。 l 音序器接口:一个比原始MIDI接口高级的MIDI编程和声音同步高层接口。它可以处理很多的MIDI协议和定时器。 l 混音器接口:控制发送信号和控制声音大小的声卡上的设备。
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
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); }
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); }