summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/pmxchords/pmxchords.lua
blob: 1539e90a48a595b9ea8bd934ce52125bb7dbe78e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
#!/usr/bin/env texlua
--
-- global constants
--
VERSION = "0.9.2"
FILE_SUFFIX="_chtr"
PMX_CMD="pmxab"

--[[
   pmxchords.lua: transpose chords (\ch.C.\) and process pmxab

   (c) Copyright 2013 Ondrej Fafejta <fafejtao@gmail.com>
       https://github.com/fafejtao/pmxChords

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

--]]

--[[
  ChangeLog:

    version 0.9.2 2013-12-13 - fixed: stop processing when pmxab fail. 
                             - Do not remove file pmxaerr.dat.

    version 0.9.1 2013-12-09 - added some options. Improved processing.

    version 0.9   2013-12-05 - rewritted code from perl to lua...
--]]

function usage()
  print("Usage:  [texlua] pmxchords.lua  basename[.pmx] ")
  print("     1. transpose chords into new file basename"..FILE_SUFFIX..".pmx")
  print("     2. call pmxab on transposed file basename"..FILE_SUFFIX..".pmx")
  print("     3. rename basename"..FILE_SUFFIX..".tex to basename.tex")
  print("options: -v  version")
  print("         -h  help")
  print("         -s  stop after fist step. e.g. generate only transposed basename"..FILE_SUFFIX..".pmx file")
end

function whoami ()
  print("This is pmxchords.lua version ".. VERSION)
end

whoami()
if #arg == 0 then
  usage()
  os.exit(0)
end

kpse.set_program_name("luatex")
require "ChordsTr"

function parseInputSignature(line)
   -- parse input signature from pmx digits line. Only one line digits format is supported!
   -- e.g.
   --    1      1       2     4      2      4        0    -1
   -- signature is -1 : F major
   local res =  string.match(line, "^%s*[0-9]+%s+[0-9]+%s+[0-9]+%s+[0-9]+%s+[0-9]+%s+[0-9]+%s+[0-9]+%s+([%+%-]?[0-9]+)%s*$")
   return tonumber(res)
end

function parseOutputSignature(line)
   -- parse output signature from pmx
   -- e.g.
   -- K-2+2
   -- output signature is +2 : D major
   local res =  string.match(line, "^K[%+%-]?[0-9]+([%+%-]?[0-9]+)")
   return tonumber(res)
end

function handleErr(msg)
   io.stderr:write(msg.."\n")
   os.exit(2)
end

--
-- get file name without .pmx suffix
--
function getBaseFileName(fileName)
   if fileName ~= "" and string.sub(fileName, -4, -1) == ".pmx" then
	  return string.sub(fileName, 1, -5)
   else
	  return fileName
   end
end

--
-- Make chords transposition. Generates new pmx file with transposed chords.
--
-- param baseName input file name without .pmx suffix
-- return outputBaseName transposed pmx file name without .pmx suffix
-- 
function makeChordsTransposition(baseName)
   local inputFileName = baseName..".pmx"
   local inputFile = io.open(inputFileName, "r")
   if(inputFile == nil) then
      handleErr("File does not exist: "..inputFileName)
   end

   local outputBaseName = baseName..FILE_SUFFIX   -- output base name without suffix. Used for renaming generated .tex file ...
   local outputFileName = outputBaseName..".pmx"
   local outputFile = io.open(outputFileName, "w")
   if(outputFile == nil) then
      handleErr("Can not create temporary file: ".. outputFileName)
   end

   --
   -- transpose chords from inputFile into outputFile
   --
   -- input and output signature will be parsed from input pmx file
   --
   local iSig = nil  -- input signature
   local chTr = nil  -- chords transposition class

   for line in inputFile:lines() do
      if(chTr ~= nil) then
		 outputFile:write(chTr:lineTranspose(line).."\n")
      else
		 outputFile:write(line.."\n")
      end
      if(iSig == nil) then
		 iSig = parseInputSignature(line)
		 if(iSig ~= nil) then
			io.stderr:write("Chords: input signature detected: "..iSig .."\n")
		 end
      elseif(chTr == nil) then
		 local oSig = parseOutputSignature(line)
		 if(oSig ~= nil) then
			io.stderr:write("Chords: output signature detected: "..oSig .."\n")
			chTr = ChordsTr(iSig, oSig)
		 end
      end
   end
   inputFile:close()
   outputFile:close()

   return outputBaseName
end

--
-- Make pmxab process.
-- param baseName input file name without .pmx suffix
-- param outputBaseName transposed pmx file name without .pmx suffix
--
function pmxabProcess(baseName, outputBaseName)
   --
   -- call pmxab to generate .tex file
   --
   os.execute(PMX_CMD .. " " .. outputBaseName )
   -- check error
   local pmxaerr = io.open("pmxaerr.dat", "r")
   if (not pmxaerr) then
       handleErr("No log file.")
   end
   linebuf = pmxaerr:read()
   err = tonumber(linebuf)
   pmxaerr:close()
   if (err ~= 0 ) then
      handleErr("PMX process fail! "..outputBaseName..".pmx")
   end

   os.rename(outputBaseName..".tex", baseName..".tex")

   -- remove temporary files
   os.remove(outputBaseName..".pmx")
   os.remove(outputBaseName..".pml")
end

narg = 1
onlyTranspose = false

repeat
   this_arg = arg[narg]
   if this_arg == "-v" then
      os.exit(0)
   elseif this_arg == "-h" then
      usage()
      os.exit(0)
   elseif this_arg == "-s" then
      onlyTranspose = true
   else
      local baseName = getBaseFileName(this_arg)
      local outputBaseName = makeChordsTransposition(baseName)

      if (onlyTranspose) then
		 os.exit(0)
      end
      pmxabProcess(baseName, outputBaseName)
   end

   narg = narg + 1
until narg > #arg