-
Notifications
You must be signed in to change notification settings - Fork 0
/
reactSong.js
139 lines (122 loc) · 3.15 KB
/
reactSong.js
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
const reactReconciler = require("react-reconciler");
const { exec } = require("child_process");
const fs = require("fs");
const Midi = require("jsmidgen");
const harmonics = require("harmonics");
const instruments = {
organ: 0x13,
electronicGuitar: 0x22,
piano: 0x00,
};
const reconciler = reactReconciler({
supportsPersistence: true,
createInstance(
type,
props,
rootContainerInstance,
hostContext,
internalInstaneHandle
) {
const node = {
type: type.toLowerCase(),
children: [],
repeat: 1,
delay: 0,
channel: 0,
};
if (props.pitch) {
node.pitch = props.pitch;
} else if (type !== "track") {
node.pitch = 64;
}
if (props.instrument) {
node.instrument = props.instrument;
}
if (props.repeat) {
node.repeat = props.repeat;
}
if (props.name) {
node.name = props.name;
}
if (props.delay) {
node.delay = props.delay;
}
if (props.channel) {
node.channel = props.channel;
}
return node;
},
createTextInstance(
text,
rootContainerInstance,
hostContext,
internalInstaneHandle
) {
return {
node: "c4",
children: [],
};
},
appendInitialChild(parent, child) {
parent.children.push(child);
},
commitUpdate(domElement, updatePayload, type, oldProps, newProps) {},
removeChildFromContainer(container, child) {},
removeChild(parent, child) {},
insertInContainerBefore(container, child, before) {},
insertBefore(container, child, before) {},
prepareUpdate(
instance,
type,
oldProps,
newProps,
rootContainerInstance,
currentHostContext
) {},
commitTextUpdate(textInstance, oldText, newText) {},
finalizeInitialChildren() {},
getChildHostContext() {},
getPublicInstance() {},
getRootHostContext() {},
prepareForCommit() {},
resetAfterCommit() {},
shouldSetTextContent() {},
clearContainer(container) {},
createContainerChildSet() {
return [];
},
appendChildToContainerChildSet(childSet, child) {
childSet.push(child);
},
finalizeContainerChildren() {},
replaceContainerChildren(file, newChildren) {
const midiFile = new Midi.File();
newChildren.forEach((trackNode) => {
const track = new Midi.Track();
if (trackNode.instrument) {
track.setInstrument(
trackNode.channel,
instruments[trackNode.instrument]
);
}
midiFile.addTrack(track);
for (let i = 0; i < trackNode.repeat; i++) {
trackNode.children.forEach((node) => {
if (node.type === "harmonics") {
harmonics.scale(node.name).forEach((chord) => {
track.addNote(trackNode.channel, chord, node.pitch, chord.delay);
});
} else if (node.type === "note") {
track.addNote(trackNode.channel, node.name, node.pitch, node.delay);
}
});
}
});
fs.writeFileSync(file, midiFile.toBytes(), "binary");
exec(`open ${file}`, () => {});
},
});
exports.render = (app, file) => {
const container = reconciler.createContainer(file, false, false);
reconciler.updateContainer(app, container, null, null);
};