# berndsdomain.org -*- Tuareg -*- vim: ft=ocaml
#
# This Liquidsoap file controls the playlists and output for polemon.org
# - written by Bernd
#
# Tree:
#
# jingles pl
# | |
# 2 \___/ 3
# |
# auto (switch)
# |
# crossfade
# |
# |
# | input.harbor ("/dj.ogg")
# | |
# | | input.harbor ("/")
# | |b |
# c \______|______/ a
# |
# full (fallback, where 1.a, 2.b, and transition)
# |
# |- mksafe
# |- clear_metadata
# |- insert_metadata (node for telnet)
# /|\
# / | \
# / | \
# / | output.icecast.mp3 (128kbps)
# / |
# / output.icecast.ogg (Q = 3)
# /
# output.icecast.ogg (Q = -1)
#
#
# This file is under public domain
#

# Run external command to check if source has permission to stream
def checkpass(user, pass)
test_process("checkpass.sh " ^ quote(user) ^ " " ^ quote(pass))
end

# Functions to write logs
def conn()
ignore(test_process("wlog connect"))
end

def disconn()
ignore(test_process("wlog disconnect"))
end

# Simple transition with 3 sec overlap.
# Source a is faded to silence, without waiting for the track to end.
def cfade(a, b)
add(normalize = false, [ fade.initial(duration = 1., b),
fade.final(duration = 1., a) ])
end

def highaacenc(_) = "aacplusenc - - 64" end
def lowaacenc(_) = "aacplusenc - - 16" end

# Log file
set( "log.file.path",
"/usr/local/var/log/liquidsoap/gnihihi.log" )

# Log to stdout, discarded when daemonized
set("log.stdout", true)

# Log all info available
set("log.level", 5)

# Log scheduler
set("scheduler.log", false)

# Telnet server on 1234
set("server.telnet", true)

# harbor input settings
set("harbor.bind_addr", "0.0.0.0")
set("harbor.port", 4010)
set("harbor.icy", true)
#set("harbor.password", "FICK NEIN")

# Jingles playlist
#jingles = mksafe(playlist( reload_mode = "seconds",
# reload = 60,
# "~liquidsoap/jingles.lst" ))

# Main playlist
pl = mksafe(playlist( reload_mode = "seconds",
reload = 60,
"~liquidsoap/playlist.lst" ))

# Live mountpoints (supports both, ICY and HTTP)
root = input.harbor( auth = checkpass,
on_connect = conn,
on_disconnect = disconn,
"/" )

ogg = input.harbor( auth = checkpass,
on_connect = conn,
on_disconnect = disconn,
"/dj.ogg" )

# Played hourly
# clock = single("file.mp3")

# merging jingles with music
#auto = random( weights = [2, 3],
# [jingles, pl] )
auto = pl

# Hourly file
# REPLACE WITH add()
#auto = add([
# auto,
# switch([(
# {0m0s},
# clock
# )])
# ])

# crossfading for playlist
auto = crossfade( start_next = 0.25,
fade_out = 0.5,
fade_in = 0.5,
auto )

# switching to DJ mountpoint with fading
full = fallback( track_sensitive = false,
transitions = [ cfade, cfade, cfade ],
[root, ogg, auto] )

# clearing metadata
full = clear_metadata(full)

# metadata node to make updates from telnet possible
full = insert_metadata( id = "meta_full",
full )

# MP3 output (128kbps)
output.icecast.mp3( host = "localhost",
port = 4000,
password = "O_o",
mount = "high.mp3",
bitrate = 128,
full )

# Ogg/Vorbis output (Q=3)
output.icecast.vorbis( host = "localhost",
port = 4000,
password = "ARGHHGHGH",
mount = "high.ogg",
quality = 3.,
full )

# Ogg/Vorbis output (Q=-1)
output.icecast.vorbis( host = "localhost",
port = 4000,
password = "DU WÜNSCHTEST?!",
mount = "low.ogg",
quality = -1.,
full )

# AAC+ output (64kbs CBR)
output.icecast.external( restart = true,
restart_on_crash = true,
process = highaacenc,
host = "localhost",
port = 4000,
password = "HAHA, ALS OB",
mount = "high.aac",
header = true,
shout_raw = true,
mime = "audio/aacp",
bitrate = 64,
full )

# AAC+ output (16kbs CBR)
output.icecast.external( restart = true,
restart_on_crash = true,
process = lowaacenc,
host = "localhost",
port = 4000,
password = "NEIN DU",
mount = "low.aac",
header = true,
shout_raw = true,
mime = "audio/aacp",
bitrate = 16,
full )

# clipinc metadata stream
clipinc = blank()
clipinc = insert_metadata( id = "meta_clipinc",
clipinc )

# mountpoiunt for secodary data
output.icecast.mp3( host = "localhost",
port = 4000,
password = "NEIN DU",
mount = "clipinc.mp3",
bitrate = 128,
clipinc )

#playlists = [pl, jingles]
playlists = [pl]

#sources = [root, ogg]

def global_skip(_) = list.iter(source.skip, playlists) "Done !" end

#def kickall(_) = list.iter(harbor.stop, sources) "Done !" end

def get_ip(_)
lines = get_process_lines("get_ip")
if list.length(lines) > 0 then
list.hd(lines)
else
"no source client connected"
end
end

#server.register( namespace = "",
# usage = "kick",
# description = "perform kick on all live sources",
# "kick",
# kickall )

server.register( namespace = "",
usage = "gskip",
description = "Perform a skip on all sources",
"gskip",
global_skip )

server.register( namespace = "",
usage = "getip",
description = "Get IP from connected harbor",
"getip",
get_ip )