-
Notifications
You must be signed in to change notification settings - Fork 5
/
klangbecken.liq
296 lines (250 loc) · 8.74 KB
/
klangbecken.liq
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
# ================================================= #
# SETTINGS #
# #
# Environment Variables: #
# - KLANGBECKEN_ALSA_DEVICE #
# ALSA device to send sound to #
# Default: default #
# #
# - KLANGBECKEN_DATA_DIR #
# Directory with data #
# Default: ./data #
# #
# - KLANGBECKEN_COMMAND #
# Klangbecken command #
# Default: python -m klangbecken #
# #
# - KLANGBECKEN_PLAYER_SOCKET #
# Path to socket to listen on #
# Default: ./klangbecken.sock #
# ================================================= #
# return value of environment variable if set, otherwise fallback
def getenv_fallback(var, fallback) =
if getenv(var) != "" then
getenv(var)
else
fallback
end
end
# log file
set("log.file", true)
set("log.stdout", true)
set("server.telnet", false)
set("server.telnet.port", 1234)
set("log.level", 3)
# socket
set("server.socket", true)
set("server.socket.path", getenv_fallback("KLANGBECKEN_PLAYER_SOCKET", "./klangbecken.sock"))
set("server.socket.permissions", 0o660) # Make socket group readable/writable
# Get the Klangbecken data directory
DATA_DIR = getenv_fallback("KLANGBECKEN_DATA_DIR", "./data")
if not file.is_directory(DATA_DIR) then
log("ERROR: Cannot find data directory: #{DATA_DIR}", level=0)
shutdown()
end
if DATA_DIR == "./data" then
set("log.file.path", "./klangbecken.log")
end
# Get the klangbecken command
KLANGBECKEN_COMMAND = getenv_fallback("KLANGBECKEN_COMMAND", "python -m klangbecken")
# Get the alsa device
ALSA_DEVICE = getenv_fallback("KLANGBECKEN_ALSA_DEVICE", "default")
on_air = ref false
# ================================================= #
# PLAYLISTS #
# ================================================= #
# calculate waiting time for repeating a track depending on its playlist
def calc_wait(playlist) =
if playlist == "music" then 172800.0 # 2 days
elsif playlist == "classics" then 172800.0 # 2 days
elsif playlist == "jingles" then 3600.0 # 1 hour
else
log("WARNING: invalid playlist: #{playlist}", level=1, label="calc_wait")
0.0
end
end
# check if track was played recently
skipped = ref 0
def check_next_func(r) =
metadata = request.metadata(r)
filename = request.filename(r)
last_play_str = string.trim(metadata["last_play"])
if filename == "" then
false
elsif last_play_str == "" then
skipped := 0
log("track was never played before: #{filename}", label="check_next_func")
true
else
# Convert ISO-8601 timestamp to epoch seconds
last_play_str = get_process_output("date +%s -d #{last_play_str}")
last_play_str = string.trim(last_play_str)
last_play = float_of_string(last_play_str)
diff = gettimeofday() - last_play
playlist = metadata["source"]
if diff < calc_wait(playlist) then
skipped := !skipped + 1
log("track was recently played: #{filename} (#{diff} seconds ago)", label="check_next_func")
if !skipped >= 20 then
skipped := 0
if !on_air then
log("WARNING: too many skipped tracks, playing #{filename} anyway", level=1, label="check_next_func")
end
true
else
false
end
else
skipped := 0
log("next: #{filename} (track was last played #{diff} seconds ago)", label="check_next_func")
true
end
end
end
# Priority queue
queue = request.equeue(id="queue", length=5.0)
# Convert mono queue entries (jingles) to stereo
queue = audio_to_stereo(queue)
# Cut silence at start and end
queue = cue_cut(queue, cue_in_metadata="cue_in", cue_out_metadata="cue_out")
# Music playlist
music = playlist(
path.concat(DATA_DIR, "music.m3u"),
id="music",
mode="randomize",
reload_mode="watch",
check_next=check_next_func,
)
# Cut silence at start and end
music = cue_cut(music, cue_in_metadata="cue_in", cue_out_metadata="cue_out")
# Classics playlist
classics = playlist(
path.concat(DATA_DIR, "classics.m3u"),
id="classics",
mode="randomize",
reload_mode="watch",
check_next=check_next_func,
)
# Cut silence at start and end
classics = cue_cut(classics, cue_in_metadata="cue_in", cue_out_metadata="cue_out")
# Jingles playlist
jingles = playlist(
path.concat(DATA_DIR, "jingles.m3u"),
id="jingles",
mode="randomize",
reload_mode="watch",
check_next=check_next_func,
)
# Convert mono jingles to stereo
jingles = audio_to_stereo(jingles)
# Cut silence at start and end
jingles = cue_cut(jingles, cue_in_metadata="cue_in", cue_out_metadata="cue_out")
# ================================================= #
# MIX MUSIC AND CLASSICS #
# ================================================= #
music = random(weights=[5, 1], [music, classics])
# ================================================= #
# INSERT JINGLE AND QUEUE TRACKS WHEN NEEDED #
# ================================================= #
insert_jingle = ref false
def jingle_timeout() =
jingle_times = [5m0s, 20m0s, 35m0s, 50m0s]
if list.fold(fun (a,b) -> a or b, false, jingle_times) then
log("Jingle up next", label="jingle_timeout")
insert_jingle := true
end
1.0
end
add_timeout(0.0, jingle_timeout)
radio = switch(id="radio", [
({!insert_jingle}, jingles),
({!on_air}, queue),
({true}, music),
])
def on_track_func(m) =
# Reset jingle playing flag
if m["source"] == "jingles" then
insert_jingle := false
end
end
radio = on_track(on_track_func, radio)
# ================================================= #
# REGISTER EXTERNAL RESTART COMMAND #
# ================================================= #
restart = ref false
def on_air_func(state) =
state = string.case(state)
state = string.trim(state)
if state == "" then
# Return state
"#{!on_air}"
else
on_air := bool_of_string(state)
if !on_air then
log("INFO: Starting Klangbecken", level=2, label="on_air_func")
restart := true
source.skip(radio)
"Klangbecken started"
else
log("INFO: Stopping Klangbecken", level=2, label="on_air_func")
"Klangbecken stopped"
end
end
end
server.register(
namespace="klangbecken",
"on_air",
on_air_func,
usage="on_air [true|false]",
description="Control if the player is on air. Returns the current state, if called without argument."
)
# Have restart delay and fade dynamically reconfigurable
# for debugging purpose
restart_delay = interactive.float("restart.delay", 1.0)
restart_fade = interactive.float("restart.fade", 1.0)
def trans(old, new) =
if !restart and source.id(new) == "radio" then
restart := false
sequence([blank(duration=restart_delay()),
fade.initial(duration=restart_fade(), new)])
else
new
end
end
radio = fallback(track_sensitive=false,
transitions=[trans],
[radio, blank(id="blank")])
# ================================================= #
# LOGGING METADATA #
# ================================================= #
to_log_filenames = ref []
def log_metadata_func(m) =
if !on_air and m["filename"] != "" then
# Prepare play logger
filename = m["filename"]
to_log_filenames := list.append(!to_log_filenames, [filename])
log("INFO: Playing: #{filename}", label="log_metadata_func", level=2)
end
end
radio = on_track(log_metadata_func, radio)
def run_play_logger() =
filename = list.hd(!to_log_filenames, default="")
to_log_filenames := list.tl(!to_log_filenames)
if (filename != "") then
log("#{KLANGBECKEN_COMMAND} playlog -d #{DATA_DIR} #{filename}", label="run_play_logger")
system("#{KLANGBECKEN_COMMAND} playlog -d #{DATA_DIR} #{filename}")
end
end
# Run the logging command in the background, not to lock up the player
exec_at(pred=fun() -> list.length(!to_log_filenames) > 0, run_play_logger)
# ================================================= #
# AUDIO PROCESSING #
# ================================================= #
# Apply calculated replay gain
radio = amplify(1., override="replaygain_track_gain", radio)
# Moderate cross-fading
radio = crossfade(start_next=.5, fade_out=1., fade_in=0., radio)
# ================================================= #
# OUTPUT #
# ================================================= #
output.alsa(id="out", device=ALSA_DEVICE, radio)