Class: Beats::Pattern
- Inherits:
-
Object
- Object
- Beats::Pattern
- Defined in:
- lib/beats/pattern.rb
Overview
Domain object which models one or more Tracks playing a part of the song at the same time. For example, a bass drum, snare drum, and hi-hat track playing the song’s chorus.
This object is like sheet music; the AudioEngine is responsible creating actual audio data for a Pattern (with the help of a Kit).
Constant Summary collapse
- FLOW_TRACK_NAME =
"flow"
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#tracks ⇒ Object
Returns the value of attribute tracks.
Instance Method Summary collapse
-
#initialize(name) ⇒ Pattern
constructor
A new instance of Pattern.
-
#same_tracks_as?(other_pattern) ⇒ Boolean
Returns whether or not this pattern has the same number of tracks as other_pattern, and that each of the tracks has the same name and rhythm.
- #step_count ⇒ Object
-
#to_yaml ⇒ Object
Returns a YAML representation of the Pattern.
-
#track(name, rhythm) ⇒ Object
Adds a new track to the pattern.
Constructor Details
#initialize(name) ⇒ Pattern
Returns a new instance of Pattern.
10 11 12 13 |
# File 'lib/beats/pattern.rb', line 10 def initialize(name) @name = name @tracks = {} end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
70 71 72 |
# File 'lib/beats/pattern.rb', line 70 def name @name end |
#tracks ⇒ Object
Returns the value of attribute tracks.
70 71 72 |
# File 'lib/beats/pattern.rb', line 70 def tracks @tracks end |
Instance Method Details
#same_tracks_as?(other_pattern) ⇒ Boolean
Returns whether or not this pattern has the same number of tracks as other_pattern, and that each of the tracks has the same name and rhythm. Ordering of tracks does not matter; will return true if the two patterns have the same tracks but in a different ordering.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/beats/pattern.rb', line 41 def same_tracks_as?(other_pattern) @tracks.keys.each do |track_name| other_pattern_track = other_pattern.tracks[track_name] if other_pattern_track.nil? || @tracks[track_name].rhythm != other_pattern_track.rhythm return false end end @tracks.length == other_pattern.tracks.length end |
#step_count ⇒ Object
34 35 36 |
# File 'lib/beats/pattern.rb', line 34 def step_count @tracks.values.collect {|track| track.rhythm.length }.max || 0 end |
#to_yaml ⇒ Object
Returns a YAML representation of the Pattern. Produces nicer looking output than the default version of to_yaml().
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/beats/pattern.rb', line 54 def to_yaml longest_track_name_length = @tracks.keys.inject(0) do |max_length, name| (name.to_s.length > max_length) ? name.to_s.length : max_length end ljust_amount = longest_track_name_length + 7 yaml = "#{@name.to_s.capitalize}:\n" @tracks.keys.sort.each do |track_name| yaml += " - #{track_name}:".ljust(ljust_amount) yaml += "#{@tracks[track_name].rhythm}\n" end yaml end |
#track(name, rhythm) ⇒ Object
Adds a new track to the pattern.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/beats/pattern.rb', line 16 def track(name, rhythm) track_key = unique_track_name(name) new_track = Track.new(name, rhythm) @tracks[track_key] = new_track # If the new track is longer than any of the previously added tracks, # pad the other tracks with trailing . to make them all the same length. # Necessary to prevent incorrect overflow calculations for tracks. longest_track_length = step_count() @tracks.values.each do |track| if track.rhythm.length < longest_track_length track.rhythm += "." * (longest_track_length - track.rhythm.length) end end new_track end |