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).
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#tracks ⇒ Object
readonly
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
-
#track(name, rhythm) ⇒ Object
Adds a new track to the pattern.
Constructor Details
#initialize(name) ⇒ Pattern
Returns a new instance of Pattern.
8 9 10 11 |
# File 'lib/beats/pattern.rb', line 8 def initialize(name) @name = name @tracks = {} end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
50 51 52 |
# File 'lib/beats/pattern.rb', line 50 def name @name end |
#tracks ⇒ Object (readonly)
Returns the value of attribute tracks.
50 51 52 |
# File 'lib/beats/pattern.rb', line 50 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.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/beats/pattern.rb', line 39 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
32 33 34 |
# File 'lib/beats/pattern.rb', line 32 def step_count @tracks.values.collect {|track| track.rhythm.length }.max || 0 end |
#track(name, rhythm) ⇒ Object
Adds a new track to the pattern.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/beats/pattern.rb', line 14 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 |