Class: Wavesync::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/wavesync/scanner.rb

Instance Method Summary collapse

Constructor Details

#initialize(source_library_path) ⇒ Scanner

: (String source_library_path) -> void



13
14
15
16
17
18
19
# File 'lib/wavesync/scanner.rb', line 13

def initialize(source_library_path)
  @source_library_path = File.expand_path(source_library_path) #: String
  Logger.configure(@source_library_path)
  @audio_files = find_audio_files #: Array[String]
  @ui = Wavesync::UI.new #: UI
  @converter = FileConverter.new #: FileConverter
end

Instance Method Details

#pull_cue_points(target_library_path, device) ⇒ Object

: (String target_library_path, Device device) -> void



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

def pull_cue_points(target_library_path, device)
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  path_resolver = PathResolver.new(@source_library_path, target_library_path, device)
  @ui.pull_progress(0, @audio_files.size, device)

  @audio_files.each_with_index do |file, index|
    audio = Audio.new(file)
    source_format = audio.format
    @ui.file_progress(file)

    if source_format.file_type == 'wav'
      target_format = device.target_format(source_format, file)
      target_path = path_resolver.resolve(file, audio, target_file_type: target_format.file_type)
      if target_path.extname.downcase == '.wav' && target_path.exist?
        target_cue_points = CueChunk.read(target_path.to_s)
        if target_cue_points.any?
          source_cue_points = audio.cue_points
          audio.write_cue_points(target_cue_points) unless CueChunk.same?(source_cue_points, target_cue_points)
        end
      end
    end

    @ui.pull_progress(index, @audio_files.size, device)
  end

  puts
  system('sync')
  elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
  Logger.log_run_time(elapsed)
end

#sync(target_library_path, device, pad: false, staged: false, mp3_bitrate: 192, &on_file_synced) ⇒ Object

: (String target_library_path, Device device, ?pad: bool, ?staged: bool, ?mp3_bitrate: Integer) ?{ (String) -> void } -> void



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

def sync(target_library_path, device, pad: false, staged: false, mp3_bitrate: 192, &on_file_synced)
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  target_library_pathname = Pathname.new(target_library_path)
  path_resolver = PathResolver.new(@source_library_path, target_library_path, device)
  skipped_count = 0
  conversion_count = 0
  @ui.sync_progress(0, @audio_files.size, device)

  @audio_files.each_with_index do |file, index|
    audio = Audio.new(file)
    bpm = audio.bpm

    source_format = audio.format
    target_format = device.target_format(source_format, file)
    target_format = target_format.with(sample_rate: nil, bit_depth: nil) if source_format.file_type == 'mp3' && target_format.file_type.nil?

    padding_seconds = nil #: Numeric?
    original_bars = nil #: Integer?
    target_bars = nil #: Integer?
    if pad && device.bar_multiple
      padding_seconds = TrackPadding.compute(audio.duration, bpm, device.bar_multiple)
      original_bars, target_bars = TrackPadding.bar_counts(audio.duration, bpm, device.bar_multiple) unless padding_seconds.zero?
      padding_seconds = nil if padding_seconds.zero?
    end

    @ui.bpm(bpm, original_bars: original_bars, target_bars: target_bars)
    @ui.file_progress(file)

    if target_format.file_type || target_format.sample_rate || target_format.bit_depth || padding_seconds
      target_ext = target_format.file_type || source_format.file_type
       = {} #: Hash[String, String]
       = audio.transliterated_tag_changes if device. && target_ext == 'mp3'
      converted = @converter.convert(audio, file, path_resolver, source_format, target_format,
                                     padding_seconds: padding_seconds,
                                     metadata: ,
                                     mp3_bitrate: mp3_bitrate,
                                     before_transcode: -> { @ui.conversion_progress(source_format, target_format, mp3_bitrate) }) do |local_temp_path|
        inject_acid_bpm(local_temp_path, bpm, device)
        inject_cue_points(local_temp_path, audio, source_format, target_format)
      end
      converted_target_path = path_resolver.resolve(file, audio, target_file_type: target_format.file_type)
      verify_written(converted_target_path, source: file) if converted
      final_target_path = converted_target_path
    else
      if device.bpm_source == :acid_chunk && bpm && File.extname(file).downcase == '.wav'
        target_path = path_resolver.resolve(file, audio)
        files_to_cleanup = path_resolver.find_files_to_cleanup(target_path, audio)
        files_to_cleanup.each { |cleanup_file| FileUtils.rm_f(cleanup_file) }
        if target_path.exist?
          copied = false
        else
          target_path.dirname.mkpath
          AcidChunk.write_bpm(file, target_path.to_s, bpm)
          verify_written(target_path, source: file)
          copied = true
        end
      else
        copied = copy_file(audio, file, path_resolver)
        target_path = path_resolver.resolve(file, audio)
        if copied
          verify_written(target_path, source: file)
          (target_path.to_s, device)
        end
      end
      final_target_path = target_path
      @ui.copy(source_format)
    end

    if !copied && !converted
      skipped_count += 1
      @ui.skip(staged: staged)
    end

    conversion_count += 1 if converted
    @ui.sync_progress(index, @audio_files.size, device)

    if on_file_synced && final_target_path
      relative_path = final_target_path.relative_path_from(target_library_pathname).to_s
      on_file_synced.call(relative_path)
    end
  end

  puts
  system('sync')
  elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
  Logger.log_run_time(elapsed)
end