Alsa库配置

时间:2017-04-07 05:20:28

标签: c python-2.7 libalsa

我正在使用Alsa库从立体声输出中找到声音样本的最大值。我正在使用S32_LE pcm格式。从我的python代码,我可以瞬间实现最大值。但是从C Alsa Lib,即时值无法获得。请帮我解决这个问题。我附上了我的python脚本以及c代码供您参考。

Python代码:

#!/usr/bin/env python
import alsaaudio, time, audioop, math
card = 'sysdefault:CARD=Default'

inp=alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NORMAL,card)
inp.setchannels(1)
inp.setrate(64000)
inp.setformat(alsaaudio.PCM_FORMAT_S32_LE)
inp.setperiodsize(1024)
val=0
loop=0
myarr=[]
count=math.pow(2,24)
while True:
           l,data=inp.read()
           if l:
                amplitude=audioop.max(data,4)
                val=int(amplitude)
                 val=val>>8
                if val > 0:
                   db=(20* math.log(val/count))+120

C代码:

 #include <alsa/asoundlib.h>
#include <stdio.h>
#include <math.h>
#define PCM_DEVICE "default"
int max=0,min=0;
    int rate, channels, seconds;
    int buff_size, loops;
    unsigned int pcm, tmp, dir;
int main(int argc, char **argv) {
        int FS = (int)(pow(2,24));
    snd_pcm_t *pcm_handle;
        int32_t value,result,i;
    snd_pcm_hw_params_t *params;
    snd_pcm_uframes_t frames;
    int32_t *buff;


    if (argc < 4) {
        printf("Usage: %s <sample_rate> <channels> <seconds>\n",
                                argv[0]);
        return -1;
    }

    rate     = atoi(argv[1]);
    channels = atoi(argv[2]);
    seconds  = atoi(argv[3]);

    /* Open the PCM device in playback mode */
    if (pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE,
                    SND_PCM_STREAM_CAPTURE, 0) < 0) 
        printf("ERROR: Can't open \"%s\" PCM device. %s\n",
                    PCM_DEVICE, snd_strerror(pcm));

    /* Allocate parameters object and fill it with default values*/
    snd_pcm_hw_params_alloca(&params);

    snd_pcm_hw_params_any(pcm_handle, params);

    /* Set parameters */
    if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params,
                    SND_PCM_ACCESS_RW_INTERLEAVED) < 0) 
        printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));

    if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params,
                        SND_PCM_FORMAT_S32_LE) < 0) 
        printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));

    if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0) 
        printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));

    if (pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0) 
        printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));

    /* Write parameters */
    if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0)
        printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));

    /* Resume information */
    printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));

    printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));

    snd_pcm_hw_params_get_channels(params, &tmp);
    printf("channels: %i ", tmp);

    if (tmp == 1)
        printf("(mono)\n");
    else if (tmp == 2)
        printf("(stereo)\n");

    snd_pcm_hw_params_get_rate(params, &tmp, 0);
    printf("rate: %d bps\n", tmp);

    printf("seconds: %d\n", seconds);   

    /* Allocate buffer to hold single period */
    snd_pcm_hw_params_get_period_size(params, &frames, 0);

    buff_size = frames*4*channels /* 2 -> sample size */;
    buff =  malloc(buff_size);
        printf("Buff Size %d\n ",frames);
    snd_pcm_hw_params_get_period_time(params, &tmp, NULL);

    printf("seconds: %d\n", tmp);   
//  for (loops =1000000/ tmp; loops < (seconds * 1000000)/tmp; loops++){

while(1) {

        if (pcm = snd_pcm_readi(pcm_handle, buff, frames) == -EPIPE) {
            printf("XRUN.\n");
            snd_pcm_prepare(pcm_handle);
        } else if (pcm < 0) {
            printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
        }

            for(i=0;i<sizeof(buff);i=i+2){
             value= buff[i];
             value=value>>8;
   //          if(max<value)
     //         max=value;
       //   if(min > value)
         //         min=value;
          if(value < 0 ) value*=-1;
          if (result<value) result=value;
         }
         // if(max<0)
           //      max=max* -1;

          //double mymax=log((double)((double)max/(double)FS));
         // int mymax1=20 * log(max/FS);
  //        double mymax=(double)20*(log((value/(double)FS)));
         // printf("MAx %ld",max);
          printf("%ld \n ",result);
        //  double mymax = (double)20.000 * (log((double)((double)max/(double)FS)));
          //printf("%ld \n ",mymax);
          result=0;
          value=0; 
          memset(buff,0,sizeof(buff)); 
//    }
        //snd_pcm_drain(pcm_handle);
    snd_pcm_close(pcm_handle);

    }
    return 0;
}

此致 的Raji

1 个答案:

答案 0 :(得分:0)

Alsa代码工作正常。我能够变异

这里我附上了我的工作Alsa lib c代码。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <alsa/asoundlib.h>
#define PCM_DEVICE "default"    
double db; 
int main()
{
  int i,var,min=0,max=0;
  int err;
  int32_t buffer[1024];
  int32_t value;int loop=0;
  int buffer_frames =128;
  int FS    = (int)(pow(2,24));
  unsigned int rate = 64000;
  snd_pcm_t *capture_handle;
  snd_pcm_hw_params_t *hw_params;
  snd_pcm_format_t format = SND_PCM_FORMAT_S32_LE;

  if ((err = snd_pcm_open (&capture_handle,PCM_DEVICE, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
    printf ("cannot open audio device %s (%s)\n", 
             PCM_DEVICE,
             snd_strerror (err));
    exit (1);
  }


  if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
    printf ("cannot allocate hardware parameter structure (%s)\n",
             snd_strerror (err));
    exit (1);
  }


  if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
    printf ("cannot initialize hardware parameter structure (%s)\n",
             snd_strerror (err));
    exit (1);
  }


  if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
    printf ("cannot set access type (%s)\n",
             snd_strerror (err));
    exit (1);
  }


  if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, format)) < 0) {
   printf ("cannot set sample format (%s)\n",
             snd_strerror (err));
    exit (1);
  }


  if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) {
    printf ("cannot set sample rate (%s)\n",
             snd_strerror (err));
    exit (1);
  }


  if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
    printf ("cannot set channel count (%s)\n",
             snd_strerror (err));
    exit (1);
  }


  if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
    printf ("cannot set parameters (%s)\n",
             snd_strerror (err));
    exit (1);
  }


  snd_pcm_hw_params_free (hw_params);


  if ((err = snd_pcm_prepare (capture_handle)) < 0) {
    printf ("cannot prepare audio interface for use (%s)\n",
             snd_strerror (err));
    exit (1);
  }


 while(1){

  if ((err = snd_pcm_readi (capture_handle, buffer, buffer_frames)) != buffer_frames) {
      printf ("read from audio interface failed (%s)\n",
               err, snd_strerror (err));
      break;

    }

    for(var=0;var<128;var=var+2){
     value=buffer[var];
      value= abs(value>>8);
      if(max<value)
           max=value;
      if(min > value)
          min=value;

    }

          db = ((double)20.000 * log((double)((double)max/(double)FS)))+120;
      printf("%lf\n",db);   
          memset(buffer,0,sizeof(buffer));
          max=0;min=0; value=0;
          sleep(0.5);
}


  snd_pcm_close (capture_handle);
  exit (0);
}