-
Notifications
You must be signed in to change notification settings - Fork 0
/
cueList.js
173 lines (149 loc) · 4.42 KB
/
cueList.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
/*
Copyright ou © ou Copr. Clément Bossut, (2018)
Ce logiciel est un programme informatique servant à écrire et jouer une conduite lumière synchronisée avec du son sur une Raspberry Pi avec PCA8596.
Ce logiciel est régi par la licence CeCILL soumise au droit français et
respectant les principes de diffusion des logiciels libres. Vous pouvez
utiliser, modifier et/ou redistribuer ce programme sous les conditions
de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
sur le site "http://www.cecill.info".
*/
const orgue = require("./orgue.js")
, fs = require('fs')
, spawn = require('child_process').spawn
let cl = {
soundPath: ''
}
, cue = {
name: 'Mise'
, state: [0]
, upTime: 10
, downTime: 10
, date: -1
}
cl.content = []
cl.orgue = orgue
orgue.init()
cl.save = function(path) {
fs.writeFileSync(path, JSON.stringify({
cueList: this.content,
soundPath: this.soundPath,
patch: this.orgue.patch
}))
}
cl.load = function(path) {
let l = JSON.parse(fs.readFileSync(path))
this.content = l.cueList
this.soundPath = l.soundPath
this.orgue.patch = l.patch
this.content.forEach((v,i,a)=>{
Object.keys(cue).forEach(k=>{
if (!v[k]) v[k] = cue[k]
})
})
}
cl.addCue = function(c=cue, n = this.content.length-1, writeState = true) {
let oldCue = this.content[n]||{}
, newCue = {}
if (writeState) c.state = this.orgue.state
Object.keys(cue).forEach(k=>{
newCue[k] = c[k] || oldCue[k] || cue[k]
})
newCue.state = newCue.state.slice() //Security, just to be sure...
this.content.splice(n+1, 0, newCue)
}
cl.removeCue = function(n) {
this.content.splice(n,1)
}
cl.updateCue = function(n) {
this.content[n].state = this.orgue.state
}
cl.applyCue = function(n) {
this.orgue.state = this.content[n].state
}
cl.play = function(pos = 0) {
//TODO CF testSpawn
}
cl.go = function(n = 0, cb = null) {
if (n < 0 || n >= this.content.length - 1) {
if (cb) cb(false, null)
return;
}
this.from = this.content[n].state
this.to = this.content[++n].state
this.downLeds = []
this.upLeds = []
let downDiff = -1
, upDiff = -1
for(let i = 0 ; i < Math.max(this.to.length, this.from.length) ; i++) {
let diff = (this.to[i]||0) - (this.from[i]||0)
if (diff < 0) {
this.downLeds.push(i)
downDiff = Math.max(downDiff, -diff)
} else if (diff > 0) {
this.upLeds.push(i)
upDiff = Math.max(upDiff, diff)
}
}
let downMs = downDiff == -1 ? 0 : this.content[n].downTime * 1000
, upMs = upDiff == -1 ? 0 : this.content[n].upTime * 1000
this.interMs = Math.max(
this.orgue.minMsInter,
Math.min(downMs/downDiff, upMs/upDiff)
)
this.downFrames = Math.round(downMs/this.interMs)
this.upFrames = Math.round(upMs/this.interMs)
this.frameCount = 0
let start = this.from.slice()
if (!this.downFrames) this.downLeds.forEach(v => start[v] = this.to[v])
if (!this.upFrames) this.upLeds.forEach(v => start[v] = this.to[v])
this.orgue.state = start
this.nextFrame = new Date().getTime()
this.timeOut = setTimeout(()=>{this.inter(cb)}, this.interMs)
}
cl.inter = function(cb) {
this.frameCount++
this.nextFrame += this.interMs
let diff = new Date().getTime() - this.nextFrame
if (diff >= this.interMs) {
let jumped = Math.floor(diff/this.interMs)
diff -= jumped * this.interMs
this.nextFrame += jumped * this.interMs
this.frameCount += jumped
}
let loop = false
, ratioUp = this.frameCount/this.upFrames
, ratioDown = this.frameCount/this.downFrames
, res = this.from.slice()
if (ratioUp < 1) {
loop = true
this.upLeds.forEach(
v => res[v] = Math.round(this.from[v] + ratioUp * (this.to[v]-this.from[v]))
)
} else {
this.upLeds.forEach(v => res[v] = this.to[v])
}
if (ratioDown < 1) {
loop = true
this.downLeds.forEach(
v => res[v] = Math.round(this.from[v] + ratioDown * (this.to[v]-this.from[v]))
)
} else {
this.downLeds.forEach(v => res[v] = this.to[v])
}
this.orgue.state = res
if (loop) this.timeOut = setTimeout(()=>{this.inter(cb)}, this.interMs - diff)
if (cb) cb(loop, this.frameCount*this.interMs)
}
cl.stop = function() {
clearTimeout(this.timeOut)
}
cl.print = function() {
for(let i = 0 ; i < this.content.length ; i++) {
let c = this.content[i]
console.log(c.name, "up", c.upTime, "down", c.downTime, c.state)
}
}
module.exports = {
proto: cl
}