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



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

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

Instance Method Details

#sync(target_library_path, device, pad: false) ⇒ Object

: (String target_library_path, Device device, ?pad: bool) -> void



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

def sync(target_library_path, device, pad: false)
  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)

    source_format = audio.format
    target_format = device.target_format(source_format, file)

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

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

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

    if target_format.file_type || target_format.sample_rate || target_format.bit_depth || padding_seconds
      converted = @converter.convert(audio, file, path_resolver, source_format, target_format,
                                     padding_seconds: padding_seconds) do
        @ui.conversion_progress(source_format, target_format)
      end
      target_path = path_resolver.resolve(file, audio, target_file_type: target_format.file_type)
    else
      copied = copy_file(audio, file, path_resolver)
      @ui.copy(source_format)
      target_path = path_resolver.resolve(file, audio)
    end

    bpm = audio.bpm
    if (copied || converted) && device.bpm_source == :acid_chunk && bpm && target_path.extname.downcase == '.wav'
      temp_path = "#{target_path}.tmp"
      AcidChunk.write_bpm(target_path.to_s, temp_path, bpm)
      FileUtils.mv(temp_path, target_path.to_s)
    end

    if converted && source_format.file_type == 'wav' && target_path.extname.downcase == '.wav'
      source_cue_points = audio.cue_points
      if source_cue_points.any?
        rescaled_cue_points = rescale_cue_points(source_cue_points, audio.sample_rate, target_format.sample_rate || audio.sample_rate)
        temp_path = "#{target_path}.tmp"
        CueChunk.write(target_path.to_s, temp_path, rescaled_cue_points)
        FileUtils.mv(temp_path, target_path.to_s)
      end
    end

    if !copied && !converted
      skipped_count += 1
      @ui.skip
    end

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

  puts
  system('sync')
end