if ((err = snd_pcm_hw_params(capture_handle, hw_params)) < 0) { printf("Error setting parameters: %s\n", snd_strerror(err)); return 1; } snd_pcm_hw_params_free(hw_params); unsigned int frames_per_period = 2048; snd_pcm_uframes_t period_size = frames_per_period * CHANNELS * 2; snd_pcm_uframes_t buffer_size = period_size * 2; if ((err = snd_pcm_set_params(capture_handle, FORMAT, SND_PCM_ACCESS_RW_INTERLEAVED, CHANNELS, rate, 1, frames_per_period)) < 0) { printf("Error setting parameters: %s\n", snd_strerror(err)); return 1; } FILE *file = fopen(argv[1], "wb"); if (!file) { printf("Error opening file for writing!\n"); return 1; } uint32_t chunk_size = 0; uint32_t subchunk_size = 16; uint16_t audio_format = 1; uint16_t num_channels = CHANNELS; uint32_t sample_rate = rate; uint32_t byte_rate = (rate * CHANNELS * 2); uint16_t block_align = (CHANNELS * 2); uint16_t bits_per_sample = 16; uint32_t data_size = 0; fwrite("RIFF", 1, 4, file); fwrite(&chunk_size, 4, 1, file); fwrite("WAVE", 1, 4, file); fwrite("fmt ", 1, 4, file); fwrite(&subchunk_size, 4, 1, file); fwrite(&audio_format, 2, 1, file); fwrite(&num_channels, 2, 1, file); fwrite(&sample_rate, 4, 1, file); fwrite(&byte_rate, 4, 1, file); fwrite(&block_align, 2, 1, file); fwrite(&bits_per_sample, 2, 1, file); fwrite("data", 1, 4, file); fwrite(&data_size, 4, 1, file); uint16_t buffer[frames_per_period * CHANNELS]; while (1) { int n = snd_pcm_readi(capture_handle, buffer, frames_per_period); if (n < 0) { printf("Error reading from PCM device: %s\n", snd_strerror(n)); break; } fwrite(buffer, 2, n * CHANNELS, file); data_size += n * CHANNELS * 2; } uint32_t file_size = data_size + 36; fseek(file, 4, SEEK_SET); fwrite(&file_size, 4, 1, file); fseek(file, 40, SEEK_SET); fwrite(&data_size, 4, 1, file); fclose(file); snd_pcm_close(capture_handle); 加上注释
时间: 2024-04-28 16:24:34 浏览: 136
/*
这段代码是一个录音程序,使用 ALSA 库进行音频捕获,将捕获到的音频写入到一个 WAV 文件中。
在程序中,首先进行 ALSA 库的初始化,然后设置音频捕获参数,包括采样率、声道数、采样大小等。
然后打开一个 WAV 文件,写入 WAV 文件头部信息,开始捕获音频并将捕获到的音频数据写入到 WAV 文件中。
最后,关闭 WAV 文件和 ALSA 库。
*/
// 引入 ALSA 库的头文件
#include <alsa/asoundlib.h>
int main(int argc, char **argv) {
// 定义 ALSA 相关的变量
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
int err;
// 打开默认音频捕获设备并进行初始化
if ((err = snd_pcm_open(&capture_handle, "default", SND_PCM_STREAM_CAPTURE, 0)) < 0) {
printf("Error opening PCM device: %s\n", snd_strerror(err));
return 1;
}
// 申请 ALSA 配置参数结构体,进行初始化
if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
printf("Error allocating hardware parameter structure: %s\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_any(capture_handle, hw_params)) < 0) {
printf("Error initializing hardware parameter structure: %s\n", snd_strerror(err));
return 1;
}
// 设置音频捕获参数
unsigned int rate = 44100;
int CHANNELS = 2;
if ((err = snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
printf("Error setting access type: %s\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_set_format(capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
printf("Error setting sample format: %s\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_set_channels(capture_handle, hw_params, CHANNELS)) < 0) {
printf("Error setting channel count: %s\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &rate, 0)) < 0) {
printf("Error setting sample rate: %s\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_set_period_size_near(capture_handle, hw_params, &frames_per_period, 0)) < 0) {
printf("Error setting period size: %s\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_set_buffer_size_near(capture_handle, hw_params, &buffer_size)) < 0) {
printf("Error setting buffer size: %s\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params(capture_handle, hw_params)) < 0) {
printf("Error setting parameters: %s\n", snd_strerror(err));
return 1;
}
// 释放 ALSA 配置参数结构体
snd_pcm_hw_params_free(hw_params);
// 计算每个周期的帧数和缓冲区大小
unsigned int frames_per_period = 2048;
snd_pcm_uframes_t period_size = frames_per_period * CHANNELS * 2;
snd_pcm_uframes_t buffer_size = period_size * 2;
// 设置音频捕获参数
if ((err = snd_pcm_set_params(capture_handle, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, CHANNELS, rate, 1, frames_per_period)) < 0) {
printf("Error setting parameters: %s\n", snd_strerror(err));
return 1;
}
// 打开要写入的 WAV 文件
FILE *file = fopen(argv[1], "wb");
if (!file) {
printf("Error opening file for writing!\n");
return 1;
}
// 写入 WAV 文件头部信息
uint32_t chunk_size = 0;
uint32_t subchunk_size = 16;
uint16_t audio_format = 1;
uint16_t num_channels = CHANNELS;
uint32_t sample_rate = rate;
uint32_t byte_rate = (rate * CHANNELS * 2);
uint16_t block_align = (CHANNELS * 2);
uint16_t bits_per_sample = 16;
uint32_t data_size = 0;
fwrite("RIFF", 1, 4, file);
fwrite(&chunk_size, 4, 1, file);
fwrite("WAVE", 1, 4, file);
fwrite("fmt ", 1, 4, file);
fwrite(&subchunk_size, 4, 1, file);
fwrite(&audio_format, 2, 1, file);
fwrite(&num_channels, 2, 1, file);
fwrite(&sample_rate, 4, 1, file);
fwrite(&byte_rate, 4, 1, file);
fwrite(&block_align, 2, 1, file);
fwrite(&bits_per_sample, 2, 1, file);
fwrite("data", 1, 4, file);
fwrite(&data_size, 4, 1, file);
// 读取音频数据并写入 WAV 文件
uint16_t buffer[frames_per_period * CHANNELS];
while (1) {
int n = snd_pcm_readi(capture_handle, buffer, frames_per_period);
if (n < 0) {
printf("Error reading from PCM device: %s\n", snd_strerror(n));
break;
}
fwrite(buffer, 2, n * CHANNELS, file);
data_size += n * CHANNELS * 2;
}
// 计算文件大小并更新 WAV 文件头部信息
uint32_t file_size = data_size + 36;
fseek(file, 4, SEEK_SET);
fwrite(&file_size, 4, 1, file);
fseek(file, 40, SEEK_SET);
fwrite(&data_size, 4, 1, file);
// 关闭 WAV 文件和 ALSA 库
fclose(file);
snd_pcm_close(capture_handle);
return 0;
}
阅读全文