Debian Codegraph @

texk/texlive/linked_scripts/pmxchords/pmxchords.lua

raw symbols SCIP
1 #! /usr/bin/env texlua
2 --
3 -- global constants
4 --
5 VERSION = "0.9.3"
6 FILE_SUFFIX = "_chtr"
7 PMX_CMD = "pmxab"
8
9 --[[
10 pmxchords.lua: transpose chords (\ch.C.\) and process pmxab
11
12 (c) Copyright 2013 Ondrej Fafejta <[email protected]>
13 https://github.com/fafejtao/pmxChords
14
15 This program is free software; you can redistribute it and/or modify it
16 under the terms of the GNU General Public License as published by the
17 Free Software Foundation; either version 2 of the License, or (at your
18 option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
23 Public License for more details.
24
25 You should have received a copy of the GNU General Public License along
26 with this program; if not, write to the Free Software Foundation, Inc.,
27 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28
29 --]]
30
31 --[[
32 ChangeLog:
33 version 0.9.3 2014-04-21 - fixed transposition problem:
34 If song began in E-minor and signature is changed in half of song to A-minor
35 transposition must be omitted.
36
37 version 0.9.2 2013-12-13 - fixed: stop processing when pmxab fail.
38 - When pmxab is called generated file pmxaerr.dat is not removed.
39
40 version 0.9.1 2013-12-09 - added some options. Improved processing.
41
42 version 0.9 2013-12-05 - rewritted code from perl to lua...
43 --]]
44
45 function usage()
46 print("Usage: [texlua] pmxchords.lua basename[.pmx] ")
47 print(" 1. transpose chords into new file basename" .. FILE_SUFFIX .. ".pmx")
48 print(" 2. call pmxab on transposed file basename" .. FILE_SUFFIX .. ".pmx")
49 print(" 3. rename basename" .. FILE_SUFFIX .. ".tex to basename.tex")
50 print("options: -v version")
51 print(" -h help")
52 print(" -s stop after fist step. e.g. generate only transposed basename" .. FILE_SUFFIX .. ".pmx file")
53 end
54
55 function whoami()
56 print("This is pmxchords.lua version " .. VERSION)
57 end
58
59 whoami()
60 if #arg == 0 then
61 usage()
62 os.exit(0)
63 end
64
65 kpse.set_program_name("luatex")
66 require"ChordsTr"
67
68 function parseInputSignature(line)
69 -- parse input signature from pmx digits line. Only one line digits format is supported!
70 -- e.g.
71 -- 1 1 2 4 2 4 0 -1
72 -- signature is -1 : F major
73 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*$")
74 return tonumber(res)
75 end
76
77 function parseOutputSignature(line)
78 -- parse output signature from pmx
79 -- e.g.
80 -- K-2+2
81 -- output signature is +2 : D major
82 local res = string.match(line, "^K[%+%-]?[0-9]+([%+%-]?[0-9]+)")
83 return tonumber(res)
84 end
85
86 --
87 -- Strip comments from line
88 -- Comments begin with % character
89 --
90 function lineWithoutComment(line)
91 local i = string.find(line, "%%")
92 if (i == nil) then
93 return line
94 end
95 return line:sub(0, i - 1)
96 end
97
98 --
99 -- Function checks only if the line contains \ch. string.
100 -- It does not check correctly if notes already started. It checks only if chords started..
101 -- Scenario bellow will fail!
102 -- d44 f a r / % first line does not contain chords symbol
103 -- % signature is changed
104 -- K+0+0
105 -- \ch.C.\ c44 e g r4 /
106 --
107 function checkChordsAlreadyStarted(line)
108 local i = string.find(lineWithoutComment(line), ChordsTr.CHORDS_BEGIN)
109 if (i ~= nil) then
110 return true
111 end
112 return false;
113 end
114
115 function handleErr(msg)
116 io.stderr:write(msg .. "\n")
117 os.exit(2)
118 end
119
120 --
121 -- get file name without .pmx suffix
122 --
123 function getBaseFileName(fileName)
124 if fileName ~= "" and string.sub(fileName, -4, -1) == ".pmx" then
125 return string.sub(fileName, 1, -5)
126 else
127 return fileName
128 end
129 end
130
131 --
132 -- Make chords transposition. Generates new pmx file with transposed chords.
133 --
134 -- param baseName input file name without .pmx suffix
135 -- return outputBaseName transposed pmx file name without .pmx suffix
136 --
137 function makeChordsTransposition(baseName)
138 local inputFileName = baseName .. ".pmx"
139 local inputFile = io.open(inputFileName, "r")
140 if (inputFile == nil) then
141 handleErr("File does not exist: " .. inputFileName)
142 end
143
144 local outputBaseName = baseName .. FILE_SUFFIX -- output base name without suffix. Used for renaming generated .tex file ...
145 local outputFileName = outputBaseName .. ".pmx"
146 local outputFile = io.open(outputFileName, "w")
147 if (outputFile == nil) then
148 handleErr("Can not create temporary file: " .. outputFileName)
149 end
150
151 --
152 -- transpose chords from inputFile into outputFile
153 --
154 -- input and output signature will be parsed from input pmx file
155 --
156 local iSig = nil -- input signature
157 local chTr = nil -- chords transposition class
158
159 -- If song began in E-minor and signature is changed in half of song to A-minor
160 -- transposition must be omitted.
161 local chordsAlreadyStarted = false;
162
163 for line in inputFile:lines() do
164 if (chTr ~= nil) then
165 outputFile:write(chTr:lineTranspose(line) .. "\n")
166 else
167 outputFile:write(line .. "\n")
168 end
169 if (iSig == nil) then
170 iSig = parseInputSignature(line)
171 if (iSig ~= nil) then
172 io.stderr:write("Chords: input signature detected: " .. iSig .. "\n")
173 end
174 elseif (chTr == nil and not chordsAlreadyStarted) then
175 local oSig = parseOutputSignature(line)
176 if (oSig ~= nil) then
177 io.stderr:write("Chords: output signature detected: " .. oSig .. "\n")
178 chTr = ChordsTr(iSig, oSig)
179 elseif (checkChordsAlreadyStarted(line)) then
180 io.stderr:write("Chords: notes already started. Chords transposition is omitted.\n")
181 chordsAlreadyStarted = true;
182 end
183 end
184 end
185 inputFile:close()
186 outputFile:close()
187
188 return outputBaseName
189 end
190
191 --
192 -- Make pmxab process.
193 -- param baseName input file name without .pmx suffix
194 -- param outputBaseName transposed pmx file name without .pmx suffix
195 --
196 function pmxabProcess(baseName, outputBaseName)
197 --
198 -- call pmxab to generate .tex file
199 --
200 os.execute(PMX_CMD .. " " .. outputBaseName)
201 -- check error
202 local pmxaerr = io.open("pmxaerr.dat", "r")
203 if (not pmxaerr) then
204 handleErr("No log file.")
205 end
206 local linebuf = pmxaerr:read()
207 local err = tonumber(linebuf)
208 pmxaerr:close()
209 if (err ~= 0) then
210 handleErr("PMX process fail! " .. outputBaseName .. ".pmx")
211 end
212
213 os.rename(outputBaseName .. ".tex", baseName .. ".tex")
214
215 -- remove temporary files
216 os.remove(outputBaseName .. ".pmx")
217 os.remove(outputBaseName .. ".pml")
218 end
219
220 narg = 1
221 onlyTranspose = false
222
223 repeat
224 local this_arg = arg[narg]
225 if this_arg == "-v" then
226 os.exit(0)
227 elseif this_arg == "-h" then
228 usage()
229 os.exit(0)
230 elseif this_arg == "-s" then
231 onlyTranspose = true
232 else
233 local baseName = getBaseFileName(this_arg)
234 local outputBaseName = makeChordsTransposition(baseName)
235
236 if (onlyTranspose) then
237 os.exit(0)
238 end
239 pmxabProcess(baseName, outputBaseName)
240 end
241
242 narg = narg + 1
243 until narg > #arg
244