Class: MultimediaParadise::Audio::AudioPlayer

Inherits:
MultimediaParadise::AudioBase show all
Defined in:
lib/multimedia_paradise/audio/audio_player/audio_player.rb

Overview

MultimediaParadise::Audio::AudioPlayer

Constant Summary collapse

MY_SONGS =
#

MY_SONGS

#
AUDIO_DIRECTORY
AUDIO_DIR =

AUDIO_DIR

MY_SONGS
RENAME_KDE_KONSOLE_TAB =
#

RENAME_KDE_KONSOLE_TAB

#
true
DEFAULT_DELAY_PLAYING_SONGS =
#

DEFAULT_DELAY_PLAYING_SONGS

The following constant is used for determining the audio delay between two songs.

A value of 0.0 would mean no delay, aka “instant play”, logically.

#
1.8
MAIN_AUDIO_PLAYER_TO_USE =

We default to mpv since as of 2021.

'mpv'
LAST_SONG_PLAYED =
#

LAST_SONG_PLAYED

#
'LAST_SONG_PLAYED.md'
USE_OSS_FOR_AUDIO_PLAYER =
#

USE_OSS_FOR_AUDIO_PLAYER

#
false
FULL_LOCATION_FOR_LAST_SONG_PLAYED =
#

FULL_LOCATION_FOR_LAST_SONG_PLAYED

#
HOME+LAST_SONG_PLAYED
MAXIMUM_ALLOWED_FILE_SIZE =
#

MAXIMUM_ALLOWED_FILE_SIZE

#
1_350_000
DEFAULT_PLAYMODE =
#

DEFAULT_PLAYMODE

#
'once'
NAME_OF_AUDIO_PLAYER =
#

NAME_OF_AUDIO_PLAYER

#
'AudioPlayer'
PLAY_MAX_N_SONGS =
#

PLAY_MAX_N_SONGS

#
1250
USE_FAVOURITE_PLAYLIST =
#

USE_FAVOURITE_PLAYLIST

#
false
AVAILABLE_SONGS =
#

AVAILABLE_SONGS

Obtain all available songs - and shuffle them a bit.

#
Dir["#{MY_SONGS}*"].shuffle
SHALL_WE_DEBUG =
#

SHALL_WE_DEBUG

#
false
PLAYLIST_CONSTANTS =
PlaylistProfile.constants
THE_LOG_FILE_IS_STORED_HERE =
#

THE_LOG_FILE_IS_STORED_HERE

#
"#{MultimediaParadise.log_dir?}"\
"log_file_for_played_songs_via_the_audio_player.md"

Constants inherited from Base

Base::ERROR, Base::ERROR_LINE, Base::NAMESPACE, Base::USE_THIS_NAMESPACE_FOR_THE_COLOURS, Base::USE_THIS_NAMESPACE_FOR_THE_CORE_COLOURS

Instance Method Summary collapse

Methods inherited from MultimediaParadise::AudioBase

#directory_to_the_local_songs?, #return_random_audio_file

Methods inherited from Base

[], #actions, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #cartoons_directory?, #clear_the_internal_hash, #cliner, #cliner_with_time_stamp, #colourized_comment, #copy_file, #crimson, #dataset_from_file_video_collection, #dd_mm_yyyy, #debug?, #default_readlines, #directory_to_realvids?, #do_not_use_opn, #dodgerblue, #does_the_video_player_support_this_commandline?, #e, #ecomment, #ecrimson, #efancy, #enable_debug, #ensure_main_encoding, #ensure_that_the_output_directory_exists, #eparse, #erev, #esystem, #ewarn, #file_video_collection?, #filter_for_audio_files, #filter_for_video_files, #forestgreen, #gold, #grey, #hh_mm_ss, #home_x_video?, #indianred, #infer_the_namespace, #internal_hash?, #is_audio_file?, #is_mkv?, #is_mp3?, #is_mp4?, #is_multimedia_file?, #is_on_roebe?, #is_video_file?, #konsole_colour_peru, #lightblue, #lightgreen, #load_yaml, #local_audio_directory?, #log_dir?, #map_symbol_to_locally_existing_file, #mediumorchid, #mediumpurple, #mediumslateblue, #mkdir, #move_file, #namespace?, #no_file_exists, #no_file_exists_at, #olive, #olivedrab, #opne, #opnecomment, #opnn, #orange, #palegoldenrod, #palegreen, #powderblue, #project_base_directory?, #rds, #remove_file, #report_pwd, #reset_the_internal_hash, #return_all_video_files, #return_pwd, #return_random_video, #rev, #royalblue, #save_what_into, #sdir, #seagreen, #seconds_to_time_format, #select_only_video_files_from, #set_be_verbose, #set_use_colours, #sfancy, #sfile, #simp, #skyblue, #slateblue, #springgreen, #steelblue, #swarn, #teal, #time_right_now, #to_hh_mm_ss, #tomato, #true_rev, #try_to_rename_kde_konsole_tab, #ucliner, #use_colours?, #use_opn?, #use_which_video_player?, #verbose_truth, #video_collection?, #violet, #yaml_directory?, #yel

Methods included from CommandlineArgumentsModule

#all_input_starts_with_a_number?, #commandline_arguments?, #commandline_arguments_as_a_string, #first_argument?, #first_non_hyphened_commandline_argument?, #set_commandline_arguments

Constructor Details

#initialize(i = nil, run_already = true) ⇒ AudioPlayer

#

initialize

#


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
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 176

def initialize(
    i           = nil,
    run_already = true
  )
  register_sigint { :pretty }
  reset
  if i
    i = set_commandline_options_and_return_the_other_options(i)
    menu(i)
  end
  case run_already
  # ======================================================================= #
  # === :default
  # ======================================================================= #
  when :default
    run_already = true
  # ======================================================================= #
  # === :dont_run_yet
  # ======================================================================= #
  when :dont_run_yet,
       :do_not_run_yet
    run_already = false
  end
  run if run_already
end

Instance Method Details

#add_all_songsObject Also known as: play_all_songs

#

add_all_songs

#


532
533
534
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 532

def add_all_songs
  add(all_songs?)
end

#add_random_songObject

#

add_random_song

This will add exactly one random song.

#


431
432
433
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 431

def add_random_song
  add_song(return_random_song)
end

#add_song(i = nil) ⇒ Object Also known as: add, add_to_playlist, add_this_song_to_playlist, add_this_song, add_songs

#

add_song (add tag)

This method will add the particular song to the playlist.

It will check whether it is an multimedia-file prior to adding it, though.

#


640
641
642
643
644
645
646
647
648
649
650
651
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 640

def add_song(
    i = nil
  )
  unless i.nil?
    [i].flatten.compact.each {|this_song|
      if is_a_multimedia_file?(this_song)
        @array_play_these_songs << this_song
      end
    }
    sanitize_songs
  end
end

#audio_dir?Boolean

#

audio_dir?

#

Returns:

  • (Boolean)


507
508
509
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 507

def audio_dir?
  AUDIO_DIR
end

#audio_player_to_use?Boolean Also known as: audio_player?, use_which_audio_player?

#

audio_player_to_use?

#

Returns:

  • (Boolean)


817
818
819
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 817

def audio_player_to_use?
  @audio_player_to_use
end

#check_whether_the_song_directory_is_emptyObject

#

check_whether_the_song_directory_is_empty

#


558
559
560
561
562
563
564
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 558

def check_whether_the_song_directory_is_empty
  if return_all_songs.empty?
    opne 'No songs found in `'+sdir(audio_dir?)+'`.'
    opne 'Perhaps you forgot to copy some songs first?'
    exit_program(:force_exit)
  end
end

#consider_renaming_the_kde_konsole_tab(i) ⇒ Object

#

consider_renaming_the_kde_konsole_tab

#


929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 929

def consider_renaming_the_kde_konsole_tab(i)
  # ======================================================================= #
  # === Rename the KDE Konsole tab here
  # ======================================================================= #
  if Object.const_defined? :Roebe
    begin
      i = File.basename(i).
          gsub(/#{File.extname(i)}$/,'').
          tr('_',' ')
    # ===================================================================== #
    # Must rescue this, in case of errors such as:
    #   invalid byte sequence in UTF-8
    # ===================================================================== #
    rescue ArgumentError => error
      opne 'An error happened (ArgumentError) which is now displayed:'
      pp error
    end
    Roebe.rename_konsole(i)
  end if RENAME_KDE_KONSOLE_TAB
end

#consider_resetting_kde_konsole_tab(use_this_as_the_new_title = '.') ⇒ Object

#

consider_resetting_kde_konsole_tab

This method can be used to re-set the KDE konsole tab name.

#


674
675
676
677
678
679
680
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 674

def consider_resetting_kde_konsole_tab(
    use_this_as_the_new_title = '.'
  )
  do_rename_konsole(
    use_this_as_the_new_title, RENAME_KDE_KONSOLE_TAB
  )
end

#delay?Boolean

#

delay?

#

Returns:

  • (Boolean)


825
826
827
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 825

def delay?
  @internal_hash[:delay].to_s
end

#determine_which_audio_player_to_use(i = @main_audio_player_to_use, overrule_if_environment_variable_was_set = true) ⇒ Object

#

determine_which_audio_player_to_use

This method will determine which audio play is to be used, for playing the audio files. Currently the two primary ones that I use are mplayer and mpv. Note that the environment variable MAIN_AUDIO_PLAYER can overrule this completely, if set.

#


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
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 277

def determine_which_audio_player_to_use(
    i                                        = @main_audio_player_to_use,
    overrule_if_environment_variable_was_set = true
  )
  case overrule_if_environment_variable_was_set
  # ======================================================================= #
  # === :ignore_environment_variable
  # ======================================================================= #
  when :ignore_environment_variable
    overrule_if_environment_variable_was_set = false
  end
  case i
  # ======================================================================= #
  # === sox
  # ======================================================================= #
  when 'sox',
       :sox
    i = 'play' # The binary from sox is actually called "play", hence this line.
  end
  if overrule_if_environment_variable_was_set
    if ENV.has_key? 'MAIN_AUDIO_PLAYER'
      i = ENV['MAIN_AUDIO_PLAYER'].to_s.dup
    end
  end
  @audio_player_to_use = i.to_s # Which audio player to use.
end

#do_play_in_a_loopObject

#

do_play_in_a_loop

#


445
446
447
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 445

def do_play_in_a_loop
  @play_in_a_loop = true
end

#do_rename_konsole(i = '.', *optional_arguments_that_are_irrelevant) ⇒ Object

#

do_rename_konsole

#


768
769
770
771
772
773
774
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 768

def do_rename_konsole(
    i = '.', *optional_arguments_that_are_irrelevant
  )
  if Object.const_defined? :Roebe
    Roebe.rename_konsole(i)
  end
end

#exit_program(shall_we_exit = shall_we_exit?, , be_verbose = true, &block) ⇒ Object

#

exit_program

This method can be used to exit this program.

#


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
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 571

def exit_program(
    shall_we_exit = shall_we_exit?,
    be_verbose    = true,
    &block
  )
   
  case shall_we_exit
  # ======================================================================= #
  # === :force_exit
  # ======================================================================= #
  when :force_exit
    shall_we_exit = true
  # ======================================================================= #
  # === :force_exit_but_be_quiet
  # ======================================================================= #
  when :force_exit_but_be_quiet
    shall_we_exit = true
    be_verbose = false
  end
  # ======================================================================= #
  # === Handle blocks next
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :be_quiet
    # ===================================================================== #
    when :be_quiet
      be_verbose = false
    end
  end
  if shall_we_exit
    @mrxvt.set_title('') if @mrxvt
    if be_verbose
      efancy 'Leaving ...'
      efancy 'Hopefully you had fun and found class'\
             'MultimediaParadise::Audio::AudioPlayer useful. Bye!'
    end
    consider_resetting_kde_konsole_tab
    exit
  else
    e "Won't exit as we are not allowed to exit."
  end
end

#get_file_size(of_this_file = the_songs_played_will_be_stored_where? ) ⇒ Object

#

get_file_size

#


372
373
374
375
376
377
378
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 372

def get_file_size(
    of_this_file = the_songs_played_will_be_stored_where?
  )
  size = 0
  size = File.size(of_this_file) if File.exist?(of_this_file)
  size
end

#input?Boolean Also known as: play_these_songs, playlist?

#

input?

#

Returns:

  • (Boolean)


620
621
622
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 620

def input?
  @array_play_these_songs
end

#main_colour?Boolean Also known as: mcol?

#

main_colour?

#

Returns:

  • (Boolean)


471
472
473
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 471

def main_colour?
  @main_colour
end
#

menu (menu tag)

#


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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 953

def menu(i)
  if i.is_a? Array
    if i.empty?
      i = 'loop'
    else
      i = i.join(' ').strip
    end
  end
  i = i.to_s.strip
  if i =~ /^\d+$/ # If input consists of only numbers.
    opne "#{rev}We will next play #{simp(i.to_s)} #{rev}songs."
    i.to_i.times { add_random_song }
  # ======================================================================= #
  # === 11:22:33
  #
  # Handle time-formats next.
  # ======================================================================= #
  elsif i =~ /^\d{1,2}:\d{1,2}:\d{1,2}$/ # If input consists of only numbers and ':'.
    # ===================================================================== #
    # User did input a time format such as "11:22:33". We will assume this
    # to mean to enter idle mode, and then play the songs via a loop.
    # ===================================================================== #
    begin
      require 'roebe/classes/at.rb'
      Roebe::At.new(i)
      do_play_in_a_loop # We play in a loop in this case.
    rescue LoadError
      e 'The file roebe/classes/at.rb is not available.'
    end
  else
    case i # case tag
    # ===================================================================== #
    # === once
    # ===================================================================== #
    when 'once','noloop','default','all', # This means we play every song exactly once, then quit.
         '','true','false',
         'all_songs',
         'play_all_songs'
      opne 'We will play all available Audio files once.'
      add_all_songs
    # ===================================================================== #
    # === help
    #
    # This entry point can be used to show the help section.
    # ===================================================================== #
    when *ARRAY_HELP_OPTIONS
      show_help :then_exit
    # ===================================================================== #
    # === rsong --last-songs?
    # ===================================================================== #
    when /^-?-?last(-|_)?songs\??$/i
      show_the_last_songs_that_were_played
      exit_program { :be_quiet }
    # ===================================================================== #
    # === --use-sox
    # ===================================================================== #
    when /^-?-?use(-|_)?sox/
      determine_which_audio_player_to_use(
        :sox, :ignore_environment_variable
      )
    # ===================================================================== #
    # === --use-mplayer
    # ===================================================================== #
    when /^-?-?use(-|_)?mplayer/
      determine_which_audio_player_to_use(
        :mplayer, :ignore_environment_variable
      )
    # ===================================================================== #
    # === rsong --log?
    # ===================================================================== #
    when /^-?-?log\??$/i
      show_the_log_file_if_it_exists
      exit_program(:force_exit_but_be_quiet)
    # ===================================================================== #
    # === OPEN
    # ===================================================================== #
    when /^-?-?OPEN$/i
      esystem "bluefish #{__FILE__}"
      exit_program(:force_exit)
    # ===================================================================== #
    # === rsong --player?
    # ===================================================================== #
    when /^-?-?player\??$/
      report_which_audio_player_to_use
      exit_program { :be_quiet }
    # ===================================================================== #
    # === random
    # ===================================================================== #
    when 'random',
         'return_random_song',
         'rand',
         'one' # Only add one song.
      add_random_song
    # ===================================================================== #
    # === chill
    # ===================================================================== #
    when 'list','chill','1'
      if Object.const_defined? :PlaylistProfile
        these_songs = PlaylistProfile::GOOD_CHILL_SONGS.map {|entry|
          audio_dir?+entry
        }
        add(these_songs.shuffle)
      end
    # ===================================================================== #
    # === playlist
    # ===================================================================== #
    when /playlist\??/i
      try_to_play_playlist
    # ===================================================================== #
    # === last?
    # ===================================================================== #
    when 'last?'
      show_last_file_played
    # ===================================================================== #
    # === dance
    # ===================================================================== #
    when 'dance','bdance','breakdance' # Im Moment hardcoded auf bdance
      these_songs = PlaylistProfile::GOOD_BDANCE_SONGS.shuffle.map {|entry|
        audio_dir?+entry
      }
      add(these_songs.shuffle)
    # ===================================================================== #
    # === loop
    # ===================================================================== #
    when 'loop','l','nil'
      # Notify the user what we are doing next.
      opne "#{main_colour?}Playing in a neverending #{sfancy('loop')}"\
           "#{main_colour?} next:"
      do_play_in_a_loop
    # ===================================================================== #
    # === genre
    # ===================================================================== #
    when 'genre','GENRES','genres','GENRE'
      unless ARGV[1]
        show_available_genres
        exit_program(:force_exit)
      end
    else # else tag
      # =================================================================== #
      # Grab all entries from the current directory that can match to the
      # input.
      # =================================================================== #
      entries = Dir['*'+i+'*']
      unless entries.empty?
        add_songs(entries)
      else
        try_to_find_genre_song(i)
      end
    end
  end
end

#obtain_file_duration_from_this_file(i) ⇒ Object

#

obtain_file_duration_from_this_file(i)

#


169
170
171
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 169

def obtain_file_duration_from_this_file(i)
  action(:FileDuration, i)
end

#pause(n_seconds = delay? ) ⇒ Object

#

pause (pause tag)

Pause for n_seconds (the first input to this method).

This method will NOT seet a new, random song - for this, you need to use another method.

#


709
710
711
712
713
714
715
716
717
718
719
720
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 709

def pause(
    n_seconds = delay?
  )
  n_seconds = n_seconds.to_f # Always keep it as a float here.
  cliner
  e "#{true_rev}We will pause for #{sfancy(n_seconds)} #{rev}seconds."
  cliner_with_time_stamp
  e "#{rev}The next song will be: "\
    "#{sfancy(playlist?[@n_songs_played])}"
  cliner
  sleep(n_seconds)
end

#perform_cleanupObject

#

perform_cleanup

We get rid of @ivars which are not too useful.

#


551
552
553
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 551

def perform_cleanup
  remove_instance_variable :@available_genres
end

#play_the_playlist(playlist = playlist? ) ⇒ Object

#

play_the_playlist

#


857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 857

def play_the_playlist(
    playlist = playlist?
  )
  if @play_in_a_loop # We wish to play in a never-ending loop.
    counter = 0
    loop {
      playlist.each {|song|
        play_this_song(song)
        pause unless @n_songs_played == playlist.size
        counter += 1
        break if counter > @play_max_n_songs
      }
      refresh_playlist
    }
  else
    playlist.each {|song|
      play_this_song(song)
      pause unless @n_songs_played == playlist.size
    } # Afterwards, we will have exited.
  end
end

#play_this_song(i, be_verbose = true) ⇒ Object

#

play_this_song (play tag)

This is the actual method that will play the song.

We will also append the filename last played.

#


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
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 729

def play_this_song(
    i, be_verbose = true
  )
  if be_verbose 
    e; opne 'The next song we will play via '+
             simp(audio_player_to_use?)+"#{rev} is:"
    e "#{N}   #{sfancy(i)}#{N}#{N}"
  end
  # ======================================================================= #
  # Next, act on .mp3 files.
  # ======================================================================= #
  if File.extname(i) == '.mp3'
    begin
      @file_duration_in_seconds = obtain_file_duration_from_this_file(i)
      if be_verbose
        e "#{rev}The length of this song is: "+
          sfancy(@file_duration_in_seconds.to_i)+rev+' seconds.'
      end
    rescue Exception => error
      e error
    end
  end
  e "  #{sfile(i)}" if be_verbose
  consider_renaming_the_kde_konsole_tab(i)
  _ = audio_player?.dup
  _ << ' -ao oss' if @use_oss_for_audio_player
  _ << ' '+i.dup
  # ======================================================================= #
  # Register which song was played. We do this before we actually play
  # the song.
  # ======================================================================= #
  register_which_song_will_be_played_next(i)
  esystem _
  @n_songs_played += 1
end

#populate_hash_songs_playedObject

#

populate_hash_songs_played

Fills up our hash with available data for the first time. Basically right now, only manipulates @hash_songs_played

#


352
353
354
355
356
357
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 352

def populate_hash_songs_played
  playlist?.each { |song|
    song = File.basename(song)
    @hash_songs_played[song] = 0
  }
end

#refresh_playlistObject

#

refresh_playlist

First clears the playlist, then adds all available songs.

#


541
542
543
544
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 541

def refresh_playlist
  clear_playlist
  add_all_songs
end

#register_sigintObject

#

register_sigint

Will act upon SIGINT recieved, and properly shut down.

#


662
663
664
665
666
667
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 662

def register_sigint
  Signal.trap('SIGINT') {
    consider_resetting_kde_konsole_tab
    exit_program(true)
  }
end

#register_this_song(this_song) ⇒ Object

#

register_this_song

This method should be invoked whenever you want to play a new song.

We will only keep the filename, not the full location.

#


518
519
520
521
522
523
524
525
526
527
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 518

def register_this_song(this_song)
  if @hash_songs_played
    this_song = File.basename(this_song)
    if @hash_songs_played.has_key? this_song
      @hash_songs_played[this_song] = @hash_songs_played[this_song] + 1
    else
      @hash_songs_played[this_song] = 1 # Initialize at 1 then.
    end
  end
end

#register_which_song_was_last_played(this_song, store_where_to = the_songs_played_will_be_stored_where? ) ⇒ Object Also known as: register_which_song_will_be_played_next

#

register_which_song_was_last_played

This method will also check if the file that is being played is too big. If it is, we will remove the log-file.

#


885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 885

def register_which_song_was_last_played(
    this_song,
    store_where_to = the_songs_played_will_be_stored_where?
  )
  $stdout.flush # Unsure why we wish to do this.
  FileUtils.touch(store_where_to) unless File.exist?(store_where_to)
  if get_file_size(store_where_to) > MAXIMUM_ALLOWED_FILE_SIZE
    remove_file(store_where_to)
  end
  unless store_where_to.include? '/'
    store_where_to = HOME_DIRECTORY+store_where_to
  end # store_where_to now defaults to: /root/LAST_SONG_PLAYED
  append_what_into(this_song+N, store_where_to)
  register_this_song(this_song)
  # ======================================================================= #
  # Since as of September 2021 this class will also try to store into
  # a log file, if possible.
  # ======================================================================= #
  store_data_about_this_song_into_a_local_file(this_song)
end

#report_which_audio_player_to_useObject

#

report_which_audio_player_to_use

#


307
308
309
310
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 307

def report_which_audio_player_to_use
  opne rev+'The audio player '+sfancy(@audio_player_to_use)+
       rev+' will be used.'
end

#resetObject

#

reset (reset tag)

#


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
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 205

def reset
  super()
  clear_playlist
  set_delay # Set the delay here.
  set_shall_we_exit
  infer_the_namespace
  # ======================================================================= #
  # === :use_colours
  # ======================================================================= #
  set_use_colours # If true then we will use colours.
  # ======================================================================= #
  # === @main_colour
  # ======================================================================= #
  @main_colour = Colours.parse('green')
  # ======================================================================= #
  # === @play_in_a_loop
  # ======================================================================= #
  @play_in_a_loop = false
  determine_which_audio_player_to_use
  # ======================================================================= #
  # === :debug
  # ======================================================================= #
  @internal_hash[:debug] = SHALL_WE_DEBUG
  # ======================================================================= #
  # === @hash_songs_played
  # ======================================================================= #
  @hash_songs_played = {} # We keep track of all the files that we played.
  # ======================================================================= #
  # === @play_max_n_songs
  # ======================================================================= #
  @play_max_n_songs = PLAY_MAX_N_SONGS # Safeguard.
  if Object.const_defined? :MrxvtOptions
    @mrxvt = MrxvtOptions.new
  else
    @mrxvt = nil
  end
  # ======================================================================= #
  # === @main_audio_player_to_use
  # ======================================================================= #
  @main_audio_player_to_use = MAIN_AUDIO_PLAYER_TO_USE
  # ======================================================================= #
  # === @use_oss_for_audio_player
  # ======================================================================= #
  @use_oss_for_audio_player = USE_OSS_FOR_AUDIO_PLAYER
  # ======================================================================= #
  # === @file_duration_in_seconds
  # ======================================================================= #
  @file_duration_in_seconds = 0 # Keeps the duration of the file in question.
  # ======================================================================= #
  # === @n_songs_played
  # ======================================================================= #
  @n_songs_played = 0 # How many songs we already played.
  # ======================================================================= #
  # === @available_genres
  # ======================================================================= #
  @available_genres = nil # Default.
  @available_genres = PLAYLIST_CONSTANTS.map {|song|
    song.to_s.sub(/GOOD_/,'').
              sub(/_SONGS/,'').
              sub(/MY_/,'').
              sub(/CURRENT_/,'')
  } if defined? PLAYLIST_CONSTANTS
end

#reset_playlistObject Also known as: clear_playlist

#

reset_playlist

#


500
501
502
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 500

def reset_playlist
  @array_play_these_songs = [] # Keep all files that we wish to play in this Array.
end

#return_all_songsObject Also known as: all_songs, get_all_songs, determine_available_songs, all_songs?

#

return_all_songs

By default, we will shuffle.

#


480
481
482
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 480

def return_all_songs
  return AVAILABLE_SONGS.shuffle
end

#return_random_songObject Also known as: fetch_random_song, get_random_song

#

return_random_song

This method will return one random song.

#


492
493
494
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 492

def return_random_song
  return_all_songs.first
end

#runObject

#

run (run tag)

#


1108
1109
1110
1111
1112
1113
1114
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 1108

def run
  populate_hash_songs_played
  show_startup_message
  check_whether_the_song_directory_is_empty
  play_the_playlist
  perform_cleanup
end

#sanitize_songsObject

#

sanitize_songs

#


628
629
630
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 628

def sanitize_songs
  playlist?.flatten! # Always flatten it afterwards.
end

#set_commandline_options_and_return_the_other_options(i) ⇒ Object

#

set_commandline_options_and_return_the_other_options

#


326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 326

def set_commandline_options_and_return_the_other_options(i)
  unless i.is_a? Symbol
    if i.respond_to?(:any?) and i.any? {|line|
        line.start_with? '--'
      }
      @commandline_options = i.select {|inner_line|
        inner_line.start_with? '--'
      }
      menu(@commandline_options)
      # ===================================================================== #
      # And finally, remove these entries that were found.
      # ===================================================================== #
      i.reject! {|entry|
        entry.start_with? '--'
      }
    end
  end
  i
end

#set_delay(i = DEFAULT_DELAY_PLAYING_SONGS) ⇒ Object

#

set_delay

This sets the default delay, when playing the audio songs in a loop.

#


365
366
367
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 365

def set_delay(i = DEFAULT_DELAY_PLAYING_SONGS)
  @internal_hash[:delay] = i
end

#set_shall_we_exit(i = true) ⇒ Object

#

set_shall_we_exit

#


383
384
385
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 383

def set_shall_we_exit(i = true)
  @shall_we_exit = i
end

#shall_we_exit?Boolean

#

shall_we_exit?

#

Returns:

  • (Boolean)


398
399
400
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 398

def shall_we_exit?
  @shall_we_exit
end

#show_available_genresObject

#

show_available_genres

#


390
391
392
393
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 390

def show_available_genres
  opne "#{rev}The available genres are:"
  pp @available_genres
end

#show_help(optional_shall_we_exit = false) ⇒ Object

#

show_help (help tag)

Show help here, then exit.

#


834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 834

def show_help(
    optional_shall_we_exit = false
  )
  cliner
  opne 'Showing the available help options next:'
  e
  colourized_comment '  - OPEN        # open this file in the editor'
  colourized_comment '  - loop        # play in an eternal loop'
  colourized_comment '  - rand        # play one random song'
  colourized_comment '  - once        # play all songs once, then quit'
  colourized_comment '  - last?       # show the last song played'
  colourized_comment '  - player?     # show which multimedia-player we will use'
  colourized_comment '  - log?        # show the log file of last-songs-played (if this file exists)'
  colourized_comment '  - last-songs? # show the last songs that '\
                     'were played, in a time-sorted manner'
  e
  cliner
  exit_program(true, false) if optional_shall_we_exit == :then_exit
end

#show_last_file_played(_ = the_songs_played_will_be_stored_where? ) ⇒ Object

#

show_last_file_played

This method will show the last song played.

#


407
408
409
410
411
412
413
414
415
416
417
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 407

def show_last_file_played(
    _ = the_songs_played_will_be_stored_where?
  )
  if File.exist? _
    e 'The last song played was:'
    efancy('  '+File.readlines(_)[-1])
    exit_program
  else
    ewarn 'But the file '+sfile(_)+RED+' does not exist.'
  end
end

#show_startup_messageObject

#

show_startup_message

Display a fancy startup message.

#


317
318
319
320
321
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 317

def show_startup_message
  e N+main_colour?+' => Starting Audio Player '+
    simp(use_which_audio_player?)+main_colour?+' ('+
    sfancy('Default delay: ')+delay?+mcol?+').'+N
end

#show_the_last_songs_that_were_playedObject

#

show_the_last_songs_that_were_played

The songs played will be stored in a .md file.

Invocation example:

rsong --last-songs?
#


916
917
918
919
920
921
922
923
924
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 916

def show_the_last_songs_that_were_played
  _ = the_songs_played_will_be_stored_where?
  if File.exist? _
    dataset = File.readlines(_)
    dataset.each {|this_song| e steelblue(this_song.strip) }
  else
    no_file_exists_at(_)
  end
end

#show_the_log_file_if_it_exists(log_file = THE_LOG_FILE_IS_STORED_HERE) ⇒ Object

#

show_the_log_file_if_it_exists

Invocation example:

rsong --log?
#


798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 798

def show_the_log_file_if_it_exists(
    log_file = THE_LOG_FILE_IS_STORED_HERE
  )
  if File.exist? log_file
    opne 'Displaying the content of the file at'
    opne '`'+sfile(log_file)+'`:'
    dataset = File.readlines(log_file).map {|entry| entry.rstrip } 
    e
    e mediumpurple(
        dataset.map {|entry| "  #{entry}" }.join("\n")
      )
  else
    opnn; no_file_exists_at(log_file)
  end
end

#show_the_playlistObject Also known as: show_songs

#

show_the_playlist

#


463
464
465
466
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 463

def show_the_playlist
  opne 'The playlist is:'
  pp playlist?
end

#shuffle_the_playlistObject Also known as: shuffle

#

shuffle_the_playlist

#


438
439
440
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 438

def shuffle_the_playlist
  playlist?.shuffle!
end

#store_data_about_this_song_into_a_local_file(this_song) ⇒ Object

#

store_data_about_this_song_into_a_local_file

#


779
780
781
782
783
784
785
786
787
788
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 779

def store_data_about_this_song_into_a_local_file(
    this_song
  )
  into = THE_LOG_FILE_IS_STORED_HERE
  what = "song name: #{this_song.ljust(30)}, "\
         "played on #{dd_mm_yyyy}, #{hh_mm_ss}\n"
  dirname = File.dirname(into)
  mkdir(dirname) unless File.directory? dirname
  append_what_into(what, into)
end

#the_songs_played_will_be_stored_where?Boolean

#

the_songs_played_will_be_stored_where?

#

Returns:

  • (Boolean)


422
423
424
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 422

def the_songs_played_will_be_stored_where?
  FULL_LOCATION_FOR_LAST_SONG_PLAYED
end

#try_to_find_genre_song(i) ⇒ Object

#

try_to_find_genre_song

This tries to find a (random) genre song.

#


687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 687

def try_to_find_genre_song(i)
  upcased = i.to_s.upcase
  if @available_genres.grep(/#{upcased}/)
    matches = PLAYLIST_CONSTANTS.select {|entry| entry =~ /#{upcased}/ }[0]
    if matches.nil? # Then try without playlist
      these_songs = Dir[audio_dir?+'*'+i+'*']
    end
    these_songs = PlaylistProfile.module_eval(matches.to_s).map {|entry|
      audio_dir?+entry
    } if matches
    add_song(these_songs.shuffle)
  end if @available_genres
end

#try_to_play_playlistObject

#

try_to_play_playlist

#


452
453
454
455
456
457
458
# File 'lib/multimedia_paradise/audio/audio_player/audio_player.rb', line 452

def try_to_play_playlist
  begin
    require 'x/AUD/playlist.rb'
    add(Playlist.playlist?)
    shuffle
  rescue LoadError; end
end