Don't like ads? PRO users don't see any ads ;-)
Guest

Guitar Fret Program v4

By: a guest on Apr 15th, 2012  |  syntax: Ruby  |  size: 3.96 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require 'rubygems'
  2. require 'win32console'
  3.  
  4. class String
  5.         #def red; colorize(self, "\e[1m\e[31m"); end
  6.         def green; colorize(self, "\e[1m\e[32m"); end
  7.         #def dark_green; colorize(self, "\e[32m"); end
  8.         #def yellow; colorize(self, "\e[1m\e[33m"); end
  9.         #def blue; colorize(self, "\e[1m\e[34m"); end
  10.         #def dark_blue; colorize(self, "\e[34m"); end
  11.         #def pur; colorize(self, "\e[1m\e[35m"); end
  12.         def colorize(text, color_code)  "#{color_code}#{text}\e[0m" end
  13. end
  14.  
  15. def note_to_n(n) #Converts a note name into a number
  16.         val = case n.upcase
  17.         when "A"
  18.                 0
  19.         when "A#", "BB"
  20.                 1
  21.         when "B", "CB"
  22.                 2
  23.         when "C", "B#"
  24.                 3
  25.         when "C#", "DB"
  26.                 4
  27.         when "D"
  28.                 5
  29.         when "D#", "EB"
  30.                 6
  31.         when "E", "FB"
  32.                 7
  33.         when "F", "E#"
  34.                 8
  35.         when "F#", "GB"
  36.                 9
  37.         when "G"
  38.                 10
  39.         when "G#", "AB"
  40.                 11
  41.         else
  42.                 0
  43.         end
  44. end
  45.  
  46. def note_to_s(s) #Converts a number into a readable note name
  47.         val = case s
  48.         when 0
  49.                 "A "
  50.         when 1
  51.                 "A#"
  52.         when 2
  53.                 "B "
  54.         when 3
  55.                 "C "
  56.         when 4
  57.                 "C#"
  58.         when 5
  59.                 "D "
  60.         when 6
  61.                 "D#"
  62.         when 7
  63.                 "E "
  64.         when 8
  65.                 "F "
  66.         when 9
  67.                 "F#"
  68.         when 10
  69.                 "G "
  70.         when 11
  71.                 "G#"
  72.         else
  73.                 "x "
  74.         end
  75. end
  76.  
  77. def input_tuning
  78.         puts "6: "
  79.         s6 = note_to_n(gets.chomp)
  80.  
  81.         puts "5: "
  82.         s5 = note_to_n(gets.chomp)
  83.  
  84.         puts "4: "
  85.         s4 = note_to_n(gets.chomp)
  86.  
  87.         puts "3: "
  88.         s3 = note_to_n(gets.chomp)
  89.  
  90.         puts "2: "
  91.         s2 = note_to_n(gets.chomp)
  92.  
  93.         puts "1: "
  94.         s1 = note_to_n(gets.chomp)
  95.  
  96.         [s1, s2, s3, s4, s5, s6]
  97. end
  98.  
  99. def fill_array(base_note, string_array)
  100.         for i in 0..23 #From fret 0 to fret 23
  101.                 if i == 0
  102.                         r = base_note #Set the note reader to the base note
  103.                         string_array[i] = r
  104.                 else
  105.                         r+=1
  106.                         if r == 12; r = 0 end #Reset note reader
  107.                         string_array[i] = r
  108.                 end
  109.         end
  110. end
  111.  
  112. def print_frets(guitar, search)
  113.         print_fretguide
  114.  
  115.         color_fret(guitar[0], search)
  116.         color_fret(guitar[1], search)
  117.         color_fret(guitar[2], search)
  118.         color_fret(guitar[3], search)
  119.         color_fret(guitar[4], search)
  120.         color_fret(guitar[5], search)
  121. end
  122.  
  123. def print_fretguide
  124.         tape = ""
  125.  
  126.         for i in 0..23
  127.                 if i == 0
  128.                         tape << "0 |"
  129.                 else
  130.                         if i < 10
  131.                                 tape << "#{i}  "
  132.                         else
  133.                                 tape << "#{i} "
  134.                         end
  135.                 end
  136.         end
  137.  
  138.         puts tape
  139. end
  140.  
  141. def color_fret(fret, search)
  142.         tape = ""
  143.  
  144.         for i in 0..(fret.length - 1)
  145.                 if i == 0
  146.                         if search.include?(fret[i])
  147.                                 tape << note_to_s(fret[i]).green << "|"
  148.                         else
  149.                                 tape << note_to_s(fret[i]) << "|"
  150.                         end
  151.                 else
  152.                         if search.include?(fret[i])
  153.                                 tape << note_to_s(fret[i]).green << " "
  154.                         else
  155.                                 tape << note_to_s(fret[i]) << " "
  156.                         end
  157.                 end
  158.         end
  159.  
  160.         puts tape
  161. end
  162.  
  163. #MAIN CODE STARTS HERE=----------------------------------------------------
  164. s1, s2, s3, s4, s5, s6 = input_tuning
  165.  
  166. puts "TUNING: #{note_to_s(s6)}, #{note_to_s(s5)}, #{note_to_s(s4)}, #{note_to_s(s3)}, #{note_to_s(s2)}, #{note_to_s(s1)}"
  167.  
  168. sa1 = Array.new(24, 0) #Fret 0 to 23 filled with 0
  169. sa2 = Array.new(24, 0)
  170. sa3 = Array.new(24, 0)
  171. sa4 = Array.new(24, 0)
  172. sa5 = Array.new(24, 0)
  173. sa6 = Array.new(24, 0)
  174.  
  175. fill_array(s1, sa1) #Fill arrays with correct note sequence
  176. fill_array(s2, sa2)
  177. fill_array(s3, sa3)
  178. fill_array(s4, sa4)
  179. fill_array(s5, sa5)
  180. fill_array(s6, sa6)
  181.  
  182. guitar = Array.new([sa1, sa2, sa3, sa4, sa5, sa6])
  183.  
  184. puts "1) Major Chord"
  185. puts "2) Minor Chord"
  186. puts "3) No chord"
  187.  
  188. case gets.chomp
  189. when "1"
  190.         puts "Bass note:"
  191.  
  192.         bass = note_to_n(gets.chomp)
  193.         case bass
  194.         when 0..4
  195.                 print_frets(guitar, [bass, bass + 4, bass + 7])
  196.         when 5..7
  197.                 print_frets(guitar, [bass, bass + 4, bass - 5])
  198.         when 8..11
  199.                 print_frets(guitar, [bass, bass - 8, bass - 5])
  200.         else
  201.                 print_frets(guitar, [bass, bass + 4, bass + 7])
  202.         end
  203. when "2"
  204.         puts "Bass note:"
  205.  
  206.         bass = note_to_n(gets.chomp)
  207.         case bass
  208.         when 0..4
  209.                 print_frets(guitar, [bass, bass + 3, bass + 7])
  210.         when 5..8
  211.                 print_frets(guitar, [bass, bass + 3, bass - 5])
  212.         when 9..11
  213.                 print_frets(guitar, [bass, bass - 9, bass - 5])
  214.         else
  215.                 print_frets(guitar, [bass, bass + 4, bass + 7])
  216.         end
  217. else
  218.         print_frets(guitar, [])
  219. end
  220.  
  221. puts "Done."
  222.  
  223. gets #If you don't run from cmd