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

Returns a new instance of Scanner.



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

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

Instance Method Details

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



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

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
    original_bars = nil
    target_bars = nil
    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 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

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