class Mass::Pattern

A single pattern written using the Mass DSL. This is the “collection”-style object which holds each Note and plays them in sequence, but has no control over their durations or pitches.

Attributes

bars[R]
name[R]
notes[R]
sequence[R]

Public Class Methods

create( name: '', bars: 4, repeat: false, sequence: nil, &block ) click to toggle source

Create a new pattern and immediately play it.

@param [String] name @param [Integer] bars @param block

# File lib/mass/pattern.rb, line 28
def self.create(
  name: '', bars: 4, repeat: false, sequence: nil, &block
)
  pattern = new(name: name, bars: bars, sequence: sequence, &block)
  pattern.play in_loop: repeat if pattern.notes.any?
  pattern
end
new(name: '', bars: 4, sequence: nil) { || ... } click to toggle source

@param [String] name @param [Integer] bars @param block

# File lib/mass/pattern.rb, line 15
def initialize(name: '', bars: 4, sequence: nil)
  @name = name
  @bars = bars
  @notes = []
  @sequence = sequence
  yield if block_given?
end

Public Instance Methods

==(other) click to toggle source

Tests equivilance bases on name

@return [Boolean] whether both patterns have the same name

# File lib/mass/pattern.rb, line 48
def ==(other)
  other.name == name
end
play(in_loop: false) click to toggle source

Play the instantiated pattern once, or in a loop if opted into it.

@param [Boolean] in_loop - defaults to false.

# File lib/mass/pattern.rb, line 40
def play(in_loop: false)
  return _play_once unless in_loop
  _play_in_loop
end

Protected Instance Methods

note(value, pitch: nil, expression: nil) click to toggle source

Part of the DSL, the note method instantiates a new Note object with the given parameters and pushes it into the pattern’s notes collection.

@private @param [Integer] value - Value of each note @param [String] pitch - String representation

of MIDI note, e.g. 'c4'

@param [Symbol] expression - Symbolic representation

of MIDI velocity, e.g. ':ff'
# File lib/mass/pattern.rb, line 64
def note(value, pitch: nil, expression: nil)
  @notes << Note.new(
    value: value,
    pitch: pitch,
    exp: expression,
    midi: sequence._midi,
    bpm: sequence._bpm
  )
end
rest(value) click to toggle source
# File lib/mass/pattern.rb, line 74
def rest(value)
  note value
end

Private Instance Methods

_play_in_loop() click to toggle source
# File lib/mass/pattern.rb, line 84
def _play_in_loop
  loop { _play_once }
end
_play_once() click to toggle source
# File lib/mass/pattern.rb, line 80
def _play_once
  notes.all?(&:play)
end