Class: Beats::Pattern

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

Instance Method Summary collapse

Constructor Details

#initialize(name, tracks = []) ⇒ Pattern

Returns a new instance of Pattern.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/beats/pattern.rb', line 8

def initialize(name, tracks=[])
  @name = name
  @tracks = {}

  longest_track_length = tracks.map {|track| track.rhythm.length }.max

  tracks.each do |track|
    track_key = unique_track_name(track.name)
    new_track = Track.new(track.name, track.rhythm.ljust(longest_track_length, Track::REST))
    @tracks[track_key] = new_track
  end

  @tracks.freeze
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



41
42
43
# File 'lib/beats/pattern.rb', line 41

def name
  @name
end

#tracksObject (readonly)

Returns the value of attribute tracks.



41
42
43
# File 'lib/beats/pattern.rb', line 41

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.

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
# File 'lib/beats/pattern.rb', line 30

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_countObject



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

def step_count
  @tracks.values.collect {|track| track.rhythm.length }.max || 0
end