diff -Naur hwebcam031/audio.c hwebcam032/audio.c --- hwebcam031/audio.c 2012-03-03 16:43:33.000000000 +0100 +++ hwebcam032/audio.c 2012-03-10 14:50:26.000000000 +0100 @@ -269,6 +269,7 @@ fprintf(stderr, MSGPREFIX"Error: calloc() failed\n"); return -1; } + ad->peak_percent = 0.0f; return 0; } @@ -322,6 +323,25 @@ return 0; } +void audio_find_peak(struct audio_data *ad) +{ + int s; + int min = INT16_MAX + 1; + int max = INT16_MIN + 1; + int16_t v; + + for (s = 0; s < ad->frames * ad->channels; s++) { + v = ad->alsabuffer[s]; + if (v < min) min = v; + if (v > max) max = v; + } + + if (abs(min) > max) + max = abs(min); + + ad->peak_percent = 100.0f * (float)max / (float)INT16_MAX; +} + int audio_close(struct audio_data *ad) { int err; diff -Naur hwebcam031/audio.h hwebcam032/audio.h --- hwebcam031/audio.h 2012-03-01 15:53:50.000000000 +0100 +++ hwebcam032/audio.h 2012-03-10 14:50:22.000000000 +0100 @@ -22,6 +22,7 @@ #include #include +#include struct audio_data { char *device; @@ -55,6 +56,7 @@ snd_pcm_uframes_t alsabuffersize; snd_pcm_sframes_t frames; snd_pcm_sframes_t avail; + float peak_percent; int verbose; }; @@ -65,6 +67,7 @@ int audio_stop(struct audio_data *ad); void audio_poll_descriptors_revents(struct audio_data *ad); int audio_update_avail_and_read(struct audio_data *ad); +void audio_find_peak(struct audio_data *ad); int audio_close(struct audio_data *ad); #endif diff -Naur hwebcam031/hwebcam.c hwebcam032/hwebcam.c --- hwebcam031/hwebcam.c 2012-03-08 14:44:16.000000000 +0100 +++ hwebcam032/hwebcam.c 2012-03-10 15:41:07.000000000 +0100 @@ -280,32 +280,30 @@ void usage(void) { + fprintf(stderr, +"usage: hwebcam [options]\n" +" -h print help\n" +" -v increment verbosity level\n" +"Video options:\n" +" -d videodev use video device videodev (/dev/video0)\n" +" -s WxH choose frame size (640x400)\n" +" -i fps choose frame rate (10)\n" +" -t val save a frame every val seconds (disabled, 0=every frame)\n" +" -e encode captured stream in ogg/theora/vorbis\n" +" -o ofile save captured stream in ofile (capture.ogg, -=stdout)\n" +" -b bitrate choose theora bitrate in bit/s (200000)\n" +" -q quality choose theora quality (0, range=[0-63])\n" #ifdef USE_SDL - fprintf(stderr, "usage: hwebcam [-h -v -d -s -i -t -e -o -b -w -q -a -D -C -r -B -Q]\n"); -#else - fprintf(stderr, "usage: hwebcam [-h -v -d -s -i -t -e -o -b -q -a -D -C -r -B -Q]\n"); -#endif - fprintf(stderr, " -h print help\n"); - fprintf(stderr, " -v increment verbosity level\n"); - fprintf(stderr, "Video options:\n"); - fprintf(stderr, " -d videodev use video device videodev (/dev/video0)\n"); - fprintf(stderr, " -s WxH choose frame size (640x400)\n"); - fprintf(stderr, " -i fps choose frame rate (10)\n"); - fprintf(stderr, " -t val save a frame every val seconds (disabled, 0=every frame)\n"); - fprintf(stderr, " -e encode captured stream in ogg/theora/vorbis\n"); - fprintf(stderr, " -o ofile save captured stream in ofile (capture.ogg, -=stdout)\n"); - fprintf(stderr, " -b bitrate choose theora bitrate in bit/s (200000)\n"); - fprintf(stderr, " -q quality choose theora quality (0, accepted values [0-63])\n"); -#ifdef USE_SDL - fprintf(stderr, " -w display stream in a sdl window\n"); +" -w display stream in a sdl window\n" #endif - fprintf(stderr, "Audio options:\n"); - fprintf(stderr, " -a include audio in captured stream\n"); - fprintf(stderr, " -D audiodev use audio device audiodev (hw:0,0)\n"); - fprintf(stderr, " -c channels choose number of channels (1)\n"); - fprintf(stderr, " -r rate choose sampling rate (32000)\n"); - fprintf(stderr, " -B bitrate choose vorbis bitrate in bit/s (48000)\n"); - fprintf(stderr, " -Q quality choose vorbis quality (0.1, accepted values [-0.1 - 1.0])\n"); +"Audio options:\n" +" -a include audio in captured stream\n" +" -D audiodev use audio device audiodev (hw:0,0)\n" +" -c channels choose number of channels (1)\n" +" -r rate choose sampling rate (32000)\n" +" -B bitrate choose vorbis bitrate in bit/s (48000)\n" +" -Q quality choose vorbis quality (0.1, range=[-0.1 - 1.0])\n" + ); exit(1); } @@ -333,8 +331,8 @@ /* audio data */ struct audio_data ad; char *audiodevice = "hw:0,0"; - unsigned int rate = 32000; - unsigned int channels = 1; + unsigned int rate = 32000, channels = 1, peak_count = 0; + float peak_percent = 0.0f; /* image data */ struct ppm_st ppmimage; @@ -844,6 +842,16 @@ /* encoding */ gettimeofday(&tea0, NULL); + + if (peak_count >= 100) { + peak_count = 0; + peak_percent = 0.0f; + } + audio_find_peak(&ad); + if (peak_percent < ad.peak_percent) + peak_percent = ad.peak_percent; + peak_count++; + vorbisbuffer = vorbis_analysis_buffer(&vds, ad.frames); for(c = 0; c < ad.channels; c++) { for (s = 0; s < ad.frames; s++) { @@ -919,14 +927,6 @@ frame_interval = (frame_stop.tv_sec - frame_start.tv_sec) * 1000000L + frame_stop.tv_usec - frame_start.tv_usec; actual_fps = (frame_interval == 0) ? fps : (1000000.0 / (double)frame_interval); -#if !DEBUG - /* print stats in verbose mode every frame if actual_fps<=10, or every 2 frames */ - if (verbose && ((int)(actual_fps+0.5)<=10 || !(i%2))) { - fprintf(stderr, "\rframe: %5d tf: %7ld us fps: %5.2f tev: %7ld us tea: %5ld us", - i, frame_interval, actual_fps, t_encv, t_enca); - fflush(stdout); - } -#endif frame_start = vd.buffer.timestamp; #ifdef USE_SDL @@ -1025,6 +1025,15 @@ } } +#if !DEBUG + /* print stats in verbose mode every frame if actual_fps<=10, or every 2 frames */ + if (verbose && ((int)(actual_fps+0.5)<=10 || !(i%2))) { + fprintf(stderr, "\rf: %5d tf: %6ldus fps: %5.2f tev: %6ldus tea: %5ldus ap: %5.1f%%", + i, frame_interval, actual_fps, t_encv, t_enca, peak_percent); + fflush(stdout); + } +#endif + /* ok, we're done with this frame */ ret = video_frame_done(&vd); if (ret == -1)