Imago
Imago

Reputation: 501

Exception in soundfile.py: io.UnsupportedOperation: seek

I am currently going through this jupiter notebook

However when running this code block, I get the error below. Is there someone familiar with this problem and has an idea how to fix/resolve this? I would appreciate any form and advice or help. Thank you in advance.

print("Source:", SAMPLE_WAV_URL)
with requests.get(SAMPLE_WAV_URL, stream=True) as response:
    metadata = torchaudio.info(response.raw)
print(metadata)

Source: https://pytorch-tutorial-assets.s3.amazonaws.com/steam-train-whistle-daniel_simon.wav
Exception ignored from cffi callback <function SoundFile._init_virtual_io.<locals>.vio_get_filelen at 0x000002487DE62C10>:
Traceback (most recent call last):
  File "C:\Users\me\anaconda3\envs\myenv\lib\site-packages\soundfile.py", line 1228, in vio_get_filelen
    file.seek(0, SEEK_END)
io.UnsupportedOperation: seek
---------------------------------------------------------------------------
LibsndfileError                           Traceback (most recent call last)
Input In [5], in <cell line: 2>()
      1 print("Source:", SAMPLE_WAV_URL)
      2 with requests.get(SAMPLE_WAV_URL, stream=True) as response:
----> 3     metadata = torchaudio.info(response.raw)
      4 print(metadata)

File ~\anaconda3\envs\myenv\lib\site-packages\torchaudio\backend\soundfile_backend.py:103, in info(filepath, format)
     84 @_mod_utils.requires_soundfile()
     85 def info(filepath: str, format: Optional[str] = None) -> AudioMetaData:
     86     """Get signal information of an audio file.
     87 
     88     Note:
   (...)
    101 
    102     """
--> 103     sinfo = soundfile.info(filepath)
    104     return AudioMetaData(
    105         sinfo.samplerate,
    106         sinfo.frames,
   (...)
    109         encoding=_get_encoding(sinfo.format, sinfo.subtype),
    110     )

File ~\anaconda3\envs\myenv\lib\site-packages\soundfile.py:464, in info(file, verbose)
    456 def info(file, verbose=False):
    457     """Returns an object with information about a `SoundFile`.
    458 
    459     Parameters
   (...)
    462         Whether to print additional information.
    463     """
--> 464     return _SoundFileInfo(file, verbose)

File ~\anaconda3\envs\myenv\lib\site-packages\soundfile.py:409, in _SoundFileInfo.__init__(self, file, verbose)
    407 def __init__(self, file, verbose):
    408     self.verbose = verbose
--> 409     with SoundFile(file) as f:
    410         self.name = f.name
    411         self.samplerate = f.samplerate

File ~\anaconda3\envs\myenv\lib\site-packages\soundfile.py:655, in SoundFile.__init__(self, file, mode, samplerate, channels, subtype, endian, format, closefd)
    652 self._mode = mode
    653 self._info = _create_info_struct(file, mode, samplerate, channels,
    654                                  format, subtype, endian)
--> 655 self._file = self._open(file, mode_int, closefd)
    656 if set(mode).issuperset('r+') and self.seekable():
    657     # Move write position to 0 (like in Python file objects)
    658     self.seek(0)

File ~\anaconda3\envs\myenv\lib\site-packages\soundfile.py:1213, in SoundFile._open(self, file, mode_int, closefd)
   1210 if file_ptr == _ffi.NULL:
   1211     # get the actual error code
   1212     err = _snd.sf_error(file_ptr)
-> 1213     raise LibsndfileError(err, prefix="Error opening {0!r}: ".format(self.name))
   1214 if mode_int == _snd.SFM_WRITE:
   1215     # Due to a bug in libsndfile version <= 1.0.25, frames != 0
   1216     # when opening a named pipe in SFM_WRITE mode.
   1217     # See http://github.com/erikd/libsndfile/issues/77.
   1218     self._info.frames = 0

LibsndfileError: Error opening <urllib3.response.HTTPResponse object at 0x000002487DDD02B0>: Error in WAV file. No 'data' chunk marker.

Upvotes: 0

Views: 1559

Answers (1)

moto
moto

Reputation: 441

The tutorial is written for Linux/macOS use cases, which use custom audio backend that is not available on Windows.

On Windows, SonudFile is used but it seems that SoundFile cannot handle file-like object based on requests.

This unfortunate situation will be worked around in the future version, but at the moment (v0.13) you need a workaround on Windows like the following, which loads the entire contents in one go.

from io import BytesIO

src = BytesIO(response.content)
torchaudio.info(src)

If you need to handle large audio file in streaming fashion, torchaudio.io.StreamReader can do that. Please refer to the tutorial. https://pytorch.org/audio/stable/tutorials/streamreader_basic_tutorial.html#sphx-glr-tutorials-streamreader-basic-tutorial-py

Upvotes: 1

Related Questions