Class: Chordy::Chord

Inherits:
Object
  • Object
show all
Defined in:
lib/chordy/chord.rb

Direct Known Subclasses

A, ASharp, B, C, CSharp, D, DSharp, E, F, FSharp, G, GSharp

Constant Summary collapse

CHORD_FLAGS =
%w(mute harmonic bend pull hammer_down slide_down slide_up dont_play vibrato)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chord, strings) ⇒ Chord

Returns a new instance of Chord.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chordy/chord.rb', line 73

def initialize chord, strings
  @strings = [-1] * strings
  pad_low = false
  if chord.instance_of? String or chord.instance_of? Symbol
    short_chords = Chord.short_chords
    if short_chords.key? chord
      chord_strings = play short_chords[chord]
      @type = short_chords[chord]
    else
      chord_strings = play chord.to_sym
      @type = chord.to_sym
    end
    pad_low = true
  elsif chord.instance_of? Array
    chord_strings = chord.to_a
  end

  @strings = chord_strings
  pad_or_trim strings, pad_low
  @flags = 0
end

Class Method Details

.all_flagsObject



6
7
8
# File 'lib/chordy/chord.rb', line 6

def self.all_flags
  CHORD_FLAGS.to_a
end

.end_of_strings(tuning, end_delimiter) ⇒ Object



32
33
34
35
# File 'lib/chordy/chord.rb', line 32

def self.end_of_strings tuning, end_delimiter
  num_strings = tuning.length
  ([ end_delimiter ] * num_strings) + [ "" ]
end

.get_num_high_strings(length) ⇒ Object

gets number of high strings in a tuning by approximation



11
12
13
# File 'lib/chordy/chord.rb', line 11

def self.get_num_high_strings length
  (length / 3.0).ceil
end


37
38
39
40
41
42
43
# File 'lib/chordy/chord.rb', line 37

def self.print_half_length_string_at string_pos, tuning, half_length_delimiter, chord_space
  if string_pos == tuning.length
    "".rjust(2)
  else
    half_length_delimiter.rjust(2, chord_space)
  end
end

.short_chordsObject



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
# File 'lib/chordy/chord.rb', line 45

def self.short_chords
  {
    :M => :major,
    :m => :minor,
    :_7 => :dominant_7,
    :_7_5 => :dominant_7_5,
    :_9 => :diminished_9,
    :M6 => :major_6,
    :M7 => :major_7,
    :M9 => :major_9,
    :m5 => :diminished_5,
    :m6 => :minor_6,
    :m7 => :minor_7,
    :m7_5 => :half_diminished_7,
    :mM7 => :minor_major_7,
    :aug5 => :augmented_5,
    :aug7 => :augmented_7,
    :aug7_5 => :augmented_major_7,
    :dim => :diminished_7,
    :dim7 => :diminished_7,
    :dim5 => :diminished_5,
    :dim9 => :diminished_9,
    :sus => :suspended_4,
    :sus4 => :suspended_4,
    :sus7 => :suspended_7,
  }
end

.start_of_strings(tuning, start_delimiter, low_to_high) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chordy/chord.rb', line 15

def self.start_of_strings tuning, start_delimiter, low_to_high
  num_strings = tuning.length
  num_high_strings = get_num_high_strings num_strings

  strings_in_downcase = tuning.map { |s| s.downcase }
  high_strings = strings_in_downcase.last(num_high_strings)
  low_strings = strings_in_downcase.first(num_strings - num_high_strings).map { |s| s.capitalize }

  strings_range = low_strings + high_strings
  if !low_to_high
    strings_range = strings_range.reverse
  end

  strings_to_print = strings_range.map { |s| s.rjust(2) + start_delimiter.rjust(2) }
  strings_to_print + [ " " * 4 ]
end

Instance Method Details

#add_flag(flag) ⇒ Object



142
143
144
145
# File 'lib/chordy/chord.rb', line 142

def add_flag flag
  @flags = @flags | flag
  self
end

#flagsObject



134
135
136
# File 'lib/chordy/chord.rb', line 134

def flags
  @flags
end

#get_index_to_print(i, low_to_high) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/chordy/chord.rb', line 147

def get_index_to_print i, low_to_high
  if low_to_high
    i
  else
    @strings.length - i - 1
  end
end

#has_flag(flag) ⇒ Object



138
139
140
# File 'lib/chordy/chord.rb', line 138

def has_flag flag
  (@flags & flag) == flag
end

#pad_or_trim(length, pad_low) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/chordy/chord.rb', line 95

def pad_or_trim length, pad_low
  if @strings.length > length
    @strings = @strings.last length
  elsif @strings.length < length
    diff = length - @strings.length
    if pad_low
      first = @strings.first
      min = @strings.min

      # play as bar chord
      bar_chord_string = ((first == min) and (min > 0) and (first > 0)) ? first : -1
      @strings = ([bar_chord_string] * diff) + @strings
    else
      @strings = @strings + [-1] * diff
    end

    self
  end
end

#play(chord_type) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/chordy/chord.rb', line 115

def play chord_type
  method_for_chord_type = "play_" + chord_type.to_s
  chord = eval(method_for_chord_type)
  if chord.length > @strings.length
    chord = chord.last @strings.length
  end

  chord
end

for printing flags on diff line



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/chordy/chord.rb', line 198

def print_flag
  to_print = ""

  if has_flag MUTE
    to_print = "M"
  elsif has_flag HARMONIC
    to_print = "H"
  end

  to_print.rjust(3).ljust(4)
end


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/chordy/chord.rb', line 155

def print_string_at i, chord_space, low_to_high=false
  to_print = chord_space
  index_to_print = get_index_to_print i, low_to_high
  string = @strings[index_to_print]
  if string != -1
    to_print = string.to_s
  end

  to_print = to_print.rjust(3, chord_space)

  if @flags != 0
    to_print = print_string_with_flag_at index_to_print, to_print, chord_space
  end

  to_print.ljust(4, chord_space)
end


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/chordy/chord.rb', line 172

def print_string_with_flag_at i, printed_string, chord_space
  to_print = printed_string
  string = @strings[i]

  if string != -1
    if has_flag DONT_PLAY
      to_print = "x".rjust(3, chord_space)
    elsif has_flag BEND
      to_print = to_print + "b"
    elsif has_flag HAMMER_DOWN
      to_print = to_print + "h"
    elsif has_flag PULL
      to_print = to_print + "p"
    elsif has_flag SLIDE_UP
      to_print = to_print + "/"
    elsif has_flag SLIDE_DOWN
      to_print = to_print + "\\"
    elsif has_flag VIBRATO
      to_print = to_print + "~"
    end
  end

  to_print
end

#reverse_strings!Object



125
126
127
128
# File 'lib/chordy/chord.rb', line 125

def reverse_strings!
  @strings = @strings.reverse
  self
end

#stringsObject



130
131
132
# File 'lib/chordy/chord.rb', line 130

def strings
  @strings
end