Class: Wavesync::Device

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, sample_rates:, file_types:, bit_depths: [], bpm_source: nil, bar_multiple: nil, unsupported_characters: [], transliterate_metadata: false, uppercase_paths: false) ⇒ Device

: (name: String, sample_rates: Array, file_types: Array, ?bit_depths: Array, ?bpm_source: Symbol?, ?bar_multiple: Integer?, ?unsupported_characters: Array, ?transliterate_metadata: bool, ?uppercase_paths: bool) -> void



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/wavesync/device.rb', line 18

def initialize(name:, sample_rates:, file_types:, bit_depths: [], bpm_source: nil, bar_multiple: nil, unsupported_characters: [], transliterate_metadata: false, uppercase_paths: false)
  @name = name
  @sample_rates = sample_rates
  @bit_depths = bit_depths
  @file_types = file_types
  @bpm_source = bpm_source
  @bar_multiple = bar_multiple
  @unsupported_characters = unsupported_characters
  @transliterate_metadata = 
  @uppercase_paths = uppercase_paths
end

Instance Attribute Details

#bar_multipleObject (readonly)

: Integer?



12
13
14
# File 'lib/wavesync/device.rb', line 12

def bar_multiple
  @bar_multiple
end

#bit_depthsObject (readonly)

: Array



9
10
11
# File 'lib/wavesync/device.rb', line 9

def bit_depths
  @bit_depths
end

#bpm_sourceObject (readonly)

: Symbol?



11
12
13
# File 'lib/wavesync/device.rb', line 11

def bpm_source
  @bpm_source
end

#file_typesObject (readonly)

: Array



10
11
12
# File 'lib/wavesync/device.rb', line 10

def file_types
  @file_types
end

#nameObject (readonly)

: String



7
8
9
# File 'lib/wavesync/device.rb', line 7

def name
  @name
end

#sample_ratesObject (readonly)

: Array



8
9
10
# File 'lib/wavesync/device.rb', line 8

def sample_rates
  @sample_rates
end

#transliterate_metadataObject (readonly)

: bool



14
15
16
# File 'lib/wavesync/device.rb', line 14

def 
  @transliterate_metadata
end

#unsupported_charactersObject (readonly)

: Array



13
14
15
# File 'lib/wavesync/device.rb', line 13

def unsupported_characters
  @unsupported_characters
end

#uppercase_pathsObject (readonly)

: bool



15
16
17
# File 'lib/wavesync/device.rb', line 15

def uppercase_paths
  @uppercase_paths
end

Class Method Details

.allObject

: () -> Array



36
37
38
# File 'lib/wavesync/device.rb', line 36

def self.all
  @all ||= load_from_yaml
end

.config_pathObject

: () -> String



31
32
33
# File 'lib/wavesync/device.rb', line 31

def self.config_path
  File.expand_path('../../config/devices.yml', __dir__ || '')
end

.find_by(name:) ⇒ Object

: (name: String) -> Device?



41
42
43
# File 'lib/wavesync/device.rb', line 41

def self.find_by(name:)
  all.find { |device| device.name == name }
end

.load_from_yamlObject

: () -> Array



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wavesync/device.rb', line 46

def self.load_from_yaml
  data = YAML.load_file(config_path)
  data.fetch('devices').map do |attrs|
    new(
      name: attrs['name'],
      sample_rates: attrs['sample_rates'],
      bit_depths: attrs['bit_depths'] || [],
      file_types: attrs['file_types'],
      bpm_source: attrs['bpm_source']&.to_sym,
      bar_multiple: attrs['bar_multiple'],
      unsupported_characters: attrs['unsupported_characters'] || [],
      transliterate_metadata: attrs['transliterate_metadata'] || false,
      uppercase_paths: attrs['uppercase_paths'] || false
    )
  end
end

Instance Method Details

#target_bit_depth(source_bit_depth) ⇒ Object

: (Integer? source_bit_depth) -> Integer?



89
90
91
92
93
# File 'lib/wavesync/device.rb', line 89

def target_bit_depth(source_bit_depth)
  return nil if source_bit_depth.nil? || bit_depths.empty? || bit_depths.include?(source_bit_depth)

  bit_depths.min_by { |n| [(n - source_bit_depth).abs, -n] }
end

#target_file_type(source_file_path) ⇒ Object

: (String source_file_path) -> String?



73
74
75
76
77
78
# File 'lib/wavesync/device.rb', line 73

def target_file_type(source_file_path)
  file_extension = File.extname(source_file_path).downcase[1..] || ''
  return nil if file_types.include?(file_extension)

  file_types.first
end

#target_format(source_format, source_file_path) ⇒ Object

: (AudioFormat source_format, String source_file_path) -> AudioFormat



64
65
66
67
68
69
70
# File 'lib/wavesync/device.rb', line 64

def target_format(source_format, source_file_path)
  AudioFormat.new(
    file_type: target_file_type(source_file_path),
    sample_rate: target_sample_rate(source_format.sample_rate),
    bit_depth: target_bit_depth(source_format.bit_depth)
  )
end

#target_sample_rate(source_sample_rate) ⇒ Object

: (Integer? source_sample_rate) -> Integer?



81
82
83
84
85
86
# File 'lib/wavesync/device.rb', line 81

def target_sample_rate(source_sample_rate)
  return nil if source_sample_rate.nil?
  return nil if sample_rates.include?(source_sample_rate)

  sample_rates.min_by { |n| [(n - source_sample_rate).abs, -n] }
end