Class: Chords::Fingering

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/chords/fingering.rb

Constant Summary collapse

DEFAULT_MAX_FRET_DISTANCE =
3

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fretboard, positions = nil) ⇒ Fingering

Returns a new instance of Fingering.



12
13
14
15
# File 'lib/chords/fingering.rb', line 12

def initialize(fretboard, positions=nil)
  @fretboard = fretboard
  @positions = positions || ([nil] * @fretboard.open_notes.size)
end

Class Method Details

.find_variations(fretboard, chord, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chords/fingering.rb', line 17

def self.find_variations(fretboard, chord, opts={})
  fingering = Fingering.new(fretboard)
  fingerings = fingering.expand(chord.notes.first)
  
  chord.notes[1..-1].each do |note|
    fingerings = fingerings.map{|f| f.expand(note, opts)}.flatten(1)
  end
  
  (opts[:duplicates] || 0).times do
    fingerings = fingerings.map{|f| f.add_duplicate(opts)}.flatten(1).uniq
  end
  
  fingerings
end

Instance Method Details

#add_duplicate(opts = {}) ⇒ Object

returns variations of this fingering with one duplicate note added



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/chords/fingering.rb', line 55

def add_duplicate(opts={})
  return [] if unused_strings < 1
  max_fret_distance = opts[:max_fret_distance] || DEFAULT_MAX_FRET_DISTANCE
  
  fingerings = []
  
  @positions.each_with_index do |pos, i|
    next unless pos.nil?
    
    each_note do |note|
      new_note_positions(note, i, max_fret_distance).each do |pos|
        new_positions = @positions.dup
        new_positions[i] = pos
        fingerings << Fingering.new(@fretboard, new_positions)
      end
    end
  end
  
  fingerings
end

#each_noteObject



76
77
78
79
80
# File 'lib/chords/fingering.rb', line 76

def each_note
  @positions.each_with_index do |pos, i|
    yield((@fretboard.open_notes[i] + pos).class) unless pos.nil?
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/chords/fingering.rb', line 82

def eql?(other)
  self.hash == other.hash
end

#expand(note, opts = {}) ⇒ Object

Returns all fingering variations of this fingering expanded with note. Expanded note wil be on a higher string than existing notes. It will also be the highest note.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chords/fingering.rb', line 36

def expand(note, opts={})
  max_fret_distance = opts[:max_fret_distance] || DEFAULT_MAX_FRET_DISTANCE
  
  fingerings = []
  
  ((highest_used_string+1)..(@positions.size-1)).each do |str_i|
    new_note_positions(note, str_i, max_fret_distance).each do |pos|
      if (@fretboard.open_notes[str_i] + pos) > highest_note
        new_positions = @positions.dup
        new_positions[str_i] = pos
        fingerings << Fingering.new(@fretboard, new_positions)
      end
    end
  end
  
  fingerings
end

#fidObject

fingering id, a unique identifier for fretboard/fingering combo



91
92
93
94
# File 'lib/chords/fingering.rb', line 91

def fid
  @fretboard.open_notes.map{|n| n.class.title}.to_s + 
    @positions.map{|pos| pos.nil? ? 'x' : pos}.to_s
end

#hashObject



86
87
88
# File 'lib/chords/fingering.rb', line 86

def hash
  @positions.hash
end

#relative(max_fret_dist) ⇒ Object

return an array of relative positions



97
98
99
100
101
102
103
104
105
106
# File 'lib/chords/fingering.rb', line 97

def relative(max_fret_dist)
  not_open = @positions.select{|p| !p.nil? and p > 0}
  
  if not_open.max <= (max_fret_dist + 1)
    @positions
  else
    diff = (not_open.min - 1)
    @positions.map{|p| (p.nil? or p == 0) ? p : p - diff}
  end
end