gpt4 book ai didi

c - C 中 ALSA 中的采样频率

转载 作者:太空宇宙 更新时间:2023-11-04 03:45:26 25 4
gpt4 key购买 nike

我有一个使用 ALSA 库记录 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(&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);

/* 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_ratefs 设置一个精确值,但我收到此警告:

     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,结果是正确的,我真的会使用第一个函数,这样我就知道我正在使用什么样本频率。

知道我该怎么做吗,或者为什么我会遇到那些争吵?

最佳答案

关于警告,编译器是正确的,您正在传递类型为 int *dir 的地址。

关于c - C 中 ALSA 中的采样频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24923477/

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