File size: 4,049 Bytes
d605f27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import formidable from "formidable";
import {DOMParser} from "xmldom";
import {MIDI} from "@k-l-lambda/music-widgets";

import * as lilyCommands from "./lilyCommands";
import * as staffSvg from "../inc/staffSvg";
import loadLilyParser from "./loadLilyParserNode";
import {LilyDocument} from "../inc/lilyParser";
import LogRecorder from "../inc/logRecorder";
import * as ScoreMaker from "./scoreMaker";
import * as constants from "./constants";
import {advancedEngrave} from "./advancedEngraver";



const formidableHandle = (name, req, res, handle) =>
	new formidable.IncomingForm().parse(req, async (err, fields, files) => {
		try {
			if (err)
				throw err;

			const result = await handle(fields, files);

			const resultObj = (typeof result === "string" || result instanceof Buffer) ? {body: result} : result;

			res.writeHead(resultObj.body ? 200 : 404, resultObj.header);
			res.write(resultObj.body);
			res.end();
		}
		catch (error) {
			console.error(`${name} error:`, error);

			res.writeHead(500);
			res.write(error.toString());
			res.end();
		}
	});


const service = {
	"/musicxml2ly": {
		post: (req, res) => formidableHandle("musicxml2ly", req, res,
			({xml, options}) => lilyCommands.xml2ly(xml, options && JSON.parse(options))),
	},


	"/midi2ly": {
		post: (req, res) => formidableHandle("midi2ly", req, res,
			({options}, {midi}) => lilyCommands.midi2ly(midi, options && JSON.parse(options))),
	},


	"/engrave": {
		post: (req, res) => formidableHandle("engrave", req, res,
			async ({source, tokenize = false, log = false}) => {
				const result = await lilyCommands.engraveSvg(source, {includeFolders: constants.LY_INCLUDE_FOLDERS});
				if (!tokenize)
					return JSON.stringify(result);

				const lilyParser = await loadLilyParser();
				const lilyDocument = new LilyDocument(lilyParser.parse(source));
				//const attributes = lilyDocument.globalAttributes({readonly: true});
				//console.log("attributes:", attributes);

				lilyDocument.interpret();

				const logger = new LogRecorder({enabled: log});

				const {doc, hashTable} = staffSvg.createSheetDocumentFromSvgs(result.svgs, source, lilyDocument, {logger, DOMParser});

				return JSON.stringify({
					...result,
					doc,
					hashTable,
					logger,
				});
			}),
	},


	"/engraveScm": {
		post: (req, res) => formidableHandle("engraveScm", req, res,
			async ({source}) => {
				const result = await lilyCommands.engraveScm(source, {includeFolders: constants.LY_INCLUDE_FOLDERS});
				return JSON.stringify(result);
			}),
	},


	"/engraveMIDI": {
		post: (req, res) => formidableHandle("engraveMIDI", req, res,
			async ({source, articulate = false}) => {
				const lilyParser = await loadLilyParser();
				const midi = await (articulate ? ScoreMaker.makeArticulatedMIDI(source, lilyParser, {includeFolders: constants.LY_INCLUDE_FOLDERS})
					: ScoreMaker.makeMIDI(source, lilyParser, {includeFolders: constants.LY_INCLUDE_FOLDERS}));
				const buffer = Buffer.from(MIDI.encodeMidiFile(midi));

				return {
					header: {
						"Content-Type": "audio/midi",
					},
					body: buffer,
				};
			}),
	},


	"/advanced-engrave": {
		post: (req, res) => new formidable.IncomingForm().parse(req, async (err, fields) => {
			try {
				if (err)
					throw err;

				const logger = new LogRecorder({enabled: !!fields.log});

				const lilyParser = await loadLilyParser();

				const task = advancedEngrave(fields.source, lilyParser, res, {
					includeFolders: constants.LY_INCLUDE_FOLDERS,
					withMIDI: !!fields.withMIDI,
					withNotation: !!fields.withNotation,
					withLilyDoc: !!fields.withLilyDoc,
					withLilyNotation: !!fields.withLilyNotation,
					logger,
				});

				res.writeHead(200);

				await task;
				res.end();
			}
			catch (err) {
				console.warn("advanced-engrave error:", err);
		
				res.writeHead(500);
				res.write(err.toString());
				res.end();
			}
		}),
	},
};


const setEnvironment = env => lilyCommands.setEnvironment(env);



export {
	setEnvironment,
	service,
	lilyCommands,
	ScoreMaker,
	advancedEngrave,
};