Skip to content

Commit

Permalink
Merge pull request #412 from jneuendorf-i4h/virtual_io_doc
Browse files Browse the repository at this point in the history
updates README: in-memory virtual IO example
  • Loading branch information
bastibe authored Oct 14, 2023
2 parents 0a8071d + 2a8d892 commit 3040e6e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,27 @@ For Python 2.x support, replace the third line with:
from urllib2 import urlopen
In-memory files
^^^^^^^^^^^^^^^

Chunks of audio, i.e. `bytes`, can also be read and written without touching the filesystem.
In the following example OGG is converted to WAV entirely in memory (without writing files to the disk):

.. code:: python
import io
import soundfile as sf
def ogg2wav(ogg: bytes):
ogg_buf = io.BytesIO(ogg)
ogg_buf.name = 'file.ogg'
data, samplerate = sf.read(ogg_buf)
wav_buf = io.BytesIO()
wav_buf.name = 'file.wav'
sf.write(wav_buf, data, samplerate)
wav_buf.seek(0) # Necessary for `.read()` to return all bytes
return wav_buf.read()
Known Issues
------------

Expand Down

0 comments on commit 3040e6e

Please sign in to comment.