Commit c572c641 authored by Ronald van Engelen's avatar Ronald van Engelen

percentage without bc and query mpd without mpc

parent 2b186f2d
......@@ -127,12 +127,27 @@ function bytes_to_si() {
printf "%s" "${si_size}"
}
function percentage() {
part="$1"
whole="$2"
echo $(( 200 * part / whole % 2 + 100 * part / whole ))
}
function query_mpd() {
mpd_command="$1"
exec 3<>/dev/tcp/${mpd_host}/${mpd_port}
printf "command_list_begin\n%s\nclose\ncommand_list_end\n" \
"${mpd_command}" >&3
cat <&3
exec 3<&-
exec 3<&-
}
function run_func() {
## run an internal function whose name is specified in $1 with
## args on the local host or remote through ssh.
## args on a remote host through ssh.
## use declare -f to first print the function in the remote
## bash session, and then execute it.
## bash session and then execute it.
func="$1"
shift
# shellcheck disable=SC2124
......@@ -533,7 +548,6 @@ function ret_formatted_time() {
function ret_mpd_now() {
## echo status command to mpd host on mpd port using bash builtin
## socket, to fill mpc_vals array with rt_ fields
prefix="rt_raw"
key_val_re="^([^\:]+):[[:space:]]+(.*)"
## store each line in an associative array, by splitting the mpc
## output lines (paramater:value)
......@@ -541,37 +555,45 @@ function ret_mpd_now() {
if [[ "${line}" =~ ${key_val_re} ]]; then
field="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"
mpd_vals["${prefix}_${field}"]="${value}"
mpd_vals["rt_raw_${field}"]="${value}"
fi
done< <(
exec 3<>/dev/tcp/${mpd_host}/${mpd_port}
printf "command_list_begin\nstatus\nclose\ncommand_list_end\n" >&3
cat <&3
exec 3<&-
exec 3<&-
)
done< <(query_mpd "status")
debug "$(declare -p mpd_vals)"
## store the double colon separated values
## (samplerate:bitdepth:channelcount, eg. 48000:24:2) of the
## audiofield in separate variables and an array
IFS=: read -r audio_samplerate audio_bitdepth audio_channelcount <<< "${mpd_vals[${prefix}_audio]}"
IFS=":" read -r audio_samplerate audio_bitdepth audio_channelcount \
<<< "${mpd_vals[rt_raw_audio]}"
mpd_vals["rt_audio_samplerate"]="${audio_samplerate}"
mpd_vals["rt_audio_bitdepth"]="${audio_bitdepth}"
mpd_vals["rt_audio_channelcount"]="${audio_channelcount}"
mpd_vals["rt_audio_bitrate"]="${mpd_vals[${prefix}_bitrate]}"
mpd_vals["rt_audio_bitrate"]="${mpd_vals[rt_raw_bitrate]}"
## store the (elapsed) playing time value (seconds:frames,
## eg. 20:123) in separate variables and an array
IFS=: read -r mpdval_time_elapsed_seconds mpdval_time_duration_seconds <<< "${mpd_vals[${prefix}_time]}"
mpd_vals["rt_mpdval_time_elapsed_seconds"]="${mpdval_time_elapsed_seconds}"
mpd_vals["rt_mpdval_time_duration_seconds"]="${mpdval_time_duration_seconds}"
IFS=":" read -r mpdval_time_elapsed_seconds mpdval_time_duration_seconds \
<<< "${mpd_vals[rt_raw_time]}"
if (( mpdval_time_elapsed_seconds > 0 )); then
mpd_vals["rt_time_elapsed_formatted"]=$(
ret_formatted_time "${mpdval_time_elapsed_seconds}"
)
else
mpd_vals["rt_time_elapsed_formatted"]
fi
if (( mpdval_time_duration_seconds > 0 )); then
mpd_vals["rt_time_duration_formatted"]=$(
ret_formatted_time "${mpdval_time_duration_seconds}"
)
mpd_vals["rt_percentage_played"]=$(( 200 * mpdval_time_elapsed_seconds / mpdval_time_duration_seconds % 2 + 100 * mpdval_time_elapsed_seconds / mpdval_time_duration_seconds ))
else
mpd_vals["rt_time_duration_formatted"]=0
fi
if (( mpdval_time_duration_seconds > 0 )) \
&& (( mpdval_time_elapsed_seconds > 0 )); then
mpd_vals["rt_percentage_played"]=$(
percentage "${mpdval_time_elapsed_seconds}" "${mpdval_time_duration_seconds}"
)
else
mpd_vals["rt_percentage_played"]=0
fi
debug "$(declare -p mpd_vals)"
}
......@@ -770,16 +792,16 @@ function get_info() {
## check presence of needed commands
if check_commands; then
debug "all needed commands are installed."
debug "all needed commands are installed."
else
display_error "not all needed commands are installed."
display_error "not all needed commands are installed."
fi
## parse command line arguments
if analyze_command_line "$@"; then
debug "analyze_command_line \"$*\" passed."
else
display_error "analyze_command_line returned an error."
display_error "analyze_command_line returned an error."
return 1
fi
if [[ ! -d "${mpd_music_dir}" ]]; then
......@@ -821,7 +843,7 @@ function get_info() {
[[ $? -ne 0 ]] && return 1
input_bitdepth="${soxi_vals[Precision]//-bit}"
input_bitrate_bit_s="$(( input_bitdepth * soxi_vals[SampleRate] * soxi_vals[Channels] ))"
input_bitrate_kbit_s=$(${cmd_bc} <<< "scale=0; ${input_bitrate_bit_s} / 1000")
input_bitrate_kbit_s="$(( input_bitrate_bit_s / 1000))"
#declare -p alsa_vals 1>&2
fi
# shellcheck disable=SC2206
......@@ -835,6 +857,7 @@ function get_info() {
mpd_bitdepth="${mpd_vals[rt_audio_bitdepth]}"
mpd_samplerate="${mpd_vals[rt_audio_samplerate]}"
mpd_bitrate="${mpd_vals[rt_audio_bitrate]}"
mpd_channelcount="${mpd_vals[rt_audio_channelcount]}"
output_bitrate="$(( output_samplerate * output_bitdepth * 2 / 1000 ))"
## determine if playback is bit perfect
color_bitdepth_file_mpd="${red}"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment