Class: SynthBlocks::Sequencer::SequencerDSL::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/synth_blocks/sequencer/sequencer_dsl.rb

Overview

The Pattern class is instantiated by the def_pattern helper

Constant Summary collapse

NOTES =

:nodoc:

%w(C C# D D# E F F# G G# A A# B)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(steps) ⇒ Pattern

:nodoc:



32
33
34
35
# File 'lib/synth_blocks/sequencer/sequencer_dsl.rb', line 32

def initialize(steps) # :nodoc:
  @steps = steps
  @sounds = []
end

Instance Attribute Details

#soundsObject (readonly)

:nodoc:



31
32
33
# File 'lib/synth_blocks/sequencer/sequencer_dsl.rb', line 31

def sounds
  @sounds
end

#stepsObject (readonly)

:nodoc:



31
32
33
# File 'lib/synth_blocks/sequencer/sequencer_dsl.rb', line 31

def steps
  @steps
end

Instance Method Details

#drum_pattern(sound, pattern) ⇒ Object

Define a drum pattern

  • sound is the sound generator object

  • pattern is a pattern in the form of a string

Defining patterns

drum_pattern bass_drum, '*---*---*---!---'
  • * represents a normal drum hit (velocity: 0.5)

  • ! represents an accented drum hit (velocity 1.0)

  • - represents a pause (no hit)



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/synth_blocks/sequencer/sequencer_dsl.rb', line 53

def drum_pattern(sound, pattern)
  events = []
  @steps.times do |i|
    if pattern.chars[i] == '*'
      events << [i, [:start, 36, 0.5]]
    elsif pattern.chars[i] == '!'
      events << [i, [:start, 36, 1.0]]
    end
  end
  @sounds.push([sound, events])
end

#note_pattern(sound, pattern) ⇒ Object

Define a note pattern

sound

sound generator base class

pattern

a note pattern

Defining a note pattern

note_pattern monosynth, [
  ['C4, D#4, G4', 2], P, P, P,
  P, P, P, P,
  P, P, P, P,
  P, P, P, P
]
  • P is a pause

  • a note step in the pattern is an array containing the note and the length of the note in steps

  • a note is a note name as a string, which consists of the note and the octave. To play chords, concatenate notes with commas



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/synth_blocks/sequencer/sequencer_dsl.rb', line 94

def note_pattern(sound, pattern)
  events = []
  @steps.times do |i|
    if pattern[i]
      notes, len = pattern[i]
      notes.split(',').each do |note|
        note_num = str2note(note)
        events << [i, [:start, note_num, 1.0]]
        events << [i + len, [:stop, note_num]]
      end
    end
  end
  @sounds.push([sound, events])
end

#run(block) ⇒ Object

:nodoc:



37
38
39
# File 'lib/synth_blocks/sequencer/sequencer_dsl.rb', line 37

def run(block) # :nodoc:
  instance_eval(&block)
end

#str2note(str) ⇒ Object

:nodoc:



65
66
67
68
69
70
71
72
73
# File 'lib/synth_blocks/sequencer/sequencer_dsl.rb', line 65

def str2note(str) # :nodoc:
  match = str.upcase.strip.match(/([ABCDEFGH]#?)(-?\d)/)
  return nil unless match
  octave = match[2].to_i + 2
  note = NOTES.index(match[1])
  if note >= 0 && octave > 0 && octave < 10
    return 12 * octave + note
  end
end