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(¶ms);
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;
}
よろしく ラジ