Commit 769b823a authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Work in progress: added a daemon to encode wav files.

Also added a toy client that can talk to the daemon, for testing purposes.
parent d4eeb122
socketpath = "/home/ceder/rgit/mp3wavweb/.socket"
BASE = '/home/ceder/tmp/2014/sample'
#!/usr/bin/env python3
import sys
import multiprocessing.connection
import mp3wavcfg
try:
c = multiprocessing.connection.Client(mp3wavcfg.socketpath, 'AF_UNIX')
except FileNotFoundError:
sys.stderr.write("no daemon running?\n")
sys.exit(1)
c.send(("encode", "2009/12 - December/13"))
while True:
msg = c.recv()
if msg[0] != "progress":
break
print("%s%%" % msg[1])
print(msg)
#!/usr/bin/env python3
import re
import os
import sys
import subprocess
import multiprocessing
import multiprocessing.connection
import mp3wavcfg
def listener_fileno(self):
return self._listener._socket.fileno()
queue = []
progress = 0.0
proc = None
def work():
global proc
global queue
setattr(multiprocessing.connection.Listener, "fileno", listener_fileno)
server = multiprocessing.connection.Listener(
mp3wavcfg.socketpath, 'AF_UNIX', 3)
clients = []
while True:
pending = [server] + clients
if proc is not None:
pending.append(proc.sentinel)
ready = multiprocessing.connection.wait(pending)
for sock in ready:
if sock is server:
clients.append(sock.accept())
elif proc is not None and sock is proc.sentinel:
proc.join()
if proc.exitcode == 0:
queue[0][1].send(("done", queue[0][0]))
else:
queue[0][1].send(("fail", queue[0][0]))
proc = None
queue = queue[1:]
start_encoding()
else:
try:
msg = sock.recv()
except EOFError:
clients.remove(sock)
else:
handle_message(sock, msg)
def exists(fn):
return os.path.isfile(os.path.join(mp3wavcfg.BASE, fn))
def handle_message(client, msg):
global queue
global progress
if msg[0] == "ping":
client.send(("pong", ))
elif msg[0] == "peek":
client.send(("status", [q[0] for q in queue], progress))
elif msg[0] == "encode":
fn = msg[1]
if exists(fn + ".wav"):
if exists(fn + ".mp3"):
client.send(("already-there", fn))
else:
queue.append((fn, client))
start_encoding()
else:
client.send(("enofile", fn))
elif msg[0] == "progress":
progress = msg[1]
queue[0][1].send(("progress", progress))
else:
client.send(("badreq", msg[0]))
def start_encoding():
global proc
global queue
if proc is not None:
return
if len(queue) == 0:
return
proc = multiprocessing.Process(target=encode, args=(queue[0][0], ))
proc.start()
def encode(fn):
c = multiprocessing.connection.Client(mp3wavcfg.socketpath, 'AF_UNIX')
cmd = ["lame", "--preset", "standard", "--nohist",
os.path.join(mp3wavcfg.BASE, fn + ".wav")]
lame = subprocess.Popen(args=cmd, bufsize=0, stdin=subprocess.DEVNULL,
stderr=subprocess.PIPE)
percent_match = re.compile(".*\\(([0-9]+)%\\)")
while True:
msg = lame.stderr.read(100)
if len(msg) == 0:
break
m = percent_match.search(str(msg))
if m is not None:
c.send(("progress", m.group(1)))
x = lame.wait()
sys.exit(x)
if __name__ == '__main__':
work()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment