This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathminidsp.js
executable file
·162 lines (137 loc) · 3.65 KB
/
minidsp.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
#!/usr/bin/env node
const Device = require('./src/device');
const program = require('commander');
const debug = require('debug')('minidsp');
const USBTransport = require('./src/transport/usb');
const NetTransport = require('./src/transport/net');
program.version('1.0.3')
.option('-t --transport <transport>', 'Set the underlying transport type (usb, net)', 'usb', /^usb|net$/)
.option('-o --opt <opt>', 'Set transport-specific parameters');
let actions = [ ];
let _device;
function parseParams(params) {
if (!params) {
return {};
}
let parts = params.split(',');
let obj = {};
parts.forEach((part) => {
let keyvalue = part.split('=');
if (keyvalue.length === 2) {
let key = keyvalue[0],
value = keyvalue[1];
obj[key.trim()] = value.trim();
}
});
return obj;
}
function transportClass(name) {
const transportMap = {
'usb': USBTransport,
'net': NetTransport,
};
if (!(name in transportMap)) {
throw new Error(`No such transport ${name}`);
}
return transportMap[name];
}
function device() {
if (_device) {
return _device;
}
debug('Instanciating transport: ', program.transport);
debug('Params:', program.opt);
let TransportClass = transportClass(program.transport);
return _device = new Device({ transport: new TransportClass(parseParams(program.opt)) });
}
program
.command('devices')
.description('List available devices')
.action(() => {
let TransportClass = transportClass(program.transport);
let devices = TransportClass.probe(parseParams(program.opt));
devices.forEach(({path, product}) => console.log(`${path}\t${product}`));
});
program
.command('input <source>')
.description('Set input source [analog|toslink|usb]')
.action((source) => {
let dsp = device();
actions.push(dsp.setInput(source));
});
program
.command('config <index>')
.description('Set active configuration [0-3]')
.action((index) => {
let dsp = device();
actions.push(dsp.setConfig(index));
});
program
.command('mute [enable]')
.description('Sets the global mute flag')
.action((enable) => {
let dsp = device();
let value = enable || 'on';
actions.push(dsp.setMute(value === 'on'));
});
program
.command('gain <gain>')
.description('Set the master gain level (acceptable range -127 dB to 0 dB)')
.action((gain) => {
let dsp = device();
actions.push(dsp.setVolume(+gain));
});
program
.command('input-gain <gain>')
.description('Sets the input gain level (-127 dB to 12 dB)')
.action((gain) => {
let dsp = device();
actions.push([ 1, 2 ]
.map((x) => dsp.getInput(x))
.reduce((prevVal,curVal) =>
prevVal.then(() => curVal.setGain(gain)), Promise.resolve()
));
});
program
.command('monitor')
.description('Monitor input levels')
.action(() => {
let dsp = device();
const inputChannels = [0, 1];
const ProgressBar = require('ascii-progress');
var bars = inputChannels.map((x) => new ProgressBar({
schema:` ${x+1} [:bar] :token`,
total: 127
})
);
setInterval(() => {
dsp.getInputLevels().then((levels) => {
let convertLevel = (x) => 1 - (x/-127);
inputChannels.forEach((i) => {
bars[i].update(convertLevel(levels[i]), {
token: levels[i].toFixed(1) + ' dBFS'
});
});
});
}, 1000/24);
});
program
.command('proxy')
.description('Runs a proxy on port 5333 intended for use with the mobile application')
.action(() => {
let dsp = device();
const createProxy = require('./src/proxy');
createProxy({
transport: dsp.transport
});
});
program.parse(process.argv);
if (actions.length) {
Promise.all(actions)
// Close the device so we can exit
.then(() => _device.close())
.catch((e) => {
console.error(e.toString());
process.exit(1);
});
}