使用alsa库写一个音频记录程序,需要按指定路径生成日期文件夹,按当前时间创建文件名,最后再根据,然后以10秒为一个单位时间保存成wav标志文件,可以使用通用播放器播放
时间: 2024-03-13 07:43:18 浏览: 101
好的,我可以帮你解答这个问题。
首先,你需要安装alsa库。你可以在终端中输入以下命令:
```
sudo apt-get install libasound2-dev
```
接下来,你需要编写一个C程序来记录音频。以下是一个示例程序,可以按照你的要求生成日期文件夹,创建文件名,并且每10秒保存一个wav文件:
```c
#include <alsa/asoundlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#define SAMPLE_RATE 44100
#define SAMPLE_WIDTH 16
#define CHANNELS 2
#define PERIOD_SIZE 1024
#define BUFFER_SIZE (PERIOD_SIZE * 2 * SAMPLE_WIDTH / 8 * CHANNELS)
void create_directory(const char *path) {
struct stat st = {0};
if (stat(path, &st) == -1) {
mkdir(path, 0700);
}
}
char *get_time_string() {
char *time_str = malloc(sizeof(char) * 20);
time_t now = time(NULL);
strftime(time_str, 20, "%Y%m%d_%H%M%S", localtime(&now));
return time_str;
}
int main(int argc, char *argv[]) {
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
char *buffer = malloc(BUFFER_SIZE);
snd_pcm_hw_params_alloca(¶ms);
// Open the default PCM device
if (snd_pcm_open(&handle, "default", SND_PCM_STREAM_CAPTURE, 0) < 0) {
printf("Error opening PCM device!\n");
return -1;
}
// Set hardware parameters
if (snd_pcm_hw_params_any(handle, params) < 0) {
printf("Error setting hardware parameters!\n");
return -1;
}
if (snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
printf("Error setting access mode!\n");
return -1;
}
if (snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE) < 0) {
printf("Error setting sample format!\n");
return -1;
}
if (snd_pcm_hw_params_set_channels(handle, params, CHANNELS) < 0) {
printf("Error setting channel count!\n");
return -1;
}
unsigned int sample_rate = SAMPLE_RATE;
if (snd_pcm_hw_params_set_rate_near(handle, params, &sample_rate, 0) < 0) {
printf("Error setting sample rate!\n");
return -1;
}
if (snd_pcm_hw_params_set_period_size_near(handle, params, &frames, 0) < 0) {
printf("Error setting period size!\n");
return -1;
}
if (snd_pcm_hw_params(handle, params) < 0) {
printf("Error setting hardware parameters!\n");
return -1;
}
// Start capturing audio
create_directory("recordings");
char *date_str = get_time_string();
char *dir_path = malloc(sizeof(char) * (strlen("recordings/") + strlen(date_str) + 1));
strcpy(dir_path, "recordings/");
strcat(dir_path, date_str);
create_directory(dir_path);
int i = 0;
while (1) {
char *file_name = malloc(sizeof(char) * (strlen(dir_path) + 25));
sprintf(file_name, "%s/%s_%04d.wav", dir_path, date_str, i++);
FILE *file = fopen(file_name, "wb");
if (file == NULL) {
printf("Error creating file %s!\n", file_name);
return -1;
}
fwrite("RIFF", 1, 4, file);
unsigned int file_size = 0;
fwrite(&file_size, 1, 4, file);
fwrite("WAVE", 1, 4, file);
fwrite("fmt ", 1, 4, file);
unsigned int fmt_size = 16;
fwrite(&fmt_size, 1, 4, file);
unsigned short audio_format = 1;
fwrite(&audio_format, 1, 2, file);
unsigned short num_channels = CHANNELS;
fwrite(&num_channels, 1, 2, file);
unsigned int sample_rate = SAMPLE_RATE;
fwrite(&sample_rate, 1, 4, file);
unsigned int byte_rate = SAMPLE_RATE * CHANNELS * SAMPLE_WIDTH / 8;
fwrite(&byte_rate, 1, 4, file);
unsigned short block_align = CHANNELS * SAMPLE_WIDTH / 8;
fwrite(&block_align, 1, 2, file);
unsigned short bits_per_sample = SAMPLE_WIDTH;
fwrite(&bits_per_sample, 1, 2, file);
fwrite("data", 1, 4, file);
unsigned int data_size = 0;
fwrite(&data_size, 1, 4, file);
snd_pcm_prepare(handle);
int frames_read = 0;
while (frames_read < PERIOD_SIZE) {
int frames_to_read = PERIOD_SIZE - frames_read;
int frames_read_now = snd_pcm_readi(handle, buffer + frames_read * CHANNELS * SAMPLE_WIDTH / 8, frames_to_read);
if (frames_read_now < 0) {
printf("Error reading from PCM device!\n");
fclose(file);
return -1;
}
frames_read += frames_read_now;
}
fwrite(buffer, 1, BUFFER_SIZE, file);
data_size = frames_read * CHANNELS * SAMPLE_WIDTH / 8;
file_size = data_size + 36;
fseek(file, 4, SEEK_SET);
fwrite(&file_size, 1, 4, file);
fseek(file, 40, SEEK_SET);
fwrite(&data_size, 1, 4, file);
fclose(file);
usleep(10000000); // Wait 10 seconds
}
free(buffer);
snd_pcm_close(handle);
return 0;
}
```
这个程序使用了alsa库来捕获音频,并且以wav格式保存到指定的文件夹中。你需要根据你的需求修改程序中的路径和文件名格式。
你可以使用以下命令编译程序:
```
gcc -o record record.c -lasound
```
然后你可以运行程序:
```
./record
```
程序将会一直运行,每10秒保存一个wav文件。你可以使用通用播放器来播放这些文件。
希望这个回答对你有所帮助!
阅读全文