Class: Fretboards::Fretboard

Inherits:
Object
  • Object
show all
Defined in:
lib/fretboards/fretboard.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf = {}, &block) ⇒ Fretboard

Returns a new instance of Fretboard.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fretboards/fretboard.rb', line 10

def initialize(conf = {}, &block)
  @labels = []
  @marks = []
  @barres = []
  @conf = {
    # default tuning is ukulele tuning
    :tuning => Fretboards::Tuning::UKULELE
  }
  @mutes = []
  @opens = []
  @offset = 0
  @label_offset = 0
  configure(conf)
  self.instance_eval block if block_given?
end

Instance Attribute Details

#barresObject (readonly)

Returns the value of attribute barres.



6
7
8
# File 'lib/fretboards/fretboard.rb', line 6

def barres
  @barres
end

#confObject (readonly)

Returns the value of attribute conf.



6
7
8
# File 'lib/fretboards/fretboard.rb', line 6

def conf
  @conf
end

#label_offsetObject (readonly)

Returns the value of attribute label_offset.



6
7
8
# File 'lib/fretboards/fretboard.rb', line 6

def label_offset
  @label_offset
end

#labelsObject (readonly)

Returns the value of attribute labels.



6
7
8
# File 'lib/fretboards/fretboard.rb', line 6

def labels
  @labels
end

#marksObject (readonly)

Returns the value of attribute marks.



6
7
8
# File 'lib/fretboards/fretboard.rb', line 6

def marks
  @marks
end

#mutesObject (readonly)

Returns the value of attribute mutes.



6
7
8
# File 'lib/fretboards/fretboard.rb', line 6

def mutes
  @mutes
end

#offsetObject (readonly)

Returns the value of attribute offset.



6
7
8
# File 'lib/fretboards/fretboard.rb', line 6

def offset
  @offset
end

#opensObject (readonly)

Returns the value of attribute opens.



6
7
8
# File 'lib/fretboards/fretboard.rb', line 6

def opens
  @opens
end

#titleObject

Returns the value of attribute title.



8
9
10
# File 'lib/fretboards/fretboard.rb', line 8

def title
  @title
end

Instance Method Details

#-(delta) ⇒ Object



179
180
181
# File 'lib/fretboards/fretboard.rb', line 179

def -(delta)
  transpose(-delta)
end

#barre(fret, from = :max, to = 1) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/fretboards/fretboard.rb', line 107

def barre(fret, from = :max, to = 1)
  if fret.is_a? Hash
    b = {}.update(fret)
  else
    from = index_to_string_number(0) if from == :max
    b = {:fret => fret, :from => from, :to => to}
  end
  @barres.push(b)
end

#cloneObject



161
162
163
164
165
166
167
168
# File 'lib/fretboards/fretboard.rb', line 161

def clone
  copy = Fretboard.new
  copy.configure(@conf)
  @marks.each { |m| copy.mark(m) }
  @barres.each { |m| copy.barre(m) }
  @mutes.each { |m| copy.mute(m) }
  copy
end

#configure(conf) ⇒ Object



27
28
29
# File 'lib/fretboards/fretboard.rb', line 27

def configure(conf)
  @conf.update(conf)
end

#fret_range(size = 4) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/fretboards/fretboard.rb', line 195

def fret_range(size = 4)
  if marks.empty?
    [1, size]
  else
    min = marks.inject { |sum, i| i[:fret] < sum[:fret] ? i : sum }[:fret]
    max = marks.inject { |sum, i| i[:fret] > sum[:fret] ? i : sum }[:fret]
    if size >= max
      [1, size]
    else
      # puts "#{self.title} pasa por el segundo hilo"
      min = 1 if min == 0
      max = (min + size) if (size > (max - min) )
      [min + offset, max + offset]
    end
  end
end

#index_to_string_number(i) ⇒ Object



147
148
149
# File 'lib/fretboards/fretboard.rb', line 147

def index_to_string_number(i)
  string_count - i
end

#label(number, offset = 0) ⇒ Object



94
95
96
97
# File 'lib/fretboards/fretboard.rb', line 94

def label(number, offset = 0)
  # TODO we're still not painting this
  @labels[offset] = number
end

#mark(s, f = nil, settings = {}) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/fretboards/fretboard.rb', line 85

def mark(s, f = nil, settings = {})
  if !s.is_a? Hash
    s = { :string => s, :fret => f }.update(settings)
  else
    s = {}.merge(s)
  end
  @marks.push(s)
end

#mark_pitch(pitch, opts = { }) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/fretboards/fretboard.rb', line 130

def mark_pitch(pitch, opts = { })
  diff = Pitch.to_diff(pitch)
  tunings = tuning_to_diffs
  if !opts[:string]
    tunings.each_with_index do |t, i|
      # puts diff - t
      if t <= diff && (!opts[:range] || opts[:range].include?(diff - t))
        # puts pitch, diff-t, opts[:range]
        mark(index_to_string_number(i), diff - t, :symbol => opts[:symbol], :finger => opts[:finger])
      end
    end
  else
    # TODO Warning if the fret number is negative
    mark(opts[:string], diff - tunings[string_number_to_index(string)])
  end
end

#mute(s) ⇒ Object



99
100
101
# File 'lib/fretboards/fretboard.rb', line 99

def mute(s)
  @mutes << s
end

#open(s) ⇒ Object



103
104
105
# File 'lib/fretboards/fretboard.rb', line 103

def open(s)
  mark({:fret => 0, :string => s})
end

#pitch_to_fret(pitch, string) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/fretboards/fretboard.rb', line 122

def pitch_to_fret(pitch, string)
  diff = Pitch.to_diff(pitch)
  tunings = tuning_to_diffs
  t = tunings[string_number_to_index(string)]
  # TODO warn if < 0
  diff - t
end

#set_label_offset(n) ⇒ Object



79
80
81
82
# File 'lib/fretboards/fretboard.rb', line 79

def set_label_offset(n)
  @label_offset = n
  self
end

#set_offset(n) ⇒ Object



74
75
76
77
# File 'lib/fretboards/fretboard.rb', line 74

def set_offset(n)
  @offset = n
  self
end

#string_countObject



117
118
119
120
# File 'lib/fretboards/fretboard.rb', line 117

def string_count
  # @conf[:string_count] ||
  @conf[:tuning].length
end

#string_number_to_index(i) ⇒ Object



151
152
153
154
155
# File 'lib/fretboards/fretboard.rb', line 151

def string_number_to_index(i)
  # 4 -> 0
  # 1 -> 3
  -(i - string_count)
end

#terse(a, opts = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fretboards/fretboard.rb', line 31

def terse(a, opts = {})
  a = a.split(/\s+/) if a.is_a?(String)
  self.title = opts[:title] if opts[:title]
  barres = {}
  a.each_with_index do |m, i|
    attrs = {}
    attrs[:string] = m.match(%r{/(\d+)})[1].to_i rescue index_to_string_number(i)
    attrs[:fret] = m.match(%r{^(\d+)})[1].to_i rescue nil
    has_mute = m.start_with?('x')
    if !attrs[:fret] && !has_mute
      if pitch = m.match(/^([a-g](es|is){0,2}[',]*)/)[1]
        attrs[:fret] = pitch_to_fret(pitch, attrs[:string])
        attrs[:pitch] = pitch
      end
    end
    attrs[:finger] = m.match(%r{-(\d+)})[1].to_i rescue nil
    attrs[:function] = m.match(%r{\(([^\)]*)\)})[1] rescue nil

    if m.include?("!") && m.include?("?")
      attrs[:symbol] = :phantom_root
    else
      attrs[:symbol] = :root if m.include?("!")
      attrs[:symbol] = :phantom if m.include?("?")
    end

    attrs.reject! { |k, v| v.nil? }
    mark(attrs) if attrs[:fret]

    mute(attrs[:string]) if has_mute

    bs = m[-1..-1] == "["
    barres[attrs[:fret]] = [attrs[:fret], attrs[:string]] if bs
    be = m[-1..-1] == "]"
    barres[attrs[:fret]] << attrs[:string] if be
  end

  barres.each do |k, b|
    fret, f, t = *b
    barre(fret, f, t || 1)
  end
  self
end

#transpose(steps) ⇒ Object Also known as: +



170
171
172
173
174
175
# File 'lib/fretboards/fretboard.rb', line 170

def transpose(steps)
  copy = self.clone
  copy.transpose_marks(steps)
  copy.transpose_barres(steps)
  copy
end

#transpose_barres(steps) ⇒ Object



189
190
191
192
193
# File 'lib/fretboards/fretboard.rb', line 189

def transpose_barres(steps)
  @barres.each do |b|
    b[:fret] += steps
  end
end

#transpose_marks(steps) ⇒ Object



183
184
185
186
187
# File 'lib/fretboards/fretboard.rb', line 183

def transpose_marks(steps)
  @marks.each do |m|
    m[:fret] += steps
  end
end

#tuning_to_diffsObject



157
158
159
# File 'lib/fretboards/fretboard.rb', line 157

def tuning_to_diffs
  @tuning_diffs ||= @conf[:tuning].map { |p| Pitch.to_diff(p) }
end