-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossaudio.cjs
executable file
·87 lines (73 loc) · 2.24 KB
/
crossaudio.cjs
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
#!/usr/bin/env node
const path = require('path')
// hack to make pkg work
const yargs = require('./node_modules/yargs')
const { hideBin } = require('./node_modules/yargs/helpers')
const easymidi = require('./node_modules/easymidi')
const { play, Params } = require('./node_modules/@crossaudio/core')
const {
$0,
_: [scriptfile],
...params
} = yargs(hideBin(process.argv))
.usage('Usage: $0 <synthfile> [options]')
.positional('synthfile', {
description: 'The crossaudio script that defines your synth',
required: true
})
.demand(1)
.example(
'$0 file.js --cutoff=74 --resonance=71',
'Setup midi CC to control synth params "cutoff" and "resonance".'
)
.example('$0 file.js --note=note', 'Send midi-message for note on/off').argv
async function main () {
const synth = (await import(path.resolve(process.cwd(), scriptfile))).default
// set intiial values to 0
const synthParams = new Params(
Object.keys(params).reduce((a, v) => {
return { ...a, [v]: 0 }
}, {})
)
function updateParam (name, value, msg) {
synthParams[name] = value
}
if (Object.keys(params).length) {
const noteParams = Object.keys(params).filter((k) => params[k] === 'note')
const ccLookup = Object.keys(params)
.filter((k) => params[k] !== 'note' && params[k] !== 'gate')
.reduce((a, k) => {
return { ...a, [params[k]]: k }
}, {})
easymidi.getInputs().forEach((device) => {
const i = new easymidi.Input(device)
if (noteParams.length) {
i.on('noteon', (m) => {
const { _type, ...msg } = m
msg.device = device
msg.type = _type
noteParams.forEach((name) => {
updateParam(name, { ...msg })
})
})
i.on('noteoff', (m) => {
const { _type, ...msg } = m
msg.device = device
msg.type = _type
noteParams.forEach((name) => {
updateParam(name, { ...msg })
})
})
}
i.on('cc', (msg) => {
// console.log('cc', { ...msg, device })
if (ccLookup[msg.controller]) {
updateParam(ccLookup[msg.controller], msg.value, { ...msg, device })
}
})
return i
})
}
play(synth, synthParams)
}
main()