shithub: rc

Download patch

ref: be6e868f376305c3d0c6c900717b0c8fd6adc115
parent: 4aaae2668c25a41d64b688007422ce892bf0301a
author: qwx <qwx@sciops.net>
date: Wed Aug 6 07:03:29 EDT 2025

tune: refactor, use bc instead of awk for slightly higher precision

audio-stretch had fp precision issues

--- a/bin/tune
+++ b/bin/tune
@@ -1,11 +1,10 @@
 #!/bin/rc
 fn usage{
-	echo usage: $0 '[-p NSEMITONE]' '[-t PERCENT]'
+	echo usage: $0 '[-p ±NSEMITONE]' '[-t PERCENT]'
 	exit usage
 }
-
-p=0
-t=100
+t=()
+p=()
 while(~ $1 -?){
 	switch($1){
 	case -p
@@ -21,35 +20,17 @@
 }
 if(~ $#p 0 && ~ $#t 0)
 	usage
-eval `{awk -v p'='^$p -v t'='^$t '
-BEGIN{
-	c = ""
-	if(p != 0){
-		if(p < 0)
-			p = 1.0 / (1.0594631 ^ -p)
-		else if(p > 0){
-			p = 1.0594631 ^ p
-			if(p > 4.0)
-				p = 4.0
-		}
-		c = "audio/stretch -s -r " p
-		if(t != 100)
-			c = c " | "
-	}
-	if(t != 100){
-		if(t < 0)
-			t = 1
-		else if(t <= 100){
-			t = 1.0 + (1 - t / 100)
-			if(t > 4.0)
-				t = 4.0
-		}else{
-			t = 100 / t
-			if(t < 0.25)
-				t = 0.25
-		}
-		c = c "audio/stretch -r " t
-	}
-	print c
+c=()
+if(! ~ $#p 0){
+	# handle x and -x; constant is 2^(1/12) but bc can only do integral
+	# exponents
+	r=`{echo '1 / (1.0594630943592953098431053149397484958171844482421875 ^ -(0+'^$p^'))' | bc -l}
+	c=($c audio/stretch -s -r $r)
 }
-'}
+if(! ~ $#t 0){
+	t=`{echo '1 + (1 - '^$t^' / 100)' | bc -l}
+	if(! ~ $#c 0)
+		c=($c '|')
+	c=($c audio/stretch -r $t)
+}
+eval $c
--