我有一个程序使用ALSA lib记录5秒的音频值,这里是代码:
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>
#include <stdio.h>
int main() {
long loops;
int rc;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir,z=0;
snd_pcm_uframes_t frames;
signed short *buffer;
FILE* inp = NULL;
FILE* inp2 =NULL;
inp = fopen("values","wb+");
inp2 = fopen("Values2","w+");
int fd = open("v",O_WRONLY);
/* 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);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params,1);
/* Sample frequency */
val = 96000;
//val2 = val;
snd_pcm_hw_params_set_rate(handle, params,
val, &dir);
printf(" %d \n", val);
/* 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, 1 channels */
buffer = (signed short*) 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);
fwrite(buffer,sizeof(signed short),size,inp);
for(z =0; z<size;z++)
fprintf(inp2,"%lf\n",buffer[z]/1.0);
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
printf(" buffer");
free(buffer);
fclose(inp);
fclose(inp2);
close(fd);
return 0;
}
我使用函数snd_pcm_hw_params_set_rate
为fs
设置了确切的值,但我收到了此警告:
warning: passing argument 4 of ‘snd_pcm_hw_params_set_rate’ makes integer from pointer without a cast [enabled by default]
val, &dir);
^
In file included from /usr/include/alsa/asoundlib.h:54:0,
from capture.c:4:
/usr/include/alsa/pcm.h:743:5: note: expected ‘int’ but argument is of type ‘int *’
int snd_pcm_hw_params_set_rate(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);
我检查documentation参数类型应该是正确的,但更有趣的是,在运行程序后,我得到另一个交战或错误:
*** Error in `./out': malloc(): memory corruption (fast): 0x0000000002462d90 ***
中止(核心倾销)
当我使用时,这不会出现: size = frames * 1; to
size = frames * 2;
结果是错误的,我试图使用较低的采样频率,但它没有帮助。
当使用时:snd_pcm_hw_params_set_rate_near
样本频率变为192000并且结果不正确,我会真正使用第一个函数,这样我就可以知道我使用的样本频率。
任何想法我怎么能做到,或者为什么我得到那些warrings?
答案 0 :(得分:1)
关于警告,编译器是正确的,您传递的dir
地址为int *
。