sox-devel@lists.sourceforge.net unofficial mirror
 help / color / mirror / code / Atom feed
* (no subject)
@ 2014-10-16 16:08 Kyle Swanson
  0 siblings, 0 replies; only message in thread
From: Kyle Swanson @ 2014-10-16 16:08 UTC (permalink / raw)
  To: sox-devel


[-- Attachment #1.1: Type: text/plain, Size: 457 bytes --]

Hi Chris,

I'd like my patch for JSON-formatted 'stat' and 'stats' output to be
considered for the next version of SoX. Currently the output of these
effects is formatted to be human readable, and if you're wanting to use any
of the outputted data in a script, you have to spend some time grep-ing and
parsing, and dealing with error checking, etc. This patch adds a '-json'
flag for each effect. Let me know if you have any thoughts!

Thanks!
Kyle Swanson

[-- Attachment #1.2: Type: text/html, Size: 573 bytes --]

[-- Attachment #2: 0001-Added-JSON-formatted-output-for-the-stat-and-stats-e.patch --]
[-- Type: application/octet-stream, Size: 9249 bytes --]

From 0ae6824753f8a954f705b2bb3e8f91ea689479a5 Mon Sep 17 00:00:00 2001
From: kylophone <kylophone@gmail.com>
Date: Thu, 16 Oct 2014 10:52:45 -0500
Subject: [PATCH] Added JSON-formatted output for the 'stat' and 'stats'
 effects.

---
 src/stat.c  | 40 +++++++++++++++++++++++++++----
 src/stats.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 113 insertions(+), 5 deletions(-)

diff --git a/src/stat.c b/src/stat.c
index cdea27c..c81c2d9 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -34,6 +34,7 @@ typedef struct {
   float *re_out;
   unsigned long fft_size;
   unsigned long fft_offset;
+  int json; //KYLO
 } priv_t;
 
 
@@ -48,6 +49,7 @@ static int sox_stat_getopts(sox_effect_t * effp, int argc, char **argv)
   stat->volume = 0;
   stat->srms = 0;
   stat->fft = 0;
+  stat->json = 0;
 
   --argc, ++argv;
   for (; argc > 0; argc--, argv++) {
@@ -69,6 +71,8 @@ static int sox_stat_getopts(sox_effect_t * effp, int argc, char **argv)
       stat->fft = 1;
     else if (!(strcmp(*argv, "-d")))
       stat->volume = 2;
+    else if (!(strcmp(*argv, "-json"))) //KYLO
+      stat->json = 1;
     else {
       lsx_fail("Summary effect: unknown option");
       return SOX_EOF;
@@ -256,6 +260,34 @@ static int sox_stat_stop(sox_effect_t * effp)
   amp = -stat->min;
   if (amp < stat->max)
     amp = stat->max;
+  
+  /* print out the info in JSON */ //KYLO
+  if (stat->json) {
+    fprintf(stderr, "{\n");
+    fprintf(stderr, "\t\"samplesRead\" : \"%d\",\n", stat->read);
+    fprintf(stderr, "\t\"lengthInSeconds\" : \"%f\",\n", (double)stat->read/effp->in_signal.rate/effp->in_signal.channels);
+    if (stat->srms)
+      fprintf(stderr, "\t\"scaledByRMS\" : \"%f\",\n", rms);
+    else
+      fprintf(stderr, "\t\"scaledBy\" : \"%f\",\n", scale);
+    fprintf(stderr, "\t\"maximumAmplitude\" : \"%f\",\n", stat->max);
+    fprintf(stderr, "\t\"minimumAmplitude\" : \"%f\",\n", stat->min);
+    fprintf(stderr, "\t\"midlineAmplitude\" : \"%f\",\n", stat->mid);
+    fprintf(stderr, "\t\"meanNorm\" : \"%f\",\n", stat->asum/ct);
+    fprintf(stderr, "\t\"meanAmplitude\" : \"%f\",\n", stat->sum1/ct);
+    fprintf(stderr, "\t\"RMSAmplitude\" : \"%f\",\n", sqrt(stat->sum2/ct));
+    fprintf(stderr, "\t\"maximumDelta\" : \"%f\",\n", stat->dmax);
+    fprintf(stderr, "\t\"minimumDelta\" : \"%f\",\n", stat->dmin);
+    fprintf(stderr, "\t\"meanDelta\" : \"%f\",\n", stat->dsum1/(ct-1));
+    fprintf(stderr, "\t\"RMSDelta\" : \"%f\",\n", sqrt(stat->dsum2/(ct-1)));
+    freq = sqrt(stat->dsum2/stat->sum2)*effp->in_signal.rate/(M_PI*2);
+    fprintf(stderr, "\t\"roughFrequency\" : \"%d\",\n", (int)freq);
+    if (amp>0)
+      fprintf(stderr, "\t\"volumeAdjustment\" : \"%f\"\n", SOX_SAMPLE_MAX/(amp*scale));
+    fprintf(stderr, "}\n");
+    return SOX_SUCCESS; 
+  } // END KYLO
+
 
   /* Just print the volume adjustment */
   if (stat->volume == 1 && amp > 0) {
@@ -296,16 +328,16 @@ static int sox_stat_stop(sox_effect_t * effp)
 
     if (x >= 3.0) {             /* use opposite encoding */
       if (effp->in_encoding->encoding == SOX_ENCODING_UNSIGNED)
-        fprintf(stderr,"\nTry: -t raw -e signed-integer -b 8 \n");
+        fprintf(stderr,"\nTry: -t raw -s -1 \n");
       else
-        fprintf(stderr,"\nTry: -t raw -e unsigned-integer -b 8 \n");
+        fprintf(stderr,"\nTry: -t raw -u -1 \n");
     } else if (x <= 1.0 / 3.0)
       ;                         /* correctly decoded */
     else if (x >= 0.5 && x <= 2.0) { /* use ULAW */
       if (effp->in_encoding->encoding == SOX_ENCODING_ULAW)
-        fprintf(stderr,"\nTry: -t raw -e unsigned-integer -b 8 \n");
+        fprintf(stderr,"\nTry: -t raw -u -1 \n");
       else
-        fprintf(stderr,"\nTry: -t raw -e mu-law -b 8 \n");
+        fprintf(stderr,"\nTry: -t raw -U -1 \n");
     } else
       fprintf(stderr, "\nCan't guess the type\n");
   }
diff --git a/src/stats.c b/src/stats.c
index e1d1809..cf45716 100644
--- a/src/stats.c
+++ b/src/stats.c
@@ -20,7 +20,7 @@
 #include <string.h>
 
 typedef struct {
-  int       scale_bits, hex_bits;
+  int       scale_bits, hex_bits, json; //KYLO. Added json.
   double    time_constant, scale;
 
   double    last, sigma_x, sigma_x2, avg_sigma_x2, min_sigma_x2, max_sigma_x2;
@@ -31,18 +31,42 @@ typedef struct {
 
 static int getopts(sox_effect_t * effp, int argc, char **argv)
 {
+  
   priv_t * p = (priv_t *)effp->priv;
+
+  int i; //KYLO. This checks for a -json flag. Removes it from argv, because '-json' doesn't play nice with the GETOPT_NUMERIC function. 
+  int minusJsonCounter = 0;
+  char** argvMinusJson[argc - 1];
+  for (i = 1; i < (argc); i++) {
+    minusJsonCounter++;
+      if (strcmp("-json", argv[i]) == 0) {
+         minusJsonCounter--;
+         p->json = 1;
+         continue;
+      }
+      argvMinusJson[minusJsonCounter] = argv[i];
+  } 
+  if (p->json) {
+    argc = argc - 1;
+    argv = argvMinusJson;
+  } //KYLO OUT
+
   int c;
   lsx_getopt_t optstate;
+
+
   lsx_getopt_init(argc, argv, "+x:b:w:s:", NULL, lsx_getopt_flag_none, 1, &optstate);
 
   p->time_constant = .05;
   p->scale = 1;
+  
   while ((c = lsx_getopt(&optstate)) != -1) switch (c) {
+
     GETOPT_NUMERIC(optstate, 'x', hex_bits      ,  2 , 32)
     GETOPT_NUMERIC(optstate, 'b', scale_bits    ,  2 , 32)
     GETOPT_NUMERIC(optstate, 'w', time_constant ,  .01 , 10)
     GETOPT_NUMERIC(optstate, 's', scale         ,  -99, 99)
+
     default: lsx_fail("invalid option `-%c'", optstate.opt); return lsx_usage(effp);
   }
   if (p->hex_bits)
@@ -193,6 +217,58 @@ static int stop(sox_effect_t * effp)
       return SOX_SUCCESS;
     }
 
+    if (p->json) { //KYLO IN. JSON OUT HERE.
+      if (n == 0) n = 1;
+      
+      fprintf(stderr, "{\n");
+      fprintf(stderr, "\"channelCount\" : \"%d\",\n", n);
+      fprintf(stderr, "\"overall\" : {\n"); // Overall Stats Here
+        fprintf(stderr, "\t\"dcOffset\" : \"%f\",\n", max_sigma_x / p->num_samples);
+        fprintf(stderr, "\t\"minLevel\" : \"%f\",\n", min);
+        fprintf(stderr, "\t\"maxLevel\" : \"%f\",\n", max);
+        fprintf(stderr, "\t\"peakLeveldB\" : \"%f\",\n", linear_to_dB(max(-min, max)));
+        fprintf(stderr, "\t\"RMSLeveldB\" : \"%f\",\n", linear_to_dB(sqrt(sigma_x2 / num_samples)));
+        fprintf(stderr, "\t\"RMSPeakdB\" : \"%f\",\n", linear_to_dB(sqrt(max_sigma_x2)));
+        fprintf(stderr, "\t\"RMSTrdB\" : ");
+          if (min_sigma_x2 != 1) fprintf(stderr, "\"%f\",\n", linear_to_dB(sqrt(min_sigma_x2)));
+          else fprintf(stderr, "\"-\",\n");
+        if (effp->flows > 1) fprintf(stderr, "\t\"crestFactor\" : \"-\",\n");
+        else fprintf(stderr, "\t\"crestFactor\" : \"%f\",\n", sigma_x2 ? avg_peak / sqrt(sigma_x2 / num_samples) : 1);
+        fprintf(stderr, "\t\"flatFactor\" : \"%f\",\n", linear_to_dB((min_runs + max_runs) / (min_count + max_count)));
+        fprintf(stderr, "\t\"pkCount\" : \"%d\",\n", atoi(lsx_sigfigs3((min_count + max_count) / effp->flows)));
+        b1 = bit_depth(mask, min, max, &b2);
+        fprintf(stderr, "\t\"bitDepth\" : \"%2u/%-2u\",\n", b1, b2);
+        fprintf(stderr, "\t\"numSamples\" : \"%d\",\n", atoi(lsx_sigfigs3((double)p->num_samples)));
+        fprintf(stderr, "\t\"lengthSeconds\" : \"%f\",\n", p->num_samples / effp->in_signal.rate);
+        fprintf(stderr, "\t\"scaleMax\" : 1.0,\n");
+        fprintf(stderr, "\t\"windowSeconds\" : \"%f\"\n", p->time_constant);
+      fprintf(stderr, "\t},\n");
+      
+        for (i = 0; i < n; i++) { //Channel Stats Here
+          fprintf(stderr, "\"ch%d\" : {\n", i + 1);
+            priv_t * q = (priv_t *)(effp - effp->flow + i)->priv;
+            fprintf(stderr, "\t\"dcOffset\" : \"%f\",\n", q->sigma_x / q->num_samples);
+            fprintf(stderr, "\t\"minLevel\" : \"%f\",\n", q->min);
+            fprintf(stderr, "\t\"maxLevel\" : \"%f\",\n", q->max);
+            fprintf(stderr, "\t\"peakLeveldB\" : \"%f\",\n", linear_to_dB(max(-q->min, q->max)));
+            fprintf(stderr, "\t\"RMSLeveldB\" : \"%f\",\n", linear_to_dB(sqrt(q->sigma_x2 / q->num_samples)));
+            fprintf(stderr, "\t\"RMSPeakdB\" : \"%f\",\n", linear_to_dB(sqrt(q->max_sigma_x2)));
+            fprintf(stderr, "\t\"RMSTrdB\" : ");
+              if (q->min_sigma_x2 != 1) fprintf(stderr, "\"%f\",\n", linear_to_dB(sqrt(q->min_sigma_x2)));
+              else fprintf(stderr, "\"-\",\n");
+            fprintf(stderr, "\t\"crestFactor\" : \"%f\",\n", q->sigma_x2? max(-q->min, q->max) / sqrt(q->sigma_x2 / q->num_samples) : 1);
+            fprintf(stderr, "\t\"flatFactor\" : \"%f\",\n", linear_to_dB((q->min_runs + q->max_runs) / (q->min_count + q->max_count)));
+            fprintf(stderr, "\t\"pkCount\" : \"%d\",\n", atoi(lsx_sigfigs3((double)(q->min_count + q->max_count))));
+            b1 = bit_depth(q->mask, q->min, q->max, &b2);
+            fprintf(stderr, "\t\"bitDepth\" : \"%2u/%-2u\"\n", b1, b2);
+          fprintf(stderr, "\t}");
+          if (i != (n - 1)) fprintf(stderr, ",");
+          fprintf(stderr, "\n");
+        }
+      fprintf(stderr, "}\n");
+      return SOX_SUCCESS;
+    } // KYLO OUT
+
     if (n == 2)
       fprintf(stderr, "             Overall     Left      Right\n");
     else if (n) {
-- 
1.9.3 (Apple Git-50)


[-- Attachment #3: Type: text/plain, Size: 306 bytes --]

------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://p.sf.net/sfu/Zoho

[-- Attachment #4: Type: text/plain, Size: 158 bytes --]

_______________________________________________
SoX-devel mailing list
SoX-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sox-devel

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2014-10-16 16:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-16 16:08 (no subject) Kyle Swanson

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/sox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).