gpt4 book ai didi

c - 用 ALSA 录音

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:32:24 30 4
gpt4 key购买 nike

我正在尝试“录制”一个音频文件,我读过这个 doc ,实际上我想在文件中将“采样”值记录为双倍值,这是我使用的代码(不起作用,我不知道为什么没有):

/* Use the newer ALSA API */
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>
#include <stdio.h>

int main() {
long loops;
int rc;
int size,z = 0 ;
unsigned int val;
double* buffer;
int dir;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;

FILE* output = NULL;
output =fopen("recod_values.txt","w");

/* Open PCM device for recording (capture). */
rc = snd_pcm_open(&handle, "default",
SND_PCM_STREAM_CAPTURE, 0);
if (rc < 0) {
fprintf(stderr,
"unable to open pcm device: %s\n",
snd_strerror(rc));
exit(1);
}

/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(&params);

/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);

/* Set the desired hardware parameters. */

/* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED);

/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle, params,
SND_PCM_FORMAT_S16_LE);

/* MONO ! channel */
snd_pcm_hw_params_set_channels(handle, params,1);

/* 96000 bits/second sampling rate */
val = 96000;
snd_pcm_hw_params_set_rate_near(handle, params,
&val, &dir);
;
/* Set period size to 32 frames. */
frames = 32;
snd_pcm_hw_params_set_period_size_near(handle,
params, &frames, &dir);

/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
fprintf(stderr,
"unable to set hw parameters: %s\n",
snd_strerror(rc));
exit(1);
}

/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params,
&frames, &dir);
size = frames * 1; /* 2 bytes/sample, 2 channels */
buffer = (double*) malloc(size);

/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params,
&val, &dir);
loops = 5000000/ val;

while (loops > 0) {
loops--;
rc = snd_pcm_readi(handle, buffer, frames);

for ( z = 0 ; z < size;z++){
fprintf(output,"%lf \n",buffer[z]);

}

}

snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
fclose(output);

return 0;
}

得到的结果是假的,这是我得到的一部分:

    22164315735966424535139159791393084768500567327664428456919459225160782460290374318673978007739264159481013271070331047993933279328468540357215794465042587451392.000000 
6279123284769190191779385445935961906015983426866033011058871857355906568111563202057623448515972440403603734829703874073506103294822799230919004382628287132004967906916958208.000000
0.000000
0.000000
0.000000
1144284986495925317233642104161717490326555645567187694497268067386154959544116448945812917900397813140052794333237528352248562790473524666519326385472977850060639877322124157668710305854399264313107197566174852391468506111174989083416952045568.000000
486016490646530877454846463864566567777058339173509861936688900655372986604461971881776121791820561656186713737600239967251495507590158168644511518013315486066797390056338302864276615681456563025411108154944185463301467774199971222422456597209993418331127808.000000
97502070478605056015384266450931746345849383201940068533539715554348065640767209105810488184602958812662140616258325471782047626172052091225732327584237457213932719329242675004694018348986553910427648.000000
0.000000
0.000000
0.000000
0.000000
0.000000

知道如何使它看起来更好吗?提前致谢 !

更新使用Audacity导入输出文件后,结果或声音与录制的声音完全无关!是我做错了吗?我的意思是这部分代码:

loops = 5000000/ val;

while (loops > 0) {
loops--;
rc = snd_pcm_readi(handle, buffer, frames);

for ( z = 0 ; z < size;z++){
fprintf(output,"%lf \n",buffer[z]);

}

}

最佳答案

snd_pcm_readi 以您使用 snd_pcm_hw_params_set_format 设置的格式写入示例,因此您必须确保 buffer 的类型匹配这。double 的等效项是 SND_PCM_FORMAT_FLOAT64

此外,malloc 需要一个以 bytes 为单位的大小,因此您必须使用

size = frames * sizeof(double);

打印样本时,您的字节数多于样本数,因此 size 是错误的计数器。返回的样本数在rc中:

for (z = 0; z < rc; z++)
fprintf(output, "%lf\n", buffer[z]);

关于c - 用 ALSA 录音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24856244/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com