New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
torachaudio.load does not load webm bytesio or _io.BufferedReader, but loads the file from hard drive #2792
Comments
This issue seems to depend on the data you are handling. Is it possible to share some sample data, which do not include any PII nor copyright issue? A complete silence is fine. |
The audio you shared has wav extension but, in fact, it is WebM format. with open("audio.wav", "rb") as f:
print(f.read(50)[30:]) prints the following
and
To handle WebM, you can use # loading from path and read the entire audio in one-go
s = torchaudio.io.StreamReader(path)
s.add_basic_audio_stream(-1)
s.process_all_packets()
waveform, = s.pop_chunks() # load from file-like object and read audio chunk-by-chunk
s = torchaudio.io.StreamReader(f)
s.add_basic_audio_stream(chunk_size)
for chunk, in s.stream():
# process waveform For the detailed usage, please checkout tutorials like |
@mthrok
|
I think you could do chunk-by-chunk decoding, which is more efficient, but not sure if this is what you want, as I do not know what application you are building. To do chunk-by-chunk decoding, you can wrap the class Wrapper:
def __init__(self, socket):
self.socket = socket
self.buffer = b''
def read(self, n):
while len(self.buffer) < n:
new_data = await self.socket.receive_bytes()
if not new_data:
break
self.buffer += new_data
data, self.buffer = self.buffer[:n], self.buffer[n:]
return data Then passing it to try:
wrapper = Wrapper(websocket)
s = torchaudio.io.StreamReader(wrapper)
for chunk in s.stream():
print(chunk.shape)
except ... |
Note you can read in one-go with file-like object input. The s = torchaudio.io.StreamReader(fileobj)
s.add_basic_audio_stream(-1)
s.process_all_packets()
waveform, = s.pop_chunks() |
I am going to close the issue, as this is not a bug with torchaudio. |
馃悰 Describe the bug
I have a
websocket
that receives chunks of data in a byte format. The browser encodes the data inaudio/webm
format. The code is like the following:Manually writing the data to
audio.wav
and then reading the file using the following code works fine with no errors:However, reading the file as a file object does not work:
It raises the following error:
PS: Creating
BytesIO
from the data and passing it to thetorchaudio.load
results in error the same as the above.Versions
Versions of relevant libraries:
[pip3] numpy==1.23.4
[pip3] torch==1.12.1
[pip3] torchaudio==0.12.1
[conda] numpy 1.23.4 pypi_0 pypi
OS
Ubuntu: 22.04
torchaudio.backend: "sox_io"
PS
I tested the same process on a webm file which was converted from a wav file, and the result was the same:
torchaudio.load
can read the file from hard drive.torchaudio.load
cannot readbytesio
or_io.BufferedReader
The text was updated successfully, but these errors were encountered: