Advertisement
gocha

Genkhord chords.txt transpose script

Sep 18th, 2011
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.19 KB | None | 0 0
  1.  
  2. -- Compatibility: Lua-5.1
  3. function split(str, pat)
  4.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  5.    local fpat = "(.-)" .. pat
  6.    local last_end = 1
  7.    local s, e, cap = str:find(fpat, 1)
  8.    while s do
  9.       if s ~= 1 or cap ~= "" then
  10.      table.insert(t,cap)
  11.       end
  12.       last_end = e+1
  13.       s, e, cap = str:find(fpat, last_end)
  14.    end
  15.    if last_end <= #str then
  16.       cap = str:sub(last_end)
  17.       table.insert(t, cap)
  18.    end
  19.    return t
  20. end
  21.  
  22.  
  23. -- transpose function main
  24. function transposeChordStr(chordSrc, keyShift)
  25.     local chordTable = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" }
  26.     local chordKeywordTable = { "C#", "D#", "F#", "G#", "A#", "C", "D", "E", "F", "G", "A", "B" }
  27.     local chordKeyIndexTable = { 1, 3, 6, 8, 10, 0, 2, 4, 5, 7, 9, 11 }
  28.  
  29.     local chordOut = ""
  30.     local iSrc = 1
  31.     while iSrc <= #chordSrc do
  32.         -- replace chord if possible
  33.         local chordReplaced = false
  34.         for iToken, chordToken in pairs(chordKeywordTable) do
  35.             local iStart, iEnd = string.find(chordSrc, chordToken, iSrc, true)
  36.             if iStart == iSrc then
  37.                 local newChord = chordTable[(chordKeyIndexTable[iToken] + keyShift) % 12 + 1]
  38.                 chordOut = chordOut .. newChord
  39.                 iSrc = iSrc + #chordToken
  40.                 chordReplaced = true
  41.                 break
  42.             end
  43.         end
  44.         --
  45.         if not chordReplaced then
  46.             chordOut = chordOut .. chordSrc:sub(iSrc, iSrc)
  47.             iSrc = iSrc + 1
  48.         end
  49.     end
  50.     return chordOut
  51. end
  52.  
  53.  
  54.  
  55. -- parse command line options
  56. local chordFilePath = "chords.txt"
  57. local keyShift = 0
  58. if #arg == 1 then
  59.     keyShift = tonumber(arg[1])
  60. elseif #arg == 2 then
  61.     chordFilePath = arg[1]
  62.     keyShift = tonumber(arg[2])
  63. else
  64.     error("Unsupported option.")
  65.     return
  66. end
  67.  
  68. -- open chords.txt
  69. local chordFile = io.open(chordFilePath, "r")
  70. if chordFile == nil then
  71.     stderr.write("Unable to open: " .. chordFilePath)
  72. end
  73.  
  74. -- process for each lines
  75. for line in chordFile:lines() do
  76.     -- split codes into an array
  77.     local chordsIn = split(line, " ")
  78.     for i, chordSrc in pairs(chordsIn) do
  79.         local chord
  80.         if i == 1 then
  81.             chord = chordSrc
  82.         else
  83.             chord = transposeChordStr(chordSrc, keyShift)
  84.             io.write(" ")
  85.         end
  86.         io.write(chord)
  87.     end
  88.     io.write("\n")
  89. end
  90.  
  91. chordFile:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement