Class: Beats::SongOptimizer

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

Overview

This class is used to transform a Song object into an equivalent Song object whose sample data will be generated faster by the audio engine.

The primary method is optimize(). Currently, it performs two optimizations:

1.) Breaks patterns into shorter patterns. Generating one long Pattern is generally
    slower than generating several short Patterns with the same combined length.
2.) Replaces Patterns which are equivalent (i.e. they have the same tracks with the
    same rhythms) with one canonical Pattern. This allows for better caching, by
    preventing the audio engine from generating the same sample data more than once.

Note that step #1 actually performs double duty, because breaking Patterns into smaller pieces increases the likelihood there will be duplicates that can be combined.

Instance Method Summary collapse

Constructor Details

#initializeSongOptimizer

Returns a new instance of SongOptimizer.



16
17
# File 'lib/beats/songoptimizer.rb', line 16

def initialize
end

Instance Method Details

#optimize(original_song, max_pattern_length) ⇒ Object

Returns a Song that will produce the same output as original_song, but should be generated faster.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/beats/songoptimizer.rb', line 21

def optimize(original_song, max_pattern_length)
  # 1.) Create a new song, cloned from the original
  optimized_song = original_song.copy_ignoring_patterns_and_flow()

  # 2.) Subdivide patterns
  optimized_song = subdivide_song_patterns(original_song, optimized_song, max_pattern_length)

  # 3.) Prune duplicate patterns
  optimized_song = prune_duplicate_patterns(optimized_song)

  optimized_song
end