iluvcapra / wavinfo

Probe WAVE Files for all metadata
https://wavinfo.readthedocs.io/
MIT License
36 stars 7 forks source link

Wavinfo doesn't work with filehandles #12

Closed RonaldAJ closed 2 years ago

RonaldAJ commented 2 years ago

I have a soundfile in memory which I received over a socket as a bytestring: data. Calling io.BytesIO(data) I can convert this to a filehandle like streamobject.

I would like to pass this to wavinfo like this:

soundbytes = io.BytesIO(data)
wav_info = WavInfoReader(soundbytes)

and then process the output:

info_dict = {"channels": wav_info.fmt.channel_count,
             "bits_per_sample": wav_info.fmt.bits_per_sample,
             "size_in_frames": wav_info.data.frame_count,
             "size_in_bytes": wav_info.data.byte_count,
            }

The changes needed to make this possible are modest, see pull request: #11 and keep the filename based functionality untouched.

While working on this it turned out that the repr function was not functional due to mixing of old and new style string formatting. In the pull request it is changed to new style string formatting.

RonaldAJ commented 2 years ago

The pull request #11 also enables the following use of wavinfo using a filehandle:

from wavinfo import WavInfoReader

wavfile = open('mywavfile.wav','rb')
wir = WavInfoReader(wavfile)

print(f'format: {wir.fmt}, frame_count: {wir.data.frame_count}')
RonaldAJ commented 2 years ago

Pull request is accepted. Problem solved.