From 0e61fa549d3d6c96f44be995dff7f9064c445c5c Mon Sep 17 00:00:00 2001 From: Josh Washburne Date: Wed, 25 Nov 2020 09:58:05 -0500 Subject: [PATCH] Code separation. --- restful.liq | 23 +++++++++++++++++ spradio.liq | 74 +++-------------------------------------------------- tweaks.liq | 43 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 70 deletions(-) create mode 100644 restful.liq create mode 100644 tweaks.liq diff --git a/restful.liq b/restful.liq new file mode 100644 index 0000000..06feecc --- /dev/null +++ b/restful.liq @@ -0,0 +1,23 @@ +def get_http_data(headers, url) = + if string.contains(prefix='https', url) then + let (status, _, data) = https.get(headers=headers, url) + let (_, status_code, _) = status + (status_code, data) + else + let (status, _, data) = http.get(headers=headers, url) + let (_, status_code, _) = status + (status_code, data) + end +end + +def post_http_data(headers, postdata, url) = + if string.contains(prefix='https', url) then + let (status, _, data) = https.post(headers=headers, data=postdata, url) + let (_, status_code, _) = status + (status_code, data) + else + let (status, _, data) = http.post(headers=headers, data=postdata, url) + let (_, status_code, _) = status + (status_code, data) + end +end \ No newline at end of file diff --git a/spradio.liq b/spradio.liq index 7dfd6f3..0671532 100644 --- a/spradio.liq +++ b/spradio.liq @@ -1,4 +1,6 @@ %include "secrets.liq" +%include "tweaks.liq" +%include "restful.liq" set("init.daemon", true) set("init.daemon.change_user", true) @@ -20,76 +22,6 @@ set("scheduler.non_blocking_queues", 3) set("audio.converter.samplerate.libsamplerate.quality", "best") -security = single(id="default", fallback_audio) - -# Tweaked custom crossfade to deal with jingles.. -def smart_cross(~start_next=5.,~fade_in=3.,~fade_out=3., - ~default=(fun (a,b) -> sequence([a, b])), - ~high=-15., ~medium=-32., ~margin=4., - ~width=2.,~conservative=false,s) - fade.out = fade.out(type="sin", duration=fade_out) - fade.in = fade.in(type="sin", duration=fade_in) - add = fun (a,b) -> add(normalize=false, [b, a]) - log = log(label="smart_cross") - - def transition(a,b,ma,mb,sa,sb) - list.iter(fun(x)-> log(level=4, "Before: #{x}"), ma) - list.iter(fun(x)-> log(level=4, "After : #{x}"), mb) - - if ma["type"] == "J" or mb["type"] == "J" then - log("Old or new file is a jingle: sequenced transition.") - sequence([sa, sb]) - elsif - # Do not fade if it's already very low. - b >= a + margin and a <= medium and b <= high - then - log("new >= old + margin, old <= medium and new <= high.") - log("Do not fade if it's already very low.") - log("Transition: crossed, no fade.") - add(sa, sb) - - # What to do with a loud end and a quiet beginning ? - # A good idea is to use a jingle to separate the two tracks, - # but that's another story. - else - # Otherwise, A and B are just too loud to overlap nicely, - # or the difference between them is too large and overlapping would - # completely mask one of them. - # log("No transition: using default.") - # default(sa, sb) - log("Transition: crossed, fade-in, fade-out.") - add(fade.out(sa), fade.in(sb)) - end - end - - cross(width=width, duration=start_next, - conservative=conservative, transition,s) -end - -def get_http_data(headers, url) = - if string.contains(prefix='https', url) then - let (status, _, data) = https.get(headers=headers, url) - let (_, status_code, _) = status - (status_code, data) - else - let (status, _, data) = http.get(headers=headers, url) - let (_, status_code, _) = status - (status_code, data) - end -end - -def post_http_data(headers, postdata, url) = - if string.contains(prefix='https', url) then - let (status, _, data) = https.post(headers=headers, data=postdata, url) - let (_, status_code, _) = status - (status_code, data) - else - let (status, _, data) = http.post(headers=headers, data=postdata, url) - let (_, status_code, _) = status - (status_code, data) - end -end - def next_song() = log = log(label="next_song") @@ -141,6 +73,8 @@ def just_played(m) = end end +security = single(id="default", fallback_audio) + radio = request.dynamic(id="main", default_duration=120., length=60., next_song) radio = map_metadata(update=false, change_meta, radio) radio = on_metadata(id="main", just_played, radio) diff --git a/tweaks.liq b/tweaks.liq new file mode 100644 index 0000000..8f8ddc9 --- /dev/null +++ b/tweaks.liq @@ -0,0 +1,43 @@ +# Tweaked custom crossfade to deal with jingles.. +def smart_cross(~start_next=5.,~fade_in=3.,~fade_out=3., + ~default=(fun (a,b) -> sequence([a, b])), + ~high=-15., ~medium=-32., ~margin=4., + ~width=2.,~conservative=false,s) + fade.out = fade.out(type="sin", duration=fade_out) + fade.in = fade.in(type="sin", duration=fade_in) + add = fun (a,b) -> add(normalize=false, [b, a]) + log = log(label="smart_cross") + + def transition(a,b,ma,mb,sa,sb) + list.iter(fun(x)-> log(level=4, "Before: #{x}"), ma) + list.iter(fun(x)-> log(level=4, "After : #{x}"), mb) + + if ma["type"] == "J" or mb["type"] == "J" then + log("Old or new file is a jingle: sequenced transition.") + sequence([sa, sb]) + elsif + # Do not fade if it's already very low. + b >= a + margin and a <= medium and b <= high + then + log("new >= old + margin, old <= medium and new <= high.") + log("Do not fade if it's already very low.") + log("Transition: crossed, no fade.") + add(sa, sb) + + # What to do with a loud end and a quiet beginning ? + # A good idea is to use a jingle to separate the two tracks, + # but that's another story. + else + # Otherwise, A and B are just too loud to overlap nicely, + # or the difference between them is too large and overlapping would + # completely mask one of them. + # log("No transition: using default.") + # default(sa, sb) + log("Transition: crossed, fade-in, fade-out.") + add(fade.out(sa), fade.in(sb)) + end + end + + cross(width=width, duration=start_next, + conservative=conservative, transition,s) +end \ No newline at end of file