我正在尝试使用alsa(asoundlib库)录制音频并将其写入.pcm文件。
这是我的代码,基于https://gist.github.com/albanpeignier/104902和Audio recording using ALSA in PCM format
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
int main(int argc, char *argv[])
{
int i;
int err;
char *buffer; //?
int buffer_frames = 128; //?
unsigned int rate = 44100; // Sample rate of 44,1 kHz
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
//Signed 16 bit Little Endian
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
if ((err = snd_pcm_open(&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf(stderr, "cannot open audio device %s (%s)\n", argv[1], snd_strerror(err));
exit (1);
}
fprintf(stdout, "audio interface opened\n");
if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
fprintf(stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror(err));
exit (1);
}
fprintf(stdout, "hw_params allocated\n");
//First, we initialize the hwparams structure with the full configuration space of the soundcard.
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);
}
fprintf(stdout, "hw_params initialized\n");
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);
}
fprintf(stdout, "hw_params access setted\n");
// Set sample format of Signed 16 bit Little Endian
if ((err = snd_pcm_hw_params_set_format(capture_handle, hw_params, format)) < 0) {
fprintf(stderr, "cannot set sample format (%s)\n", snd_strerror(err));
exit (1);
}
fprintf(stdout, "hw_params format setted\n");
// Set sample rate of 44,1 kHz
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);
}
fprintf(stdout, "hw_params rate setted\n");
if ((err = snd_pcm_hw_params_set_channels(capture_handle, hw_params, 2)) < 0) {
fprintf(stderr, "cannot set channel count (%s)\n", snd_strerror(err));
exit (1);
}
fprintf(stdout, "hw_params channels setted\n");
if ((err = snd_pcm_hw_params(capture_handle, hw_params)) < 0) {
fprintf(stderr, "cannot set parameters (%s)\n", snd_strerror(err));
exit (1);
}
fprintf(stdout, "hw_params setted\n");
snd_pcm_hw_params_free(hw_params);
fprintf(stdout, "hw_params freed\n");
if ((err = snd_pcm_prepare(capture_handle)) < 0) {
fprintf(stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror(err));
exit(1);
}
fprintf(stdout, "audio interface prepared\n");
buffer = malloc(128 * snd_pcm_format_width(format) / 8 * 2);
fprintf(stdout, "buffer allocated %d\n", 128 * snd_pcm_format_width(format) / 8 * 2);
//open pcm file to write data to
int fd = open("MicIn.pcm", O_CREAT | O_RDWR, 0666);
i = 0;
//for (i = 0; i < 10; ++i)
while (++i) {
if ((err = snd_pcm_readi(capture_handle, buffer, buffer_frames)) != buffer_frames) {
fprintf(stderr, "%d: read from audio interface failed (%s)\n", err, snd_strerror(err));
exit(1);
}
write(fd, buffer, 128 * snd_pcm_format_width(format) / 8 * 2);
fprintf(stdout, "read %d done\n", i);
}
close(fd);
free(buffer);
fprintf(stdout, "buffer freed\n");
snd_pcm_close(capture_handle);
fprintf(stdout, "audio interface closed\n");
exit (0);
}
我尝试了以下方法:
ffplay -autoexit -f f32le -ac 1 -ar 44100 MicIn.pcm
使我的音质令人恐怖,但有些白噪声,但至少我能听到录音。我想知道为什么这行得通,因为f32le不是我在代码中使用的格式ffplay -autoexit -f s16le -ac 1 -ar 44100 MicIn.pcm
即使正确的格式也不会发出声音吗?有人可以帮助我吗?为什么我的质量不好,为什么只能用格式错误的f32le读取?