-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
216 lines (169 loc) · 5.26 KB
/
Copy pathindex.mjs
File metadata and controls
216 lines (169 loc) · 5.26 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
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
import osc from "osc"
import { input } from "@inquirer/prompts"
const config = {
caspar: {},
companion: {}
}
const currentState = {
current: 0,
total: 0,
name: null,
fps: 0
}
let resetTimeout = null
const casparOSCPort = await input({
message: "OSC Port:",
default: 5254,
})
const casparSourceChannel = await input({
message: "CasparCG Server Source Channel:",
default: 2,
})
const casparSourceLayer = await input({
message: "CasparCG Server Source Layer:",
default: 10,
})
const companionIP = await input({
message: "Bitfocus Companion IP:",
default: "0.0.0.0",
})
const companionOSCPort = await input({
message: "Bitfocus Companion OSC Port:",
default: 12321,
})
const companionVariable = await input({
message: "Bitfocus Companion Custom Variable:",
default: "caspar_clip_left"
})
config.caspar.ip = "0.0.0.0"
config.caspar.oscPort = parseInt(casparOSCPort)
config.caspar.channel = parseInt(casparSourceChannel)
config.caspar.layer = parseInt(casparSourceLayer)
config.companion.ip = companionIP
config.companion.oscPort = parseInt(companionOSCPort)
config.companion.variable = companionVariable
const oscClient = new osc.UDPPort({
localAddress: config.caspar.ip,
localPort: config.caspar.oscPort,
remoteAddress: config.companion.ip,
remotePort: config.companion.oscPort
})
const formatTimecode = (t) => `${(t.m).toString().padStart(2, "0")}:${(t.s).toString().padStart(2, "0")}.${(t.f).toString().padStart(3, "0")}`
const processState = (block) => {
if (block.name === null) {
return "Empty"
}
if (block.current === -1) {
return null
}
if (block.fps === 0) {
const calcTimings = (time) => {
const seconds = Math.floor(time);
const milliseconds = Math.floor((time - seconds) * 1000);
const minutes = Math.floor(seconds / 60)
return formatTimecode({ m: minutes % 60, s: seconds % 60, f: milliseconds })
}
return calcTimings(block.total - block.current)
} else {
const calcTimings = (time) => {
const cs = Math.floor(time / block.fps)
const cm = Math.floor(cs / 60)
const cf = (time % block.fps);
return formatTimecode({ m: cm % 60, s: cs % 60, f: cf })
}
return calcTimings(block.total - block.current)
}
}
const emitState = () => {
const toSend = processState(currentState)
if (toSend === null) {
return
}
oscClient.send({
address: `/custom-variable/${config.companion.variable}/value`,
args: [
{
type: "s",
value: toSend
}
]
}, config.companion.ip, config.companion.oscPort)
}
const clearState = () => {
if (currentState.paused) { return }
currentState.current = 0
currentState.total = 0
currentState.name = null
currentState.fps = 0
emitState()
}
oscClient.on("open", function() {
console.log("Listening for OSC over UDP: CasparCG Server")
console.log(`Host: ${config.caspar.ip}, Port: ${config.caspar.oscPort}`)
console.log(" ")
})
const onOscMessage = function(message) {
// console.log(message)
if (message.address === `/channel/${config.caspar.channel}/stage/layer/${config.caspar.layer}/paused`) {
currentState.paused = message.args[0]
if (currentState.paused) {
if (resetTimeout != null) {
clearTimeout(resetTimeout)
}
resetTimeout = setTimeout(clearState, 150)
}
} else if (message.address === `/channel/${config.caspar.channel}/stage/layer/${config.caspar.layer}/file/frame` || message.address === `/channel/${config.caspar.channel}/stage/layer/${config.caspar.layer}/frame`) {
currentState.current = message.args[0].low
currentState.total = message.args[1].low
emitState()
} else if(message.address === `/channel/${config.caspar.channel}/stage/layer/${config.caspar.layer}/foreground/file/time`) {
currentState.current = message.args[0]
currentState.total = message.args[1]
currentState.fps = 0
emitState()
} else if (message.address === `/channel/${config.caspar.channel}/stage/layer/${config.caspar.layer}/file/fps`) {
currentState.fps = message.args[0]
emitState()
} else if (message.address === `/channel/${config.caspar.channel}/stage/layer/${config.caspar.layer}/file/path` || message.address === `/channel/${config.caspar.channel}/stage/layer/${config.caspar.layer}/foreground/file/name`) {
if (resetTimeout != null) {
clearTimeout(resetTimeout)
}
resetTimeout = setTimeout(clearState, 150)
currentState.name = message.args[0].slice(0, -4).split("/").at(-1)
emitState()
}
}
oscClient.on("message", onOscMessage)
oscClient.open()
oscClient.on("error", function(error) {
console.log("CasparCG Server error:")
console.log(JSON.stringify(error))
console.log(" ")
})
const stdin = process.openStdin()
const exit = () => {
oscClient.off("message", onOscMessage)
setTimeout(clearState, 200)
setTimeout(process.exit, 400)
}
process.on("SIGINT", function() {
console.log("Process exit event: Caught interrupt signal")
exit()
})
process.on("SIGQUIT", function() {
console.log("Process exit event: Quit")
exit()
})
process.on("SIGTERM", function() {
console.log("Process exit event: Killed")
exit()
})
stdin.on("beforeExit", () => {
exit()
})
stdin.on("exit", (code) => {
exit()
oscClient.close()
console.log("Process exit event with code: ", code)
})
stdin.resume()