pylon / streamp3

Streaming MP3 decoder for Python
Apache License 2.0
28 stars 2 forks source link

Some minor changes + adaptation to provide a copy of the input #3

Closed goto40 closed 5 years ago

goto40 commented 5 years ago
brentspell commented 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.

goto40 commented 5 years ago

Thanks. I agree with you.