Advertisement
guitarplayer616

Fingerstyle Music Theory Program

Sep 21st, 2018
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.25 KB | None | 0 0
  1. --     E  E  E  E  E  E     E  E  E  E  E  S  S    E  E  E  E  E  E  
  2. --E||--------------------|----------------------|--------------------|
  3. --B||--0-----0--0-----0--|--0-----0--0-----2h-3-|--0-----0--0-----0--|
  4. --G||-----4--------4-----|-----4--------4-------|-----4--------4-----|
  5. --D||--------------------|----------------------|--------------------|
  6. --A||--2-----------------|----------------------|--2-----------------|
  7. --E||-----------2--------|--3--------0----------|-----------2--------|
  8.  
  9.  
  10. --fingers G# 1234 1235 43451
  11. local tArgs = {...}
  12. if #tArgs == 0 then
  13.     term.setTextColor(colors.yellow)
  14.     print("Usage: [programName] [key] [notes] <capo>")
  15.     term.setTextColor(colors.red)
  16.     print("ie fingers G# 55345653454323 1")
  17.     print("  lowercase [key] if minor scale")
  18.     error()
  19. end
  20.  
  21. local circleOfFifths = {'C','G','D','A','E','B','F#','C#','G#','D#','A#','F'}
  22.  
  23. NoteConverter = {
  24.     noteOrder = {'A','A#','B','C','C#','D','D#','E','F','F#','G','G#'},
  25.     scale = {},
  26.     major = true,
  27.     startingNote = string.upper(tostring(tArgs[1])),
  28.     inputKey = tArgs[1],
  29.     inputNotes = tostring(tArgs[2]),
  30.     decipheredNotes = {},
  31.     iterator = 0,
  32.     noteTable = {},
  33.     capo = tArgs[3] or 0,
  34.    
  35.     findNoteIndex = function(self,note)
  36.         for i,v in pairs(self.noteOrder) do
  37.             if v == note then
  38.                 return i
  39.             end
  40.         end
  41.     end,
  42.    
  43.     findScale = function(self)
  44.         --assuming major
  45.         local noteIndex = self:findNoteIndex(string.upper(self.inputKey))
  46.         local ifCapital = string.sub(self.inputKey,1,1)
  47.         if ifCapital:byte() >= 65 and ifCapital:byte() <= 90 then
  48.             self.major = true
  49.         elseif ifCapital:byte() >= 97 and ifCapital:byte() <= 122 then
  50.             self.major = false
  51.         else
  52.             error("[key] is either false or nil, try \"A#\" or \"a\"")
  53.         end
  54.         --1,step2,step3,half4,step5,step6,step7,half8
  55.         local majorPattern = {0,2,4,5,7,9,11,12}
  56.         local minorPattern = {0,2,3,5,7,8,10,12}
  57.         local selectedPattern = majorPattern
  58.         if not self.major then
  59.             selectedPattern = minorPattern
  60.         end
  61.         for i,v in pairs(selectedPattern) do
  62.             local num = tonumber(noteIndex) + v
  63.             num = num - 1
  64.             num = num%#self.noteOrder
  65.             num = num + 1
  66.             table.insert(self.scale,self.noteOrder[ num ])
  67.         end
  68.     end,
  69.    
  70.    
  71.     decipherAllNumbers = function(self)
  72.         while true do
  73.             self.iterator = self.iterator + 1
  74.             local number = tonumber(self.inputNotes:sub(self.iterator,self.iterator))
  75.             if not number then
  76.                 break
  77.             end
  78.             table.insert(self.decipheredNotes,self.scale[number])
  79.         end
  80.     end,
  81.  
  82.     printOutput = function(self)
  83.         for i,v in pairs(self.scale) do
  84.             write(v)
  85.             write(" ")
  86.         end
  87.         print()
  88.         for i,v in pairs(self.decipheredNotes) do
  89.             write(v)
  90.             write(" ")
  91.         end
  92.         print()
  93.     end,
  94.    
  95.     printNoteTable = function(self)
  96.         for i,v in pairs(self.noteTable) do
  97.             write(i)
  98.             write(": ")
  99.             for _,val in pairs(v) do
  100.                 write(val)
  101.                 write(" ")
  102.             end
  103.             print()
  104.         end
  105.     end,
  106.  
  107.     printNumTable = function(self)
  108.         print()
  109.         print("   01234")
  110.         print("--------")
  111.         for i,v in pairs(self.noteTable) do
  112.             write(i)
  113.             write(": ")
  114.             for _,val in pairs(v) do
  115.                 --if found in scale then present corresponding num else x
  116.                 local found = false
  117.                 for a,b in pairs(self.scale) do
  118.                     if val == b then
  119.                         found = a
  120.                         break
  121.                     end
  122.                 end
  123.                 if found then
  124.                     write(found)   
  125.                 else
  126.                     write("x")
  127.                 end
  128.             end
  129.             print()
  130.         end
  131.         print("--------")
  132.     end,
  133.    
  134.     transposeNote = function(self,charNote,nHalfSteps)
  135.         --transposeNote('G#',-2) --> A#
  136.         local indexNote = self:findNoteIndex(charNote)
  137.         if not indexNote then
  138.             error(tostring(charNote).." is not a note")
  139.         end
  140.         indexNote = indexNote + nHalfSteps
  141.         local result = self.noteOrder[((indexNote-1)%#self.noteOrder)+1]
  142.         return result
  143.     end,
  144.    
  145.     determineCapo = function(self)
  146.        
  147.         local majorKeys = {'C','G','D','A','E'}
  148.         local minorKeys = {'A','D','E'}
  149.         local desiredKeys = majorKeys
  150.         if not self.major then
  151.             desiredKeys = minorKeys
  152.         end
  153.         for i = 1,11 do
  154.             i = i * -1
  155.             local note = self:transposeNote(self.startingNote,i)
  156.             for a,b in pairs(desiredKeys) do
  157.                 if b == note:upper() then
  158.                     write("capo ")
  159.                     write(i*-1)
  160.                     write(" ")
  161.                     print(note)
  162.                 end
  163.             end
  164.         end
  165.     end,
  166.    
  167.     fillNoteTable = function(self)
  168.         --determines the note of the open 6th string when the capo is applied
  169.         --determines each note at the capo for each open string
  170.         --fills out a the notetable for the first 4 frets of the highest 4 strings
  171.         --local Eindex = self:findNoteIndex('E')
  172.         --Eindex = Eindex + capo
  173.         --local S6th = self.noteOrder[((Eindex-1)%#self.noteOrder)+1]
  174.         self.noteTable[6] = {self:transposeNote('E',self.capo)}
  175.         self.noteTable[5] = {self:transposeNote(self.noteTable[6][1],5)}
  176.         self.noteTable[4] = {self:transposeNote(self.noteTable[5][1],5)}   
  177.         self.noteTable[3] = {self:transposeNote(self.noteTable[4][1],5)}   
  178.         self.noteTable[2] = {self:transposeNote(self.noteTable[3][1],4)}   
  179.         self.noteTable[1] = {self:transposeNote(self.noteTable[2][1],5)}
  180.        
  181.         for i=1,6 do
  182.             for j = 1,4 do
  183.                 table.insert(self.noteTable[i],self:transposeNote(self.noteTable[i][1],j)) 
  184.             end
  185.         end
  186.        
  187.     end,
  188. }
  189.  
  190. NoteConverter:findScale()
  191. NoteConverter:decipherAllNumbers()
  192. NoteConverter:printOutput()
  193. NoteConverter:fillNoteTable()
  194. NoteConverter:printNumTable()
  195. NoteConverter:determineCapo()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement