diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2014-09-22 21:38:37 +0200 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2014-09-22 21:38:37 +0200 |
commit | 01bcfc5d692236bffeec5b8de2c27138e71d2bd6 (patch) | |
tree | caeef2a186acb074388484f78c4445353afe5ac1 | |
parent | a0bbc1d61bff38e5db76d3e11813551977411543 (diff) | |
download | toolame-dab-01bcfc5d692236bffeec5b8de2c27138e71d2bd6.tar.gz toolame-dab-01bcfc5d692236bffeec5b8de2c27138e71d2bd6.tar.bz2 toolame-dab-01bcfc5d692236bffeec5b8de2c27138e71d2bd6.zip |
Add -L option to print level
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | options.h | 1 | ||||
-rw-r--r-- | toolame.c | 49 | ||||
-rw-r--r-- | utils.c | 33 | ||||
-rw-r--r-- | utils.h | 20 | ||||
-rw-r--r-- | zmqoutput.c | 8 | ||||
-rw-r--r-- | zmqoutput.h | 2 |
7 files changed, 107 insertions, 8 deletions
@@ -32,6 +32,7 @@ HEADERS = \ subband.h \ tables.h \ toolame.h \ + utils.h \ xpad.h \ zmqoutput.h @@ -58,6 +59,7 @@ c_sources = \ ath.c \ encode_new.c \ zmqoutput.c \ + utils.c \ xpad.c OBJ = $(c_sources:.c=.o) @@ -17,6 +17,7 @@ typedef struct used for VBR in LAME */ int verbosity; /* 2 by default. 0 is no output at all */ int enable_jack; /* 1=use JACK input, 2=use wav input */ + int show_level; /* 1=show the sox-like audio level measurement */ } options; @@ -24,6 +24,7 @@ #include "encode_new.h" #include "toolame.h" #include "xpad.h" +#include "utils.h" #include <assert.h> @@ -128,6 +129,10 @@ int main (int argc, char **argv) int lg_frame; int i; + /* Keep track of peaks */ + int peak_left = 0; + int peak_right = 0; + char* mot_file = NULL; /* Used to keep the SNR values for the fast/quick psy models */ @@ -198,22 +203,44 @@ int main (int argc, char **argv) nch = frame.nch; error_protection = header.error_protection; - while (get_audio (&musicin, buffer, num_samples, nch, &header) > 0) { + unsigned long samps_read; + while ((samps_read = get_audio(&musicin, buffer, num_samples, nch, &header)) > 0) { + unsigned long j; + for (j = 0; j < samps_read; j++) { + peak_left = MAX(peak_left, buffer[0][j]); + } + for (j = 0; j < samps_read; j++) { + peak_right = MAX(peak_right, buffer[1][j]); + } + + // We can always set the zmq peaks, even if the output is not + // used, it just writes some variables + zmqoutput_set_peaks(peak_left, peak_right); + if (glopts.verbosity > 1) if (++frameNum % 10 == 0) { + + fprintf(stderr, "[%4u", frameNum); + if (mot_file) { - fprintf (stderr, "[%4u %s ]\r", - frameNum, - xpad_len > 0 ? "p" : " " - ); + fprintf(stderr, " %s", + xpad_len > 0 ? "p" : " "); + } + + if (glopts.show_level) { + fprintf(stderr, " (%6d|%-6d) ", + peak_left, peak_right); + + fprintf(stderr, "] [%6s|%-6s]\r", + level(0, &peak_left), + level(1, &peak_right) ); } else { - fprintf (stderr, "[%4u]\r", - frameNum); + fprintf(stderr, "]\r"); } } - fflush (stderr); + fflush(stderr); win_buf[0] = &buffer[0][0]; win_buf[1] = &buffer[1][0]; @@ -633,6 +660,7 @@ void usage (void) fprintf (stdout, "\t-x force byte-swapping of input\n"); fprintf (stdout, "\t-g swap channels of input file\n"); fprintf (stdout, "\t-j enable jack input\n"); + fprintf (stdout, "\t-L enable audio level display\n"); fprintf (stdout, "Output\n"); fprintf (stdout, "\t-m mode channel mode : s/d/j/m (dflt %4c)\n", DFLT_MOD); @@ -700,6 +728,7 @@ void short_usage (void) * syntax: * * -j turns on JACK input + * -L turns on audio level display * -m is followed by the mode * -y is followed by the psychoacoustic model number * -s is followed by the sampling rate @@ -836,6 +865,10 @@ void parse_args (int argc, char **argv, frame_info * frame, int *psy, argUsed = 1; break; + case 'L': + glopts.show_level = 1; + break; + case 's': argUsed = 1; srate = atof (arg); @@ -0,0 +1,33 @@ +#include "utils.h" +#include <unistd.h> +#include <stdint.h> +#include <math.h> + +/* Taken from sox */ +const char* level(int channel, int* peak) +{ + static char const * const text[][2] = { + /* White: 2dB steps */ + {"", ""}, {"-", "-"}, {"=", "="}, {"-=", "=-"}, + {"==", "=="}, {"-==", "==-"}, {"===", "==="}, {"-===", "===-"}, + {"====", "===="}, {"-====", "====-"}, {"=====", "====="}, + {"-=====", "=====-"}, {"======", "======"}, + /* Red: 1dB steps */ + {"!=====", "=====!"}, + }; + int const red = 1, white = NUMOF(text) - red; + + double linear = (double)(*peak) / INT16_MAX; + + int vu_dB = linear ? floor(2 * white + red + linear_to_dB(linear)) : 0; + + int index = vu_dB < 2 * white ? + MAX(vu_dB / 2, 0) : + MIN(vu_dB - white, red + white - 1); + + *peak = 0; + + return text[index][channel]; + +} + @@ -0,0 +1,20 @@ +#ifndef _UTILS_H_ +#define _UTILS_H_ + +#include <math.h> +#include <stdint.h> + +#define MIN(a,b) (((a)<(b))?(a):(b)) +#define MAX(a,b) (((a)>(b))?(a):(b)) + +#define NUMOF(l) (sizeof(l) / sizeof(*l)) + +#define linear_to_dB(x) (log10(x) * 20) + +/* Calculate the little string containing a bargraph + * 'VU-meter' from the peak value measured + */ +const char* level(int channel, int* peak); + +#endif + diff --git a/zmqoutput.c b/zmqoutput.c index 6d9db68..75a3543 100644 --- a/zmqoutput.c +++ b/zmqoutput.c @@ -13,6 +13,14 @@ unsigned char* zmqbuf; // buffer size) size_t zmqbuf_len; +static int zmq_peak_left = 0; +static int zmq_peak_right = 0; + +void zmqoutput_set_peaks(int left, int right) +{ + zmq_peak_left = left; + zmq_peak_right = right; +} int zmqoutput_open(Bit_stream_struc *bs, char* uri) { diff --git a/zmqoutput.h b/zmqoutput.h index a4eb556..36b8e9b 100644 --- a/zmqoutput.h +++ b/zmqoutput.h @@ -28,5 +28,7 @@ int zmqoutput_write_byte(Bit_stream_struc *bs, unsigned char data); void zmqoutput_close(Bit_stream_struc *bs); +void zmqoutput_set_peaks(int left, int right); + #endif |