aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.c
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2017-02-12 19:02:45 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2017-02-12 19:02:45 +0100
commitb396a7eff34173fd4a9e48d8e4cfa5bab7fa603f (patch)
tree34e1d78c8c358cf329aa6c049e5ca02bcf87d82f /src/utils.c
downloadODR-SourceCompanion-b396a7eff34173fd4a9e48d8e4cfa5bab7fa603f.tar.gz
ODR-SourceCompanion-b396a7eff34173fd4a9e48d8e4cfa5bab7fa603f.tar.bz2
ODR-SourceCompanion-b396a7eff34173fd4a9e48d8e4cfa5bab7fa603f.zip
Add initial copy-pasted code
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..24da427
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,40 @@
+#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);
+
+ return text[index][channel];
+}
+
+size_t strlen_utf8(const char *s) {
+ size_t result = 0;
+
+ // ignore continuation bytes - only count single/leading bytes
+ while (*s)
+ if ((*s++ & 0xC0) != 0x80)
+ result++;
+
+ return result;
+}