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:, bit_depths:, file_types:, bpm_source: nil, bar_multiple: nil) ⇒ Device

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



15
16
17
18
19
20
21
22
# File 'lib/wavesync/device.rb', line 15

def initialize(name:, sample_rates:, bit_depths:, file_types:, bpm_source: nil, bar_multiple: nil)
  @name = name
  @sample_rates = sample_rates
  @bit_depths = bit_depths
  @file_types = file_types
  @bpm_source = bpm_source
  @bar_multiple = bar_multiple
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

Class Method Details

.allObject

: () -> Array



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

def self.all
  @all ||= load_from_yaml
end

.config_pathObject

: () -> String



25
26
27
# File 'lib/wavesync/device.rb', line 25

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

.find_by(name:) ⇒ Object

: (name: String) -> Device?



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

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

.load_from_yamlObject

: () -> Array



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wavesync/device.rb', line 40

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']
    )
  end
end

Instance Method Details

#target_bit_depth(source_bit_depth) ⇒ Object

: (Integer? source_bit_depth) -> Integer?



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

def target_bit_depth(source_bit_depth)
  return nil if source_bit_depth.nil? || 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?



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

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



55
56
57
58
59
60
61
# File 'lib/wavesync/device.rb', line 55

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?



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

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