From 8ce08fa2289bac1cfae441628e18c8399dcab228 Mon Sep 17 00:00:00 2001 From: Breizh Date: Wed, 10 Aug 2022 18:37:33 +0200 Subject: [PATCH] Changement du script i3blocks/mpd --- .config/i3blocks/config | 2 +- .i3blocks/mpd | 226 +++++++++++++++++++++------------------ .i3blocks/toggle-sink | 5 +- .local/bin/music-convert | 33 ++++-- 4 files changed, 149 insertions(+), 117 deletions(-) diff --git a/.config/i3blocks/config b/.config/i3blocks/config index ab0a3cb..1b30b38 100644 --- a/.config/i3blocks/config +++ b/.config/i3blocks/config @@ -41,7 +41,7 @@ interval=once # Supported players are: spotify, vlc, audacious, xmms2, mplayer, and others. [mpd] #instance=spotify -interval=1 +interval=persist signal=13 #markup=none diff --git a/.i3blocks/mpd b/.i3blocks/mpd index 69da39d..f45a43d 100755 --- a/.i3blocks/mpd +++ b/.i3blocks/mpd @@ -1,27 +1,24 @@ #!/bin/bash -runtime="${XDG_RUNTIME_DIR}/i3blocks" -[[ ! -d "$runtime" ]] && mkdir -p "$runtime" +# Fonction de réinitialisation en cas de perte de la connexion +self-reset() { + echo "[--:-- --:-- --:--]" + sleep 5 + exec "$0" +} -if ! systemctl --user is-active mpd &>/dev/null -then - origstatus='off' -else - origstatus="$(mpc status | sed -n '2p' | grep -Eo '^\[.*\]' | tr -d '[]')" - [[ -z "$origstatus" ]] && origstatus='stop' -fi +trap self-reset PIPE -LONG=40 -SHORT=20 +# Initialisation de la connexion au serveur +coproc nc -U /home/breizh/.mpd/socket +IN=${COPROC[1]} +OUT=${COPROC[0]} +#echo "password xxx" >&${IN} +#sed '/^OK$/q' <&$OUT &>/dev/null -case $BLOCK_BUTTON in - 1) mpc -q prev ;; - 2) mpc -q toggle ;; - 3) mpc -q next ;; - #4) mpc -q volume +2 ;; - #5) mpc -q volume -2 ;; - #?*) notify-send "Test" "$BLOCK_BUTTON" ;; -esac +# Initialisation des valeurs par défaut et des fonctions +declare -i SCROLL=50 I=0 +declare -- artist song oldfile human_time() { local seconds="$1" @@ -43,100 +40,123 @@ human_time() { return } +# Initialisation de l’état +echo "status" >&$IN +while read -t 1 -u $OUT output +do + [[ "$output" == "OK" ]] && break + o[${output%%:*}]="${output##*: }" +done + +# Boucle principale +while [[ -n "$COPROC_PID" ]] +do + # Gestion du clic, fait office de délai si aucune action (une seconde). + # En cas de clic, l’actualisation de l’affichage sera instantanné. + read -t 1 BLOCK_BUTTON -status="" -case $origstatus in - stop ) status="" ;; - paused ) status="" ;; - playing ) status="" ;; - off ) status="" ;; -esac - -if [[ "$origstatus" != "stop" ]] && [[ "$origstatus" != "off" ]] -then - if [[ ! -f $runtime/mpd ]] - then - mpc -f '[[%artist%§|%name%§]%title%|[%file%]' current > ${runtime}/mpd - fi - - song=$(<${runtime}/mpd) + case $BLOCK_BUTTON in + 1|3) [[ "${o["state"]}" == stop ]] \ + && echo "play" >&${IN} \ + && sed '/^OK$/q' <&$OUT &>/dev/null ;;& + 2) [[ "${o["state"]}" == stop ]] \ + && echo "play" >&${IN} \ + || echo "pause" >&${IN} ;;& + 1) echo "previous" >&${IN} ;;& + 3) echo "next" >&${IN} ;;& + 1|2|3) sed '/^OK$/q' <&$OUT &>/dev/null ;; + esac - if [[ "${#song}" -gt $LONG ]] - then - scrolling=long - elif [[ "${#song}" -gt $SHORT ]] - then - scrolling=short - else - scrolling=none - fi - - if [[ "$scrolling" != "none" ]] - then - scroll="$(<${runtime}/mpd-scroll)" + # Réinitialisation de l’état + unset o + declare -A o - if [[ -z "${scroll// /}" ]] - then - printf "%-${LONG}s" "$(<${runtime}/mpd)" > ${runtime}/mpd-scroll - song="$(<${runtime}/mpd-scroll)" - else - song="$(<${runtime}/mpd-scroll)" - printf "%-${LONG}s" "${song:1}" > ${runtime}/mpd-scroll - fi - fi - - time="$(echo -e 'status\nclose' | nc -U $MPD_HOST | grep time \ - | cut -d' ' -f2)" + # Récupération de l’état + echo "status" >&$IN + while read -t 1 -u $OUT output + do + [[ "$output" == "OK" ]] && break + o[${output%%:*}]="${output##*: }" + done - time_elapsed=$(human_time "${time%:*}") - time_total=$(human_time "${time#*:}") - time_left=$(human_time "$(( ${time#*:} - ${time%:*} ))") + # Récupération des informations du morceau en cours + echo "currentsong" >&$IN + while read -t 1 -u $OUT output + do + [[ "$output" == "OK" ]] && break + o[${output%%:*}]="${output##*: }" + done - longtime="[$time_elapsed $time_left ${time_total/00:00/--:--}]" - shorttime="$time_elapsed $time_left" - - case $scrolling in - long) - longsong="${song}" - shortsong="${song}" - ;; - short) - longsong="$(<${runtime}/mpd)" - shortsong="${song}" - ;; - none) - longsong="$(<${runtime}/mpd)" - shortsong="$longsong" - ;; + # Choix des couleurs selon l’état + case "${o["state"]}" in + play ) status="" ;; + pause ) status="" ;; + stop ) status="" ;; esac - longsong="${longsong:0:$LONG}" - shortsong="${shortsong:0:$SHORT}" - longsong="${longsong//&/&}" - shortsong="${shortsong//&/&}" - if [[ "$longsong" =~ § ]] + # En cas de changement de morceau, + # on réinitialise l’affichage défilant + if [[ "${o["file"]}" != "$oldfile" ]] then - longsong="${longsong/§/ }" - elif [[ "$song" =~ § ]] - then - longsong="${longsong}" - fi - if [[ "$shortsong" =~ § ]] - then - shortsong="${shortsong/§/ }" - elif [[ "$song" =~ § ]] - then - shortsong="${shortsong}" + unset bloc1 bloc2 bloc3 bloc4 + artist="${o["Artist"]:-${o["Name"]}}" + song="${o["Title"]}" + [[ -z "$artist$song" ]] && song="${o["file"]}" + + if [[ "$(( ${#song} + ${#artist} + 1 ))" -gt "$SCROLL" ]] + then + scroll=true + song=" $song " + else + scroll=false + song=" $song" + fi + + bloc1="$artist" + bloc2="$song" + + oldfile="${o["file"]}" + I=0 fi - echo "${longsong} $status$longtime" - echo "${shortsong} $status$shorttime" -else - [[ -f ${runtime}/mpd ]] && rm ${runtime}/mpd - [[ -f ${runtime}/mpd-scroll ]] && rm ${runtime}/mpd-scroll - printf "$status[--:-- --:-- --:--]\n" " " - printf "$status--:-- --:--\n" " " -fi + # Gestion du défilement + if "$scroll" + then + unset bloc1 bloc2 bloc3 bloc4 + declare -- bloc1 bloc2 bloc3 bloc4 + bloc1="${artist:$I:$SCROLL}" + if [[ "${#bloc1}" -eq 0 ]] + then + bloc2="${song:$(( I - ${#artist} )):$SCROLL}" + if [[ "$(( ${#bloc1} + ${#bloc2} ))" -lt "$SCROLL" ]] + then + bloc3="${artist:0:$(( SCROLL - ${#bloc2} ))}" + if [[ "$(( ${#bloc1} + ${#bloc2} + ${#bloc3} ))" -lt "$SCROLL" ]] + then + bloc4="${song:0:$(( SCROLL - ${#bloc1} - ${#bloc2} - ${#bloc3} ))}" + fi + fi + else + bloc2="${song:0:$(( SCROLL - ${#bloc1} ))}" + if [[ "$(( ${#bloc1} + ${#bloc2} ))" -lt "$SCROLL" ]] + then + bloc3="${artist:0:$(( SCROLL - ${#bloc2} - ${#bloc1} ))}" + fi + fi + I+=1 + [[ "$I" -ge "$(( ${#artist} + ${#song} ))" ]] && I=0 + fi + + # Gestion du temps + time_elapsed=$(human_time "${o["time"]%:*}") + time_total=$(human_time "${o["time"]#*:}") + time_left=$(human_time "$(( ${o["time"]#*:} - ${o["time"]%:*} ))") + + time="[${time_elapsed:---:--} ${time_left:---:--} ${time_total:---:--}]" + + # DEBUG + printf '%s%s%s%s%s\n' "$bloc1" "$bloc2" "$bloc3" "$bloc4" " $status$time" +done diff --git a/.i3blocks/toggle-sink b/.i3blocks/toggle-sink index 8133d17..3ed6c15 100755 --- a/.i3blocks/toggle-sink +++ b/.i3blocks/toggle-sink @@ -1,5 +1,6 @@ #!/bin/bash +SINK="alsa_output.pci-0000_2b_00.3.analog-stereo" currentport=$(pactl list sinks | grep -Po "Active Port: analog.*") @@ -7,10 +8,10 @@ if [[ -n "$button" ]] then if [[ "$currentport" == "Active Port: analog-output-lineout" ]] then - pactl set-sink-port "alsa_output.pci-0000_2b_00.3.analog-stereo.5" "analog-output-headphones" + pactl set-sink-port "$SINK" "analog-output-headphones" echo "casque" else - pactl set-sink-port "alsa_output.pci-0000_2b_00.3.analog-stereo.5" "analog-output-lineout" + pactl set-sink-port "$SINK" "analog-output-lineout" echo "front " fi pkill -RTMIN+10 i3blocks diff --git a/.local/bin/music-convert b/.local/bin/music-convert index b0d882b..fa257a6 100755 --- a/.local/bin/music-convert +++ b/.local/bin/music-convert @@ -31,7 +31,7 @@ do # On entre dans le répertoire à traiter cd "${rootdir}/${dir}" - + # Pour chaque fichier for file in * do @@ -59,11 +59,13 @@ do declare -a running running=($(jobs -p)) - if [[ "${#running[@]}" -ge 6 ]] + if [[ "${#running[@]}" -ge 10 ]] then wait -n fi + togain="true" + ( echo " $file" ffmpeg -n -loglevel error -i "file:$file" -c:a libopus -b:a 256K "file:${dstdir}${dstfile}" @@ -76,14 +78,23 @@ do fi done wait - ( - cd "${dstdir}" - shopt -s nullglob - echo "[${blue}GAIN${reset}] $dir" - loudgain -S -a -k -q -s e *.opus &>/dev/null && \ - echo "[${green}GAIN${reset}] $dir" || \ - echo "[${red}GAIN${reset}] $dir" - shopt -u nullglob - ) & disown $! + + # Si le moindre fichier a été converti, on refait les tags replay gain + + if ${togain} && [[ "${dir}" != "Vidéos/" ]] + then + ( + cd "${dstdir}" + shopt -s nullglob + echo "[${blue}GAIN${reset}] $dir" + loudgain -S -a -k -q -s e *.opus &>/dev/null && \ + echo "[${green}GAIN${reset}] $dir" || \ + echo "[${red}GAIN${reset}] $dir" + shopt -u nullglob + ) & disown $! + + # Réinitialise le tag togain + togain="false" + fi done wait