Class: Wavesync::Device
- Inherits:
-
Object
- Object
- Wavesync::Device
- Defined in:
- lib/wavesync/device.rb
Instance Attribute Summary collapse
-
#bar_multiple ⇒ Object
readonly
: Integer?.
-
#bit_depths ⇒ Object
readonly
: Array.
-
#bpm_source ⇒ Object
readonly
: Symbol?.
-
#file_types ⇒ Object
readonly
: Array.
-
#name ⇒ Object
readonly
: String.
-
#sample_rates ⇒ Object
readonly
: Array.
Class Method Summary collapse
-
.all ⇒ Object
: () -> Array.
-
.config_path ⇒ Object
: () -> String.
-
.find_by(name:) ⇒ Object
: (name: String) -> Device?.
-
.load_from_yaml ⇒ Object
: () -> Array.
Instance Method Summary collapse
- #initialize(name:, sample_rates:, bit_depths:, file_types:, bpm_source: nil, bar_multiple: nil) ⇒ Device constructor
-
#target_bit_depth(source_bit_depth) ⇒ Object
: (Integer? source_bit_depth) -> Integer?.
-
#target_file_type(source_file_path) ⇒ Object
: (String source_file_path) -> String?.
-
#target_format(source_format, source_file_path) ⇒ Object
: (AudioFormat source_format, String source_file_path) -> AudioFormat.
-
#target_sample_rate(source_sample_rate) ⇒ Object
: (Integer? source_sample_rate) -> Integer?.
Constructor Details
#initialize(name:, sample_rates:, bit_depths:, file_types:, bpm_source: nil, bar_multiple: nil) ⇒ Device
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 = end |
Instance Attribute Details
#bar_multiple ⇒ Object (readonly)
: Integer?
12 13 14 |
# File 'lib/wavesync/device.rb', line 12 def @bar_multiple end |
#bit_depths ⇒ Object (readonly)
: Array
9 10 11 |
# File 'lib/wavesync/device.rb', line 9 def bit_depths @bit_depths end |
#bpm_source ⇒ Object (readonly)
: Symbol?
11 12 13 |
# File 'lib/wavesync/device.rb', line 11 def bpm_source @bpm_source end |
#file_types ⇒ Object (readonly)
: Array
10 11 12 |
# File 'lib/wavesync/device.rb', line 10 def file_types @file_types end |
#name ⇒ Object (readonly)
: String
7 8 9 |
# File 'lib/wavesync/device.rb', line 7 def name @name end |
#sample_rates ⇒ Object (readonly)
: Array
8 9 10 |
# File 'lib/wavesync/device.rb', line 8 def sample_rates @sample_rates end |
Class Method Details
.all ⇒ Object
: () -> Array
30 31 32 |
# File 'lib/wavesync/device.rb', line 30 def self.all @all ||= load_from_yaml end |
.config_path ⇒ Object
: () -> String
25 26 27 |
# File 'lib/wavesync/device.rb', line 25 def self.config_path File.('../../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_yaml ⇒ Object
: () -> 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 |