转载自: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
- /*
- This example reads from the default PCM device
- and writesto standard output for 5 seconds of data.
- */
- /* Use the newer ALSA API*/
- #define ALSA_PCM_NEW_HW_PARAMS_API
- #include <alsa/asoundlib.h>
- /**************************************************************/
- #define ID_RIFF 0x46464952
- #define ID_WAVE 0x45564157
- #define ID_FMT 0x20746d66
- #define ID_DATA 0x61746164
- typedef unsigned long uint32_t;
- typedef unsigned short uint16_t;
- #define FORMAT_PCM 1
- static uint32_t totle_size = 0;
- struct wav_header {
- /* RIFF WAVE Chunk*/
- uint32_t riff_id;
- uint32_t riff_sz;
- uint32_t riff_fmt;
- /* Format Chunk*/
- uint32_t fmt_id;
- uint32_t fmt_sz;
- uint16_t audio_format;
- uint16_t num_channels;
- uint32_t sample_rate;
- uint32_t byte_rate;/* sample_rate* num_channels * bps/ 8 */
- uint16_t block_align;/* num_channels* bps / 8 */
- uint16_t bits_per_sample;
- /* Data Chunk*/
- uint32_t data_id;
- uint32_t data_sz;
- }__attribute__((packed));
- static struct wav_header hdr;
- /**************************************************************/
- int record_file(unsigned rate, unsigned channels,int fd, unsigned count)
- {
- long loops;
- int val;
- int rc;
- int size;
- snd_pcm_t *handle;
- snd_pcm_hw_params_t *params;
- int dir;
- snd_pcm_uframes_t frames;
- char *buffer; /* TODO*/
- /* Open PCM devicefor recording (capture).*/
- rc = snd_pcm_open(&handle,"plughw:0,0", SND_PCM_STREAM_CAPTURE, 0);
- if(rc < 0){
- fprintf(stderr,"unable to open pcm device: %s\n", snd_strerror(rc));