Closed goto40 closed 5 years ago
Thanks so much for your interest in the project. I'm glad you have found it useful.
I can understand the need for accessing the underlying byte stream while you are decoding it. However, I think that teeing the input stream within the MP3 decoder is not the right place to do so. This unnecessarily complicates the implementation of the decoder and varies the return value of the iterator based on a configuration parameter, which could be counterintuitive and confusing for consumers.
I think a simpler solution would be to implement this orthogonal feature in a tee class that wraps the raw stream. This tee would return its input from the read
function, while simultaneously directing the raw bytes elsewhere (a callback function, another stream, etc.). Then, the tee could be passed as the stream parameter to the MP3Decoder
class, which would read from it to decode the MP3 frames. An example implementation using an input/output stream would be the following:
from streamp3 import MP3Decoder
class StreamTee:
def __init__(self, input, output):
self._input = input
self._output = output
def read(self, size):
data = self._input.read(size)
self._output.write(data)
return data
with open('tests/streamp3/data/stereo.mp3', 'rb') as source:
with open('/tmp/test.mp3', 'wb') as target:
decoder = MP3Decoder(StreamTee(source, target))
for chunk in decoder:
assert isinstance(chunk, bytes)
assert len(chunk) % 4 == 0
Thanks again for your interest and feedback.
Thanks. I agree with you.
I used your lib to stream an mp3 stream. In order to provide a copy of the stream (to record the stream to a file) I found it useful to add the proposed change.
Take a look at it. For sure there are other possibilities to implement the desired feature. I will be happy to discuss the proposed changes.
I also added my IDE config files to .gitignore.
Then, I filled some data in setup.cfg in order to run flake8 without conflicting with my venv (virtual env) folder.