Class: Beats::KitBuilder

Inherits:
Object
  • Object
show all
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 Method Summary collapse

Constructor Details

#initialize(base_path) ⇒ KitBuilder

Returns a new instance of KitBuilder.



14
15
16
17
# File 'lib/beats/kit_builder.rb', line 14

def initialize(base_path)
  @base_path = base_path
  @labels_to_filenames = {}
end

Instance Method Details

#add_item(label, filename) ⇒ Object



19
20
21
# File 'lib/beats/kit_builder.rb', line 19

def add_item(label, filename)
  @labels_to_filenames[label] = absolute_file_name(filename)
end

#build_kitObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/beats/kit_builder.rb', line 27

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

Returns:

  • (Boolean)


23
24
25
# File 'lib/beats/kit_builder.rb', line 23

def has_label?(label)
  @labels_to_filenames.keys.include?(label)
end