Class: AudioEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/audioengine.rb

Overview

This class actually generates the output audio data that is saved to disk.

To produce audio data, it needs two things: a Song and a Kit. The Song tells it which sounds to trigger and when, while the Kit provides the sample data for each of these sounds.

Example usage, assuming song and kit are already defined:

engine = AudioEngine.new(song, kit)
engine.write_to_file("my_song.wav")

Constant Summary collapse

SAMPLE_RATE =
44100
PACK_CODE =

All output sample data is assumed to be 16-bit

"s*"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(song, kit) ⇒ AudioEngine

Returns a new instance of AudioEngine.



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

def initialize(song, kit)
  @song = song
  @kit = kit
  
  @step_sample_length = AudioUtils.step_sample_length(SAMPLE_RATE, @song.tempo) 
  @composited_pattern_cache = {}
end

Instance Attribute Details

#step_sample_lengthObject (readonly)

Returns the value of attribute step_sample_length.



76
77
78
# File 'lib/audioengine.rb', line 76

def step_sample_length
  @step_sample_length
end

Instance Method Details

#write_to_file(output_file_name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/audioengine.rb', line 24

def write_to_file(output_file_name)
  packed_pattern_cache = {}
  num_tracks_in_song = @song.total_tracks
  samples_written = 0
  
  # Open output wave file and preparing it for writing sample data.
  wave_file = BeatsWaveFile.new(@kit.num_channels, SAMPLE_RATE, @kit.bits_per_sample)
  file = wave_file.open_for_appending(output_file_name)

  # Generate each pattern's sample data, or pull it from cache, and append it to the wave file.
  incoming_overflow = {}
  @song.flow.each do |pattern_name|
    key = [pattern_name, incoming_overflow.hash]
    unless packed_pattern_cache.member?(key)
      sample_data = generate_pattern_sample_data(@song.patterns[pattern_name], incoming_overflow)

      if @kit.num_channels == 1
        # Don't flatten the sample data Array, since it is already flattened. That would be a waste of time, yo.
        packed_pattern_cache[key] = {:primary        => sample_data[:primary].pack(PACK_CODE),
                                     :overflow       => sample_data[:overflow],
                                     :primary_length => sample_data[:primary].length}
      else
        packed_pattern_cache[key] = {:primary        => sample_data[:primary].flatten.pack(PACK_CODE),
                                     :overflow       => sample_data[:overflow],
                                     :primary_length => sample_data[:primary].length}
      end
    end

    file.syswrite(packed_pattern_cache[key][:primary])
    incoming_overflow = packed_pattern_cache[key][:overflow]
    samples_written += packed_pattern_cache[key][:primary_length]
  end

  # Write any remaining overflow from the final pattern
  final_overflow_composite = AudioUtils.composite(incoming_overflow.values, @kit.num_channels)
  final_overflow_composite = AudioUtils.scale(final_overflow_composite, @kit.num_channels, num_tracks_in_song)
  if @kit.num_channels == 1
    file.syswrite(final_overflow_composite.pack(PACK_CODE))
  else
    file.syswrite(final_overflow_composite.flatten.pack(PACK_CODE))
  end
  samples_written += final_overflow_composite.length
  
  # Now that we know how many samples have been written, go back and re-write the correct header.
  file.sysseek(0)
  wave_file.write_header(file, samples_written)

  file.close()

  return wave_file.calculate_duration(SAMPLE_RATE, samples_written)
end