linux下usb音频记录程序,采集8000,双通,记录10s切换记录下一个文件,统计是否丢包,文件名为系统当前时间,文文件记录路径在/mnt/disk/record/Audio/下,用c写
时间: 2024-01-21 22:02:46 浏览: 86
以下是一个简单的USB音频记录程序的示例代码,可以满足你的需求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include <time.h>
#include <unistd.h>
#define PCM_DEVICE "default"
#define PCM_FILENAME "/mnt/disk/record/Audio/%s.pcm"
#define PCM_CHANNELS 2
#define PCM_RATE 8000
#define PCM_FORMAT SND_PCM_FORMAT_S16_LE
#define RECORD_TIME 10
#define RECORD_SIZE (PCM_RATE * PCM_CHANNELS * (PCM_FORMAT == SND_PCM_FORMAT_S16_LE ? 2 : 1) * RECORD_TIME)
int main(int argc, char **argv) {
int err;
char filename[256];
time_t rawtime;
struct tm *timeinfo;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
char *buffer;
int size, count, file_count;
FILE *file;
// 初始化文件计数器
file_count = 0;
// 初始化PCM设备
if ((err = snd_pcm_open(&capture_handle, PCM_DEVICE, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf(stderr, "cannot open audio device %s (%s)\n", PCM_DEVICE, snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
fprintf(stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_any(capture_handle, hw_params)) < 0) {
fprintf(stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf(stderr, "cannot set access type (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_set_format(capture_handle, hw_params, PCM_FORMAT)) < 0) {
fprintf(stderr, "cannot set sample format (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_set_channels(capture_handle, hw_params, PCM_CHANNELS)) < 0) {
fprintf(stderr, "cannot set channel count (%s)\n", snd_strerror(err));
exit(1);
}
unsigned int rate = PCM_RATE;
if ((err = snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &rate, 0)) < 0) {
fprintf(stderr, "cannot set sample rate (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params(capture_handle, hw_params)) < 0) {
fprintf(stderr, "cannot set parameters (%s)\n", snd_strerror(err));
exit(1);
}
snd_pcm_hw_params_free(hw_params);
if ((err = snd_pcm_prepare(capture_handle)) < 0) {
fprintf(stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror(err));
exit(1);
}
// 初始化缓冲区
size = snd_pcm_frames_to_bytes(capture_handle, RECORD_SIZE);
buffer = (char *) malloc(size);
if (!buffer) {
fprintf(stderr, "cannot allocate memory for audio buffer\n");
exit(1);
}
// 循环记录音频数据
while (1) {
// 生成文件名
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(filename, sizeof(filename), PCM_FILENAME, asctime(timeinfo));
filename[strlen(filename) - 1] = '\0'; // 去掉末尾的换行符
// 打开文件
file = fopen(filename, "wb");
if (!file) {
fprintf(stderr, "cannot open file %s\n", filename);
exit(1);
}
// 记录音频数据
count = 0;
while (count < RECORD_SIZE) {
if ((err = snd_pcm_readi(capture_handle, buffer, RECORD_SIZE - count)) != RECORD_SIZE - count) {
fprintf(stderr, "read from audio interface failed (%s)\n", snd_strerror(err));
exit(1);
}
fwrite(buffer, 1, RECORD_SIZE - count, file);
count += RECORD_SIZE - count;
}
fclose(file);
// 更新文件计数器
file_count++;
// 统计是否丢包
int lost_count = 0;
for (int i = 0; i < size; i += 2) {
if (buffer[i] == 0 && buffer[i + 1] == 0) {
lost_count++;
}
}
printf("File %d: %d packets lost\n", file_count, lost_count);
// 等待一段时间,准备记录下一个文件
sleep(1);
}
// 释放资源
free(buffer);
snd_pcm_close(capture_handle);
return 0;
}
```
请注意,这只是一个简单的示例代码,可能还需要进行更多的优化和改进。此外,文件名使用的是系统当前时间,每个文件记录的时间为10秒,文件名中没有记录丢包信息。你可以根据自己的需求进行修改。
阅读全文