Class: Wavesync::SetlistEditor
- Inherits:
-
Object
- Object
- Wavesync::SetlistEditor
- Defined in:
- lib/wavesync/setlist_editor.rb
Constant Summary collapse
- KEY_MAP =
{ 'a' => :add, 'u' => :move_up, 'd' => :move_down, 'r' => :remove, 'q' => :quit, ' ' => :toggle_play, 'j' => :jump_to_next_cue, "\e[A" => :cursor_up, "\e[B" => :cursor_down }.freeze
Instance Attribute Summary collapse
-
#player_index ⇒ Object
writeonly
Sets the attribute player_index.
-
#player_offset ⇒ Object
writeonly
Sets the attribute player_offset.
-
#player_pid ⇒ Object
writeonly
Sets the attribute player_pid.
-
#player_started_at ⇒ Object
writeonly
Sets the attribute player_started_at.
-
#player_state ⇒ Object
: Symbol.
-
#player_track ⇒ Object
writeonly
Sets the attribute player_track.
-
#selected ⇒ Object
readonly
: untyped.
-
#setlist ⇒ Object
readonly
: untyped.
-
#ui ⇒ Object
readonly
: untyped.
Instance Method Summary collapse
-
#advance_and_play ⇒ Object
: () -> void.
-
#check_player ⇒ Object
: () -> void.
-
#display_name(relative) ⇒ Object
: (String relative) -> String.
-
#format_duration(seconds) ⇒ Object
: (Float? seconds) -> String?.
-
#format_pitch_shift(semitones) ⇒ Object
: (Float semitones) -> String.
-
#handle_action(action) ⇒ Object
: (Symbol action) -> Symbol?.
-
#initialize(setlist, library_path) ⇒ SetlistEditor
constructor
: (Setlist setlist, String library_path) -> void.
-
#kill_player ⇒ Object
: () -> void.
-
#pitch_shift_semitones(source_bpm, target_bpm) ⇒ Object
: ((String | Integer)? source_bpm, (String | Integer)? target_bpm) -> Float?.
-
#playback_bar(elapsed, total_duration, bar_width, cue_fractions: []) ⇒ Object
: (Float elapsed, Float total_duration, Integer bar_width, ?cue_fractions: Array) -> String.
-
#playback_elapsed ⇒ Object
: () -> Float.
-
#relative_path(absolute) ⇒ Object
: (String absolute) -> String.
-
#run ⇒ Object
: () -> void.
-
#track_bpm(path) ⇒ Object
: (String? path) -> (String | Integer)?.
-
#track_cue_fractions(path) ⇒ Object
: (String? path) -> Array.
-
#track_duration(path) ⇒ Object
: (String? path) -> Float?.
-
#visible_length(string) ⇒ Object
: (String? string) -> Integer.
Constructor Details
#initialize(setlist, library_path) ⇒ SetlistEditor
: (Setlist setlist, String library_path) -> void
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/wavesync/setlist_editor.rb', line 28 def initialize(setlist, library_path) @setlist = setlist #: Setlist @library_path = library_path #: String Logger.configure(@library_path) @prompt = TTY::Prompt.new(interrupt: :exit, active_color: :red) #: untyped @ui = UI.new #: UI @selected = @setlist.tracks.empty? ? nil : 0 #: Integer? @player_pid = nil #: Integer? @player_track = nil #: String? @player_index = nil #: Integer? @player_state = :stopped #: Symbol @player_offset = 0 #: Numeric @player_started_at = nil #: Time? end |
Instance Attribute Details
#player_index=(value) ⇒ Object (writeonly)
Sets the attribute player_index
25 26 27 |
# File 'lib/wavesync/setlist_editor.rb', line 25 def player_index=(value) @player_index = value end |
#player_offset=(value) ⇒ Object (writeonly)
Sets the attribute player_offset
25 26 27 |
# File 'lib/wavesync/setlist_editor.rb', line 25 def player_offset=(value) @player_offset = value end |
#player_pid=(value) ⇒ Object (writeonly)
Sets the attribute player_pid
25 26 27 |
# File 'lib/wavesync/setlist_editor.rb', line 25 def player_pid=(value) @player_pid = value end |
#player_started_at=(value) ⇒ Object (writeonly)
Sets the attribute player_started_at
25 26 27 |
# File 'lib/wavesync/setlist_editor.rb', line 25 def player_started_at=(value) @player_started_at = value end |
#player_state ⇒ Object
: Symbol
23 24 25 |
# File 'lib/wavesync/setlist_editor.rb', line 23 def player_state @player_state end |
#player_track=(value) ⇒ Object (writeonly)
Sets the attribute player_track
25 26 27 |
# File 'lib/wavesync/setlist_editor.rb', line 25 def player_track=(value) @player_track = value end |
#selected ⇒ Object (readonly)
: untyped
24 25 26 |
# File 'lib/wavesync/setlist_editor.rb', line 24 def selected @selected end |
#setlist ⇒ Object (readonly)
: untyped
24 25 26 |
# File 'lib/wavesync/setlist_editor.rb', line 24 def setlist @setlist end |
#ui ⇒ Object (readonly)
: untyped
24 25 26 |
# File 'lib/wavesync/setlist_editor.rb', line 24 def ui @ui end |
Instance Method Details
#advance_and_play ⇒ Object
: () -> void
495 496 497 498 499 500 |
# File 'lib/wavesync/setlist_editor.rb', line 495 def advance_and_play return if @selected.nil? || @selected >= @setlist.tracks.size - 1 @selected += 1 start_player(@setlist.tracks[@selected]) end |
#check_player ⇒ Object
: () -> void
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/wavesync/setlist_editor.rb', line 471 def check_player return unless @player_pid player_pid = @player_pid result = Process.waitpid(@player_pid, Process::WNOHANG) return unless result @player_pid = nil @player_track = nil @player_index = nil @player_state = :stopped @player_offset = 0 advance_and_play rescue Errno::ECHILD => e Logger.log_error(e, call_site: 'SetlistEditor#check_player', arguments: { player_pid: }) @player_pid = nil @player_track = nil @player_index = nil @player_state = :stopped @player_offset = 0 advance_and_play end |
#display_name(relative) ⇒ Object
: (String relative) -> String
252 253 254 |
# File 'lib/wavesync/setlist_editor.rb', line 252 def display_name(relative) File.basename(relative, '.*').sub(/\A\d+[\s.\-_]+/, '') end |
#format_duration(seconds) ⇒ Object
: (Float? seconds) -> String?
180 181 182 183 184 185 186 187 |
# File 'lib/wavesync/setlist_editor.rb', line 180 def format_duration(seconds) return nil unless seconds total_seconds = seconds.to_i mins = total_seconds / 60 secs = total_seconds % 60 "#{mins}:#{secs.to_s.rjust(2, '0')}" end |
#format_pitch_shift(semitones) ⇒ Object
: (Float semitones) -> String
124 125 126 127 |
# File 'lib/wavesync/setlist_editor.rb', line 124 def format_pitch_shift(semitones) sign = semitones >= 0 ? '+' : '' "#{sign}#{semitones.round(2)}" end |
#handle_action(action) ⇒ Object
: (Symbol action) -> Symbol?
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 |
# File 'lib/wavesync/setlist_editor.rb', line 362 def handle_action(action) case action when :cursor_up @selected = [@selected - 1, 0].max unless @setlist.tracks.empty? nil when :cursor_down @selected = [@selected + 1, @setlist.tracks.size - 1].min unless @setlist.tracks.empty? nil when :toggle_play toggle_playback nil when :add add_track nil when :remove remove_track nil when :move_up move_track(:up) nil when :move_down move_track(:down) nil when :jump_to_next_cue jump_to_next_cue nil when :quit @setlist.save :quit end end |
#kill_player ⇒ Object
: () -> void
449 450 451 452 453 454 455 456 457 458 |
# File 'lib/wavesync/setlist_editor.rb', line 449 def kill_player return unless @player_pid player_pid = @player_pid Process.kill('TERM', @player_pid) @player_pid = nil rescue Errno::ESRCH => e Logger.log_error(e, call_site: 'SetlistEditor#kill_player', arguments: { player_pid: }) @player_pid = nil end |
#pitch_shift_semitones(source_bpm, target_bpm) ⇒ Object
: ((String | Integer)? source_bpm, (String | Integer)? target_bpm) -> Float?
113 114 115 116 117 118 119 120 121 |
# File 'lib/wavesync/setlist_editor.rb', line 113 def pitch_shift_semitones(source_bpm, target_bpm) return nil unless source_bpm && target_bpm source = source_bpm.to_f target = target_bpm.to_f return nil if source.zero? 12.0 * Math.log2(target / source) end |
#playback_bar(elapsed, total_duration, bar_width, cue_fractions: []) ⇒ Object
: (Float elapsed, Float total_duration, Integer bar_width, ?cue_fractions: Array) -> String
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 |
# File 'lib/wavesync/setlist_editor.rb', line 216 def (elapsed, total_duration, , cue_fractions: []) ratio = total_duration.positive? ? [elapsed / total_duration, 1.0].min : 0.0 filled = (ratio * ).round = Array.new() { |i| i < filled ? '█' : '░' } cue_position_colors = {} #: Hash[Integer, Symbol] cue_fractions.each do |fraction| position = (fraction * ( - 1)).round.clamp(0, - 1) cue_position_colors[position] = position == filled ? :highlight : :surface end cue_position_colors.each_key { |position| [position] = '◆' } result = +'' run_start = 0 while run_start < if cue_position_colors.key?(run_start) result << @ui.color([run_start], cue_position_colors[run_start]) run_start += 1 else run_end = run_start run_end += 1 while run_end < && !cue_position_colors.key?(run_end) result << @ui.color(([run_start...run_end] || []).join, :surface) run_start = run_end end end result end |
#playback_elapsed ⇒ Object
: () -> Float
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/wavesync/setlist_editor.rb', line 190 def playback_elapsed case @player_state when :playing (@player_offset + (Time.now - @player_started_at)).to_f when :paused @player_offset.to_f else 0.0 end end |
#relative_path(absolute) ⇒ Object
: (String absolute) -> String
175 176 177 |
# File 'lib/wavesync/setlist_editor.rb', line 175 def relative_path(absolute) absolute.sub("#{@library_path}/", '') end |
#run ⇒ Object
: () -> void
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/wavesync/setlist_editor.rb', line 44 def run enter_fullscreen loop do check_player render action = KEY_MAP[read_key] next unless action result = handle_action(action) break if result == :quit end ensure exit_fullscreen stop_playback end |
#track_bpm(path) ⇒ Object
: (String? path) -> (String | Integer)?
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/wavesync/setlist_editor.rb', line 61 def track_bpm(path) return nil if path.nil? @track_bpms ||= {} #: Hash[String, (String | Integer)?] return @track_bpms[path] if @track_bpms.key?(path) @track_bpms[path] = begin Audio.new(path).bpm rescue StandardError => e Logger.log_error(e, call_site: 'SetlistEditor#track_bpm', arguments: { path: }) nil end end |
#track_cue_fractions(path) ⇒ Object
: (String? path) -> Array
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/wavesync/setlist_editor.rb', line 91 def track_cue_fractions(path) return [] if path.nil? @track_cue_fractions ||= {} #: Hash[String, Array[Float]] return @track_cue_fractions[path] if @track_cue_fractions.key?(path) @track_cue_fractions[path] = begin audio = Audio.new(path) sample_rate = audio.sample_rate duration = audio.duration if sample_rate && duration&.positive? audio.cue_points.map { |cue_point| cue_point[:sample_offset].to_f / sample_rate / duration } else [] #: Array[Float] end rescue StandardError => e Logger.log_error(e, call_site: 'SetlistEditor#track_cue_fractions', arguments: { path: }) [] #: Array[Float] end end |
#track_duration(path) ⇒ Object
: (String? path) -> Float?
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/wavesync/setlist_editor.rb', line 76 def track_duration(path) return nil if path.nil? @track_durations ||= {} #: Hash[String, Float?] return @track_durations[path] if @track_durations.key?(path) @track_durations[path] = begin Audio.new(path).duration rescue StandardError => e Logger.log_error(e, call_site: 'SetlistEditor#track_duration', arguments: { path: }) nil end end |
#visible_length(string) ⇒ Object
: (String? string) -> Integer
209 210 211 212 213 |
# File 'lib/wavesync/setlist_editor.rb', line 209 def visible_length(string) return 0 unless string string.gsub(/\e\[[0-9;]*[A-Za-z]/, '').length end |