-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.lua
275 lines (228 loc) · 6.01 KB
/
parser.lua
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
local inspect = require("inspect")
local M = {}
--
-- Utility functions
--
-- Trim string (used to cut CR/LF symbols from the string)
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
-- Convert a character to a number
local function charToNum(char)
if char == "." then return 0 end
local num = string.byte(char) - 48
if num > 9 then
num = num - 7
end
return num
end
--
-- Parser functions
--
-- Parse the list of integers with a loop mark
local function parseLoopedIntList(str)
local list = {}
local loop = 1
local pos = 1
str = trim(str) .. ","
for v in string.gmatch(str, "(%w+),") do
if string.sub(v, 1, 1) == "L" then
v = string.sub(v, 2)
loop = pos
end
table.insert(list, tonumber(v))
pos = pos + 1
end
return list, loop
end
-- Parse module meta data (name, speed, etc)
local function parseModuleMeta(vtm, module, idx)
local str
repeat
idx = idx + 1
str = trim(vtm[idx])
if string.len(str) ~= 0 then
local match = string.gmatch(str, "(%w+)=(.*)")
local k, v = match()
if k == "IntFreq" then
module.intFreq = tonumber(v)
elseif k == "Speed" then
module.speed = tonumber(v)
elseif k == "PlayOrder" then
module.playOrder, module.playLoop = parseLoopedIntList(v)
for i = 1, #module.playOrder do
module.playOrder[i] = module.playOrder[i] + 1
end
else
module.meta[k] = v
end
end
until string.len(str) == 0
return idx
end
-- Parse ornament
local function parseOrnament(vtm, module, idx)
local number = tonumber(string.match(vtm[idx], "(%d+)"))
idx = idx + 1
local ornament, loop = parseLoopedIntList(vtm[idx])
module.ornaments[number] = {
loop = loop,
values = ornament,
}
return idx
end
-- Parse a single note from a pattern
local function parseNote(str, channel, curSample, curOrnament, curVolume)
local note = {}
local pitches = {C = 0, D = 2, E = 4, F = 5, G = 7, A = 9, B = 11}
-- Pitch
local pitchStr = string.sub(str, 1, 1)
if pitchStr ~= "-" then
if pitchStr == "R" then -- Pause
note.pitch = -1
else
local pitch = pitches[pitchStr]
if string.sub(str, 2, 2) == "#" then
pitch = pitch + 1
end
local octave = tonumber(string.sub(str, 3, 3))
if octave == nil then
print(str)
end
pitch = pitch + octave * 12
note.pitch = pitch
end
end
-- Sample
local sample = charToNum(string.sub(str, 5, 5))
if sample ~= 0 then
curSample[channel] = sample
note.sample = sample
end
-- Envelope
local envelope = charToNum(string.sub(str, 6, 6))
if envelope ~= 0 and envelope ~= 15 then
note.envelope = envelope
end
-- Ornament
local ornament = charToNum(string.sub(str, 7, 7))
if ornament == 0 then
if string.sub(str, 6, 6) ~= "." then
curOrnament[channel] = 0
note.ornament = 0
end
else
curOrnament[channel] = charToNum(string.sub(str, 7, 7))
note.ornament = curOrnament[channel]
end
-- Volume
local volume = charToNum(string.sub(str, 8, 8))
if volume > 0 then
curVolume[channel] = volume
note.volume = volume
end
-- FX
local fx = charToNum(string.sub(str, 10, 10)) * 0x1000 +
charToNum(string.sub(str, 11, 11)) * 0x100 +
charToNum(string.sub(str, 12, 12)) * 0x10 +
charToNum(string.sub(str, 13, 13))
if fx ~= 0 then
note.fx = fx
end
if note.pitch ~= nil then
note.sample = curSample[channel]
note.ornament = curOrnament[channel]
note.volume = curVolume[channel]
end
return note
end
-- Parse a pattern
local function parsePattern(vtm, module, idx)
local number = tonumber(string.match(vtm[idx], "(%d+)")) + 1
local str
local pattern = {}
local curSample = {1, 1, 1}
local curOrnament = {0, 0, 0}
local curVolume = {15, 15, 15}
local noiseOffset = 0
repeat
-- Iterate throw pattern rows
idx = idx + 1
str = trim(vtm[idx])
local row = {}
if string.len(str) ~= 0 then
-- Parse notes of each channel
table.insert(row, parseNote(string.sub(str, 9, 21), 1, curSample, curOrnament, curVolume))
table.insert(row, parseNote(string.sub(str, 23, 35), 2, curSample, curOrnament, curVolume))
table.insert(row, parseNote(string.sub(str, 37), 3, curSample, curOrnament, curVolume))
noiseOffset = charToNum(string.sub(str, 6, 6)) * 0x10 + charToNum(string.sub(str, 7, 7))
table.insert(row, noiseOffset)
table.insert(pattern, row)
end
until string.len(str) == 0
module.patterns[number] = pattern
return idx
end
-- Parse VT2 module as a list of string
local function parseVT2(vtm)
local modules = {}
local idx = 1
local module = nil
while idx <= #vtm do
local str = trim(vtm[idx])
if string.len ~= 0 then
if str == "[Module]" then
if module ~= nil then
table.insert(modules, module)
end
module = {
meta = {},
speed = 0,
intFreq = 0,
playOrder = {},
playLoop = 0,
samples = {},
ornaments = {},
patterns = {}
}
idx = parseModuleMeta(vtm, module, idx)
elseif string.sub(str, 1, 4) == "[Sam" then
-- We ignore samples
elseif string.sub(str, 1, 4) == "[Orn" then
idx = parseOrnament(vtm, module, idx)
elseif string.sub(str, 1, 4) == "[Pat" then
idx = parsePattern(vtm, module, idx)
end
end
idx = idx + 1
end
if module ~= nil then
table.insert(modules, module)
end
return modules
end
--
-- Public API
--
-- Read file
function M.readFile(filename)
local file = io.open(filename, "r")
if file == nil then return nil end
local lines = {}
local str
repeat
local str = file:read("*line")
if str ~= nil then
table.insert(lines, str)
end
until str == nil
io.close(file)
return lines
end
-- Load VT2 module from a file
function M.load(filename)
local vtm = M.readFile(filename)
if vtm == nil then return nil end
return parseVT2(vtm)
end
return M