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

Returns a new instance of Device.



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

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)

Returns the value of attribute bar_multiple.



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

def bar_multiple
  @bar_multiple
end

#bit_depthsObject (readonly)

Returns the value of attribute bit_depths.



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

def bit_depths
  @bit_depths
end

#bpm_sourceObject (readonly)

Returns the value of attribute bpm_source.



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

def bpm_source
  @bpm_source
end

#file_typesObject (readonly)

Returns the value of attribute file_types.



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

def file_types
  @file_types
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#sample_ratesObject (readonly)

Returns the value of attribute sample_rates.



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

def sample_rates
  @sample_rates
end

Class Method Details

.allObject



21
22
23
# File 'lib/wavesync/device.rb', line 21

def self.all
  @all ||= load_from_yaml
end

.config_pathObject



17
18
19
# File 'lib/wavesync/device.rb', line 17

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

.find_by(name:) ⇒ Object



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

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

.load_from_yamlObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wavesync/device.rb', line 29

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



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

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



51
52
53
54
55
56
# File 'lib/wavesync/device.rb', line 51

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



43
44
45
46
47
48
49
# File 'lib/wavesync/device.rb', line 43

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



58
59
60
61
62
# File 'lib/wavesync/device.rb', line 58

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

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