-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBGM_mp3play_v2
101 lines (87 loc) · 3.32 KB
/
BGM_mp3play_v2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
#
# Background Music Box (BMB)
#
# 2019/09/05
#
# Shows current song, and let you select serveral songs in playlist
# you need (again) lsof tool to detect current song playing
# you need also mp3info to obtain play length of current song
# Type `sudo apt install lsof mp3info` to install
#
# This script provides some functions how a graphical music player CAN look like
# Plesae dear community, feel free to improve this script ;)
#
# by cyperghost for https://retropie.org.uk/
# https://retropie.org.uk/forum/topic/21029
# ---- Set variables ----
BGM_PATH="$HOME/RetroPie/bgm"
BGM_PLAYER="mpg123"
BGM_PATH="$(realpath $BGM_PATH)"
BGM_TYPE=".*\.\(mp3\|ogg\)"
PLAYER_PID="$(pgrep -f $BGM_PLAYER)"
PLAYER_INSTANCE="$(pgrep -c -f $BGM_PLAYER)"
PLAYER_SHUFFLE="$BGM_PLAYER -q -Z $BGM_PATH/*.mp3 < /dev/null"
# ---- function calls ----
# Dialogs - dialog_error parse test, dialog_yesno parse text and dialogtitle
# Display dialog --msgbox with text parsed with by function call
function dialog_error() {
dialog --title " Error! " --msgbox "$1" 7 45
}
function dialog_yesno() {
dialog --title " $2 " --yesno "$1" 10 55
}
# ---- Script Start ----
[[ -d $BGM_PATH ]] || { dialog_error "Directory $BGM_PATH not found! Exit!"; exit; }
[[ $PLAYER_INSTANCE -gt 1 ]] && { dialog_error "There are $PLAYER_INSTANCE instances of $BGM_PLAYER running! Only 1 instance supported!"; exit; }
if [[ $PLAYER_INSTANCE -eq 0 ]]; then
dialog_yesno "$BGM_PLAYER not running!\nShould I try to start it using shuffle mode?\n\nShuffle command: $PLAYER_SHUFFLE"
[[ $? -eq 0 ]] || exit
$PLAYER_SHUFFLE &
exit
fi
# Build file array
cd "$BGM_PATH"
while read -r i; do
array+=("${i#*/}" "Estimated length: $(mp3info "$i" -p %m:%02s)")
done < <(find -maxdepth 1 -iregex $BGM_TYPE -type f | sort)
# Get current song and number of song
songsinplaylist="$(ps aux | grep $BGM_PLAYER | grep -o $BGM_PATH | wc -l)"
songname=$(lsof -c $BGM_PLAYER -F | grep "$BGM_PATH")
songname="${songname##*/}"
mp3length=$(mp3info "$songname" -p %m:%02s)
songname="${songname%.*}"
# Build dialog
while true; do
cmd=(dialog --backtitle "Currently Playing: $songname -- $mp3length" \
--title " The Background Music Box " \
--extra-button --extra-label " PlayList " \
--ok-label " Let's play " --cancel-label " Cancel " \
--help-button --help-label " Shuffle " \
--stdout --no-items --item-help --default-item "$file" \
--menu "Currently $((${#array[@]}/2)) music files found in $BGM_PATH\n$songsinplaylist tracks are active in current Playlist!\n${#farray[@]} tracks stored to new Playlist!" 16 68 12)
file=$("${cmd[@]}" "${array[@]}")
button=$?
# Do actions
case $button in
0) #Select/Okay Button
kill $PLAYER_PID >/dev/null 2>&1
sleep 0.5
[[ ${#farray[@]} -eq 0 || "${farray[-1]}" != "$BGM_PATH/$file" ]] && farray=("$BGM_PATH/$file" ${farray[@]})
$BGM_PLAYER -q "${farray[@]}" < /dev/null &
exit
;;
1) #Cancel Button
exit
;;
2) #HELP/SHUFFLE Button
kill $PLAYER_PID >/dev/null 2>&1
sleep 0.5
$PLAYER_SHUFFLE &
exit
;;
3) #EXTRA/PLAYLIST Button
farray+=("$BGM_PATH/$file")
;;
esac
done