python-soundfile icon indicating copy to clipboard operation
python-soundfile copied to clipboard

read() RAW files doesn't update frame count for dynamic .raw file

Open krishnakamathk opened this issue 2 years ago • 2 comments

Hello, I've a .raw file that gets updated every 'x' ms, and my goal is to read it in a realtime manner. However, once I instantiate an sf_file instance, the frame count doesn't get updated till I re-do instantiation and do a seek(). Is there a way around this? Apologies if this is not the right forum.

Thank you.

        pos = sf_file.tell()
        num_frames = 0

        while (num_frames < self._frames_per_buffer):
            data = sf_file.read(
                frames=frames_per_buffer, always_2d=always_2d)

            # check number of frames
            num_frames = data.shape[0]

            # wait for data to be available
            if (num_frames < frames_per_buffer):
                time.sleep(frame_duration_s)
                # I wish following statement wasn't needed since we are reading from a file that's been updated
                sf_file = sf.SoundFile(
                    self._filename, channels=self._channels, subtype=self._subtype, samplerate=self._samplerate)
                sf_file.seek(pos)

krishnakamathk avatar Oct 20 '22 18:10 krishnakamathk

This behavior is entirely up to libsndfile, I'm afraid. But since it's a raw file, you can probably read the file contents trivially with e.g. Numpy, and forego soundfile entirely.

bastibe avatar Oct 21 '22 12:10 bastibe

Thank you @bastibe, will let you know if I find something.

krishnakamathk avatar Oct 24 '22 16:10 krishnakamathk