2017-04-07 53 views
0

Alsaライブラリを使用して、ステレオ出力からサウンドサンプルの最大値を検索しています.i S32_LE pcmフォーマットを使用しています。私のpythonコードから、私は瞬時に最大値を達成することができます。しかし、C Alsa Libから、瞬時値は得られません。これを解決するために私を助けてください。私はあなたの参照のための私のpythonスクリプトだけでなく、Cコードを添付しています。Alsaライブラリ設定

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

よろしく ラジ

答えて

0

のAlsaコードが正常に動作しています。私は変えられることができます

ここで私は私の働くAlsaのlibのコードを付けました。

#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); 
} 
関連する問題