class MultimediaParadise::AudioPlayer
Constants
- AUDIO_DIR
- AVAILABLE_SONGS
#¶ ↑
AVAILABLE_SONGS
¶ ↑Obtain all available songs - and shuffle them a bit.
#¶ ↑
- 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.
#¶ ↑
- DEFAULT_PLAYMODE
#¶ ↑
DEFAULT_PLAYMODE
¶ ↑#¶ ↑
- FULL_LOCATION_FOR_LAST_SONG_PLAYED
#¶ ↑
FULL_LOCATION_FOR_LAST_SONG_PLAYED
¶ ↑#¶ ↑
- LAST_SONG_PLAYED
#¶ ↑
LAST_SONG_PLAYED
¶ ↑#¶ ↑
- MAIN_AUDIO_PLAYER_TO_USE
- MAXIMUM_ALLOWED_FILE_SIZE
#¶ ↑
MAXIMUM_ALLOWED_FILE_SIZE
¶ ↑# ¶ ↑
- MY_SONGS
#¶ ↑
MY_SONGS
¶ ↑#¶ ↑
- NAMESPACE
#¶ ↑
NAMESPACE
¶ ↑#¶ ↑
- NAME_OF_AUDIO_PLAYER
#¶ ↑
NAME_OF_AUDIO_PLAYER
¶ ↑#¶ ↑
- PLAYLIST_CONSTANTS
- PLAY_MAX_N_SONGS
#¶ ↑
PLAY_MAX_N_SONGS
¶ ↑#¶ ↑
- RENAME_KDE_KONSOLE_TAB
#¶ ↑
RENAME_KDE_KONSOLE_TAB
¶ ↑#¶ ↑
- SHALL_WE_DEBUG
#¶ ↑
SHALL_WE_DEBUG
¶ ↑#¶ ↑
- THE_LOG_FILE_IS_STORED_HERE
#¶ ↑
THE_LOG_FILE_IS_STORED_HERE
¶ ↑#¶ ↑
- USE_FAVOURITE_PLAYLIST
#¶ ↑
USE_FAVOURITE_PLAYLIST
¶ ↑#¶ ↑
- USE_OSS_FOR_AUDIO_PLAYER
#¶ ↑
USE_OSS_FOR_AUDIO_PLAYER
¶ ↑#¶ ↑
Public Class Methods
#¶ ↑
initialize¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 30 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 # ======================================================================= # # === :dont_run_yet # ======================================================================= # when :dont_run_yet, :do_not_run_yet run_already = false end run if run_already end
Public Instance Methods
#¶ ↑
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.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 422 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_player_to_use?¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 600 def audio_player_to_use? @audio_player_to_use end
#¶ ↑
check_whether_the_song_directory_is_empty
¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 340 def check_whether_the_song_directory_is_empty if return_all_songs.empty? opnn; e 'No songs found in `'+sdir(audio_dir?)+'`.' opnn; e 'Perhaps you forgot to copy some songs first?' exit_program(:force_exit) end end
#¶ ↑
consider_renaming_the_kde_konsole_tab
¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/tab.rb, line 14 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 opnn; e '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
¶ ↑
This method can be used to re-set the KDE konsole tab name.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 456 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
#¶ ↑
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.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 59 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
#¶ ↑
exit_program
¶ ↑
This method can be used to exit this program.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 353 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::AudioPlayer useful. Bye!' end consider_resetting_kde_konsole_tab exit else e "Won't exit as we are not allowed to exit." end end
#¶ ↑
input?¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 402 def input? @array_play_these_songs end
#¶ ↑
main_colour?¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 253 def main_colour? @main_colour end
#¶ ↑
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.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 491 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)} seconds." cliner_with_time_stamp e 'The next song will be: '+sfancy( playlist?[@n_songs_played] ) cliner sleep(n_seconds) end
#¶ ↑
play_the_playlist
¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 640 def play_the_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
(play tag)¶ ↑
This is the actual method that will play the song.
We will also append the filename last played.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 512 def play_this_song( i, be_verbose = true ) if be_verbose e; opnn; e 'The next song we will play via '+ simp(audio_player_to_use?)+' 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 'The length of this song is: '+ sfancy("#{@file_duration_in_seconds.to_i} 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_played
¶ ↑
Fills up our hash with available data for the first time. Basically right now, only manipulates @hash_songs_played
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 134 def populate_hash_songs_played playlist?.each { |song| song = File.basename(song) @hash_songs_played[song] = 0 } end
#¶ ↑
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.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 300 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 method will also check if the file that is being played is too big. If it is, we will remove the log-file.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 666 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
#¶ ↑
reset (reset tag)¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/reset.rb, line 14 def reset super() clear_playlist set_delay # Set @delay here. set_shall_we_exit # ======================================================================= # # === @namespace # ======================================================================= # @namespace = NAMESPACE # ======================================================================= # # === @use_colours # ======================================================================= # @use_colours = true # 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 # ======================================================================= # @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 # ======================================================================= # # === @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_playlist
¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 282 def reset_playlist @array_play_these_songs = [] # Keep all files that we wish to play in this Array. end
#¶ ↑
return_random_song
¶ ↑
This method will return one random song.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 274 def return_random_song return_all_songs.first end
#¶ ↑
set_commandline_options_and_return_the_other_options
¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 108 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
#¶ ↑
show_help
(help tag)¶ ↑
Show help here, then exit.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 617 def show_help( optional_shall_we_exit = false ) cliner opnn; e '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
¶ ↑
This method will show the last song played.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 189 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_message
¶ ↑
Display a fancy startup message.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 99 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_played
¶ ↑
The songs played will be stored in a .md file.
Invocation example:
rsong --last-songs?
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 697 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 e 'No file exists at '+sfile(_)+'.' end end
#¶ ↑
show_the_log_file_if_it_exists
¶ ↑
Invocation example:
rsong --log?
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 581 def show_the_log_file_if_it_exists( log_file = THE_LOG_FILE_IS_STORED_HERE ) if File.exist? log_file opnn; e 'Displaying the content of the file at' opnn; e '`'+sfile(log_file)+'`:' dataset = File.readlines(log_file).map {|entry| entry.rstrip } e e mediumpurple( dataset.map {|entry| " #{entry}" }.join("\n") ) else opnn; e 'No file exists at '+sfile(log_file)+'.' end end
#¶ ↑
show_the_playlist
¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 245 def show_the_playlist opnn; e 'The playlist is:' pp playlist? end
#¶ ↑
shuffle_the_playlist
¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 220 def shuffle_the_playlist playlist?.shuffle! end
#¶ ↑
store_data_about_this_song_into_a_local_file
¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 562 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
#¶ ↑
try_to_find_genre_song
¶ ↑
This tries to find a (random) genre song.
#¶ ↑
# File lib/multimedia_paradise/audio/audio_player/misc.rb, line 469 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