bastibe / python-soundfile

SoundFile is an audio library based on libsndfile, CFFI, and NumPy
BSD 3-Clause "New" or "Revised" License
701 stars 108 forks source link

Cannot write to a TemporaryFile or BytesIO #393

Closed jfsantos closed 1 year ago

jfsantos commented 1 year ago

I checked the code and noticed it is supposed to support writing to virtual IO, however the following examples fail:

import tempfile
import io
import soundfile
import numpy as np

x = np.random.randn(1, 44100)

# LibsndfileError: Error opening <_io.BufferedRandom name=18>: Format not recognised.
f = tempfile.TemporaryFile()
soundfile.write(f, x, 44100, format='WAV', subtype='PCM_16')

# LibsndfileError: Error opening <_io.BytesIO object at 0x1051d9a30>: Format not recognised.
f = io.BytesIO()
soundfile.write(f, x, 44100, format='WAV', subtype='PCM_16')

Am I doing anything wrong? This is with version 0.12.1 and Python 3.10.9. I get the same issue both on Linux and macOS.

bastibe commented 1 year ago

the data should be frames x channels, not channels x frames. So you're trying to write a file with 44100 channels of one sample each. libsndfile does not support WAV files with this many channels, which is why it throws the error.