cpp source file: #include <stdio.h> #include <stdlib.h> #include <alsa/asoundlib.h> #define ALSA_PCM_NEW_HW_PARAMS_API #define debug 0 void printBuffer(unsigned char* buf,int size){ printf("print buffer(%d):/n",size); int charid=0; short *v1,*v2; for(int i=0;i<size;i+=4){ v1=(short*)(buf+i); v2=(short*)(buf+i+2); printf("%d/t%d/t%d/n",i,*v1,*v2); } printf("/n"); } int main (int argc, char *argv[]) { int i; int err; int capture_frame_number; int buffersize; unsigned char* buf;//[buffersize]; snd_pcm_t *capture_handle; snd_pcm_hw_params_t *hw_params; unsigned int rate=44100; unsigned int channel=2; unsigned int bits=16; unsigned int recordtime=5; // 5 second unsigned int framesize; char deviceName[]="default";//"default"; capture_frame_number=rate*recordtime; framesize=channel*bits/8; buffersize=capture_frame_number*framesize; buf=(unsigned char*)malloc(buffersize); memset(buf,0,buffersize); if ((err = snd_pcm_open (&capture_handle, deviceName, SND_PCM_STREAM_CAPTURE, 0)) < 0) { fprintf (stderr, "cannot open audio device %s (%s)/n", argv[1], snd_strerror (err)); exit (1); } if(debug) printf("m 1/n"); if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) { fprintf (stderr, "cannot allocate hardware parameter structure (%s)/n", snd_strerror (err)); exit (1); } if(debug) printf("m 2/n"); if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) { fprintf (stderr, "cannot initialize hardware parameter structure (%s)/n", snd_strerror (err)); exit (1); } if(debug) printf("m 3/n"); if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { fprintf (stderr, "cannot set access type (%s)/n", snd_strerror (err)); exit (1); } if(debug) printf("m 4/n"); if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) { fprintf (stderr, "cannot set sample format (%s)/n", snd_strerror (err)); exit (1); } if(debug) printf("m 5/n"); if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) { fprintf (stderr, "cannot set sample rate (%s)/n", snd_strerror (err)); exit (1); } if(debug) printf("m 6/n"); if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, channel)) < 0) { fprintf (stderr, "cannot set channel count (%s)/n", snd_strerror (err)); exit (1); } if(debug) printf("m 7/n"); if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) { fprintf (stderr, "cannot set parameters (%s)/n", snd_strerror (err)); exit (1); } if(debug) printf("m 8/n"); snd_pcm_hw_params_free (hw_params); if(debug) printf("m 9/n"); if ((err = snd_pcm_prepare (capture_handle)) < 0) { fprintf (stderr, "cannot prepare audio interface for use (%s)/n", snd_strerror (err)); exit (1); } if(debug) printf("m 10/n"); fprintf(stderr,"start recording/n"); //for (i = 0; i < 100; ++i) { // snd_pcm_sframes_t snd_pcm_readi (snd_pcm_t *pcm, // void *buffer, // snd_pcm_uframes_t size (one frame = 4 byte) // ) if ((err = snd_pcm_readi (capture_handle, buf, capture_frame_number)) != capture_frame_number) { fprintf (stderr, "read from audio interface failed (%s)/n", snd_strerror (err)); exit (1); } printBuffer(buf,buffersize); //if(debug) printf("m 10, i=%d/n",i); //} if(debug) printf("m 11/n"); snd_pcm_close (capture_handle); //printBuffer(buf,buffersize); exit (0); } makefile: all: gcc alsa_recorder.cpp -o alsa_recorder -lasound ./alsa_recorder > test.dat gnuplot gnuplot.sc gnuplot.sc: #!/usr/bin/env gnuplot set datafile commentschars ";" set terminal svg set output "test.svg" set multiplot layout 2,1 set ylabel "sample value" set bmargin 0 set format x "" ymax=32768 set yrange [-ymax:ymax] #set ytics -0.8,0.2 set key bottom plot "test.dat" using 1:2 every 50 with lines lc rgbcolor "#a0a0b0" title "left channel" set xlabel "time (s)" set bmargin set tmargin 0 set format x "%g" ymax=32768 set yrange [-ymax:ymax] #set ytics -1.0,0.2,0.8 set key top plot "test.dat" using 1:3 every 50 with lines lc rgbcolor "#a0a0b0" title "right channel" unset multiplot