forked from savonet/ai-radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.liq
280 lines (234 loc) · 5.14 KB
/
main.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
%include "config.liq"
base_dir = path.dirname(argv(0))
audio_dir = path.concat(base_dir, "audio")
background_dir = path.concat(base_dir, "background")
hls_dir = path.concat(base_dir, "hls")
font =
path.concat(
base_dir,
"MesloLGS NF Bold.ttf"
)
let {px, vw, vh, rem, width, height} = video.canvas.virtual_10k.actual_720p
video.frame.width := width
video.frame.height := height
## AI functions
def mk_prompt(old, new) =
old =
list.map(
fun (m) ->
begin
title = m["title"]
artist = m["artist"]
"#{title} by #{artist}"
end,
old
)
old =
string.concat(
separator=
", ",
old
)
new_title = new["title"]
new_artist = new["artist"]
new =
"#{new_title} by #{new_artist}"
"You are a radio DJ, you speak in the style of the old 50s early rock'n'roll \
DJs. The following songs were just played: #{old}. Next playing is #{new}. \
Can you describe the musical characteristics of each song that ended and how \
they connect with each other? Then introduce the next song. Make sure to \
include style, year, instruments and cultural context and anecdotes and fun \
facts. Limit your response to 200 words and make sure that it sounds \
entertaining and fun."
end
def generate_speech(prompt) =
let {choices = [{message = {content}}]} =
openai.chat(
key=openai_api_key,
[
{
role="system",
content=
"You are a helpful assistant."
},
{role="user", content=prompt}
]
)
tmp_file = file.temp("dj", ".mp3")
on_data = file.write.stream(tmp_file)
openai.speech(key=openai_api_key, voice="onyx", on_data=on_data, content)
request.create(
"annotate:title=\"AI DJ\":tmp:#{tmp_file}"
)
end
## Audio setup
next_song = ref([])
def check_next(r) =
ignore(request.resolve(r))
request.read_metadata(r)
next_song := request.metadata(r)
true
end
radio = playlist(audio_dir, prefetch=2, check_next=check_next)
append_queue = request.queue()
past_songs = ref([])
def process_dj_metadata(m) =
past_songs := [...past_songs(), m]
if
list.length(past_songs()) == 4
then
songs_history = past_songs()
past_songs := []
prompt = mk_prompt(songs_history, next_song())
thread.run({append_queue.push(generate_speech(prompt))})
end
end
radio = source.on_metadata(radio, process_dj_metadata)
radio = fallback(track_sensitive=true, [append_queue, radio])
current_title = ref("")
current_artist = ref("")
def update_current_song(m) =
current_title := m["title"]
current_artist := m["artist"]
end
radio.on_metadata(update_current_song)
def position() =
source.elapsed(radio) / source.duration(radio)
end
def remaining() =
time = source.remaining(radio)
seconds = string.of_int(digits=2, int(time mod 60.))
minutes = string.of_int(digits=2, int(time / 60.))
"#{minutes}:#{seconds}"
end
## Video setup
background = playlist(background_dir)
background =
video.add_rectangle(
color=0x333333,
alpha=0.4,
x=4609 @ px,
y=234 @ px,
width=5468 @ px,
height=429 @ px,
background
)
def add_text_with_backdrop(~x, ~y, ~size, ~color, text) =
background =
video.add_text(
color=0x000000,
font=font,
x=x @ px,
y=(y + 15) @ px,
size=size @ rem,
text,
background
)
video.add_text(
color=color,
font=font,
x=x @ px,
y=y @ px,
size=size @ rem,
text,
background
)
end
background =
add_text_with_backdrop(
x=4687,
y=250,
size=2.15,
color=0xFCB900,
"Teardown The List Jockey"
)
background =
video.add_rectangle(
color=0x333333,
alpha=0.4,
x=0 @ px,
y=4375 @ px,
width=1. @ vw,
height=960 @ px,
background
)
background =
video.add_rectangle(
color=0xfcb900,
alpha=0.7,
x=0 @ px,
y=5015 @ px,
width=1. @ vw,
height=15 @ px,
background
)
background =
video.add_rectangle(
color=0xfcb900,
x=210 @ px,
y=2554 @ px,
height=1609 @ px,
width=1609 @ px,
background
)
background =
video.add_text(
color=0xFCB900,
font=font,
speed=0,
x=234 @ px,
y=4437 @ px,
size=1.5 @ rem,
current_title,
background
)
background =
video.add_text(
color=0xFCB900,
font=font,
speed=0,
x=234 @ px,
y=4710 @ px,
size=1.5 @ rem,
current_artist,
background
)
background =
video.add_rectangle(
color=0xfcb900,
x=0 @ px,
y=5285 @ px,
height=50 @ px,
width={position() @ vw},
background
)
background =
video.add_text(
size=rem(1.),
x=234 @ px,
y=5039 @ px,
color=0xcccccc,
font=font,
{
"Next in #{remaining()}"
},
background
)
radio = source.mux.video(video=background, radio)
radio =
video.add_cover(
x=234 @ px,
y=2578 @ px,
width=1562 @ px,
height=1562 @ px,
default="./default-cover.jpg",
radio
)
enc =
%ffmpeg(
format = "mpegts",
%audio(codec = "aac", b = "192k"),
%video(codec = "libx264", preset = "ultrafast", g = 50)
)
streams = [("radio", enc)]
output.file.hls(segment_duration=2., hls_dir, streams, mksafe(radio))