Class: Mass::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/mass/pattern.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: '', bars: 4, sequence: nil) ⇒ Pattern

Returns a new instance of Pattern.

Parameters:

  • name (String) (defaults to: '')
  • bars (Integer) (defaults to: 4)
  • block


15
16
17
18
19
20
21
# 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

Instance Attribute Details

#barsObject (readonly)

Returns the value of attribute bars.



10
11
12
# File 'lib/mass/pattern.rb', line 10

def bars
  @bars
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/mass/pattern.rb', line 10

def name
  @name
end

#notesObject (readonly)

Returns the value of attribute notes.



10
11
12
# File 'lib/mass/pattern.rb', line 10

def notes
  @notes
end

#sequenceObject (readonly)

Returns the value of attribute sequence.



10
11
12
# File 'lib/mass/pattern.rb', line 10

def sequence
  @sequence
end

Class Method Details

.create(name: '', bars: 4, repeat: false, sequence: nil, &block) ⇒ Object

Create a new pattern and immediately play it.

Parameters:

  • name (String) (defaults to: '')
  • bars (Integer) (defaults to: 4)
  • block


28
29
30
31
32
33
34
# 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

Instance Method Details

#==(other) ⇒ Boolean

Tests equivilance bases on name

Returns:

  • (Boolean)

    whether both patterns have the same name



48
49
50
# File 'lib/mass/pattern.rb', line 48

def ==(other)
  other.name == name
end

#play(in_loop: false) ⇒ Object

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

Parameters:

  • in_loop (Boolean) (defaults to: false)
    • defaults to false.



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

def play(in_loop: false)
  return _play_once unless in_loop
  _play_in_loop
end