-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
126 lines (113 loc) · 2.68 KB
/
main.js
File metadata and controls
126 lines (113 loc) · 2.68 KB
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
const { app, BrowserWindow, ipcMain } = require('electron');
const fs = require('fs');
const path = require('path');
const nanotimer = require('nanotimer');
const _ = require('lodash');
const midi = require('midi');
let win = null;
let project = {};
let outputs = [];
let timer = null;
let interval = 0;
let isPlaying = false;
let playheadPosition = 0;
function createWindow() {
win = new BrowserWindow({
width: 1024,
height: 700,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
win.loadFile(path.join(__dirname, 'html', 'index.html'));
}
app.whenReady().then(() => {
createWindow();
load();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
ipcMain.on('action', (event, msg) => {
switch (msg.action) {
case 'load':
load(err => {
if (!err) {
win.webContents.send('projectLoaded', project);
}
});
break;
case 'play':
play();
break;
case 'stop':
stop();
break;
}
});
function load(next) {
fs.readFile(path.join(__dirname, 'data', 'example.json'), 'utf-8', (err, data) => {
if (err) {
if (next) next(err);
return;
}
project = JSON.parse(data);
outputs = [];
_.each(project.tracks, track => {
outputs[track.port] = new midi.output();
outputs[track.port].openPort(track.port);
});
if (next) next(null, data);
});
}
function noteOn(note, velocity, port) {
outputs[port].sendMessage([144, note, velocity]);
}
function noteOff(note, port) {
outputs[port].sendMessage([128, note, 0]);
}
function note(message) {
let duration = message[3];
duration *= parseInt(interval);
noteOn(message[1], message[2], message[4]);
new nanotimer().setTimeout((n, p) => {
noteOff(n, p);
}, [message[1], message[4]], duration + 'm');
}
function setPlayheadPosition(value) {
playheadPosition = value;
if (win) {
win.webContents.send('playheadPositionUpdated', playheadPosition);
}
}
function play() {
isPlaying = true;
function tickHandler() {
_.each(project.tracks, track => {
const clip = track.clips[0];
const events = clip.events[playheadPosition % clip.length];
_.each(events, event => {
switch (event[0]) {
case 'note':
note(event.concat(track.port));
break;
}
});
});
setPlayheadPosition(playheadPosition + 1);
}
interval = (15000 / project.bpm).toString() + 'm';
timer = new nanotimer();
timer.setInterval(tickHandler, '', interval);
}
function stop() {
if (isPlaying) {
isPlaying = false;
timer.clearInterval();
} else {
setPlayheadPosition(0);
}
}