Skip to content

Commit

Permalink
Refactored code (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
spietras authored Jul 20, 2024
1 parent 38f463a commit 1f54fc6
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 68 deletions.
4 changes: 4 additions & 0 deletions src/config/index.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let config = {}

%include "streamcast/index.liq"
%include "server.liq"
6 changes: 6 additions & 0 deletions src/config/server.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let config.server = {}

let config.server.host =
utils.env.get(default="0.0.0.0", "FUSION__SERVER__HOST")
let config.server.port =
int_of_string(utils.env.get(default="9000", "FUSION__SERVER__PORT"))
12 changes: 12 additions & 0 deletions src/config/streamcast/icy.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let config.streamcast.icy = {}

let config.streamcast.icy.host =
utils.env.get(default="localhost", "FUSION__STREAMCAST__ICY__HOST")
let config.streamcast.icy.port =
int_of_string(utils.env.get(default="8000", "FUSION__STREAMCAST__ICY__PORT"))
let config.streamcast.icy.user =
utils.env.get(default="source", "FUSION__STREAMCAST__ICY__USER")
let config.streamcast.icy.password =
utils.env.get(default="password", "FUSION__STREAMCAST__ICY__PASSWORD")
let config.streamcast.icy.mount =
utils.env.get(default="radio.mp3", "FUSION__STREAMCAST__ICY__MOUNT")
3 changes: 3 additions & 0 deletions src/config/streamcast/index.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let config.streamcast = {}

%include "icy.liq"
4 changes: 4 additions & 0 deletions src/constants/index.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let constants = {}

%include "metadata.liq"
%include "paths.liq"
3 changes: 3 additions & 0 deletions src/constants/metadata.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let constants.metadata = {}

let constants.metadata.keys = ["title", "artist"]
5 changes: 5 additions & 0 deletions src/constants/paths.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let constants.paths = {}

let constants.paths.source = "src/"
let constants.paths.resources = "#{constants.paths.source}resources/"
let constants.paths.fallback = "#{constants.paths.resources}audio/fallback.mp3"
13 changes: 13 additions & 0 deletions src/inputs.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let inputs = {}

let inputs.music = single(constants.paths.fallback)

let inputs.live =
buffer(
input.srt(
bind_address=config.server.host,
port=config.server.port,
read_timeout=null(),
write_timeout=null()
)
)
74 changes: 6 additions & 68 deletions src/main.liq
Original file line number Diff line number Diff line change
@@ -1,68 +1,6 @@
### Constants

SOURCE_PATH = "src/"
RESOURCES_PATH = "#{SOURCE_PATH}/resources/"
FALLBACK_PATH = "#{RESOURCES_PATH}/audio/fallback.mp3"

ALLOWED_METADATA_KEYS = ["title", "artist"]

### Utils

def get_env(~default="", key) =
list.assoc(default=default, key, environment())
end

def filter_metadata(keys, source) =
predicate = fun (md) -> list.mem(fst(md), keys)

metadata.map(
update=false, strip=true, fun (md) -> list.filter(predicate, md), source
)
end

### Variables

server_host = get_env(default="0.0.0.0", "FUSION__SERVER__HOST")
server_port = int_of_string(get_env(default="9000", "FUSION__SERVER__PORT"))
streamcast_icy_host =
get_env(default="localhost", "FUSION__STREAMCAST__ICY__HOST")
streamcast_icy_port =
int_of_string(get_env(default="8000", "FUSION__STREAMCAST__ICY__PORT"))
streamcast_icy_user = get_env(default="source", "FUSION__STREAMCAST__ICY__USER")
streamcast_icy_password =
get_env(default="password", "FUSION__STREAMCAST__ICY__PASSWORD")
streamcast_icy_mount =
get_env(default="radio.mp3", "FUSION__STREAMCAST__ICY__MOUNT")

### Inputs

music = single(FALLBACK_PATH)

live =
input.srt(
bind_address=server_host,
port=server_port,
read_timeout=null(),
write_timeout=null()
)
live = buffer(live)

### Stream

radio = fallback(track_sensitive=false, [live, music])
radio = filter_metadata(ALLOWED_METADATA_KEYS, radio)
radio = mksafe(radio)

### Outputs

output.icecast(
%mp3,
host=streamcast_icy_host,
port=streamcast_icy_port,
user=streamcast_icy_user,
password=streamcast_icy_password,
mount=streamcast_icy_mount,
icy_metadata=ALLOWED_METADATA_KEYS,
icy_song=fun (_) -> null(),
radio
)
%include "constants/index.liq"
%include "utils/index.liq"
%include "config/index.liq"
%include "inputs.liq"
%include "streams.liq"
%include "outputs.liq"
11 changes: 11 additions & 0 deletions src/outputs.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output.icecast(
%mp3,
host=config.streamcast.icy.host,
port=config.streamcast.icy.port,
user=config.streamcast.icy.user,
password=config.streamcast.icy.password,
mount=config.streamcast.icy.mount,
icy_metadata=constants.metadata.keys,
icy_song=fun (_) -> null(),
streams.radio
)
9 changes: 9 additions & 0 deletions src/streams.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let streams = {}

let streams.radio =
mksafe(
utils.metadata.filter(
constants.metadata.keys,
fallback(track_sensitive=false, [inputs.live, inputs.music])
)
)
5 changes: 5 additions & 0 deletions src/utils/env.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let utils.env = {}

def utils.env.get(~default="", key) =
list.assoc(default=default, key, environment())
end
4 changes: 4 additions & 0 deletions src/utils/index.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let utils = {}

%include "env.liq"
%include "metadata.liq"
9 changes: 9 additions & 0 deletions src/utils/metadata.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let utils.metadata = {}

def utils.metadata.filter(keys, source) =
predicate = fun (md) -> list.mem(fst(md), keys)

metadata.map(
update=false, strip=true, fun (md) -> list.filter(predicate, md), source
)
end

0 comments on commit 1f54fc6

Please sign in to comment.