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的类型和格式:

 
 
  1. #include <iostream> 
  2. #include <alsa/asoundlib.h> 
  3.   
  4. int main() 
  5. std::cout << "ALSA library version: " << SND_LIB_VERSION_STR << std::endl; 
  6. std::cout << "PCM stream types: " << std::endl; 
  7. for (int val=0; val <= SND_PCM_STREAM_LAST; ++val) 
  8. std::cout << snd_pcm_stream_name((snd_pcm_stream_t)val) << std::endl; 
  9. std::cout << std::endl; 
  10.   
  11. std::cout << "PCM access types: " << std::endl; 
  12. for (int val=0; val <= SND_PCM_ACCESS_LAST; ++val) 
  13. std::cout << snd_pcm_access_name((snd_pcm_access_t)val) << std::endl; 
  14. std::cout << std::endl; 
  15.   
  16. std::cout << "PCM subformats: " << std::endl; 
  17. for (int val=0; val <= SND_PCM_SUBFORMAT_LAST; ++val) 
  18. std::cout << snd_pcm_subformat_name((snd_pcm_subformat_t)val) << " ("
  19.  << snd_pcm_subformat_description((snd_pcm_subformat_t)val) << ")" << std::endl; 
  20. std::cout << std::endl; 
  21.   
  22. std::cout << "PCM states: " << std::endl; 
  23. for (int val=0; val <= SND_PCM_STATE_LAST; ++val) 
  24. std::cout << snd_pcm_state_name((snd_pcm_state_t)val) << std::endl; 
  25. std::cout << std::endl; 
  26.   
  27. std::cout << "PCM formats: " << std::endl; 
  28. for (int val=0; val <= SND_PCM_FORMAT_LAST; ++val) 
  29. std::cout << snd_pcm_format_name((snd_pcm_format_t)val) << " ("
  30.  << snd_pcm_format_description((snd_pcm_format_t)val) << ")" << std::endl; 
  31. std::cout << std::endl; 
  32.       

2.打开PCM设备和设置参数

 
 
  1. #include <iostream> 
  2. #include <alsa/asoundlib.h> 
  3.   
  4. int main() 
  5.        int rc; 
  6.        snd_pcm_t* handle; 
  7.        snd_pcm_hw_params_t*params; 
  8.        unsigned int val, val2; 
  9.        int dir; 
  10.        snd_pcm_uframes_t frames; 
  11.   
  12.        if ( (rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) 
  13.        { 
  14. std::cerr << "unable to open pcm devices: " << snd_strerror(rc) << std::endl; 
  15. exit(1); 
  16.        } 
  17.        snd_pcm_hw_params_alloca(&params); 
  18.        snd_pcm_hw_params_any(handle, params); 
  19.        snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); 
  20.        snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE); 
  21.        snd_pcm_hw_params_set_channels(handle, params, 2); 
  22.        val = 44100; 
  23.        snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); 
  24.        if ( (rc = snd_pcm_hw_params(handle, params)) < 0) 
  25.        { 
  26. std::cerr << "unable to set hw parameters: "
  27.  << snd_strerror(rc) << std::endl; 
  28. exit(1); 
  29.        } 
  30.        std::cout << "PCM handle name = "
  31.  << snd_pcm_name(handle) << std::endl; 
  32.        std::cout << "PCM state = "
  33.  << snd_pcm_state_name(snd_pcm_state(handle)) << std::endl; 
  34.        snd_pcm_hw_params_get_access(params, (snd_pcm_access_t *)&val); 
  35.        std::cout << "access type = "
  36.  << snd_pcm_access_name((snd_pcm_access_t)val) << std::endl; 
  37.        snd_pcm_hw_params_get_format(params, (snd_pcm_format_t*)(&val)); 
  38.        std::cout << "format = << snd_pcm_format_name((snd_pcm_format_t)val) << ("
  39.  << snd_pcm_format_description((snd_pcm_format_t)val) << ")" << std::endl; 
  40.       snd_pcm_hw_params_get_subformat(params, (snd_pcm_subformat_t *)&val); 
  41.       std::cout << "subformat = << 
  42.     snd_pcm_subformat_name((snd_pcm_subformat_t)val) << ("
  43.  << snd_pcm_subformat_description((snd_pcm_subformat_t)val) << ")" << std::endl; 
  44.       snd_pcm_hw_params_get_channels(params, &val); 
  45.       std::cout << "channels = " << val << std::endl; 
  46.       snd_pcm_hw_params_get_rate(params, &val, &dir); 
  47.       std::cout << "rate = " << val << " bps" << std::endl; 
  48.        snd_pcm_hw_params_get_period_time(params, &val, &dir); 
  49.       std::cout << "period time = " << val << " us" << std::endl; 
  50.       snd_pcm_hw_params_get_period_size(params, &frames, &dir); 
  51.       std::cout << "period size = " << static_cast<int>(frames) << " frames" << std::endl; 
  52.        snd_pcm_hw_params_get_buffer_time(params, &val, &dir); 
  53.       std::cout << "buffer time = " << val << " us" << std::endl; 
  54.        snd_pcm_hw_params_get_buffer_size(params, (snd_pcm_uframes_t *) &val); 
  55.       std::cout << "buffer size = " << val << " frames" << std::endl; 
  56.       snd_pcm_hw_params_get_periods(params, &val, &dir); 
  57.       std::cout << "periods per buffer = " << val << " frames" << std::endl; 
  58.        snd_pcm_hw_params_get_rate_numden(params, &val, &val2); 
  59.       std::cout << "exact rate = " << val/val2 << " bps" << std::endl; 
  60.       val = snd_pcm_hw_params_get_sbits(params); 
  61.       std::cout << "significant bits = " << val << std::endl; 
  62.       snd_pcm_hw_params_get_tick_time(params, &val, &dir); 
  63.       std::cout << "tick time = " << val << " us" << std::endl; 
  64.       val = snd_pcm_hw_params_is_batch(params); 
  65.       std::cout << "is batch = " << val << std::endl; 
  66.       val = snd_pcm_hw_params_is_block_transfer(params); 
  67.       std::cout << "is block transfer = " << val << std::endl; 
  68.       val = snd_pcm_hw_params_is_double(params); 
  69.       std::cout << "is double = " << val << std::endl; 
  70.        val = snd_pcm_hw_params_is_half_duplex(params); 
  71.       std::cout << "is half duplex = " << val << std::endl; 
  72.        val = snd_pcm_hw_params_is_joint_duplex(params); 
  73.       std::cout << "is joint duplex = " << val << std::endl; 
  74.        val = snd_pcm_hw_params_can_overrange(params); 
  75.       std::cout << "can overrange = " << val << std::endl; 
  76.       val = snd_pcm_hw_params_can_mmap_sample_resolution(params); 
  77.       std::cout << "can mmap = " << val << std::endl; 
  78.       val = snd_pcm_hw_params_can_pause(params); 
  79.       std::cout << "can pause = " << val << std::endl; 
  80.       val = snd_pcm_hw_params_can_resume(params); 
  81.       std::cout << "can resume = " << val << std::endl; 
  82.       val = snd_pcm_hw_params_can_sync_start(params); 
  83.       std::cout << "can sync start = " << val << std::endl; 
  84.       snd_pcm_close(handle); 
  85.       return 0; 

3.一个简单的声音播放程序

 
 
  1. #include <iostream> 
  2. #include <alsa/asoundlib.h> 
  3.   
  4. int main() 
  5.        long loops; 
  6.        int rc; 
  7.        int size; 
  8.        snd_pcm_t* handle; 
  9.        snd_pcm_hw_params_t* params; 
  10.        unsigned int val; 
  11.        int dir; 
  12.        snd_pcm_uframes_t frames; 
  13.        char* buffer; 
  14.   
  15.        if ( (rc = snd_pcm_open(&handle, "default"
  16. , SND_PCM_STREAM_PLAYBACK, 0)) < 0) 
  17.        { 
  18. std::cerr << "unable to open pcm device: " << snd_strerror(rc) << std::endl; 
  19. exit(1); 
  20.        } 
  21.        snd_pcm_hw_params_alloca(&params); 
  22.        snd_pcm_hw_params_any(handle, params); 
  23.        snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); 
  24.        snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE); 
  25.        snd_pcm_hw_params_set_channels(handle, params, 2); 
  26.        val = 44100; 
  27.        snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
  28.        frames = 32; 
  29.        snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); 
  30.        if ( (rc = snd_pcm_hw_params(handle, params)) < 0) 
  31.        { 
  32. std::cerr << "unable to set hw paramseters: " << snd_strerror(rc) << std::endl; 
  33. exit(1); 
  34.        } 
  35.        snd_pcm_hw_params_get_period_size(params, &frames, &dir); 
  36.        size = frames * 4; 
  37.        buffer = new char[size]; 
  38.        snd_pcm_hw_params_get_period_time(params, &val, &dir); 
  39.        loops = 5000000 / val; 
  40.        while (loops > 0) { 
  41.               loops--; 
  42.               if ( (rc = read(0, buffer, size)) == 0) 
  43.               { 
  44. std::cerr << "end of file on input" << std::endl; 
  45. break
  46.               } 
  47.               else if (rc != size) 
  48. std::cerr << "short read: read " << rc << " bytes" << std::endl; 
  49.   
  50.               if ( (rc = snd_pcm_writei(handle, buffer, frames)) == -EPIPE) 
  51.               { 
  52. std::cerr << "underrun occurred" << std::endl; 
  53. snd_pcm_prepare(handle); 
  54.               } 
  55.               else if (rc < 0) 
  56. std::cerr << "error from writei: " << snd_strerror(rc) << std::endl; 
  57.               else if (rc != (int)frames) 
  58. std::cerr << "short write, write " << rc << " frames" << std::endl; 
  59.        } 
  60.   
  61.        snd_pcm_drain(handle); 
  62.        snd_pcm_close(handle); 
  63.        free(buffer); 
  64.   
  65.        return 0; 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值