-
Notifications
You must be signed in to change notification settings - Fork 0
/
beats-old
1215 lines (999 loc) · 26.3 KB
/
beats-old
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# beats - Simple and fast music player.
## Status
# Project restarted: Deadline 10 Sep 2023.
# FixIt: change currently_playing_* to currently_playing_*
# Disable unicode(Perfomance).
LC_ALL=C
LANG=C
## NO Configs.
## FixIt: Follow modern solution of doing it.
# Colors
color_selected='\e[32m'
color_playing='\e[33m'
color_barbg=3
color_progressbar=4
# COL0='\e[0;30m'
# COL1='\e[0;31m'
# COL2='\e[0;32m'
# COL3='\e[0;33m'
# COL4='\e[0;34m'
# COL5='\e[0;35m'
# COL6='\e[0;36m'
# COL7='\e[0;37m'
# COLR='\e[0m'
# version (YY.MM.DD)
BEATS_VERSION=23.08.24
# Must be executed only once the player statrts.
player_start_(){
#printf '\e[?1049h' # Use alternative screen buffer.
#printf '\e[2J' # Clear the screen.
printf '\e[?25l' # Hide the cursor.
printf '\e[?7l' # Disable line wrapping.
printf '\e[1;%sr' "$LINES" # Limit scrolling.
stty -echo # Hide echoing of user input
# Get termwindow Geometry.
get_term_size_
}
# Must be executed on the player termination.
player_exit_(){
# Kill Player
## FixIt: change dontkillplayer to killplayer and reverse booleans realted.
[[ $dont_kill_player != true ]] && {
kill_beat_ "$player_pid"
update_data_ ST 'STOPPED'
}
## FixIt: look for a better solution for repeat keys
xset r rate 500 # Disable Repeat key rate
#printf '\e[2J' # Clear screen.
printf '\e[?7h' # Enables Line Wrap.
printf '\e[?25h' # Restore cursor.
printf '\e[;r' # Default Scrolling limit.
#printf '\e[?1049l' # Return to main screen.
stty echo # Show echoing of user.
exit 0
}
# Get Terminal Size(Lines x Columns).
get_term_size_(){
shopt -s checkwinsize
(:;:) # Sleep for micro secons.
}
# Get Beats name.
get_beats_name_(){
local Index
unset Beat_Name # Array which stores beats name.
for i in "${!playlist[@]}"; do
for Index in "${playlist[$i]}"*; do
case "$Index" in
# Store beats hvaing following formats.
*'.mp3' | *'.mp4' | *'.m4a' | *'.acc' |\
*'.wma' | *'.ogg' | *'.wav' | *'.ogg' |\
*'.opus' | *'.flac' | *'aiff' ) Beat_Name+=("$Index") ;;
esac
done
done
# Show message if no beat found.
if (( ${#Beat_Name[@]} == 0 )); then
status_bar_ "No Beats found."
bsleep_ 1
fi
# Beats are not searched.
searched=false
}
# Search for beats with matching string.
find_beats_(){
# $1 = String to searched.
# Re-initialize the Beats data.
[[ "$searched" == 'true' ]] && get_beats_name_
# Reset session specific variables.
selected_index=
# Uppercase or Lowercase both side to enable fuzzyfinding.
case "$screen" in
'MAIN')
local List
for Index in "${!Beat_Name[@]}"; do
if [[ "${Beat_Name[$Index]^^}" == *"${1^^}"* ]]; then
List+=("${Beat_Name[$Index]}")
fi
done
unset Beat_Name
Beat_Name=("${List[@]}")
;;
'QUEUE')
local List
for Index in "${!Queue_Name[@]}"; do
if [[ "${Queue_Name[$Index]^^}" == *"${1^^}"* ]]; then
List+=("${Queue_Name[$Index]}")
fi
done
unset Queue_Name
Queue_Name=("${List[@]}")
;;
esac
# Show message if no beat found.
if (( ${#Beat_Name[@]} == 0 )); then
status_bar_ "No Beats found."
bsleep_ 1
fi
# Beats are Search with a sequence.
searched=true
}
arguments_(){
# Argument Handler
local TotArg=$#
for ((;i++<$TotArg;)); do
case "$1" in
# Check if user need help.
'-h'|'--help')
echo "Can't help right now."
exit 0
;;
'-v'|'--version')
printf '%s\n' "$Version"
exit 0
;;
*)
# Play beat directly if specified and existed.
if [[ -f "$1" ]]; then
## direct play
echo "not done yet"
exit
# Specify the directory where to search for beats.
elif [[ -d "$1" ]]; then
# Replace ~ with $HOME.
BEATS_DIR="${1//\~/$HOME}"
# Add / if not already given.
[[ "$BEATS_DIR" != *'/' ]] && BEATS_DIR="$BEATS_DIR"'/'
else
printf 'beats: error: Unknown option or file: %s\n' "$1" 1>&2
exit 2
fi
esac
shift
done
}
input_handler_(){
unset input
# Read input(exit function if idel for sometime).
# Read one Normal or special key.
read $1 -rs -N 1 input
# Read special escape key.
[[ "$input" == $'\e' ]] && {
read -t 0.01 -rsn5 input
input='\e'"$input"
}
# Flush extra input
#read -t 0.01 -sn50
case "$input" in
# Special Keys
$'\n') printf 'ENTER' ;;
' ') printf 'SPACE' ;;
# !Tab must remain below space and enter
$'\t'|[[:blank:]])
printf 'TAB' ;;
$'\177'|$'\b')
printf 'BACKSPC';;
'\E'|'\e')
printf 'ESCAPE' ;;
# Arrow Keys
'\e[A'|'k'|'\eOA')
printf 'UP' ;;
'\e[B'|'j'|'\eOB')
printf 'DOWN' ;;
'\e[D'|'h'|'\eOC')
printf 'LEFT' ;;
'\e[C'|'l'|'\eOC')
printf 'RIGHT' ;;
# Remaining keys
*) echo -n "$input";;
esac
}
# Kill current playing beat.
kill_beat_(){
player_pid="${1:-"$player_pid"}"
kill "$player_pid" 2>/dev/null
update_data_ ST 'STOPPED'
}
# List available beats.
list_beats_(){
# $1 - Number of beats to show
[[ $1 ]] && ListEnd=$((ListStart + $1)) || ListEnd=$((ListStart+LINES))
# List limited beats that can fit on screen.
for (( i=$ListStart;i<$ListEnd;i++ )); do
# If exceed the limit then display empty line.
if (( $i < 0 || $i > $((Total-1)) )); then
local Temp=""
else
local Temp="${Beat_Name[$i]%.*}"
Temp="${Temp##*/}"
fi
# Colour selected_index.
if [[ $selected_index -eq $i ]]; then
echo -en "$color_selected${Temp^^}\e[0m\e[K\n"
# Colour currently_playing.
elif [[ $currently_playing_index -eq $i ]]; then
echo -en "$color_playing${Temp}\e[0m\e[K\n"
# Remaining beats.
else
echo -en "$Temp\e[K\n"
fi
done
}
# Initialize the player.
initialize_(){
# Duration of current playing beat
# Using $(<) is problematic here
curr_duration="$(cat "$DATA_FILE2" 2>/dev/null)"
# Initialize after a beat is completely played.
if (( $curr_duration == -1 && ${#Queue_Name[@]} != 0 )); then
play_status='STOPPED'
# Update curr_duration in data file.
curr_duration=0
update_data_ CD 0
# Apply play_modes, when a beat is completely played.
case "${play_mode^^}" in
# Shuffle one after another.
'SHUFFLE')
;;
# Repeat a particular beat.
'REPEAT')
case "${mode_sequence^^}" in
'REVERSED')
queue_index=$((queue_index-1))
if (( $queue_index == 0 )); then
queue_index=0
curr_duration=0
update_data_ CD 0
fi
;;
# FORWARD
*)
queue_index=$((queue_index+1))
if (( $queue_index -ge $((Total-1)) )); then
queue_index=$((Total-1))
# When Whole Queue Finishes then stop
# entering the above if statement.
curr_duration=0
update_data_ CD 0
fi
;;
esac
update_data_ QI "$queue_index"
ffplay_play_ "${Queue_Name[$queue_index]}"
;;
# Randomly play any beat.
'RANDOM')
local Temp=$((RANDOM%10))
## WIP
ffplay_play_ "${Beat_Name[$Temp]}"
;;
esac
fi
# Set total duration equal to current duration, if any error occurs.
total_duration=${total_duration:-$curr_duration}
# Persent of beat played.
#PlayPercentage="$(bc -l <<< "($curr_duration/$total_duration)*100" 2>/dev/null)"
#PlayPercentage=${PlayPercentage%%.*}'%'
# Source data files when needed.
[[ "${source_data:-false}" == 'true' ]] && {
source_data_
source_data=false
}
}
setup_tui_(){
# Total Beats.
case "$screen" in
"QUEUE")
Total="${#Queue_Name[@]}"
;;
*)
Total="${#Beat_Name[@]}"
;;
esac
# Space(LINES) taken by other modules.
# 1 for space taken by Progress Bar.
# 1 for space taken by Status Bar.
#ReservedLines=1
# Select the middle of the total beats.
if [[ -z "$selected_index" ]]; then
case "$(( (Total-1)%2 ))" in
'0') select_ "$(( (Total-1)/2 ))" ;; # For event number.
* ) select_ "$(( ( (Total-1)/2 )+1 ))" ;; # For odd number.
esac
fi
initialize_listSE_ "$selected_index"
}
# Initialize selected_index Variable.
select_(){
selected_index=${1:-"$selected_index"}
# Limit selected_index range. It can't be less than zero and
# can't be more than total beats available to select.
if (( $selected_index < 0 )); then
selected_index=0
elif (( $selected_index > $((Total-1)) )); then
# Case, in which Total no. of beats are zero.
if (( $Total == 0 )); then
selected_index=0
else
selected_index="$((Total-1))"
fi
fi
}
# Initialize ListStart and LinstEnd variables based on current selection.
# In such a way that selected beat will remain at the center.
initialize_listSE_(){
# Set selected_index if given by argument.
select_ "${1:-"$selected_index"}"
case "$((LINES%2))" in
'0') local Temp=$(( LINES/2 )) ;; # For even number.
* ) local Temp=$(( (LINES/2) + 1 )) ;; # For odd number.
esac
# (selected_index+1) : We need the index of selected_index beat(1 to N)
# rathen than the index of the selected array(0 to N-1).
ListStart=$(( (selected_index+1) - (Temp) ))
#ListEnd=$(( (selected_index+1) + (LINES-Temp) - ReservedLines ))
}
status_bar_(){
# Set position.
#(( $# > 0 )) && printf '\e[m\e[%s;0H\e[K' "$1" || printf '\e[m\e[%s;0H\e[K' "$LINES"
# Show custom text.
if (( $# >= 1 )); then
printf '\r\e[42m%*s' "$COLUMNS"
printf '\r\e[30m%s\e[m' "$1"
# Default status.
else
# Draw colored bar
printf '\e[42m%*s\r\e[30m' "$COLUMNS"
# Right (must remain above Left)
printf "%${COLUMNS}s" "$((total_duration/60))/$((curr_duration/60))s "
# Left
Temp=${currently_playing##*/}
printf "\r%s\e[m" " ${Temp%.*} | $play_status | $play_mode"
fi
}
# Show players status.
# status_bar_(){
# # Set position.
# (( $# > 0 )) && printf '\e[m\e[%s;0H\e[K' "$1" || printf '\e[m\e[%s;0H\e[K' "$LINES"
#
# # Show custom text.
# if (( $# > 1 )); then
# echo -en "$2\e[m"
#
# # Default status.
# else
# # Draw colored bar
# printf '\e[42m%*s\r' "$COLUMNS"
#
# # Text color
# printf '\e[30m'
# echo -n "$selected_index/$Total (${total_duration}/${curr_duration})ms $play_status $play_mode ${queue_index}QI ${currently_playing_index}LP"
# echo -en "\e[m"
# fi
# }
# Shows the Progress of currently playing beat.
progress_bar_(){
PlayProgress="$(bc -l <<<"$curr_duration/( $total_duration/$COLUMNS )" 2>/dev/null)"
PlayProgress=${PlayProgress%%.*}
local Temp='\e[K\e[1;31m' # Color Bar
for (( i=0; i++<${PlayProgress:-0}; )); do Temp+="─"; done; Temp+='\e[0m'
for (( ;i++<=$COLUMNS; )); do Temp+="─"; done
printf '%s\n' "$(echo -en "$Temp")"
}
# Play given beat.
ffplay_play_(){
# Kill Previously playing beat, if any.
kill_beat_ "$player_pid"
# If file is not a music file, Don't do anything.
# FixIt: Make this check run while scaning/searching for music
case "$(file -b --mime-type "$1")" in
'video/mp4' | 'audio/mpeg')
# Play the beat and parse the output(continue in background).
ffplay -nodisp -autoexit -loglevel info -hide_banner $2 "$1" 2>&1 | ffplay_parse_ &
# Get PID of new ffplay.
player_pid="$!"
# Pause until Done signal is sent from ffplay_parse_ function.
[[ "$(< "$DATA_FILE4")" == 'Done' ]]
update_data_ PI "$(echo "$player_pid")"
# Update currently_playing
update_data_ LP "$1"
# Update currently_playing_index
get_lastplayed_index_
update_data_ LI "$currently_playing_index"
# update Status
update_data_ ST "PLAYING"
;;
*)
if [[ -e "$1" ]]; then
status_bar_ "$LINES" "Not a Music file, or is it?"
else
status_bar_ "$LINES" "Beat don't exist anymore."
fi
sleep 0.5
;;
esac
}
ffplay_parse_(){
# Once-time parse.
local LINES Words
while read -s LINES; do
Words=($LINES)
# Get beats playing duration.
if [[ "${Words[0]}" == "Duration:" ]]; then
IFS=: read -a Duration <<< "${Words[1]//,/}"
elif [[ "${Words[0]}" == "Stream" ]]; then
break
fi
done
# If last Word don't match that means there is an error in playing.
[[ "${Words[0]}" == 'Stream' ]] && {
# Update Duration data.
update_data_ DH "${Duration[0]}" # Duration Hour.
update_data_ DM "${Duration[1]}" # Duration Minute.
update_data_ DS "${Duration[2]%%.*}" # Duration Second.
update_data_ DO "${Duration[2]##*.}" # Duration MilliSecond.
update_data_ TD "$(( (${Duration[0]} * 316000) + \
(${Duration[1]} * 3600 ) + \
(${Duration[2]%%.*} * 60) + \
(${Duration[2]##*.} ) )) " # Total Duration in MilliSecond.
}
# Continue player(send signal to ffplay_play_ function).
echo 'Done' > "$DATA_FILE4"
# Loop Until numbers(Current Duration) appears.
while read -r -d $'\r' Index; do
[[ "${Index%%.*}" == [0-9]* ]] && break
done
# Loop Parse(Loop until Music is Stops/Ends).
local Durr
while read -r -d $'\r' Index; do
Durr="${Index%% *}"
echo -en "$(bc <<< "(${Durr%%.*} * 60) + ${Durr##*.}" )" > "$DATA_FILE2" # Convert into MilliScond.
done
echo -en "-1" > "$DATA_FILE2" # When done playing.
}
# Handle fuctions related to data.
reset_data_(){
# Reset data file(empty).
# Reset 1nd data file.
printf '' >$DATA_FILE
# Reset 2nd data file.
printf '' >$DATA_FILE2
# unset curr_duration total_duration 2>/dev/null
# unset duration_hour duration_minutes duration_seconds 2>/dev/null
# Don't unset player_pid and currently_playing as, they are
# needed for misc tasks(kill ffplay) and for next start.
}
create_data_(){
# Creates data file.
# Contains variables state.
DATA_FILE="/tmp/Beats-$BEATS_VERSION/beats.data"
# Contains current duration.
DATA_FILE2="/tmp/Beats-$BEATS_VERSION/currdur"
# Contains Queue list in sequence.
DATA_FILE3="/tmp/Beats-$BEATS_VERSION/queuelist"
# FIFO file to communicate b/w parallel running functions.
DATA_FILE4="/tmp/Beats-$BEATS_VERSION/msgfifo"
mkdir -p "${DATA_FILE%/*}" \
"${DATA_FILE2%/*}" \
"${DATA_FILE4%/*}" \
"${DATA_FILE3%/*}"
touch "$DATA_FILE" \
"$DATA_FILE2" \
"$DATA_FILE3"
mkfifo "$DATA_FILE4" 2>/dev/null
[[ "$(< "$DATA_FILE")" == "" ]] && {
echo -e "currently_playing=" > "$DATA_FILE"
echo -e "currently_playing_index=" >>"$DATA_FILE"
echo -e "player_pid=" >>"$DATA_FILE"
echo -e "play_status=STOPPED" >>"$DATA_FILE"
echo -e "duration_hour=0" >>"$DATA_FILE"
echo -e "duration_minutes=0" >>"$DATA_FILE"
echo -e "duration_seconds=0" >>"$DATA_FILE"
echo -e "total_duration=0" >>"$DATA_FILE"
echo -e "queue_index=0" >>"$DATA_FILE"
}
[[ "$(< "$DATA_FILE2")" == "" ]] && {
# curr_duration
echo "0" > "$DATA_FILE2"
}
}
# Source data files.
source_data_(){
# Source DATA_FILE
for (( i=0; i++<20; )); do
[[ "$(< "$DATA_FILE")" == "" ]] && {
continue
sleep 0.05
}
source "$DATA_FILE"
break
done
# Source current duration
for (( i=0; i++<20; )); do
curr_duration="$(< $DATA_FILE2)"
[[ "$curr_duration" == "" ]] && {
continue
sleep 0.05
}
break
done
# Source queue list.
unset line i
while IFS= read -sr line; do
Queue_Name[$i]="$line"
((i++))
done < "$DATA_FILE3"
}
update_data_(){
case "$1" in
# CD = Current Duration
'CD')
echo "${2:-0}" > "$DATA_FILE2"
;;
# QU = Queue List
'QU')
printf '' > "$DATA_FILE3"
printf '%s\n' "${Queue_Name[@]}" >> "${DATA_FILE3}"
# Print queue list line by line in sequence.
#for i in "${!Queue_Name[@]}"; do
# echo "${Queue_Name[$i]}" >> "$DATA_FILE3"
# #echo "${Queue_Name[$i]}"
#done
;;
# Remaining.
*)
case "$1" in
esac
echo "$Temp" > "$DATA_FILE"
# Source the modified data
source_data_
esac
}
save_on_exit_(){
# LP = currently_playing
echo "currently_playing=\"${2}\"
\ player_pid=\"${2}\"
queue_index=${2:-'0'}" > "$DATA_FILE"
}
short_(){
echo
}
# Create a queue list
create_queue_(){
# Create a fresh queue list(refrence from available beats).
Temp="${1:-0}"
unset Queue_Name
# Store from [Selected - Total] beats.
for (( i=$Temp; i<$Total; i++ )); do
Queue_Name[$i]="${Beat_Name[$i]}"
done
# Store from [0 - Selected] beats.
(( $Temp != 0 )) && {
for (( i=0; i<$((Temp-1)); i++ )); do
Queue_Name[$i]="${Beat_Name[$i]}"
done
}
update_data_ QU
}
# Remove a element from queue list.
queue_remove_(){
unset Queue_Name[${1:-0}]
Queue_Name=("${Queue_Name[@]}")
}
# Add a element in the queue list.
queue_add_(){
local Temp=${1:-0}
Queue_Name+=("${Queue_Name[$Temp]}")
}
# Swap the possition of two beats.
queue_swap_(){
(( $# >= 2)) && {
# Don't swap if beats not exist.
[[ -z "${Queue_Name[$1]}" && -z "${Queue_Name[$2]}" ]] || {
local Temp="${Queue_Name[$2]}"
Queue_Name[$2]="${Queue_Name[$1]}"
Queue_Name[$1]="$Temp"
}
}
}
# Refresh the player.
refresh_(){
get_term_size_
# Don't fetch beats name if
[[ "$searched" != 'true' && "$queue_showed" != 'true' ]] && get_beats_name_
setup_tui_
}
# Fetch array-index of last played beat.
get_lastplayed_index_(){
for (( i=0; i<$Total;i++ )); do
if [[ "${Beat_Name[$i]}" == "${currently_playing}" ]]; then
currently_playing_index="$i"
break
fi
done
# If not found.
currently_playing_index=${currently_playing_index:-'0'}
update_data_ LI "$currently_playing_index"
}
main_screen_(){
initialize_
# Reset cursor to the top left edge.
printf '\e[0;0H'
list_beats_ "$((LINES - 2))" # -2 for the lines reserved by bars
progress_bar_
# Dont change its position (last module must be not produce newline)
status_bar_
local input="$(input_handler_ '-t 0.9')"
case "$input" in
# Play selected beat.
'ENTER')
case "${play_status^^}" in
'PLAYING')
ffplay_play_ "${Beat_Name[$selected_index]}"
;;
'PAUSED') ffplay_play_ "${Beat_Name[$selected_index]}"
;;
'STOPPED')
ffplay_play_ "${Beat_Name[$selected_index]}"
;;
esac
# Set queue index.
queue_index="$selected_index"
update_data_ QI "$queue_index"
update_data_ QU "$queue_index"
currently_playing_index=$selected_index
;;
# Pause/resume beat.
'SPACE')
case "${play_status^^}" in
'PLAYING')
kill $player_pid 2>/dev/null
play_status='PAUSED'
;;
'PAUSED')
ffplay_play_ "$currently_playing" "-ss $((curr_duration/60))"
;;
'STOPPED')
if (( $curr_duration == 0 )); then
ffplay_play_ "${Beat_Name[$selected_index]}"
queue_index="$selected_index"
update_data_ QI "$queue_index"
currently_playing_index=$selected_index
else
ffplay_play_ "$currently_playing" "-ss $((curr_duration/60))"
fi
;;
esac
;;
# Move selection up.
'UP')
initialize_listSE_ "$((selected_index-1))"
;;
# Move selection down.
'DOWN')
initialize_listSE_ "$((selected_index+1))"
;;
'LEFT')
currently_playing_index=$((--currently_playing_index))
(( $currently_playing_index < 0 )) && currently_playing_index=0
ffplay_play_ "${Beat_Name[$currently_playing_index]}"
;;
'RIGHT')
currently_playing_index=$((++currently_playing_index))
(( $currently_playing_index > $((Total-1)) )) && currently_playing_index=$((Total-1))
ffplay_play_ "${Beat_Name[$currently_playing_index]}"
;;
# Queue Screen.
'2')
#get_queue_beats_
screen='QUEUE'
setup_tui_
;;
# Add selected song to Queue.
'c')
add_queue_ "$selected_index"
;;
# Remove selected song from Queue.
'd')
## FixIt: make the delection function do reverse check
# because the newely added song will be at the last
remove_queue_ "$selected_index"
;;
# Run commands.
":")
local output input
while :; do
status_bar_ ":$output"
read -t 1 -rs -N 1 input
case "$input" in
# Enter.
$'\n') break ;;
# Escape.
$'\e') output= ; break ;;
# Backspace
$'\177'|$'\b') output="${output%?}" ;;
# remaing non-special char
[[:print:]]) output="$output$input" ;;
esac
done
# Jump
if [[ "$output" =~ ^[0-9]+$ ]]; then
initialize_listSE_ "$((output-1))"
# Remaining commands
else
common_commands_ "$output"
fi
;;
# Go to Previous Screen
'ESCAPE')
if [[ "$searched" == 'true' ]]; then
get_beats_name_
setup_tui_
else
refresh_
fi
;;
# Remaining commands
*)
common_commands_ "$input"
;;
esac
}
list_queue_beats_(){
# $1 - Number of beats to show
[[ $1 ]] && ListEnd=$((ListStart + $1)) || ListEnd=$((ListStart+LINES))
# List limited beats that can fit on screen.
for (( i=$ListStart;i<$ListEnd;i++ )); do
# If exceed the limit then display empty line.
if (( $i < 0 || $i > $((Total-1)) )); then
local Temp="$i"
else
local Temp="${Queue_Name[$i]%.*}"
Temp="${Temp##*/}"
fi
# Colour selected_index.
if [[ $selected_index -eq $i ]]; then
echo -en "$COL2${Temp^^}$COLR\e[K\n"
# Remaining beats.
else
echo -en "$Temp\e[K\n"
fi
done
}
common_commands_(){
case "$1" in
'[' | ']')
xset r rate 1 # Disable Repeat key rate
esac
case "$1" in
# Seek Backward.
'[')
if [[ "${play_status^^}" == 'PLAYING' ]]; then
kill_beat_ "$player_pid"
play_status='PLAYING'
fi
while :; do
curr_duration="$((curr_duration - seek_interval))"
progress_bar_ "$((LINES-1))"
read -t 0.1 -d'[' 2>/dev/null || break
done
# Dont let curr_duration go below 0.
if (( $curr_duration < 0 )); then
update_data_ CD "0"
curr_duration="0"
else
update_data_ CD "$curr_duration" # Convert into Seconds.
fi
# Continue Playing if already playing.
[[ "${play_status^^}" == 'PLAYING' ]] && ffplay_play_ "$currently_playing" "-ss $((curr_duration/60))"
;;
# Seek Forward.
']')
# Kill if already playing
if [[ "${play_status^^}" == 'PLAYING' ]]; then
kill_beat_ "$player_pid"
play_status='PLAYING'
fi
while :; do
curr_duration="$((curr_duration + seek_interval))"
progress_bar_ "$((LINES-1))"
read -t 0.1 -d']' 2>/dev/null || break
done
# Dont let curr_duration go above total_duration.
if (( $curr_duration > $total_duration )); then
update_data_ CD "$total_duration"
curr_duration="$total_duration"
else
update_data_ CD "$curr_duration" # Convert into Seconds.
fi
# Continue Playing if already playing.
[[ "${play_status^^}" == 'PLAYING' ]] && ffplay_play_ "$currently_playing" "-ss $((curr_duration/60))"
;;
# Exit (dont Stop Music).
'q')
exit
;;
# Exit and Stop Music.
'Q')
dont_kill_player=true
exit
;;
# Refresh.
'r')
refresh_
;;
# Stops Music.
'x')
kill_beat_