chordy 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +8 -0
- data/Gemfile +16 -0
- data/LICENSE.rdoc +24 -0
- data/README.rdoc +196 -0
- data/Rakefile +47 -0
- data/lib/a_chords.rb +81 -0
- data/lib/a_sharp_chords.rb +83 -0
- data/lib/b_chords.rb +81 -0
- data/lib/c_chords.rb +81 -0
- data/lib/c_sharp_chords.rb +83 -0
- data/lib/chord.rb +193 -0
- data/lib/chordy.rb +287 -0
- data/lib/chordy_script.rb +5 -0
- data/lib/d_chords.rb +81 -0
- data/lib/d_sharp_chords.rb +83 -0
- data/lib/e_chords.rb +81 -0
- data/lib/f_chords.rb +81 -0
- data/lib/f_sharp_chords.rb +83 -0
- data/lib/g_chords.rb +81 -0
- data/lib/g_sharp_chords.rb +83 -0
- data/lib/section.rb +13 -0
- data/lib/text.rb +11 -0
- data/lib/tuning.rb +109 -0
- data/test/helper.rb +21 -0
- data/test/test_chordy.rb +49 -0
- metadata +187 -0
    
        data/lib/b_chords.rb
    ADDED
    
    | @@ -0,0 +1,81 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'chord'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            class B < Chord
         | 
| 6 | 
            +
              def play_major
         | 
| 7 | 
            +
                [2, 2, 4, 4, 4, 2]
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              def play_minor
         | 
| 11 | 
            +
                [2, 2, 4, 4, 3, 2]
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              def play_dominant_7
         | 
| 15 | 
            +
                [2, 2, 4, 2, 4, 2]
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              def play_dominant_7_5
         | 
| 19 | 
            +
                [-1, 2, 1, 2, 0, 1]
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              def play_major_6
         | 
| 23 | 
            +
                [2, 2, 4, 4, 4, 4]
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              def play_major_7
         | 
| 27 | 
            +
                [2, 2, 4, 3, 4, 2]
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              def play_major_9
         | 
| 31 | 
            +
                [-1, 2, 1, 2, 2, 2]
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
              def play_minor_6
         | 
| 35 | 
            +
                [-1, 2, 0, 1, 3, 2]
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              def play_minor_7
         | 
| 39 | 
            +
                [-1, 2, 0, 2, 0, 2]
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              def play_half_diminished_7
         | 
| 43 | 
            +
                [1, 0, 0, 2, 0, 1]
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              def play_minor_major_7
         | 
| 47 | 
            +
                [2, 2, 4, 3, 3, 2]
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              def play_augmented_5
         | 
| 51 | 
            +
                [3, -1, 5, 4, 4, 3]
         | 
| 52 | 
            +
              end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
              def play_augmented_7
         | 
| 55 | 
            +
                [-1, 2, 1, 2, 0, 3]
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
              def play_augmented_major_7
         | 
| 59 | 
            +
                [-1, 2, -1, 3, 4, 3]
         | 
| 60 | 
            +
              end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
              def play_diminished_5
         | 
| 63 | 
            +
                [1, -1, 3, 4, 3, 1]
         | 
| 64 | 
            +
              end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
              def play_diminished_7
         | 
| 67 | 
            +
                [-1, -1, 0, 1, 0, 1]
         | 
| 68 | 
            +
              end
         | 
| 69 | 
            +
             | 
| 70 | 
            +
              def play_diminished_9
         | 
| 71 | 
            +
                [-1, 2, 1, 2, 1, 2]
         | 
| 72 | 
            +
              end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
              def play_suspended_4
         | 
| 75 | 
            +
                [2, 2, 4, 4, 5, 2]
         | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              def play_suspended_7
         | 
| 79 | 
            +
                [2, 2, 2, 2, 5, 2]
         | 
| 80 | 
            +
              end
         | 
| 81 | 
            +
            end
         | 
    
        data/lib/c_chords.rb
    ADDED
    
    | @@ -0,0 +1,81 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'chord'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            class C < Chord
         | 
| 6 | 
            +
              def play_major
         | 
| 7 | 
            +
                [3, 3, 2, 0, 1, 0]
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              def play_minor
         | 
| 11 | 
            +
                [-1, 3, 1, 0, 4, 3]
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              def play_dominant_7
         | 
| 15 | 
            +
                [0, 3, 2, 3, 1, 0]
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              def play_dominant_7_5
         | 
| 19 | 
            +
                [-1, 1, 2, 3, 1, 2]
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              def play_major_6
         | 
| 23 | 
            +
                [0, 3, 2, 1, 1, 0]
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              def play_major_7
         | 
| 27 | 
            +
                [0, 3, 2, 0, 0, 0]
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              def play_major_9
         | 
| 31 | 
            +
                [0, 3, 2, 3, 3, 0]
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
              def play_minor_6
         | 
| 35 | 
            +
                [-1, 3, 1, 2, 1, 3]
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              def play_minor_7
         | 
| 39 | 
            +
                [-1, 1, 1, 3, 1, 3]
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              def play_half_diminished_7
         | 
| 43 | 
            +
                [-1, 1, 1, 3, 1, 2]
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              def play_minor_major_7
         | 
| 47 | 
            +
                [-1, 2, 1, 0, 0, 3]
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              def play_augmented_5
         | 
| 51 | 
            +
                [0, 3, 2, 1, 1, 0]
         | 
| 52 | 
            +
              end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
              def play_augmented_7
         | 
| 55 | 
            +
                [-1, 1, 2, 3, 1, 4]
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
              def play_augmented_major_7
         | 
| 59 | 
            +
                [0, 3, 2, 1, 0, 0]
         | 
| 60 | 
            +
              end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
              def play_diminished_5
         | 
| 63 | 
            +
                [-1, -1, 4, 5, 4, 2]
         | 
| 64 | 
            +
              end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
              def play_diminished_7
         | 
| 67 | 
            +
                [-1, -1, 1, 2, 1, 2]
         | 
| 68 | 
            +
              end
         | 
| 69 | 
            +
             | 
| 70 | 
            +
              def play_diminished_9
         | 
| 71 | 
            +
                [-1, 3, 2, 3, 2, 3]
         | 
| 72 | 
            +
              end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
              def play_suspended_4
         | 
| 75 | 
            +
                [3, 3, 3, 0, 1, 1]
         | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              def play_suspended_7
         | 
| 79 | 
            +
                [1, 1, 3, 3, 1, 1]
         | 
| 80 | 
            +
              end
         | 
| 81 | 
            +
            end
         | 
| @@ -0,0 +1,83 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'chord'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            class CSharp < Chord
         | 
| 6 | 
            +
              def play_major
         | 
| 7 | 
            +
                [1, 4, 3, 1, 2, 1]
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              def play_minor
         | 
| 11 | 
            +
                [0, -1, 2, 1, 2, 4]
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              def play_dominant_7
         | 
| 15 | 
            +
                [1, 2, 3, 1, 2, 1]
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              def play_dominant_7_5
         | 
| 19 | 
            +
                [-1, 2, 3, 4, 2, 3]
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
              
         | 
| 22 | 
            +
              def play_major_6
         | 
| 23 | 
            +
                [1, 1, 3, 1, 2, 1]
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              def play_major_7
         | 
| 27 | 
            +
                [1, 4, 3, 1, 1, 1]
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              def play_major_9
         | 
| 31 | 
            +
                [1, 2, 1, 1, 2, 1]
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
              def play_minor_6
         | 
| 35 | 
            +
                [0, 4, 2, 3, 2, 4]
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              def play_minor_7
         | 
| 39 | 
            +
                [0, 2, 2, 4, 2, 4]
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
              
         | 
| 42 | 
            +
              def play_half_diminished_7
         | 
| 43 | 
            +
                [0, 2, 2, 4, 2, 3]
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              def play_minor_major_7
         | 
| 47 | 
            +
                [0, 3, 2, 1, 1, 4]
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              def play_augmented_5
         | 
| 51 | 
            +
                [-1, 0, 3, 2, 2, 1]
         | 
| 52 | 
            +
              end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
              def play_augmented_7
         | 
| 55 | 
            +
                [-1, 2, 3, 4, 2, 5]
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
              
         | 
| 58 | 
            +
              def play_augmented_major_7
         | 
| 59 | 
            +
                [1, 4, 3, 2, 1, 1]
         | 
| 60 | 
            +
              end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
              def play_diminished_5
         | 
| 63 | 
            +
                [0, -1, 2, 0, 2, 3]
         | 
| 64 | 
            +
              end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
              def play_diminished_7
         | 
| 67 | 
            +
                [-1, -1, 2, 3, 2, 3]
         | 
| 68 | 
            +
              end
         | 
| 69 | 
            +
             | 
| 70 | 
            +
              def play_diminished_9
         | 
| 71 | 
            +
                [-1, 4, 3, 4, 3, 4]
         | 
| 72 | 
            +
              end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
              def play_suspended_4
         | 
| 75 | 
            +
                [4, 4, 6, 6, 7, 4]
         | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              def play_suspended_7
         | 
| 79 | 
            +
                [2, 2, 4, 4, 2, 2]
         | 
| 80 | 
            +
              end
         | 
| 81 | 
            +
            end
         | 
| 82 | 
            +
             | 
| 83 | 
            +
            DFlat = CSharp
         | 
    
        data/lib/chord.rb
    ADDED
    
    | @@ -0,0 +1,193 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class Chord
         | 
| 4 | 
            +
              CHORD_FLAGS = %w(mute harmonic bend pull hammer_down slide_down slide_up dont_play vibrato)
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              def self.all_flags
         | 
| 7 | 
            +
                CHORD_FLAGS.to_a
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
              
         | 
| 10 | 
            +
              def self.start_of_strings tuning, start_delimiter
         | 
| 11 | 
            +
                num_strings = tuning.length
         | 
| 12 | 
            +
                (0...num_strings).map { |s| tuning[s].rjust(2) + start_delimiter.rjust(2) } + [ " " * 4 ]
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
              
         | 
| 15 | 
            +
              def self.end_of_strings tuning, end_delimiter
         | 
| 16 | 
            +
                num_strings = tuning.length
         | 
| 17 | 
            +
                ([ end_delimiter ] * num_strings) + [ "" ]
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              def self.print_half_length_string_at string_pos, tuning, half_length_delimiter, chord_space
         | 
| 21 | 
            +
                if string_pos == tuning.length
         | 
| 22 | 
            +
                  "".rjust(2)
         | 
| 23 | 
            +
                else
         | 
| 24 | 
            +
                  half_length_delimiter.rjust(2, chord_space)
         | 
| 25 | 
            +
                end
         | 
| 26 | 
            +
              end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
              def self.short_chords
         | 
| 29 | 
            +
                {
         | 
| 30 | 
            +
                  :M => :major,
         | 
| 31 | 
            +
                  :m => :minor,
         | 
| 32 | 
            +
                  :_7 => :dominant_7,
         | 
| 33 | 
            +
                  :_7_5 => :dominant_7_5,
         | 
| 34 | 
            +
                  :_9 => :diminished_9,
         | 
| 35 | 
            +
                  :M6 => :major_6,
         | 
| 36 | 
            +
                  :M7 => :major_7,
         | 
| 37 | 
            +
                  :M9 => :major_9,
         | 
| 38 | 
            +
                  :m5 => :diminished_5,
         | 
| 39 | 
            +
                  :m6 => :minor_6,
         | 
| 40 | 
            +
                  :m7 => :minor_7,
         | 
| 41 | 
            +
                  :m7_5 => :half_diminished_7,
         | 
| 42 | 
            +
                  :mM7 => :minor_major_7,
         | 
| 43 | 
            +
                  :aug5 => :augmented_5,
         | 
| 44 | 
            +
                  :aug7 => :augmented_7,
         | 
| 45 | 
            +
                  :aug7_5 => :augmented_major_7,
         | 
| 46 | 
            +
                  :dim => :diminished_7,
         | 
| 47 | 
            +
                  :dim7 => :diminished_7,
         | 
| 48 | 
            +
                  :dim5 => :diminished_5,
         | 
| 49 | 
            +
                  :dim9 => :diminished_9,
         | 
| 50 | 
            +
                  :sus => :suspended_4,
         | 
| 51 | 
            +
                  :sus4 => :suspended_4,
         | 
| 52 | 
            +
                  :sus7 => :suspended_7,
         | 
| 53 | 
            +
                }
         | 
| 54 | 
            +
              end
         | 
| 55 | 
            +
              
         | 
| 56 | 
            +
              def initialize chord, strings
         | 
| 57 | 
            +
                @strings = [-1] * strings
         | 
| 58 | 
            +
                pad_low = false
         | 
| 59 | 
            +
                if chord.instance_of? String or chord.instance_of? Symbol
         | 
| 60 | 
            +
                  short_chords = Chord.short_chords
         | 
| 61 | 
            +
                  if short_chords.key? chord
         | 
| 62 | 
            +
                    chord_strings = play short_chords[chord]
         | 
| 63 | 
            +
                    @type = short_chords[chord]
         | 
| 64 | 
            +
                  else
         | 
| 65 | 
            +
                    chord_strings = play chord.to_sym
         | 
| 66 | 
            +
                    @type = chord.to_sym
         | 
| 67 | 
            +
                  end
         | 
| 68 | 
            +
                  pad_low = true
         | 
| 69 | 
            +
                elsif chord.instance_of? Array
         | 
| 70 | 
            +
                  chord_strings = chord.to_a
         | 
| 71 | 
            +
                end
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                @strings = chord_strings
         | 
| 74 | 
            +
                pad_or_trim strings, pad_low
         | 
| 75 | 
            +
                @flags = 0
         | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
              
         | 
| 78 | 
            +
              def pad_or_trim length, pad_low
         | 
| 79 | 
            +
                if @strings.length > length
         | 
| 80 | 
            +
                  @strings = @strings.last length
         | 
| 81 | 
            +
                elsif @strings.length < length
         | 
| 82 | 
            +
                  diff = length - @strings.length
         | 
| 83 | 
            +
                  if pad_low
         | 
| 84 | 
            +
                    first = @strings.first
         | 
| 85 | 
            +
                    min = @strings.min
         | 
| 86 | 
            +
                    
         | 
| 87 | 
            +
                    # play as bar chord
         | 
| 88 | 
            +
                    bar_chord_string = ((first == min) and (min > 0) and (first > 0)) ? first : -1
         | 
| 89 | 
            +
                    @strings = ([bar_chord_string] * diff) + @strings
         | 
| 90 | 
            +
                  else
         | 
| 91 | 
            +
                    @strings = @strings + [-1] * diff
         | 
| 92 | 
            +
                  end
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                  self
         | 
| 95 | 
            +
                end
         | 
| 96 | 
            +
              end
         | 
| 97 | 
            +
             | 
| 98 | 
            +
              def play chord_type
         | 
| 99 | 
            +
                method_for_chord_type = "play_" + chord_type.to_s
         | 
| 100 | 
            +
                chord = eval(method_for_chord_type)
         | 
| 101 | 
            +
                chord
         | 
| 102 | 
            +
              end
         | 
| 103 | 
            +
             | 
| 104 | 
            +
              def strings
         | 
| 105 | 
            +
                @strings
         | 
| 106 | 
            +
              end
         | 
| 107 | 
            +
             | 
| 108 | 
            +
              def flags
         | 
| 109 | 
            +
                @flags
         | 
| 110 | 
            +
              end
         | 
| 111 | 
            +
             | 
| 112 | 
            +
              def has_flag flag
         | 
| 113 | 
            +
                (@flags & flag) == flag
         | 
| 114 | 
            +
              end
         | 
| 115 | 
            +
             | 
| 116 | 
            +
              def add_flag flag
         | 
| 117 | 
            +
                @flags = @flags | flag
         | 
| 118 | 
            +
                self
         | 
| 119 | 
            +
              end
         | 
| 120 | 
            +
             | 
| 121 | 
            +
              def print_string_at i, chord_space
         | 
| 122 | 
            +
                to_print = chord_space
         | 
| 123 | 
            +
                string = @strings[i]
         | 
| 124 | 
            +
                if string != -1
         | 
| 125 | 
            +
                  to_print = string.to_s
         | 
| 126 | 
            +
                end
         | 
| 127 | 
            +
             | 
| 128 | 
            +
                to_print = to_print.rjust(3, chord_space)
         | 
| 129 | 
            +
               
         | 
| 130 | 
            +
                if @flags != 0
         | 
| 131 | 
            +
                  to_print = print_string_with_flag_at i, to_print, chord_space
         | 
| 132 | 
            +
                end
         | 
| 133 | 
            +
                
         | 
| 134 | 
            +
                to_print.ljust(4, chord_space)
         | 
| 135 | 
            +
              end
         | 
| 136 | 
            +
             | 
| 137 | 
            +
              def print_string_with_flag_at i, printed_string, chord_space
         | 
| 138 | 
            +
                to_print = printed_string
         | 
| 139 | 
            +
                string = @strings[i]
         | 
| 140 | 
            +
             | 
| 141 | 
            +
                if string != -1
         | 
| 142 | 
            +
                  if has_flag DONT_PLAY
         | 
| 143 | 
            +
                    to_print = "x".rjust(3, chord_space)
         | 
| 144 | 
            +
                  elsif has_flag BEND
         | 
| 145 | 
            +
                    to_print = to_print + "b"
         | 
| 146 | 
            +
                  elsif has_flag HAMMER_DOWN
         | 
| 147 | 
            +
                    to_print = to_print + "h"
         | 
| 148 | 
            +
                  elsif has_flag PULL
         | 
| 149 | 
            +
                    to_print = to_print + "p"
         | 
| 150 | 
            +
                  elsif has_flag SLIDE_UP
         | 
| 151 | 
            +
                    to_print = to_print + "/"
         | 
| 152 | 
            +
                  elsif has_flag SLIDE_DOWN
         | 
| 153 | 
            +
                    to_print = to_print + "\\"
         | 
| 154 | 
            +
                  elsif has_flag VIBRATO
         | 
| 155 | 
            +
                    to_print = to_print + "~"
         | 
| 156 | 
            +
                  end
         | 
| 157 | 
            +
                end
         | 
| 158 | 
            +
             | 
| 159 | 
            +
                to_print
         | 
| 160 | 
            +
              end
         | 
| 161 | 
            +
             | 
| 162 | 
            +
              # for printing flags on diff line
         | 
| 163 | 
            +
              def print_flag 
         | 
| 164 | 
            +
                to_print = ""
         | 
| 165 | 
            +
             | 
| 166 | 
            +
                if has_flag MUTE
         | 
| 167 | 
            +
                  to_print = "M"
         | 
| 168 | 
            +
                elsif has_flag HARMONIC
         | 
| 169 | 
            +
                  to_print = "H"
         | 
| 170 | 
            +
                end
         | 
| 171 | 
            +
             | 
| 172 | 
            +
                to_print.rjust(3).ljust(4)
         | 
| 173 | 
            +
              end 
         | 
| 174 | 
            +
            end
         | 
| 175 | 
            +
             | 
| 176 | 
            +
            Chord::CHORD_FLAGS.each_with_index do |name,i|
         | 
| 177 | 
            +
              Chord.class_eval <<-ENDOFEVAL
         | 
| 178 | 
            +
                #{name.upcase} = #{2**i}
         | 
| 179 | 
            +
                private 
         | 
| 180 | 
            +
                def #{name}()
         | 
| 181 | 
            +
                  add_flag #{name.upcase}
         | 
| 182 | 
            +
                end
         | 
| 183 | 
            +
                ENDOFEVAL
         | 
| 184 | 
            +
            end
         | 
| 185 | 
            +
             | 
| 186 | 
            +
            Chord.short_chords.values.each do |chord_type|
         | 
| 187 | 
            +
              chord_type_str = chord_type.to_s
         | 
| 188 | 
            +
              Chord.class_eval <<-ENDOFEVAL
         | 
| 189 | 
            +
                 def play_#{chord_type_str}
         | 
| 190 | 
            +
                   [-1] * 6
         | 
| 191 | 
            +
                 end
         | 
| 192 | 
            +
                 ENDOFEVAL
         | 
| 193 | 
            +
            end
         | 
    
        data/lib/chordy.rb
    ADDED
    
    | @@ -0,0 +1,287 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'chord'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            require 'c_chords'
         | 
| 6 | 
            +
            require 'c_sharp_chords'
         | 
| 7 | 
            +
            require 'd_chords'
         | 
| 8 | 
            +
            require 'd_sharp_chords'
         | 
| 9 | 
            +
            require 'e_chords'
         | 
| 10 | 
            +
            require 'f_chords'
         | 
| 11 | 
            +
            require 'f_sharp_chords'
         | 
| 12 | 
            +
            require 'g_chords'
         | 
| 13 | 
            +
            require 'g_sharp_chords'
         | 
| 14 | 
            +
            require 'a_chords'
         | 
| 15 | 
            +
            require 'a_sharp_chords'
         | 
| 16 | 
            +
            require 'b_chords'
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            require 'text'
         | 
| 19 | 
            +
            require 'section'
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            require 'tuning'
         | 
| 22 | 
            +
            include Tuning
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            module Chordy
         | 
| 25 | 
            +
              $line_length = 8
         | 
| 26 | 
            +
              $separator_length = 40
         | 
| 27 | 
            +
              $chords = []
         | 
| 28 | 
            +
              $auto = true
         | 
| 29 | 
            +
              $tuning = tuning_6_standard.map { |e| e.capitalize  }
         | 
| 30 | 
            +
              $reverse = false
         | 
| 31 | 
            +
             | 
| 32 | 
            +
              # printing delimiters
         | 
| 33 | 
            +
              $chord_space = "-"
         | 
| 34 | 
            +
              $half_length_delimiter = "|"
         | 
| 35 | 
            +
              $start_delimiter = "["
         | 
| 36 | 
            +
              $end_delimiter = "]"
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              def auto a=true
         | 
| 39 | 
            +
                $auto = if a then true else false end
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              def no_auto
         | 
| 43 | 
            +
                auto false
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              def line_length a
         | 
| 47 | 
            +
                if a.instance_of? Fixnum
         | 
| 48 | 
            +
                  $line_length = a
         | 
| 49 | 
            +
                  do_print
         | 
| 50 | 
            +
                else
         | 
| 51 | 
            +
                  puts "Invalid length"
         | 
| 52 | 
            +
                end
         | 
| 53 | 
            +
              end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
              def clear
         | 
| 56 | 
            +
                $chords = []
         | 
| 57 | 
            +
                do_print
         | 
| 58 | 
            +
              end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
              # TODO document + examples
         | 
| 61 | 
            +
             | 
| 62 | 
            +
              def set_tuning_with_padding tuning
         | 
| 63 | 
            +
                longest_tuning_str_length = tuning.max.length
         | 
| 64 | 
            +
                $tuning = tuning.map { |e| e.capitalize.rjust(longest_tuning_str_length) }
         | 
| 65 | 
            +
                
         | 
| 66 | 
            +
                $chords = $chords.each { |e| e.pad_or_trim $tuning.length, true }
         | 
| 67 | 
            +
              end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
              def tune new_tuning
         | 
| 70 | 
            +
                to_do_print = false
         | 
| 71 | 
            +
                strings = [6, 7, 8]
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                if new_tuning.is_a? Array
         | 
| 74 | 
            +
                  if strings.include? new_tuning.length
         | 
| 75 | 
            +
                    set_tuning_with_padding new_tuning
         | 
| 76 | 
            +
                    to_do_print = true
         | 
| 77 | 
            +
                  else
         | 
| 78 | 
            +
                    puts "Invalid tuning; only " + strings.join(",") + " strings are allowed" 
         | 
| 79 | 
            +
                  end
         | 
| 80 | 
            +
                else
         | 
| 81 | 
            +
                  if is_tuning? new_tuning.to_s
         | 
| 82 | 
            +
                    new_tuning = eval("#{new_tuning}")
         | 
| 83 | 
            +
                    set_tuning_with_padding new_tuning
         | 
| 84 | 
            +
                    to_do_print = true
         | 
| 85 | 
            +
                  else
         | 
| 86 | 
            +
                    puts "Unknown or invalid tuning"
         | 
| 87 | 
            +
                  end
         | 
| 88 | 
            +
                end
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                if to_do_print
         | 
| 91 | 
            +
                  do_print
         | 
| 92 | 
            +
                end
         | 
| 93 | 
            +
              end
         | 
| 94 | 
            +
             | 
| 95 | 
            +
              def check_sharp_or_flat_chord chord_name
         | 
| 96 | 
            +
                chord = chord_name.capitalize
         | 
| 97 | 
            +
                sharp_re = /!$/
         | 
| 98 | 
            +
                flat_re = /_$/
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                if sharp_re =~ chord
         | 
| 101 | 
            +
                  chord = chord.gsub(sharp_re, "Sharp")
         | 
| 102 | 
            +
                elsif flat_re =~ chord
         | 
| 103 | 
            +
                  chord = chord.gsub(flat_re, "Flat")
         | 
| 104 | 
            +
                end
         | 
| 105 | 
            +
             | 
| 106 | 
            +
                chord
         | 
| 107 | 
            +
              end
         | 
| 108 | 
            +
             | 
| 109 | 
            +
              def check_chord_class chord_name
         | 
| 110 | 
            +
                eval("defined?(#{chord_name}) == 'constant' and #{chord_name}.class == Class")
         | 
| 111 | 
            +
              end
         | 
| 112 | 
            +
             | 
| 113 | 
            +
              def play chords, chord_type=:major
         | 
| 114 | 
            +
                chord = nil
         | 
| 115 | 
            +
                begin
         | 
| 116 | 
            +
                  if chords.instance_of? Array
         | 
| 117 | 
            +
                    chord = Chord.new(chords, $tuning.length)
         | 
| 118 | 
            +
                  else
         | 
| 119 | 
            +
                    chord_name = chords.to_s
         | 
| 120 | 
            +
                    if !check_chord_class chord_name
         | 
| 121 | 
            +
                      chord_name = check_sharp_or_flat_chord chord_name
         | 
| 122 | 
            +
                    end
         | 
| 123 | 
            +
             | 
| 124 | 
            +
                    chord_init = "#{chord_name}.new :#{chord_type}, #{$tuning.length}"
         | 
| 125 | 
            +
                    chord = eval(chord_init)
         | 
| 126 | 
            +
                  end
         | 
| 127 | 
            +
             | 
| 128 | 
            +
                  $chords.push chord
         | 
| 129 | 
            +
                  do_print
         | 
| 130 | 
            +
                rescue NameError => ne
         | 
| 131 | 
            +
                  puts "Unknown chord or chord type"
         | 
| 132 | 
            +
                  puts ne.message
         | 
| 133 | 
            +
                rescue Exception => e
         | 
| 134 | 
            +
                  puts e.class.to_s
         | 
| 135 | 
            +
                  puts e.message
         | 
| 136 | 
            +
                end
         | 
| 137 | 
            +
             | 
| 138 | 
            +
                chord
         | 
| 139 | 
            +
              end
         | 
| 140 | 
            +
             | 
| 141 | 
            +
              def text text
         | 
| 142 | 
            +
                $chords.push Text.new(text)
         | 
| 143 | 
            +
                do_print
         | 
| 144 | 
            +
              end
         | 
| 145 | 
            +
             | 
| 146 | 
            +
              def section title=""
         | 
| 147 | 
            +
                $chords.push Section.new(title, $separator_length)
         | 
| 148 | 
            +
                do_print
         | 
| 149 | 
            +
              end
         | 
| 150 | 
            +
             | 
| 151 | 
            +
              def separator 
         | 
| 152 | 
            +
                section
         | 
| 153 | 
            +
              end
         | 
| 154 | 
            +
             | 
| 155 | 
            +
              def do_print
         | 
| 156 | 
            +
                if $auto
         | 
| 157 | 
            +
                  print_chords
         | 
| 158 | 
            +
                end
         | 
| 159 | 
            +
              end
         | 
| 160 | 
            +
             | 
| 161 | 
            +
              def print_chords
         | 
| 162 | 
            +
                lines_to_print = []
         | 
| 163 | 
            +
                chord_index = 0
         | 
| 164 | 
            +
                chords_in_section = 0
         | 
| 165 | 
            +
                tuning_length = $tuning.length
         | 
| 166 | 
            +
                is_done = false
         | 
| 167 | 
            +
                is_new_line = true
         | 
| 168 | 
            +
                is_even_line_length = ($line_length % 2) == 0
         | 
| 169 | 
            +
                is_next_chord_section_or_text = false
         | 
| 170 | 
            +
                to_print_start_chords = false
         | 
| 171 | 
            +
                to_skip_end_strings = false
         | 
| 172 | 
            +
             | 
| 173 | 
            +
                while !is_done
         | 
| 174 | 
            +
                  if is_new_line or to_print_start_chords
         | 
| 175 | 
            +
                    if $chords[chord_index].is_a? Chord
         | 
| 176 | 
            +
                      start_strings = Chord.start_of_strings $tuning, $start_delimiter
         | 
| 177 | 
            +
                      start_strings.each { |s| lines_to_print.push s }
         | 
| 178 | 
            +
                    end
         | 
| 179 | 
            +
                    to_print_start_chords = false
         | 
| 180 | 
            +
                    is_new_line = false
         | 
| 181 | 
            +
                  end
         | 
| 182 | 
            +
             | 
| 183 | 
            +
                  last_chord_lines = lines_to_print.last(tuning_length + 1)
         | 
| 184 | 
            +
                  curr_chord = $chords[chord_index]
         | 
| 185 | 
            +
                  if curr_chord.is_a? Chord
         | 
| 186 | 
            +
                    last_chord_lines.each_with_index do |line,i|
         | 
| 187 | 
            +
                      if i == tuning_length
         | 
| 188 | 
            +
                        line << curr_chord.print_flag
         | 
| 189 | 
            +
                      else
         | 
| 190 | 
            +
                        line << curr_chord.print_string_at(i, $chord_space)
         | 
| 191 | 
            +
                      end
         | 
| 192 | 
            +
                    end
         | 
| 193 | 
            +
                    
         | 
| 194 | 
            +
                    chords_in_section = chords_in_section + 1
         | 
| 195 | 
            +
                    to_skip_end_strings = false
         | 
| 196 | 
            +
                  elsif ($chords[chord_index].is_a? Text) or ($chords[chord_index].is_a? Section)
         | 
| 197 | 
            +
                    lines_to_print.push $chords[chord_index].to_s
         | 
| 198 | 
            +
                    to_skip_end_strings = true
         | 
| 199 | 
            +
                    chords_in_section = 0
         | 
| 200 | 
            +
                    
         | 
| 201 | 
            +
                    if $chords[chord_index + 1].is_a? Chord
         | 
| 202 | 
            +
                      to_print_start_chords = true
         | 
| 203 | 
            +
                    end
         | 
| 204 | 
            +
                  end
         | 
| 205 | 
            +
             | 
| 206 | 
            +
                  chord_index = chord_index + 1
         | 
| 207 | 
            +
                  if ($chords[chord_index].is_a? Text) or ($chords[chord_index].is_a? Section)
         | 
| 208 | 
            +
                    is_next_chord_section_or_text = true
         | 
| 209 | 
            +
                  else
         | 
| 210 | 
            +
                    is_next_chord_section_or_text = false
         | 
| 211 | 
            +
                  end
         | 
| 212 | 
            +
                  
         | 
| 213 | 
            +
                  if ((chords_in_section % $line_length) == 0) or (chord_index == $chords.length) or is_next_chord_section_or_text
         | 
| 214 | 
            +
                    if to_skip_end_strings
         | 
| 215 | 
            +
                      to_skip_end_strings = false
         | 
| 216 | 
            +
                    else
         | 
| 217 | 
            +
                      end_strings = Chord.end_of_strings $tuning, $end_delimiter
         | 
| 218 | 
            +
                      last_chord_lines.each_with_index do |line, i|
         | 
| 219 | 
            +
                        line << end_strings[i]
         | 
| 220 | 
            +
                      end
         | 
| 221 | 
            +
                    end
         | 
| 222 | 
            +
             | 
| 223 | 
            +
                    # start the next actual line
         | 
| 224 | 
            +
                    lines_to_print.push ""
         | 
| 225 | 
            +
                    is_new_line = true
         | 
| 226 | 
            +
                  elsif (chords_in_section % $line_length) == ($line_length / 2) and is_even_line_length
         | 
| 227 | 
            +
                    last_chord_lines.each_with_index do |line, i| 
         | 
| 228 | 
            +
                      line << Chord.print_half_length_string_at(i, $tuning, $half_length_delimiter, $chord_space)
         | 
| 229 | 
            +
                    end
         | 
| 230 | 
            +
                  end
         | 
| 231 | 
            +
                  
         | 
| 232 | 
            +
                  if is_next_chord_section_or_text
         | 
| 233 | 
            +
                    is_new_line = false
         | 
| 234 | 
            +
                  end
         | 
| 235 | 
            +
             | 
| 236 | 
            +
                  if chord_index >= $chords.length
         | 
| 237 | 
            +
                    is_done = true
         | 
| 238 | 
            +
                  end
         | 
| 239 | 
            +
                end
         | 
| 240 | 
            +
             | 
| 241 | 
            +
                # print the buffer
         | 
| 242 | 
            +
                lines_to_print.each { |l| puts l }
         | 
| 243 | 
            +
                nil
         | 
| 244 | 
            +
              end
         | 
| 245 | 
            +
             | 
| 246 | 
            +
              Chord::CHORD_FLAGS.each_with_index do |name,i|
         | 
| 247 | 
            +
                eval <<-ENDOFEVAL
         | 
| 248 | 
            +
                def #{name}
         | 
| 249 | 
            +
                  saved_auto = $auto
         | 
| 250 | 
            +
                  saved_chord_index = $chords.length
         | 
| 251 | 
            +
                  $auto = false
         | 
| 252 | 
            +
                  begin
         | 
| 253 | 
            +
                    chord = yield if block_given?
         | 
| 254 | 
            +
                    
         | 
| 255 | 
            +
                    num_new_chords = $chords.length - saved_chord_index
         | 
| 256 | 
            +
                    $chords.last(num_new_chords).each { |c| c.send :#{name} }
         | 
| 257 | 
            +
                  rescue Exception => e
         | 
| 258 | 
            +
                    puts e.class.to_s
         | 
| 259 | 
            +
                    puts e.message
         | 
| 260 | 
            +
                  end
         | 
| 261 | 
            +
             | 
| 262 | 
            +
                  $auto = saved_auto
         | 
| 263 | 
            +
                  do_print
         | 
| 264 | 
            +
                  chord
         | 
| 265 | 
            +
                end
         | 
| 266 | 
            +
                ENDOFEVAL
         | 
| 267 | 
            +
             | 
| 268 | 
            +
                if name != "dont_play"
         | 
| 269 | 
            +
                  eval <<-ENDOFEVAL
         | 
| 270 | 
            +
                  def play_#{name} chords, chord_type=:major
         | 
| 271 | 
            +
                    #{name} { play chords, chord_type }
         | 
| 272 | 
            +
                  end
         | 
| 273 | 
            +
                  ENDOFEVAL
         | 
| 274 | 
            +
                end
         | 
| 275 | 
            +
              end
         | 
| 276 | 
            +
             | 
| 277 | 
            +
              Chord.short_chords.values.each do |s|
         | 
| 278 | 
            +
                short_chord_name = s.to_s
         | 
| 279 | 
            +
                eval <<-ENDOFEVAL
         | 
| 280 | 
            +
                  def #{short_chord_name}
         | 
| 281 | 
            +
                    :#{s}
         | 
| 282 | 
            +
                  end
         | 
| 283 | 
            +
                  ENDOFEVAL
         | 
| 284 | 
            +
              end
         | 
| 285 | 
            +
            end
         | 
| 286 | 
            +
             | 
| 287 | 
            +
            include Chordy
         |