added buffered reads from disk io and transcoding
This commit is contained in:
+3
-6
@@ -1,6 +1,5 @@
|
||||
import queue
|
||||
import logging
|
||||
import io
|
||||
import os
|
||||
import threading
|
||||
from functools import cached_property
|
||||
@@ -28,8 +27,7 @@ class AudioStreamer(threading.Thread):
|
||||
|
||||
@cached_property
|
||||
def silence(self):
|
||||
with (Path(__file__).parent / 'silence.mp3').open('rb') as stream:
|
||||
return io.BytesIO(stream.read())
|
||||
return transcoder.open(Path(__file__).parent / 'silence.mp3', bufsize=2*self.chunk_size)
|
||||
|
||||
@cached_property
|
||||
def _shout(self):
|
||||
@@ -92,13 +90,12 @@ class AudioStreamer(threading.Thread):
|
||||
logger.debug("Load event cleared.")
|
||||
|
||||
def _read_chunk(self, filehandle):
|
||||
chunk = filehandle.read(self.chunk_size)
|
||||
return chunk
|
||||
return filehandle.read(self.chunk_size)
|
||||
|
||||
def play_file(self, track: Path):
|
||||
logger.debug(f"Streaming {track.stem = }")
|
||||
self._shout.set_metadata({"song": track.stem})
|
||||
with transcoder.open(track) as fh:
|
||||
with transcoder.open(track, bufsize=2*self.chunk_size) as fh:
|
||||
return self.play_from_stream(fh)
|
||||
|
||||
def play_from_stream(self, stream):
|
||||
|
||||
@@ -7,7 +7,7 @@ import ffmpeg
|
||||
logger = logging.getLogger('transcoder')
|
||||
|
||||
|
||||
def open(infile: Path):
|
||||
def open(infile: Path, bufsize: int = 4096):
|
||||
"""
|
||||
Return a stream of mp3 data for the given path on disk.
|
||||
|
||||
@@ -18,7 +18,7 @@ def open(infile: Path):
|
||||
suffix = infile.suffix.lower()
|
||||
if suffix == '.mp3':
|
||||
logger.debug(f"Not transcoding mp3 {infile = }")
|
||||
return infile.open('rb')
|
||||
return infile.open('rb', buffering=bufsize)
|
||||
|
||||
ffmpeg_args = (
|
||||
ffmpeg
|
||||
@@ -29,7 +29,7 @@ def open(infile: Path):
|
||||
)
|
||||
|
||||
# Force close STDIN to prevent ffmpeg from trying to read from it. silly ffmpeg.
|
||||
proc = subprocess.Popen(ffmpeg_args, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||
proc = subprocess.Popen(ffmpeg_args, bufsize=bufsize, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||
proc.stdin.close()
|
||||
logger.debug(f"Spawned ffmpeg (PID {proc.pid}) with args {ffmpeg_args = }")
|
||||
return proc.stdout
|
||||
|
||||
Reference in New Issue
Block a user