-
Notifications
You must be signed in to change notification settings - Fork 2
/
haps.js
360 lines (319 loc) · 12.5 KB
/
haps.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
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
class HapsPlotSettings {
constructor(thickness, padding, gain) {
this.thickness = thickness;
this.padding = padding;
this.gain = gain;
}
}
var hapsPlotSettings = new HapsPlotSettings(3, 4, 200);
class HapsTransient {
constructor(hapsFile, json) {
this.hapsFile = hapsFile;
this.json = json;
}
plot(canvas) {
const ctx = canvas.getContext("2d");
ctx.strokeStyle = 'yellow';
ctx.moveTo(0, 0);
let length = this.hapsFile.getHapsLength();
//console.log('length', length);
//console.log('hapsTransient', hapsTransient);
const notes = this.json.m_notes;
//console.log('notes', notes);
notes.forEach(note => {
//console.log('note', note);
const noteLength = note.m_length;
//console.log('noteLength', noteLength);
const hapticEffect = note.m_hapticEffect;
const amplitudeModulation = hapticEffect.m_amplitudeModulation;
if (!amplitudeModulation) {
const startingPoint = note.m_startingPoint;
const noteGain = note.m_gain;
const noteLength2 = note.m_length;
const startTime = startingPoint / length * (canvas.width - hapsPlotSettings.thickness);
const endTime = (startingPoint + noteLength2) / length * (canvas.width - hapsPlotSettings.thickness);
const x = startTime;
const y = noteGain;
// lines
ctx.beginPath();
ctx.lineWidth = 1;
ctx.moveTo(x, canvas.height);
ctx.lineTo(x, canvas.height - y * hapsPlotSettings.gain);
ctx.stroke();
ctx.closePath();
// draw a circle
ctx.beginPath();
ctx.lineWidth = 5;
ctx.arc(x, canvas.height - y * hapsPlotSettings.gain, hapsPlotSettings.thickness / 2, 0, 2 * Math.PI);
//ctx.fill();
ctx.stroke();
ctx.closePath();
}
});
}
}
class HapsMelody {
constructor(hapsFile, json) {
this.hapsFile = hapsFile;
this.json = json;
}
plot(canvas) {
const ctx = canvas.getContext("2d");
ctx.moveTo(0, 0);
ctx.lineWidth = 1;
let length = this.hapsFile.getHapsLength();
//console.log('length', length);
// draw each note
let index = 0;
let lastPoint = {};
//console.log('melody', melody);
const notes = this.json.m_notes;
//console.log('notes', notes);
let lastValue = null;;
notes.forEach(note => {
//console.log('note', note);
const noteStartingPoint = note.m_startingPoint;
//console.log('noteStartingPoint', noteStartingPoint);
const noteLength = note.m_length;
//console.log('noteLength', noteLength);
const hapticEffect = note.m_hapticEffect;
const amplitudeModulation = hapticEffect.m_amplitudeModulation;
if (amplitudeModulation) {
//console.log('amplitudeModulation', amplitudeModulation);
const keyframes = amplitudeModulation.m_keyframes;
//console.log('keyframes', keyframes);
keyframes.forEach(keyframe => {
//console.log('keyframe', keyframe);
let time = keyframe.m_time;
time += noteStartingPoint;
const value = keyframe.m_value;
const x1 = 0;
const y1 = canvas.height;
const x2 = time / length * canvas.width;
const y2 = canvas.height - value * hapsPlotSettings.gain;
if (index == 0) {
this.hapsFile.drawLine(ctx, 'cyan',
x1, y1,
x2, y2,
);
} else {
this.hapsFile.drawLine(ctx, '#309dde',
lastPoint.x, lastPoint.y,
x2, y2,
);
}
lastValue =
{
x: x2,
y: y2,
};
++index;
});
}
});
}
}
class HapsFile {
constructor(path, callback) {
this.path = path;
this.json = null;
this.ready = false;
this.transient = null;
this.melodies = [];
this.readFile(callback);
}
getHapsLength() {
let length = 0;
if (this.json) {
let vibration = this.json.m_vibration;
if (vibration) {
let melodies = vibration.m_melodies;
if (melodies && melodies.length) {
for (let m = 0; m < melodies.length; ++m) {
let melody = melodies[m];
if (melody) {
let notes = melody.m_notes;
if (notes && notes.length) {
for (let n = 0; n < notes.length; ++n) {
let note = notes[n];
if (note) {
let startingPoint = note.m_startingPoint;
let noteLength = note.m_length;
let endTime = startingPoint + noteLength;
length = Math.max(length, endTime);
//console.log('endTime', endTime);
}
}
}
}
}
}
return length;
} else {
return 0;
}
}
}
getHapsTransient() {
return this.transient;
}
getHapsMelodies() {
return this.melodies;
}
getHapsMelodyIndex(hapsMelody) {
return this.melodies.indexOf(hapsMelody);
}
getHapsAmplitude(hapsTime) {
var amplitude = 0;
let transient = this.getHapsTransient();
if (transient) {
const notes = transient.json.m_notes;
notes.forEach(note => {
const noteStartingPoint = note.m_startingPoint;
const noteLength = note.m_length;
const hapticEffect = note.m_hapticEffect;
const amplitudeModulation = hapticEffect.m_amplitudeModulation;
if (!amplitudeModulation) {
if (hapsTime >= noteStartingPoint && hapsTime < noteStartingPoint + noteLength) {
amplitude = Math.max(amplitude, note.m_gain);
}
}
});
}
this.getHapsMelodies().forEach(melody => {
const notes = melody.json.m_notes;
notes.forEach(note => {
const noteStartingPoint = note.m_startingPoint;
const hapticEffect = note.m_hapticEffect;
const amplitudeModulation = hapticEffect.m_amplitudeModulation;
if (amplitudeModulation) {
//console.log('amplitudeModulation', amplitudeModulation);
const keyframes = amplitudeModulation.m_keyframes;
//console.log('keyframes', keyframes);
//console.log('keyframes.length', keyframes.length);
// find nearest keyframe using hapsTime and keyframe.m_time
// interpolate between keyframes
// loop through each keyframe
// check if hapsTime is between previous keyframe and current keyframe
// get the value of both keyframes
// interpolate between the two keyframes
// set the amplitude to the new value if larger than the old amplitude
//console.log('search', hapsTime);
for (let k = 1; k < keyframes.length; ++k) {
const prevKeyframe = keyframes[k - 1];
const keyframe = keyframes[k];
//console.log('k-1', k-1, prevKeyframe.m_time);
//console.log('k', k, keyframe.m_time);
if (k == 1 && hapsTime < (noteStartingPoint + prevKeyframe.m_time)) {
// search time is less than the previous keyframe
// amplitude is zero
//console.log('skip');
break;
}
if ((noteStartingPoint + prevKeyframe.m_time) <= hapsTime && hapsTime < (noteStartingPoint + keyframe.m_time)) {
//console.log('search', hapsTime, 'should be matched?');
//console.log('k-1', k - 1, prevKeyframe.m_time, prevKeyframe.m_value);
//console.log('k', k, keyframe.m_time, keyframe.m_value);
// interpolate
const prevTime = (noteStartingPoint + prevKeyframe.m_time);
const prevValue = prevKeyframe.m_value;
const time = (noteStartingPoint + keyframe.m_time);
const value = keyframe.m_value;
const t = (hapsTime - prevTime) / (time - prevTime);
const newValue = prevValue + (value - prevValue) * t;
amplitude = Math.max(amplitude, newValue);
break;
}
}
}
});
});
return amplitude;
}
drawLine(ctx, color, x1, y1, x2, y2) {
ctx.strokeStyle = color;
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
plotTransient(canvas) {
if (this.transient) {
this.transient.plot(canvas);
}
}
plotMelody(canvas, hapsMelody) {
hapsMelody.plot(canvas);
}
plotOutput(canvas) {
const ctx = canvas.getContext("2d");
let length = this.getHapsLength();
//console.log('length', length);
/*
const transient = this.getHapsTransient();
if (transient) {
transient.plot(canvas);
}
this.getHapsMelodies().forEach(melody => {
melody.plot(canvas);
});
*/
ctx.strokeStyle = 'white';
ctx.lineWidth = 1;
ctx.moveTo(0, 0);
for (let x = 0; x < canvas.width; ++x) {
let canvasTime = x / canvas.width * length;
var amplitude = this.getHapsAmplitude(canvasTime);
// lines
ctx.beginPath();
ctx.lineWidth = 1;
ctx.moveTo(x, canvas.height);
ctx.lineTo(x, canvas.height - amplitude * hapsPlotSettings.gain);
ctx.stroke();
ctx.closePath();
}
}
readJson(text) {
//console.log('text', text);
this.json = JSON.parse(text);
//console.log('json', json);
const vibration = this.json.m_vibration;
//console.log('vibration', vibration);
const melodies = vibration.m_melodies;
//console.log('melodies', melodies);
this.melodies = [];
melodies.forEach(melody => {
let isTransient = false;
const notes = melody.m_notes;
//console.log('notes', notes);
notes.forEach(note => {
const hapticEffect = note.m_hapticEffect;
const amplitudeModulation = hapticEffect.m_amplitudeModulation;
if (!amplitudeModulation) {
isTransient = true;
}
});
if (isTransient) {
const hapsTransient = new HapsTransient(this, melody);
this.transient = hapsTransient;
} else {
const hapsMelody = new HapsMelody(this, melody);
this.melodies.push(hapsMelody);
}
});
this.ready = true;
}
async readFile(callback) {
if (!this.path) {
return;
}
await fetch(this.path)
// get result
.then(response => {
response.text()
.then(text => {
this.readJson(text);
callback(this);
});
});
}
}