-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.js
118 lines (102 loc) · 3.26 KB
/
client.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
const editor = document.getElementById('editor');
let editorContent = '';
let channels = {};
const play = (args = []) => {
const [chan = 0, start, duration, velocity] = args
channels[chan] && channels[chan].play(start,duration,velocity);
}
const loadSamples = ({files=[], path= ''}) => {
const samples = files.filter((name) => name.includes('.wav'))
samplers = samples.map(function (filename,i) {
sampleMap[i] = `${path}/${filename}`
return {
name: filename.split('.wav')[0].substring(0,8),
};
});
};
const save = ({filename}) => filename && socket.sendSaveData(filename, JSON.stringify(gridData));
const openFile = ({filename, data})=> {
gridData = JSON.parse(data);
const splitPath = filename.split('/')
Editor.filename = splitPath[splitPath.length - 1].replace('.gull', '');
Editor.redraw();
}
const socket = Listener({
onLoadSamples: (...args) => {loadSamples(...args);},
onPlay: play,
onSave: save,
onOpenFile: openFile
});
const replaceChar = (line, rowId, char) => {
const chars = line.split('');
if (chars.length > rowId) {
chars[rowId] = char;
}
return chars.join('');
};
const remoteEdit = (rowId, lineId, char) => {
Editor.remoteEditChar(rowId, lineId, char)
// const lines = editor.value.split('\n')
// if (lines.length > lineId) {
// lines[lineId] = replaceChar(lines[lineId],rowId, char);
// editor.value = lines.join('\n');
// }
};
const cleanupChannels = () => {
Object.keys(channels).forEach(key => {
channels[key].forEach(machine => {
machine.dispose();
});
});
};
initChannels = () => {
for(let i = 0; i<36 ; i++) {
const channelId = i.toString(36).toUpperCase();
channels[channelId] = Machine();
}
channels['external'] = Machine('external');
};
initChannels();
const parseChannelId = (chan) => chan[0].params[0] || null;
const loadMachine = (chan) => {
if (chan[0]) {
switch(chan[0].type) {
case 'C':
const channelId = parseChannelId(chan);
channelId && channels[channelId].load(chan);
break;
case 'E':
channels['external'].load(chan);
break;
}
}
};
const parseEditorContent = () => {
gridData.forEach(function(line, y) {
// todo: clear machine that are not defined in grid
let chan = [];
line.forEach((cell, x) => {
if (cell.type === 'param') return ;
if (Object.keys(parsingMap).includes(cell.char)) {
Editor.cleanUnusedParams(x,y);
cell.type = 'block';
let start = y + 1;
let end = y + parsingMap[cell.char];
const params = [];
for (let l = start; l <= end ; l++) {
const cell = gridData[l][x];
params.push(cell.char);
gridData[l][x] = { ...cell, type: 'param' };
}
chan.push({
type: cell.char,
params
})
} else {
loadMachine(chan);
chan = [];
}
});
});
};
Editor.onUpdate = parseEditorContent;