-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathaudio.liq
More file actions
348 lines (321 loc) · 8.33 KB
/
audio.liq
File metadata and controls
348 lines (321 loc) · 8.33 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# Multiband compression. The list in argument specifies
# - the `frequency` below which we should apply compression (it is above previous band)
# - the `attack` time (ms)
# - the `release` time (ms)
# - the compression `ratio`
# - the `threshold` for compression
# - the `gain` for the band
# @category Source / Audio processing
# @param ~limit Also apply limiting to bands.
# @param l Parameters for compression bands.
# @param s Source on which multiband compression should be applied.
# @flag extra
def compress.multiband(~limit=true, ~wet=getter(1.), s, l) =
# Check that the bands are with increasing frequencies.
for
i
=
0
to
list.length(l) - 2
do
if
getter.get(list.nth(l, i + 1).frequency) <
getter.get(list.nth(l, i).frequency)
then
failwith("Bands should be sorted.")
end
end
# Process a band
def band(low, band) =
high =
if
getter.is_constant(band.frequency)
and
getter.get(band.frequency) >= float_of_int(audio.samplerate()) / 2.
then
infinity
else
band.frequency
end
s = filter.iir.eq.low_high(low=low, high=high, s)
s =
compress(
attack=band.attack,
release=band.release,
threshold=band.threshold,
ratio=band.ratio,
gain=band.gain,
s
)
if limit then limiter(s) else s end
end
ls =
list.mapi(
fun (i, b) ->
band(if i == 0 then 0. else list.nth(l, i - 1).frequency end, b),
l
)
c = add(normalize=false, ls)
s =
if
not getter.is_constant(wet) or getter.get(wet) != 1.
then
add(
normalize=false, [amplify({1. - getter.get(wet)}, s), amplify(wet, c)]
)
else
c
end
# Seal l element type
if false then () else list.hd(l) end
# Limit to avoid bad surprises
limiter(s)
end
# Compress and normalize, producing a more uniform and "full" sound.
# @category Source / Audio processing
# @flag extra
# @param s The input source.
def nrj(s) =
compress(threshold=-15., ratio=3., gain=3., normalize(s))
end
# Multiband-compression.
# @category Source / Audio processing
# @flag extra
# @param s The input source.
def sky(s) =
# 3-band crossover
low = fun (s) -> filter.iir.eq.low(frequency=168., s)
mh = fun (s) -> filter.iir.eq.high(frequency=100., s)
mid = fun (s) -> filter.iir.eq.low(frequency=1800., s)
high = fun (s) -> filter.iir.eq.high(frequency=1366., s)
# Add back
add(
normalize=false,
[
compress(
attack=100.,
release=200.,
threshold=-20.,
ratio=6.,
gain=6.7,
knee=0.3,
low(s)
),
compress(
attack=100.,
release=200.,
threshold=-20.,
ratio=6.,
gain=6.7,
knee=0.3,
mid(mh(s))
),
compress(
attack=100.,
release=200.,
threshold=-20.,
ratio=6.,
gain=6.7,
knee=0.3,
high(s)
)
]
)
end
# Generate DTMF tones.
# @flag extra
# @category Source / Sound synthesis
# @param ~duration Duration of a tone (in seconds).
# @param ~delay Dealy between two successive tones (in seconds).
# @param dtmf String describing DTMF tones to generates: it should contains characters 0 to 9, A to D, or * or #.
def replaces dtmf(~duration=0.1, ~delay=0.05, dtmf) =
l = ref([])
for
i
=
0
to
string.length(dtmf) - 1
do
c = string.sub(dtmf, start=i, length=1)
let (row, col) =
if
c == "1"
then
(697., 1209.)
elsif c == "2" then (697., 1336.)
elsif c == "3" then (697., 1477.)
elsif c == "A" then (697., 1633.)
elsif c == "4" then (770., 1209.)
elsif c == "5" then (770., 1336.)
elsif c == "6" then (770., 1477.)
elsif c == "B" then (770., 1633.)
elsif c == "7" then (852., 1209.)
elsif c == "8" then (852., 1336.)
elsif c == "9" then (852., 1477.)
elsif c == "C" then (852., 1633.)
elsif c == "*" then (941., 1209.)
elsif c == "0" then (941., 1336.)
elsif c == "#" then (941., 1477.)
elsif c == "D" then (941., 1633.)
else
(0., 0.)
end
s = add([sine(row, duration=duration), sine(col, duration=duration)])
l := blank(duration=delay)::l()
l := s::l()
end
l = list.rev(l())
sequence(l)
end
# Mixing table controllable via source methods and optional
# server/telnet commands.
# @flag extra
# @category Source / Audio processing
# @param ~id Force the value of the source ID.
# @param ~register_server_commands Register corresponding server commands
def mix(~id=null(), ~register_server_commands=true, sources) =
id = string.id.default(default="mixer", id)
inputs =
list.map(
fun (s) ->
begin
volume = ref(1.)
is_selected = ref(false)
is_single = ref(false)
{volume=volume, selected=is_selected, single=is_single, source=s}
end,
sources
)
insert_metadata_fn = ref(fun (_) -> ())
sources =
list.map(
fun (input) ->
begin
s = amplify(input.volume, input.source)
s =
source.on_track(
s, fun (_) -> if input.single() then input.selected := false end
)
s =
source.on_metadata(
s,
fun (m) ->
begin
fn = insert_metadata_fn()
fn(m)
end
)
switch([(input.selected, s)])
end,
inputs
)
s = add(sources)
let {metadata = _, ...tracks} = source.tracks(s)
s = source(tracks)
s = insert_metadata(s)
insert_metadata_fn := s.insert_metadata
let {track_marks = _, ...tracks} = source.tracks(s)
s = source(id=id, tracks)
if
register_server_commands
then
def status(input) =
"ready=#{source.is_ready(
input.source
)} selected=#{input.selected()} single=#{input.single()} volume=#{int_of_float(
input.volume() * 100.
)}% remaining=#{source.remaining(input.source)}"
end
server.register(
namespace=source.id(s),
description="Skip current track on all enabled sources.",
"skip",
fun (_) ->
begin
list.iter(
fun (input) ->
if input.selected() then source.skip(input.source) end,
inputs
)
"OK"
end
)
server.register(
namespace=source.id(s),
description="Set volume for a given source.",
usage="volume <source nb> <vol%>",
"volume",
fun (v) ->
begin
try
let [i, v] = r/\s/.split(v)
input = list.nth(inputs, int_of_string(i))
input.volume := float_of_string(v)
status(input)
catch _ do
"Usage: volume <source nb> <vol%>"
end
end
)
server.register(
namespace=source.id(s),
description="Enable/disable a source.",
usage="select <source nb> <true|false>",
"select",
fun (arg) ->
begin
try
let [i, b] = r/\s/.split(arg)
input = list.nth(inputs, int_of_string(i))
input.selected := (b == "true")
status(input)
catch _ do
"Usage: select <source nb> <true|false>"
end
end
)
server.register(
namespace=source.id(s),
description="Enable/disable automatic stop at the end of track.",
usage="single <source nb> <true|false>",
"single",
fun (arg) ->
begin
try
let [i, b] = r/\s/.split(arg)
input = list.nth(inputs, int_of_string(i))
input.single := (b == "true")
status(input)
catch _ do
"Usage: single <source nb> <true|false>"
end
end
)
server.register(
namespace=source.id(s),
description="Display current status.",
"status",
fun (i) ->
begin
try
status(list.nth(inputs, int_of_string(i)))
catch _ do
"Usage: status <source nb>"
end
end
)
server.register(
namespace=source.id(s),
description="Print the list of input sources.",
"inputs",
fun (_) ->
string.concat(
separator=" ",
list.map(fun (input) -> source.id(input.source), inputs)
)
)
end
s.{inputs=inputs}
end