#!/bin/bash
# Read player's current speed and output the wanted speed
# pause +- value will not work.
# No music + or - 1 octave is always no music.

# references: https://fr.wikipedia.org/wiki/Gamme_temp%C3%A9r%C3%A9e
#	The A 429.62 Hz is based on universal phasical constants and on the mass of the water molecule H2O;
#	See Henry,M.; Bridoux,S. Symmetry, Music and Water, Preprints 2024, 2024011231
#		https://www.preprints.org/manuscript/202401.1231/v1

speed="$3"

speedcalc() {
	if [[ "`locale decimal_point`" == , ]] ; then
		speed=$(echo ${speed/,/.})
	fi
	speed=$(echo $speed*$1|bc -l)
	if [[ "`locale decimal_point`" == , ]] ; then
		if [[ "$2" == "alsaplayer" ]] ; then
		speed=$(echo ${speed/./,})
		else
		speed=$(echo ${speed/,/.})
		fi
	fi
	echo $speed
}

# Change the default speed to another/default A tuning,
# ie: 440, 432, 429.62
#     speedref <frequency of the A note> <alsaplayer|mplayer>
# Note than normal restore the A 440ħz = speed 1;
#	the other value are cummulatives.
speedref() {
	SpeedRef=$(echo $1/440|bc -l)
	speedcalc $SpeedRef $2
}

speedcase() {
case "$1" in
	max)
		if [[ "$2" == "alsaplayer" ]] ; then
		echo 10
		else
		echo 100
		fi
		;;
	normal)
		echo 1
		;;
	432)
		speedref 432 "$2"
		;;
	429.62)
		speedref 429.62 "$2"
		;;
	pause)
		echo 0
		;;
	back)
		echo -1
		;;
	min)
		if [[ "$2" == "alsaplayer" ]] ; then
		echo -10
		else
		speedcalc 0.01 "$2"
		fi
		;;
	+5octave)
		speedcalc 32 "$2"
		;;
	+4octave)
		speedcalc 16 "$2"
		;;
	+3octave)
		speedcalc 8 "$2"
		;;
	+2octave)
		speedcalc 4 "$2"
		;;
	+1octave)
		speedcalc 2 "$2"
		;;
	+1septieme)
		speedcalc 1.88774862536 "$2"
		;;
	+1sixte)
		speedcalc 1.68179283051 "$2"
		;;
	+1quinte)
		speedcalc 1.49830707688 "$2"
		;;
	+1quarte)
		speedcalc 1.33483985417 "$2"
		;;
	+1tierce)
		speedcalc 1.25992104989 "$2"
		;;
	+1ton)
		speedcalc 1.12246204831 "$2"
		;;
	+1/2ton)
		speedcalc 1.05946309436 "$2"
		;;
	+1comma)
		speedcalc 531441/524288 "$2"
		;;
	-1comma)
		speedcalc 524288/531441 "$2"
		;;
	-1/2ton)
		speedcalc 0.943874312682 "$2"
		;;
	-1ton)
		speedcalc 0.89089871814 "$2"
		;;
	-1tierce)
		speedcalc 0.793700525984 "$2"
		;;
	-1quarte)
		speedcalc 0.749153538438 "$2"
		;;
	-1quinte)
		speedcalc 0.667419927085 "$2"
		;;
	-1sixte)
		speedcalc 0.594603557501 "$2"
		;;
	-1septieme)
		speedcalc 0.52973154718 "$2"
		;;
	-1octave)
		speedcalc 0.5 "$2"
		;;
	-2octave)
		speedcalc 0.25 "$2"
		;;
	-3octave)
		speedcalc 0.125 "$2"
		;;
	-4octave)
		speedcalc 0.0625 "$2"
		;;
	-5octave)
		speedcalc 0.03125 "$2"
		;;
	-6octave)
		speedcalc 0.015625 "$2"
		;;
	*)
		echo 1
		;;
esac
}

speedcase $1 $2
