Skip to content

Instantly share code, notes, and snippets.

@joerg-krause
Last active June 8, 2017 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joerg-krause/6661cfa54957ecb6c9332251d215440c to your computer and use it in GitHub Desktop.
Save joerg-krause/6661cfa54957ecb6c9332251d215440c to your computer and use it in GitHub Desktop.
upmpdcli songcast sender script
#!/bin/sh
# Place this file into /usr/share/upmpdcli/src_scripts
/usr/lib/luvi_modules/luvit-hbm10/scsender.lua $2
#
# Fragment only, showing the audio_output parameters
#
# Configuring audio outputs
audio_output {
type "alsa"
name "ALSA"
device "softvol"
mixer_control "Master"
enabled "yes"
}
audio_output {
type "fifo"
name "FIFO"
path "/tmp/mpdfifo"
format "44100:16:2"
enabled "no"
}
#!/usr/bin/luvit
-- Songcast Sender script for upmpdcli
-- Requires:
-- luvit [https://github.com/luvit/luvit]
-- lit-uuid [https://github.com/gsick/lit-uuid]
local fs = require("fs")
local spawn = require("childprocess").spawn
local uv = require("uv")
-- Toggle MPD audio output to FIFO
os.execute("mpc enable only FIFO >/dev/null")
-- Helper function to parse a string into hex presentation
local string = string or {}
function string.tohex(str)
return (str:gsub('.', function (c)
return string.format('%02X', string.byte(c))
end))
end
-- Get a MAC address from wlan0 or eth0
local function mac()
local mac
local addresses = uv.interface_addresses()
for name, info in pairs(addresses) do
if name == "wlan0" or name == "eth0" then
for _, v in pairs(info) do
if v.family == "inet" then
mac = v.mac
break
end
end
end
end
return mac
end
-- Generate a uuid from the MAC address
local function uuid()
local UUID = require("uuid")
local mac = mac()
local uuid
if mac then
uuid = UUID(mac:tohex())
else
uuid = UUID()
end
return uuid
end
-- Read persistent UDN
local function udn()
local udn = fs.readFileSync("/var/lib/upmpdcli/udn")
if not udn then
udn = uuid()
fs.mkdirpSync("/var/lib/upmpdcli")
fs.writeFileSync("/var/lib/upmpdcli/udn", udn)
end
return udn
end
local udn = udn()
local port = 6600
local name = args and args[2] or hostname
local args = {
"-f", "/tmp/mpdfifo",
"-A", "44100:16:2:1",
"-a", 0,
"-u", udn,
"-n", name,
"-p",
}
-- Signal handler
local sigterm = uv.new_signal()
uv.signal_start(sigterm, "sigterm", function(signal)
-- Toggle MPD audio output back to ALSA
os.execute("mpc enable only ALSA >/dev/null")
os.exit(1)
end)
-- Spawn mpd2sc
local mpd2sc = spawn("/usr/bin/mpd2sc", args)
mpd2sc.stderr:on("data", function (data)
process.stderr:write(data)
end)
mpd2sc.stdout:on("data", function (data)
local s = ("Ok %s %s"):format(port, data)
process.stdout:write(s)
end)
#
# Fragment only, showing the Songcast Receiver parameters
#
#sclogfilename = /var/log/scmpdcli.log
#scloglevel = 3
scplaymethod = alsa
sccvttype = NONE
scsendermpdport = 6600
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment