我试图“记录”一个音频文件,我读过这个文档,实际上我想将“抽样”值记录为一个文件中的双倍,这是我使用的代码(它不工作,我没有提示为什么它不能):
/* 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;
}get的结果是错误的,以下是我得到的结果的一部分:
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 知道怎么让这看起来更好吗?提前谢谢!
更新后,导入输出文件与奥迪,结果或声音绝对没有任何事情与录制的声音!我这样做不对吗?我是说代码的这一部分:
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]);
}
}发布于 2014-07-21 07:40:38
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]);https://stackoverflow.com/questions/24856244
复制相似问题