关于USB-AUDIO使用ALSA编程的一点问题

转载自:http://blog.chinaunix.net/uid-25272011-id-3153434.html

最近在调试一款原相PAP7501摄像头中的USB的麦克风,USB层走的应该是标准的UAC协议,具体可以见USB的官网:http://www.usb.org/developers/devclass_docs#approved,而音频部分则可以跑目前Linux标准的ALSA的PCM接口,对于硬件CODEC来说,与其是完全兼容的。
     给出一份参考代码:
     这个是仿照arecord写的一个简略的测试代码,保存为wav格式的。
1、recod.c


  1. /*

  2. This example reads from the default PCM device
  3. and writesto standard output for 5 seconds of data.

  4. */

  5. /* Use the newer ALSA API*/
  6. #define ALSA_PCM_NEW_HW_PARAMS_API

  7. #include <alsa/asoundlib.h>

  8. /**************************************************************/
  9. #define ID_RIFF 0x46464952
  10. #define ID_WAVE 0x45564157
  11. #define ID_FMT 0x20746d66
  12. #define ID_DATA 0x61746164

  13. typedef unsigned long uint32_t;
  14. typedef unsigned short uint16_t;

  15. #define FORMAT_PCM 1

  16. static uint32_t totle_size = 0;

  17. struct wav_header {
  18.     /* RIFF WAVE Chunk*/
  19.     uint32_t riff_id;
  20.     uint32_t riff_sz;
  21.     uint32_t riff_fmt;
  22.     /* Format Chunk*/
  23.     uint32_t fmt_id;
  24.     uint32_t fmt_sz;
  25.     uint16_t audio_format;
  26.     uint16_t num_channels;
  27.     uint32_t sample_rate;
  28.     uint32_t byte_rate;/* sample_rate* num_channels * bps/ 8 */
  29.     uint16_t block_align;/* num_channels* bps / 8 */
  30.     uint16_t bits_per_sample;
  31.     /* Data Chunk*/
  32.     uint32_t data_id;
  33.     uint32_t data_sz;
  34. }__attribute__((packed));

  35. static struct wav_header hdr;

  36. /**************************************************************/
  37. int record_file(unsigned rate, unsigned channels,int fd, unsigned count)
  38. {
  39.     long loops;
  40.     int val;
  41.     int rc;
  42.     int size;
  43.     snd_pcm_t *handle;
  44.     snd_pcm_hw_params_t *params;
  45.     int dir;
  46.     snd_pcm_uframes_t frames;
  47.     char *buffer;                    /* TODO*/

  48.     /* Open PCM devicefor recording (capture).*/
  49.     rc = snd_pcm_open(&handle,"plughw:0,0", SND_PCM_STREAM_CAPTURE, 0);
  50.     if(rc < 0){
  51.         fprintf(stderr,"unable to open pcm device: %s\n", snd_strerror(rc));
  52.     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值