简体   繁体   English

C中ALSA中的采样频率

[英]Sample frequency in ALSA in C

I have a program that records 5sec of the audio values using the ALSA lib , here is the code : 我有一个使用ALSA lib记录5sec音频值的程序,这是代码:

#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;
}

I'm using the function snd_pcm_hw_params_set_rate to set an exact value for fs but I get this warning : 我正在使用函数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);

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 : 我已经检查了文档 ,参数类型应该正确,但是更有趣的是,在运行程序之后,我又遇到了warring或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. 当使用时: snd_pcm_hw_params_set_rate_near样本频率更改为192000,结果是正确的,我真的会使用第一个函数,这样我就知道我使用的样本频率是多少。

any idea how I can do it, or why do I get those warrings ? 知道我该怎么做,或者为什么我会产生这些冲突?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM