Class: Wavesync::SetEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/wavesync/set_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

Instance Method Summary collapse

Constructor Details

#initialize(set, library_path) ⇒ SetEditor

: (Set set, String library_path) -> void



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wavesync/set_editor.rb', line 27

def initialize(set, library_path)
  @set = set #: Set
  @library_path = library_path #: String
  @prompt = TTY::Prompt.new(interrupt: :exit, active_color: :red) #: untyped
  @ui = UI.new #: UI
  @selected = @set.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

Parameters:

  • value

    the value to set the attribute player_index to.



24
25
26
# File 'lib/wavesync/set_editor.rb', line 24

def player_index=(value)
  @player_index = value
end

#player_offset=(value) ⇒ Object (writeonly)

Sets the attribute player_offset

Parameters:

  • value

    the value to set the attribute player_offset to.



24
25
26
# File 'lib/wavesync/set_editor.rb', line 24

def player_offset=(value)
  @player_offset = value
end

#player_started_at=(value) ⇒ Object (writeonly)

Sets the attribute player_started_at

Parameters:

  • value

    the value to set the attribute player_started_at to.



24
25
26
# File 'lib/wavesync/set_editor.rb', line 24

def player_started_at=(value)
  @player_started_at = value
end

#player_stateObject

: Symbol



22
23
24
# File 'lib/wavesync/set_editor.rb', line 22

def player_state
  @player_state
end

#player_track=(value) ⇒ Object (writeonly)

Sets the attribute player_track

Parameters:

  • value

    the value to set the attribute player_track to.



24
25
26
# File 'lib/wavesync/set_editor.rb', line 24

def player_track=(value)
  @player_track = value
end

#selectedObject (readonly)

: untyped



23
24
25
# File 'lib/wavesync/set_editor.rb', line 23

def selected
  @selected
end

#setObject (readonly)

: untyped



23
24
25
# File 'lib/wavesync/set_editor.rb', line 23

def set
  @set
end

#uiObject (readonly)

: untyped



23
24
25
# File 'lib/wavesync/set_editor.rb', line 23

def ui
  @ui
end

Instance Method Details

#advance_and_playObject

: () -> void



486
487
488
489
490
491
# File 'lib/wavesync/set_editor.rb', line 486

def advance_and_play
  return if @selected.nil? || @selected >= @set.tracks.size - 1

  @selected += 1
  start_player(@set.tracks[@selected])
end

#display_name(relative) ⇒ Object

: (String relative) -> String



247
248
249
# File 'lib/wavesync/set_editor.rb', line 247

def display_name(relative)
  File.basename(relative, '.*').sub(/\A\d+[\s.\-_]+/, '')
end

#format_duration(seconds) ⇒ Object

: (Float? seconds) -> String?



175
176
177
178
179
180
181
182
# File 'lib/wavesync/set_editor.rb', line 175

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



119
120
121
122
# File 'lib/wavesync/set_editor.rb', line 119

def format_pitch_shift(semitones)
  sign = semitones >= 0 ? '+' : ''
  "#{sign}#{semitones.round(2)}"
end

#handle_action(action) ⇒ Object

: (Symbol action) -> Symbol?



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
# File 'lib/wavesync/set_editor.rb', line 357

def handle_action(action)
  case action
  when :cursor_up
    @selected = [@selected - 1, 0].max unless @set.tracks.empty?
    nil
  when :cursor_down
    @selected = [@selected + 1, @set.tracks.size - 1].min unless @set.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
    @set.save
    :quit
  end
end

#pitch_shift_semitones(source_bpm, target_bpm) ⇒ Object

: ((String | Integer)? source_bpm, (String | Integer)? target_bpm) -> Float?



108
109
110
111
112
113
114
115
116
# File 'lib/wavesync/set_editor.rb', line 108

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



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
# File 'lib/wavesync/set_editor.rb', line 211

def playback_bar(elapsed, total_duration, bar_width, cue_fractions: [])
  ratio = total_duration.positive? ? [elapsed / total_duration, 1.0].min : 0.0
  filled = (ratio * bar_width).round

  bar = Array.new(bar_width) { |i| i < filled ? '' : '' }

  cue_position_colors = {} #: Hash[Integer, Symbol]
  cue_fractions.each do |fraction|
    position = (fraction * (bar_width - 1)).round.clamp(0, bar_width - 1)
    cue_position_colors[position] = position == filled ? :highlight : :surface
  end
  cue_position_colors.each_key { |position| bar[position] = '' }

  result = +''
  run_start = 0
  while run_start < bar_width
    if cue_position_colors.key?(run_start)
      result << @ui.color(bar[run_start], cue_position_colors[run_start])
      run_start += 1
    else
      run_end = run_start
      run_end += 1 while run_end < bar_width && !cue_position_colors.key?(run_end)
      result << @ui.color((bar[run_start...run_end] || []).join, :surface)
      run_start = run_end
    end
  end
  result
end

#playback_elapsedObject

: () -> Float



185
186
187
188
189
190
191
192
193
194
# File 'lib/wavesync/set_editor.rb', line 185

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



170
171
172
# File 'lib/wavesync/set_editor.rb', line 170

def relative_path(absolute)
  absolute.sub("#{@library_path}/", '')
end

#runObject

: () -> void



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wavesync/set_editor.rb', line 42

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)?



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wavesync/set_editor.rb', line 59

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
    nil
  end
end

#track_cue_fractions(path) ⇒ Object

: (String? path) -> Array



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wavesync/set_editor.rb', line 87

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
    [] #: Array[Float]
  end
end

#track_duration(path) ⇒ Object

: (String? path) -> Float?



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/wavesync/set_editor.rb', line 73

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
    nil
  end
end

#visible_length(string) ⇒ Object

: (String? string) -> Integer



204
205
206
207
208
# File 'lib/wavesync/set_editor.rb', line 204

def visible_length(string)
  return 0 unless string

  string.gsub(/\e\[[0-9;]*[A-Za-z]/, '').length
end