Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # https://forum.strawberrymusicplayer.org/topic/3417/optional-gap-between-each-song-playback
- #
- # Poor man's custom gap for the Strawberry audio player via "Stop after this track" feature.
- # Use case: Introduce a silent period after each track.
- ###################################### IMPORTANT ################################################
- # #
- # Close Strawberry first and make sure the process doesn't exist. #
- # Otherwise you may backup a corrupted database. #
- # #
- # Backup the database: #
- # echo cp ~/.local/share/strawberry/strawberry/strawberry.db{,__$(date +'%Y-%m-%d_%H:%M')__OK} #
- # ls -l ~/.local/share/strawberry/strawberry/ #
- # #
- # Install the latest version of Strawberry (1.2.4 as of now) from #
- # https://github.com/strawberrymusicplayer/strawberry/releases #
- # #
- #################################################################################################
- # On the sunny side:
- # After running the script, every finished track will be followed by the chosen period of
- # silent time, and then the next track will be played. During the gap, the player is stopped.
- # Double clicks anywhere on the playlist work normally while playing or in Pause.
- # The previous attempt (a different approach using Previous and pausing there, with an older
- # Strawberry) lead to freezes of the player, other programs and the sound system. Never happened
- # with this one, but be cautious.
- # Limitations/nuisances:
- # The script uses D-Bus MPRIS interface, which doesn't account for the end of a track nor can
- # it detect how Play, Stop and other actions were initiated. There is no way to differentiate
- # between mouse clicks, commands or actions automatically performed by the player.
- # So, if the user clicks on Stop, the information received by the script will be the same as
- # that of "Stop after this track", and it will start the countdowm of the silent period. Use
- # pause instead to stop the audio temporarily, or interrupt the script.
- #
- # The OSD/notification system of Strawberry is not controllable externally, aside from the two
- # actions assignable to keyboard shortcuts. If it is active, it will notify the stop of every track.
- # And in any case, the album cover pane will disappear when the player is stopped, as usual.
- # This alpha is to test and decide if the benefit of the general idea outweights its disadvantages.
- # Several things are on the to-do list and may be achieved or not depending on availability of wisdom
- # and possible comments: better argument handling, change of track during silence, start/end from a
- # desktop icon, time units for the gap (minutes, currently only seconds are used), alternatives for
- # the dbus tools, user input at runtime, custom layout of the metadata shown in the terminal,
- # image-to-ascii for the album cover -kidding-, etc.
- # Tested only for the general case of a regular static playlist.
- ##### User config.
- gap_secs_default="30" # The gap: seconds to wait before playing the next track.
- show_countdown="yes" # A little feedback in the terminal; not necessary.
- countdown_step="0.2" # "Faster seconds" for the first tests. Set to 1 when done.
- #############################################################################
- short="${0##*\/}"
- _debug_=":"
- _help () {
- echo "Usage: $short [-h|--help] [-nc] [N]"
- echo "Gap maker for Strawberry. Stops it for N seconds after each track and plays the next one."
- echo " N : Seconds. Default: $gap_secs_default"
- echo " -nc : Don't show countdown."
- # echo " --verbose: "
- echo " -h : This help."
- echo ""
- echo " Example: ./$short -nc 35"
- echo " Quit: Control-C $short v0.1-alpha - \"Gapper's Delight.\""
- }
- _args () {
- while : ; do
- case "$1" in
- "--debug") _debug_="echo " ; shift ;;
- "-h"|"--help") _help ; exit ;;
- "-nc"|"--no-countdown") show_countdown="no" ; shift ;;
- "--verbose") _debug_="echo " ; shift ;;
- "") gap_secs="$gap_secs_default" ; break ;;
- *) [[ "$1" =~ ^[0-9]+ ]] && gap_secs="$1" || \
- echo "Ignored parameter: $1" >&2
- break ;;
- esac
- done
- }
- _checkTools () {
- for tool in gdbus dbus-monitor; do
- which "$tool" >/dev/null 2>&1 || {
- echo "$tool not found."
- missing="yes" ; }
- done
- [[ "$missing" == "yes" ]] && {
- echo "Can't find the above program(s). Exiting..."
- exit 1
- }
- }
- args=( "$@" )
- _args "${args[@]}"
- _playerStatus () {
- player_status="$(gdbus call -e --dest org.mpris.MediaPlayer2.strawberry --object-path /org/mpris/MediaPlayer2 --method org.freedesktop.DBus.Properties.Get org.mpris.MediaPlayer2.Player PlaybackStatus)"
- }
- _metadataAlt () {
- metadata="$(gdbus call -e --dest org.mpris.MediaPlayer2.strawberry --object-path /org/mpris/MediaPlayer2 --method org.freedesktop.DBus.Properties.Get org.mpris.MediaPlayer2.Player Metadata)"
- # xesam's quoting is worth it
- mod1="${metadata#????}"
- mod1="${mod1%????}"$'\n'
- mod1="${mod1//, \'mpris:/$'\n'}"
- mod1="${mod1//, \'xesam:/$'\n'}"
- mod1="${mod1/>, \'year\': </>$'\n'year\': <}"
- mod1="${mod1//\': <[/=}"
- mod1="${mod1//\': </=}"
- mod1="${mod1//\]>$'\n'/$'\n'}"
- mod1="${mod1/length=int64 /length=}"
- mod1="${mod1/trackid=objectpath \'/trackid=\'}"
- mod1="${mod1//>$'\n'/$'\n'}"
- mod1="${mod1/\{sv\} \{$'\n'/}"
- mod1="${mod1%$'\n'}"
- while read -r -u 8 line ; do
- eval "$line"
- done 8<<<"$mod1"
- art_url="$artUrl"
- duration="${length%???}"
- playlist_pos="${trackid##*\/}"
- album_artist="$albumArtist"
- birth="${contentCreated/T/ }"
- last_used="${lastUsed/T/ }"
- track_number="$trackNumber"
- use_count="$useCount"
- }
- _printMetadata () {
- echo " playlist_pos: $(( playlist_pos + 1 ))"
- echo " artist: $artist"
- echo " title: $title"
- echo " duration: $duration"
- echo " album: $album"
- echo " year: $year"
- echo " track_number: $track_number"
- echo " album_artist: $album_artist"
- echo " genre: $genre"
- echo " composer: $composer"
- echo " comment: $comment"
- echo " bitrate: $bitrate"
- echo " url: $url"
- echo " art_url: $art_url"
- echo " birth: $birth"
- echo " last_used: $last_used"
- echo " use_count: $use_count"
- }
- _gap () {
- unset cdi_length
- countdown="$gap_secs"
- [[ "$show_countdown" != "yes" ]] && {
- echo -n "Waiting... "
- sleep "$gap_secs" ; } || {
- cd0_length=$(( 13 + ${#countdown} ))
- echo -n " Countdown: $countdown "
- ((countdown--))
- for ((i=countdown; i>=0; i--)); do
- sleep "$countdown_step"
- [[ "$show_countdown" == "yes" ]] && echo -n "$i "
- cdi_length="$(( cdi_length + ${#i} + 1 ))"
- done
- len="$(( cd0_length + cdi_length ))"
- echo -ne "\r"
- printf "%${len}s"
- echo -ne "\r"
- }
- }
- ############################################################### end functions
- date -u
- _checkTools
- _metadataAlt
- [[ "$_debug_" == ":" ]] || _printMetadata
- st="strawberry"
- coproc dbus_monitor {
- dbus-monitor --monitor --session \
- "type='signal',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',path='/org/mpris/MediaPlayer2'" | \
- grep --line-buffered --no-group-separator -A 1 ' string "PlaybackStatus"'
- }
- coproc_pid="$dbus_monitor_PID"
- $_debug_ "File descriptor is ${dbus_monitor[1]} -- \$dbus_monitor_PID is $dbus_monitor_PID -- \$\$ is $$ "
- $_debug_ "playlist_pos is $(( playlist_pos + 1 ))"
- [[ -n "$title" ]] && echo "TÃtle: $title"
- while read -r line; do
- $_debug_
- $_debug_ ">> $line"
- case "$line" in
- 'string "PlaybackStatus"') : ;;
- 'variant string "Paused"' ) $_debug_ $'\x0a'"PAUSED"
- paused="yes" ;;
- 'variant string "Playing"' ) $_debug_ $'\x0a'"PLAYING"
- _metadataAlt
- [[ "$_debug_" == ":" ]] || _printMetadata
- [[ "$paused" != "yes" ]] && {
- echo "Title: $title" ; }
- paused="no"
- $_debug_ "PLAYING NUM $(( playlist_pos + 1 ))"
- $st -q
- $_debug_ "requested STOP-after" ;;
- 'variant string "Stopped"') $_debug_ $'\x0a'"STOPPED (waiting)"
- $_debug_ "Waiting $gap_secs"
- _metadataAlt
- [[ "$_debug_" == ":" ]] || _printMetadata
- _gap
- $st -k $(( playlist_pos + 1 )) ;;
- *) $_debug_ "Stray line: --$line--" ;;
- esac
- done <&"${dbus_monitor[0]}"
- wait "${dbus_monitor[1]}"
- exit
- #############################################################################
- exit
- # Better safe than sorry: backup the database!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement