我试图"记录"一个音频文件,我已经阅读了这个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(¶ms);
/* 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]);
}
}
答案 0 :(得分:0)
snd_pcm_readi
以您使用snd_pcm_hw_params_set_format
设置的格式写入示例,因此您必须确保buffer
的类型与此匹配。
等同于double
的是SND_PCM_FORMAT_FLOAT64
。
此外,malloc
想要字节中的大小,因此您必须使用
size = frames * sizeof(double);
打印样本时,字节数多于样本数,因此size
将是错误的计数器。
返回的样本数量为rc
:
for (z = 0; z < rc; z++)
fprintf(output, "%lf\n", buffer[z]);