c - Alsa library configuration -
i using alsa library find maximum value of sound samples stereo output.i using s32_le pcm format. python code , can able acheive max values instantaneously. c alsa lib ,instant values not able get. please me solve . have attached python script c code reference.
python code:
#!/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 code:
#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 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 , fill 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 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); // (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 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; }
regards raji
alsa code working fine . can able getsound variation
here have attached working alsa lib c code .
#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 use (%s)\n", snd_strerror (err)); exit (1); } while(1){ if ((err = snd_pcm_readi (capture_handle, buffer, buffer_frames)) != buffer_frames) { printf ("read 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); }
Comments
Post a Comment