Reputation: 5432
I have a program that records 5sec of the audio values using the ALSA lib , here is the code :
#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;
}
I'm using the function snd_pcm_hw_params_set_rate
to set an exact value for fs
but I get this warning :
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);
I've check the documentation the parameter type should be correct, but more interesting is that after the running the program, I get another warring or error :
*** Error in `./out': malloc(): memory corruption (fast): 0x0000000002462d90 ***
Aborted (core dumped)
this don't show up when I use :
size = frames * 1; to
size = frames * 2;
an the result is just wrong, I tried to use a lower sample frequency but it didn't help.
when use : snd_pcm_hw_params_set_rate_near
the sample freuqency changes to 192000 and the result is than correct, I would really use the first function so I get to know what sample freuqcy I'm using.
any idea how I can do it, or why do I get those warrings ?
Upvotes: 1
Views: 1713
Reputation: 371
Regarding the warning, compiler is right, you're passing the address of dir
which is of type int *
.
Upvotes: 1