Module: Chordy

Extended by:
Chordy, Util, Util::Tuning
Included in:
Chordy
Defined in:
lib/chordy.rb,
lib/chordy/chord.rb,
lib/chordy/chords/a.rb,
lib/chordy/chords/b.rb,
lib/chordy/chords/c.rb,
lib/chordy/chords/d.rb,
lib/chordy/chords/e.rb,
lib/chordy/chords/f.rb,
lib/chordy/chords/g.rb,
lib/chordy/chords/a_sharp.rb,
lib/chordy/chords/c_sharp.rb,
lib/chordy/chords/d_sharp.rb,
lib/chordy/chords/f_sharp.rb,
lib/chordy/chords/g_sharp.rb

Defined Under Namespace

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

Constant Summary collapse

BFlat =
ASharp
DFlat =
CSharp
EFlat =
DSharp
GFlat =
FSharp
AFlat =
GSharp

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Tuning

is_tuning?, tuning_6_a, tuning_6_b, tuning_6_c, tuning_6_d, tuning_6_drop_a, tuning_6_drop_c, tuning_6_drop_d, tuning_6_standard, tuning_7_a, tuning_7_a_sharp, tuning_7_c, tuning_7_d, tuning_7_drop_a, tuning_7_drop_g, tuning_7_drop_g_sharp, tuning_7_standard, tuning_8_a, tuning_8_drop_d_sharp, tuning_8_drop_e, tuning_8_e, tuning_8_f, tuning_8_standard

Instance Attribute Details

#autoObject

Returns the value of attribute auto.



14
15
16
# File 'lib/chordy.rb', line 14

def auto
  @auto
end

#chord_spaceObject

Returns the value of attribute chord_space.



15
16
17
# File 'lib/chordy.rb', line 15

def chord_space
  @chord_space
end

#chordsObject

Returns the value of attribute chords.



14
15
16
# File 'lib/chordy.rb', line 14

def chords
  @chords
end

#end_delimiterObject

Returns the value of attribute end_delimiter.



15
16
17
# File 'lib/chordy.rb', line 15

def end_delimiter
  @end_delimiter
end

#half_length_delimiterObject

Returns the value of attribute half_length_delimiter.



15
16
17
# File 'lib/chordy.rb', line 15

def half_length_delimiter
  @half_length_delimiter
end

#line_lengthObject

Returns the value of attribute line_length.



14
15
16
# File 'lib/chordy.rb', line 14

def line_length
  @line_length
end

#low_to_highObject

Returns the value of attribute low_to_high.



14
15
16
# File 'lib/chordy.rb', line 14

def low_to_high
  @low_to_high
end

#separator_lengthObject

Returns the value of attribute separator_length.



14
15
16
# File 'lib/chordy.rb', line 14

def separator_length
  @separator_length
end

#start_delimiterObject

Returns the value of attribute start_delimiter.



15
16
17
# File 'lib/chordy.rb', line 15

def start_delimiter
  @start_delimiter
end

#tuningObject

Returns the value of attribute tuning.



14
15
16
# File 'lib/chordy.rb', line 14

def tuning
  @tuning
end

Instance Method Details

#check_chord_class(chord_name) ⇒ Object



113
114
115
# File 'lib/chordy.rb', line 113

def check_chord_class chord_name
  eval("defined?(#{chord_name}) == 'constant' and #{chord_name}.class == Class")
end

#check_sharp_or_flat_chord(chord_name) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chordy.rb', line 99

def check_sharp_or_flat_chord chord_name
  chord = chord_name.capitalize
  sharp_re = /!$/
  flat_re = /_$/

  if sharp_re =~ chord
    chord = chord.gsub(sharp_re, "Sharp")
  elsif flat_re =~ chord
    chord = chord.gsub(flat_re, "Flat")
  end

  chord
end

#clearObject



59
60
61
62
# File 'lib/chordy.rb', line 59

def clear
  Chordy.chords = []
  do_print
end

#do_auto(a = true) ⇒ Object



30
31
32
33
# File 'lib/chordy.rb', line 30

def do_auto a=true
  Chordy.auto = a
  Chordy.auto
end

#do_high_to_lowObject



45
46
47
48
# File 'lib/chordy.rb', line 45

def do_high_to_low
  Chordy.low_to_high = false
  do_print
end

#do_low_to_highObject



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

def do_low_to_high
  Chordy.low_to_high = true
  do_print
end

#do_printObject



166
167
168
169
170
# File 'lib/chordy.rb', line 166

def do_print
  if Chordy.auto
    print_chords
  end
end

#no_autoObject



35
36
37
38
# File 'lib/chordy.rb', line 35

def no_auto
  Chordy.auto = false
  Chordy.auto
end

#play(chords, chord_type_or_direction = :major) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/chordy.rb', line 117

def play chords, chord_type_or_direction=:major
  chord = nil
  begin
    if chords.instance_of? Array
      chord = Chord.new(chords, Chordy.tuning.length)

      # play high-to-low, unless :low_to_high is specified
      if chord_type_or_direction != :low_to_high
        chord.reverse_strings!
      end
    else
      chord_name = chords.to_s
      if !check_chord_class chord_name
        chord_name = check_sharp_or_flat_chord chord_name
      end

      chord_init = "#{chord_name}.new :#{chord_type_or_direction}, #{Chordy.tuning.length}"
      chord = eval(chord_init)
    end

    Chordy.chords.push chord
    do_print
  rescue NameError => ne
    puts "Unknown chord or chord type"
    puts ne.message
    puts ne.backtrace
  rescue Exception => e
    puts e.class.to_s
    puts e.message
    puts e.backtrace
  end

  chord
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/chordy.rb', line 172

def print_chords
  lines_to_print = []
  chord_index = 0
  chords_in_section = 0
  tuning_length = Chordy.tuning.length
  is_done = false
  is_new_line = true
  is_even_line_length = (Chordy.line_length % 2) == 0
  is_next_chord_section_or_text = false
  to_print_start_chords = false
  to_skip_end_strings = false

  chords = Chordy.chords.to_a
  chords.select { |c| c.is_a? Util::Section } .map { |s| s.separator_length = Chordy.separator_length }

  while !is_done
    if is_new_line or to_print_start_chords
      if chords[chord_index].is_a? Chord
        start_strings = Chord.start_of_strings Chordy.tuning, Chordy.start_delimiter, Chordy.low_to_high

        start_strings.each { |s| lines_to_print.push s }
      end
      to_print_start_chords = false
      is_new_line = false
    end

    last_chord_lines = lines_to_print.last(tuning_length + 1)
    curr_chord = chords[chord_index]
    if curr_chord.is_a? Chord
      last_chord_lines.each_with_index do |line, i|
        if i == tuning_length
          line << curr_chord.print_flag
        else
          line << curr_chord.print_string_at(i, Chordy.chord_space, Chordy.low_to_high)
        end
      end

      chords_in_section = chords_in_section + 1
      to_skip_end_strings = false
    elsif (chords[chord_index].is_a? Util::Text) or (chords[chord_index].is_a? Util::Section)
      lines_to_print.push chords[chord_index].to_s
      to_skip_end_strings = true
      chords_in_section = 0

      if chords[chord_index + 1].is_a? Chord
        to_print_start_chords = true
      end
    end

    chord_index = chord_index + 1
    if (chords[chord_index].is_a? Util::Text) or (chords[chord_index].is_a? Util::Section)
      is_next_chord_section_or_text = true
    else
      is_next_chord_section_or_text = false
    end

    if ((chords_in_section % Chordy.line_length) == 0) or (chord_index == chords.length) or is_next_chord_section_or_text
      if to_skip_end_strings
        to_skip_end_strings = false
      else
        end_strings = Chord.end_of_strings Chordy.tuning, Chordy.end_delimiter
        last_chord_lines.each_with_index do |line, i|
          line << end_strings[i]
        end
      end

      # start the next actual line
      lines_to_print.push ""
      is_new_line = true
    elsif (chords_in_section % Chordy.line_length) == (Chordy.line_length / 2) and is_even_line_length
      last_chord_lines.each_with_index do |line, i|
        line << Chord.print_half_length_string_at(i, Chordy.tuning, Chordy.half_length_delimiter, Chordy.chord_space)
      end
    end

    if is_next_chord_section_or_text
      is_new_line = false
    end

    if chord_index >= chords.length
      is_done = true
    end
  end

  # print the buffer
  lines_to_print.each { |l| puts l }
  nil
end


261
262
263
264
265
266
267
# File 'lib/chordy.rb', line 261

def print_chords_to_string
  sio = StringIO.new
  old_stdout, $stdout = $stdout, sio
  print_chords
  $stdout = old_stdout
  sio.string
end

#section(title = "") ⇒ Object



157
158
159
160
# File 'lib/chordy.rb', line 157

def section title=""
  Chordy.chords.push Util::Section.new(title, Chordy.separator_length)
  do_print
end

#separatorObject



162
163
164
# File 'lib/chordy.rb', line 162

def separator
  section
end

#set_chords_to_tuning(tuning) ⇒ Object

TODO document + examples



66
67
68
69
70
71
# File 'lib/chordy.rb', line 66

def set_chords_to_tuning tuning
  longest_tuning_str_length = tuning.max.length
  Chordy.tuning = tuning.map { |e| e.rjust(longest_tuning_str_length) }

  Chordy.chords.select { |c| c.is_a? Chord } .each { |e| e.pad_or_trim Chordy.tuning.length, true }
end

#set_line_length(a) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/chordy.rb', line 50

def set_line_length a
  if a.instance_of? Fixnum
    Chordy.line_length = a
    do_print
  else
    puts "Invalid length"
  end
end

#text(text) ⇒ Object



152
153
154
155
# File 'lib/chordy.rb', line 152

def text text
  Chordy.chords.push Util::Text.new(text)
  do_print
end

#tune(new_tuning, direction = :low_to_high) ⇒ Object



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

def tune new_tuning, direction=:low_to_high
  to_do_print = false
  strings = [6, 7, 8]

  if new_tuning.is_a? Array
    if direction == :high_to_low
      new_tuning = new_tuning.reverse
    end

    set_chords_to_tuning new_tuning
    to_do_print = true
  else
    if is_tuning? new_tuning.to_s
      new_tuning = eval("#{new_tuning}")
      set_chords_to_tuning new_tuning
      to_do_print = true
    else
      puts "Unknown or invalid tuning"
    end
  end

  if to_do_print
    do_print
  end
end