Class: Beats::KitBuilder
- Inherits:
-
Object
- Object
- Beats::KitBuilder
- Defined in:
- lib/beats/kit_builder.rb
Defined Under Namespace
Classes: InvalidSoundFormatError, SoundFileNotFoundError
Constant Summary collapse
- BITS_PER_SAMPLE =
16
- SAMPLE_FORMAT =
"pcm_#{BITS_PER_SAMPLE}".to_sym
- SAMPLE_RATE =
44100
Instance Attribute Summary collapse
-
#composite_replacements ⇒ Object
readonly
Returns the value of attribute composite_replacements.
Instance Method Summary collapse
- #add_item(label, filenames) ⇒ Object
- #build_kit ⇒ Object
- #has_label?(label) ⇒ Boolean
-
#initialize(base_path) ⇒ KitBuilder
constructor
A new instance of KitBuilder.
Constructor Details
#initialize(base_path) ⇒ KitBuilder
Returns a new instance of KitBuilder.
14 15 16 17 18 |
# File 'lib/beats/kit_builder.rb', line 14 def initialize(base_path) @base_path = base_path @labels_to_filenames = {} @composite_replacements = {} end |
Instance Attribute Details
#composite_replacements ⇒ Object (readonly)
Returns the value of attribute composite_replacements.
67 68 69 |
# File 'lib/beats/kit_builder.rb', line 67 def composite_replacements @composite_replacements end |
Instance Method Details
#add_item(label, filenames) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/beats/kit_builder.rb', line 20 def add_item(label, filenames) if filenames.is_a?(Array) if filenames.empty? raise SoundFileNotFoundError, "Kit sound '#{label}' is an empty composite sound (i.e. \"[]\"), which is not valid." end @composite_replacements[label] = [] filenames.each do |filename| unless filename.is_a?(String) raise SoundFileNotFoundError, "The Kit sound '#{label}' contains an invalid file: '#{filename}'" end composite_replacement = "#{label}-#{File.basename(filename, ".*")}" @labels_to_filenames[composite_replacement] = absolute_file_name(filename) @composite_replacements[label] << composite_replacement end else @labels_to_filenames[label] = absolute_file_name(filenames) @composite_replacements.delete(label) end end |
#build_kit ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/beats/kit_builder.rb', line 46 def build_kit # Load each sample buffer filenames_to_buffers = {} @labels_to_filenames.values.uniq.each do |filename| filenames_to_buffers[filename] = load_sample_buffer(filename) end # Convert each buffer to the same sample format num_channels = filenames_to_buffers.values.map(&:channels).max || 1 canonical_format = WaveFile::Format.new(num_channels, SAMPLE_FORMAT, SAMPLE_RATE) filenames_to_buffers.values.each {|buffer| buffer.convert!(canonical_format) } labels_to_buffers = {} @labels_to_filenames.each do |label, filename| labels_to_buffers[label] = filenames_to_buffers[filename].samples end labels_to_buffers[Kit::PLACEHOLDER_TRACK_NAME] = [] Kit.new(labels_to_buffers, num_channels, BITS_PER_SAMPLE) end |
#has_label?(label) ⇒ Boolean
42 43 44 |
# File 'lib/beats/kit_builder.rb', line 42 def has_label?(label) @labels_to_filenames.keys.include?(label) end |