Linux 通用音频睡眠倒计时器

Linux 通用音频睡眠倒计时器

我创建了一个 bash 脚本大胆的在 X 分钟内暂停播放(并在过去 10 分钟内逐渐降低音量)。这个脚本很好地满足了我使用 Audacious 时的需求。但是,现在我要切换到死豆

我有两个选择:修改现有脚本以与 DeaDBeeF 配合使用,或者创建一个可与几乎所有 Linux 或 KDE 音频播放器配合使用的通用方法。我正在寻求这两种方法的帮助。

但是,我无法找到 DeaDBeeF 的文档,这些文档允许我按照 Audacious 的方式继续操作。如果我能找到文档(并且如果 DeaDBeeF 提供等效命令),我可以轻松修改现有脚本。或者,并且可能需要更多帮助,我可以实现一个通用脚本。

我不是开发人员,所以我的首选实现是一个简单的 bash 脚本(尽管我对其他同样简单的实现选项持开放态度)。

如前所述,我正在寻求帮助来修改我的 bash 脚本,以便与 DeaDBeeF 一起使用或以通用方式工作。这是我的脚本:

#!/bin/bash

function_pid=0

function musicsleep(){
    countdown=$(($1*60-1))
    datestop=$((`date +%s` + $countdown));
    echo "pausing music in $1 minutes..."

    #begin reducing sound volume 10 minutes (600 sec) from end
    voldown=$(($datestop - 600))

    while [ "$datestop" -ge `date +%s` ]; do
        datestr=$(printf "%s" $(date -u --date @$(($datestop + 1 - `date +%s`)) +%H:%M:%S))
        echo -ne "$datestr\r";
        kdialog --passivepopup "music will pause in: $datestr" 14
        if [ `date +%s` -ge "$voldown" ]; then
            currvol=$(audtool get-volume)
            #reduce volume in 40 steps of 2 = 80% reduction (starting at 100) over 10 min
            newvol=$(($currvol - 2))
            audtool set-volume $newvol
            echo -ne "\t\tvolume = $newvol\r"
            kdialog --passivepopup "volume = $newvol" 14
        fi
        sleep 15
    done
    audtool playback-pause
    echo "music paused at $(date)"
    audtool set-volume 100
}

while true; do

    choice=$(kdialog --combobox "Music Sleep Time:" "cancel" "stop" "5" "10" "15" "20" "25" "30" "35" "45" "45" "50" "60" "90" "120" "180" "360" "480" --default "25");
    retval="$?"
    if [ "$retval" -eq 0 ]; then
            if [ $function_pid -ne 0 ]; then
                kill $function_pid
                function_pid=0
                echo "countdown process terminated..."
            fi

            if [ "$choice" == cancel ]; then
                echo "countdown canceled by user at $(date)"
                kdialog --passivepopup "countdown canceled" 10
            elif [ "$choice" == stop ]; then
                audtool playback-stop
                echo "music playback stopped at $(date)"
                kdialog --passivepopup "music playback stopped" 10
            else
                echo "countdown (re)set by user at $(date)"
                kdialog --passivepopup "countdown started" 5
                audresult=$(audtool playlist-shuffle-status)
                if [ "$audresult" == "on" ]; then audtool playlist-shuffle-toggle; fi
                audtool playback-playing
                isplaying=$?
                audtool playback-paused
                ispaused=$?
                audtool playback-stopped
                isstopped=$?
                echo "isplaying=$isplaying, ispaused=$ispaused, isstopped=$isstopped"
                if [[ "$isstopped" -eq 0 || "$ispaused" -eq 0 ]]; then audtool playback-play; echo "playback started"; fi
                musicsleep "$choice" &
                function_pid=$!
                echo "function_pid=$function_pid"
            fi
            audtool set-volume 100

    elif [ "$retval" -eq 1 ]; then
            echo "exiting musicsleep"
            #audtool set-volume 100
            break
    else
            kdialog --error "ERROR";
    fi;

done

该脚本中实现的基本功能是:

  • 从 GUI 中选择睡眠倒计时器值
  • 倒计时器开始时禁用随机(随机)播放
  • X 分钟后暂停音频播放
  • 在 GUI 中显示剩余时间(或通过桌面通知)
  • 在 GUI 中显示当前音量级别
  • 在最后 10 分钟内逐渐减小音量
  • 播放暂停后,将音量重置为初始值
  • 允许将倒计时器重置为新值而不中断播放
  • 允许取消倒计时器而不中断播放
  • 从倒数计时器 GUI 手动停止播放

一些问题是:

DeadDBeeF 有等效的命令吗?

如果是,等效 DeaDBeeF 命令的文档在哪里?

对于已经熟悉 DeaDBeeF 的人来说,我是否可以期望简单地用 DeaDBeeF 命令替换脚本中现有的 Audacious 命令?或者差异是否大到需要完全重写脚本?

如果具有上述功能的通用倒计时器实现起来相对简单,有人愿意指导我吗?我认为体积部分很容易实现:

pactl set-sink-volume "$MY_SINK" "$VOLUME_CHANGE"  

来源

当谈到暂停播放时,据我所知,以下命令不太符合我的脚本的要求:

pactl suspend-sink "$MY_SINK"
pactl suspend-sink "$MY_SINK" FALSE

上述方法不仅暂停播放音频,还禁用所有应用程序的所有声音,并且似乎还有其他潜在的副作用。

据我所见这里,使用 pactl 控制播放和暂停(跨任何音频播放器)看起来很困难。当然必须有一个更简单的方法。如果它是 KDE 特定的,我没问题。

答案1

如果你查看 deadbeef 来源主程序help()函数列出了对您来说应该足够的选项:

--pause              Pause playback
--nowplaying FMT     Print formatted track name to stdout
--nowplaying-tf FMT  Print formatted track name to stdout
--volume [NUM]       Print or set deadbeef volume level

答案2

这是我最终使用的脚本。由于该--volume选项在稳定版本中尚不可用,pactl因此我使用了命令。

这是DeaDBeeF 的倒计时睡眠定时器。它支持除我的 Audacious 脚本中的一项功能之外的所有功能。他们是:

  • 从 GUI (kdialog) 选择睡眠倒数计时器值
  • X 分钟后暂停音频播放
  • 在 GUI 中显示剩余时间(或通过桌面通知)
  • 在 GUI 中显示当前音量级别
  • 在最后 10 分钟内逐渐减小音量
  • 播放暂停后,将音量重置为初始值
  • 允许将倒计时器重置为新值而不中断播放
  • 允许取消倒计时器而不中断播放
  • 从倒数计时器 GUI 手动停止播放
  • 如果停止,请从倒数计时器 GUI 再次开始播放

代码:

#!/bin/bash

echo "DeaDBeeF control version 0.9"
function_pid=0

echo "true" > /dev/shm/allow_resetting_pactl_volume

function get_default_sink_name() {
    pacmd stat | awk -F": " '/^Default sink name: /{print $2}'
}

function get_default_sink_volume() {
    pacmd list-sinks | awk '/^\s+name: /{indefault = $2 == "<'$(get_default_sink_name)'>"} /^\s+volume: / && indefault {print $5; exit}'
}

function set_default_sink_volume() {
    pactl set-sink-volume $(get_default_sink_name) "$1"
}
#source: https://unix.stackexchange.com/a/251920/15010

function music_sleep(){
    countdown=$(($1*60-1))
    datestop=$((`date +%s` + $countdown));
    echo "pausing DeaDBeeF in $1 minutes..."

    #begin reducing sound volume 10 minutes (600 sec) from end
    voldown=$(($datestop - 600))

    while [ "$datestop" -ge `date +%s` ]; do
        datestr=$(printf "%s" $(date -u --date @$(($datestop + 1 - `date +%s`)) +%H:%M:%S))
        echo -ne "$datestr\r";
        #echo "$datestr";
        kdialog --passivepopup "DeaDBeeF will pause in: $datestr" 3
        if [ $(</dev/shm/allow_resetting_pactl_volume) == "true" ]; then
            countdown_saved_pactl_volume=$(get_default_sink_volume)
            echo $countdown_saved_pactl_volume > /dev/shm/countdown_saved_pactl_volume
        fi
        if [ `date +%s` -ge "$voldown" ]; then
            echo "false" > /dev/shm/allow_resetting_pactl_volume
            #reduce volume in 40 steps of 2 = 80% reduction (starting at 100) over 10 min
            set_default_sink_volume "-2%"
            currvol=$(get_default_sink_volume)
            echo -ne "\t\tvolume = $currvol\r"
            #echo "volume = $currvol"
            kdialog --passivepopup "volume = $currvol" 3
        fi
        sleep 15
    done

    deadbeef --pause
    echo "DeaDBeeF paused at $(date)"

    countdown_saved_pactl_volume=$(</dev/shm/countdown_saved_pactl_volume)
    set_default_sink_volume "$countdown_saved_pactl_volume"
    echo "true" > /dev/shm/allow_resetting_pactl_volume
}

countdown_saved_pactl_volume=$(get_default_sink_volume)
echo $countdown_saved_pactl_volume > /dev/shm/countdown_saved_pactl_volume
echo "initial volume is: $countdown_saved_pactl_volume"

while true; do

    choice=$(kdialog --combobox "          DeaDBeeF Sleep Timer:          " "cancel" "stop" "5" "10" "15" "20" "25" "30" "35" "45" "45" "50" "60" "90" "120" "180" "360" "480" --default "30");
    retval="$?"

    if [ "$retval" -eq 0 ]; then
            if [ $function_pid -ne 0 ]; then
                    kill $function_pid
                    function_pid=0
                    echo "countdown process terminated..."
            fi

            if [ "$choice" == cancel ]; then
                    echo "countdown canceled by user at $(date)"
                    kdialog --passivepopup "countdown canceled" 5
            elif [ "$choice" == stop ]; then
                    deadbeef --stop
                    echo "DeaDBeeF playback stopped at $(date)"
                    kdialog --passivepopup "DeaDBeeF playback stopped" 5
            else
                    echo "countdown (re)set by user at $(date)"
                    kdialog --passivepopup "countdown started" 5
                    #audresult=$(audtool playlist-shuffle-status)
                    #if [ "$audresult" == "on" ]; then audtool playlist-shuffle-toggle; fi
                    isplaying=$(deadbeef --nowplaying-tf "%isplaying%")
                    ispaused=$(deadbeef --nowplaying-tf "%ispaused%")
                    echo "isplaying=$isplaying, ispaused=$ispaused"
                    if [[ "$isplaying" -ne 1 || "$ispaused" -eq 1 ]]; then deadbeef --play-pause; echo "playback started"; fi
                    music_sleep "$choice" &
                    function_pid=$!
                    echo "function_pid=$function_pid"
            fi
            countdown_saved_pactl_volume=$(</dev/shm/countdown_saved_pactl_volume)
            set_default_sink_volume "$countdown_saved_pactl_volume"
            echo "reset volume to: $countdown_saved_pactl_volume"
            allow_reset_saved_volume="true"
            echo "true" > /dev/shm/allow_resetting_pactl_volume

    elif [ "$retval" -eq 1 ]; then
            echo "exiting DeaDBeeFctl"
            break
    else
            kdialog --error "ERROR";
    fi;

done

rm /dev/shm/allow_resetting_pactl_volume
rm /dev/shm/countdown_saved_pactl_volume

相关内容