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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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439


import fs from "fs";
import path from "path";
import child_process from "child-process-promise";
import {MIDI} from "@k-l-lambda/music-widgets";
import {Writable} from "stream";

import asyncCall from "../inc/asyncCall";
import {SingleLock} from "../inc/mutex";
import {preprocessXml} from "./xmlTools";
import * as lilyAddon from "./lilyAddon";



let MUSICXML2LY_PATH;
let MIDI2LY_PATH;
let LILYPOND_PATH;


let env;	// setEnvironment must be called before use commands
const setEnvironment = e => {
	env = e;

	MUSICXML2LY_PATH = filePathResolve(env.LILYPOND_DIR, "musicxml2ly");
	MIDI2LY_PATH = filePathResolve(env.LILYPOND_DIR, "midi2ly");
	LILYPOND_PATH = filePathResolve(env.LILYPOND_DIR, "lilypond");

	emptyCache();

	if (env.LILYPOND_ADDON) {
		if (env.LILYPOND_ADDON_ASSETS_DIR) {
			process.env.LILYPOND_PATH = path.join(env.LILYPOND_ADDON_ASSETS_DIR, "bin/lilypond");
			process.env.GUILE_LOAD_PATH = path.join(env.LILYPOND_ADDON_ASSETS_DIR, "share/guile/1.8");
			process.env.FONTCONFIG_PATH = path.join(env.LILYPOND_ADDON_ASSETS_DIR, "share/fonts");
		}

		lilyAddon.loadAddon(env.LILYPOND_ADDON);
	}
};


const _WINDOWS = process.platform === "win32";


const genHashString = (len = 8) => Buffer.from(Math.random().toString()).toString("base64").substr(3, 3 + len);


const filePathResolve = (...parts: string[]): string => {
	const result = path.join(...parts);
	return _WINDOWS ? `"${result}"` : result;
};


const emptyCache = async () => {
	// empty temporary directory
	try {
		if (env.TEMP_DIR) {
			if (_WINDOWS)
				await child_process.exec(`del /q "${env.TEMP_DIR}*"`);
			else
				await child_process.exec(`rm ${env.TEMP_DIR}*`);
			console.log("Temporary directory clear.");
		}
	}
	catch (err) {
		if (_WINDOWS)
			console.log("emptyCache error:", err);
	}
};


export interface LilyProcessOptions {
	// xml
	language?: string;
	removeMeasureImplicit?: boolean;
	replaceEncoding?: boolean;
	removeNullDynamics?: boolean;
	fixHeadMarkup?: boolean;
	fixBackSlashes?: boolean;
	roundTempo?: boolean;
	escapedWordsDoubleQuotation?: boolean;
	removeTrivialRests?: boolean;
	removeBadMetronome?: boolean;
	removeInvalidHarmonies?: boolean;
	removeAllHarmonies?: boolean;
	fixChordVoice?: boolean;
	fixBarlines?: boolean;
	removeInvalidClef?: boolean;

	// lilypond
	pointClick?: boolean;
	midi?: boolean;
	removeBreak?: boolean;
	removePageBreak?: boolean;
	removeInstrumentName?: boolean;
	removeTempo?: boolean;
	tupletReplace?: boolean;
};




const postProcessLy = (ly: string, {
	pointClick = true,
	midi = true,
	removeBreak = false,
	removePageBreak = false,
	removeInstrumentName = false,
	removeTempo = false,
	tupletReplace = false,
} = {}): string => {
	let result = ly;

	if (pointClick)
		result = result.replace(/\\pointAndClickOff\n/g, "");

	if (midi)
		result = result.replace(/%  \\midi/g, "\\midi");

	if (removeBreak)
		result = result.replace(/\s\\break\s/g, " ");

	if (removePageBreak)
		result = result.replace(/\s\\pageBreak\s/g, " ");

	if (removeInstrumentName)
		result = result.replace(/\\set Staff\.instrumentName/g, "% \\set Staff.instrumentName");

	if (removeTempo)
		result = result.replace(/\\tempo /g, "% \\tempo ");

	if (tupletReplace) {
		result = result
			.replace(/4\*128\/384/g, "8*2/3")
			.replace(/4\*64\/384/g, "16*2/3");
	}

	return result;
};


const xml2ly = async (xml: string, {language = "english", ...options}: LilyProcessOptions): Promise<string> => {
	xml = preprocessXml(xml, options);
	//console.log("xml:", options, xml.substr(0, 100));

	const hash = genHashString();
	const xmlFileName = `${env.TEMP_DIR}xml2ly-${hash}.xml`;
	await asyncCall(fs.writeFile, xmlFileName, xml);

	const lyFileName = `${env.TEMP_DIR}xml2ly-${hash}.ly`;

	if (env.MUSICXML2LY_BY_PYTHON) {
		await child_process.spawn(path.resolve(env.LILYPOND_DIR, "python"), [
			path.resolve(env.LILYPOND_DIR, "musicxml2ly.py"), xmlFileName, "-o", lyFileName,
			...(language ? ["-l", language] : []),
		]);
	}
	else
		await child_process.exec(`${MUSICXML2LY_PATH} ${xmlFileName} -o ${lyFileName} ${language ? "-l " + language : ""}`, {maxBuffer: 0x80000});
	//console.log("musicxml2ly:", result.stdout, result.stderr);

	const ly = await asyncCall(fs.readFile, lyFileName);

	return postProcessLy(ly.toString(), options);
};


const midi2ly = async (midi, options: LilyProcessOptions): Promise<string> => {
	const hash = genHashString();
	//const midiFileName = `${env.TEMP_DIR}midi2ly-${hash}.midi`;
	//await asyncCall(fs.writeFile, midiFileName, midi);

	const lyFileName = `${env.TEMP_DIR}midi2ly-${hash}-midi.ly`;

	let result;
	if (env.MUSICXML2LY_BY_PYTHON) {
		result = await child_process.spawn(path.resolve(env.LILYPOND_DIR, "python"),
			[path.resolve(env.LILYPOND_DIR, "midi2ly.py"), midi.path, "-o", lyFileName]);
	}
	else
		result = await child_process.exec(`${MIDI2LY_PATH} ${midi.path} -o ${lyFileName}`);
	console.log("midi2ly:", result.stdout, result.stderr);

	const ly = await asyncCall(fs.readFile, lyFileName);

	return postProcessLy(ly.toString(), options);
};


const postProcessSvg = (svg: string): string => {
	return svg.replace(/textedit:[^"]+:(\d+:\d+:\d+)/g, "textedit:$1");
};


//const nameNumber = name => Number(name.match(/-(\d+)\./)[1]);


// lilypond command line output file pattern
const FILE_BORN_OUPUT_PATTERN = /output\sto\s`(.+)'/;


export interface EngraverOptions {
	onProcStart?: () => void|Promise<void>;
	onMidiRead?: (content: MIDI.MidiData, options?: {filePath: string}) => void|Promise<void>;
	onSvgRead?: (index: number, content: string, options?: {filePath: string}) => void|Promise<void>;
	includeFolders?: string[];	// include folder path should be relative to TEMP_DIR
};

export interface EngraverResult {
	logs: string;
	svgs: string[];
	midi: MIDI.MidiData;
	errorLevel: number;
};


const engraveSvgCli = async (source: string,
	{onProcStart, onMidiRead, onSvgRead, includeFolders = []}: EngraverOptions = {},
): Promise<Partial<EngraverResult>> => {
	const hash = genHashString();
	const sourceFilename = `${env.TEMP_DIR}engrave-${hash}.ly`;

	await asyncCall(fs.writeFile, sourceFilename, source);
	//console.log("ly source written:", sourceFilename);

	let midi = null;
	const svgs = [];

	const fileReady = new SingleLock<string>();

	const loadFile = async filename => {
		//console.log("loadFile:", filename);

		// wait for file writing finished
		//await new Promise(resolve => setTimeout(resolve, 100));

		const captures = filename.match(/\.(\w+)$/);
		if (!captures) {
			console.warn("invalid filename:", filename);
			return;
		}
		const [_, ext] = captures;

		const filePath = path.resolve(env.TEMP_DIR, filename);
		//console.log("file output:", filePath, ext);

		switch (ext) {
		case env.MIDI_FILE_EXTEND: {
			// wait extra time for MIDI file
			//await fileReady.lock();
			await new Promise(resolve => setTimeout(resolve, 100));

			const buffer = await asyncCall(fs.readFile, filePath);
			if (!buffer.length)
				console.warn("empty MIDI buffer:", filename);

			midi = MIDI.parseMidiData(buffer);

			await onMidiRead && onMidiRead(midi, {filePath});
		}

			break;
		case "svg": {
			// eslint-disable-next-line
			const captures = filename.match(/(\d+)\.svg$/);
			const index = captures ? Number(captures[1]) - 1 : 0;

			const buffer = await asyncCall(fs.readFile, filePath);
			if (!buffer.length)
				console.warn("empty SVG buffer:", filename);

			const svg = postProcessSvg(buffer.toString());
			svgs[index] = svg;

			//console.log("svg load:", filePath);
			await onSvgRead && onSvgRead(index, svg, {filePath});
		}

			break;
		}
	};

	const loadProcs: Promise<void>[] = [];

	const checkFile = async (line: string) => {
		fileReady.release(line);

		// skip error messages in command line output
		if (FILE_BORN_OUPUT_PATTERN.test(line)) {
			let newLine = await fileReady.lock();
			while (/error|warning/.test(newLine))
				newLine = await fileReady.lock();

			let captures;
			const exp = new RegExp(FILE_BORN_OUPUT_PATTERN.source, "g");
			while (captures = exp.exec(line))
				loadProcs.push(loadFile(captures[1]));
		}
	};

	const includeParameters = includeFolders.map(folder => `--include=${folder}`).join(" ");

	const proc: any = child_process.exec(`${LILYPOND_PATH} -dbackend=svg -o ${env.TEMP_DIR} ${includeParameters} ${sourceFilename}`,
		{maxBuffer: 0x100000});
	//const proc = child_process.spawn(LILYPOND_PATH, ["-dbackend=svg", `-o ${env.TEMP_DIR}`, sourceFilename]);
	proc.childProcess.stdout.on("data", checkFile);
	proc.childProcess.stderr.on("data", checkFile);

	const startPromise = onProcStart && onProcStart();
	if (startPromise)
		loadProcs.push(startPromise);

	const result = await proc;

	fileReady.release();

	await Promise.all(loadProcs);

	//console.log("svgs:", svgs.length);

	const validCount = svgs.filter(svg => svg).length;
	if (validCount < svgs.length)
		console.warn("svg loading incompleted: ", validCount, svgs.length);

	return {
		logs: result.stderr,
		svgs,
		midi,
	};
};


const engraveSvgWithStreamCli = async (source: string, output: Writable, {includeFolders = []}: {includeFolders?: string[]} = {}) => {
	const hash = genHashString();
	const sourceFilename = `${env.TEMP_DIR}engrave-${hash}.ly`;

	await asyncCall(fs.writeFile, sourceFilename, source);

	const writing = new SingleLock();
	const fileReady = new SingleLock();

	const loadFile = async line => {
		const [_, filename] = line.match(FILE_BORN_OUPUT_PATTERN);
		const [__, ext] = filename.match(/\.(\w+)$/);

		switch (ext) {
		case "svg": {
			await fileReady.lock();
			await writing.wait();
	
			writing.lock();

			const filePath = path.resolve(env.TEMP_DIR, filename);
			const fileStream = fs.createReadStream(filePath);
			fileStream.pipe(output, {end: false});

			fileStream.on("close", () => output.write("\n\n\n\n"));

			writing.release();
		}

			break;
		}
	};

	const checkFile = line => {
		fileReady.release();

		if (FILE_BORN_OUPUT_PATTERN.test(line))
			loadFile(line);
	};

	const includeParameters = includeFolders.map(folder => `--include=${folder}`).join(" ");

	const proc: any = child_process.exec(`${LILYPOND_PATH} -dbackend=svg -o ${env.TEMP_DIR} ${includeParameters} ${sourceFilename}`);
	proc.childProcess.stdout.on("data", checkFile);
	proc.childProcess.stderr.on("data", checkFile);

	proc.then(() => fileReady.release());

	return proc;
};


const engraveScm = async (source: string, {onProcStart, includeFolders = []}: {
	onProcStart?: () => void|Promise<void>,
	includeFolders?: string[],	// include folder path should be relative to TEMP_DIR
} = {}): Promise<{
	logs: string,
	scm: string,
}> => {
	const hash = genHashString();
	const sourceFilename = `${env.TEMP_DIR}engrave-${hash}.ly`;
	const targetFilename = `${env.TEMP_DIR}engrave-${hash}.scm`;

	await fs.promises.writeFile(sourceFilename, source);
	//console.log("ly source written:", sourceFilename);

	const includeParameters = includeFolders.map(folder => `--include=${folder}`).join(" ");

	const proc = child_process.exec(`${LILYPOND_PATH} -dbackend=scm -o ${env.TEMP_DIR} ${includeParameters} ${sourceFilename}`,
		{maxBuffer: 0x100000});

	await onProcStart && onProcStart();

	const result = await proc;

	const scmBuffer = await fs.promises.readFile(targetFilename);

	return {
		logs: result.stderr,
		scm: scmBuffer.toString(),
	};
};


const engraveSvg = async (source: string, options: EngraverOptions = {}): Promise<Partial<EngraverResult>> =>
	(env.LILYPOND_ADDON ? lilyAddon.engraveSvg : engraveSvgCli)(source, options);


const engraveSvgWithStream = async (source: string, output: Writable, options: {includeFolders?: string[]} = {}) =>
	(env.LILYPOND_ADDON ? lilyAddon.engraveSvgWithStream : engraveSvgWithStreamCli)(source, output, options);



export {
	xml2ly,
	midi2ly,
	engraveSvg,
	engraveSvgCli,
	engraveSvgWithStream,
	engraveSvgWithStreamCli,
	engraveScm,
	setEnvironment,
	emptyCache,
	postProcessSvg,
};