Staudio on a wav stream in memory (instead of a file on disk)

Hi folks,

I fell in love with streamlit for buiding interactive educational webapps for understanding signal processing (see my project)

I absolutely need to be able to play sounds from generated content, and saving+reading it from a file is too slow. I know several people have mentioned this problem before and there is an official request about this. I just want to bring an almost solution and see if someone else can contribute.
This ALMOST works :

import streamlit as st
from numpy import *
import scipy.io.wavfile as wav
import io
fe=10000;
t=arange(0.0,1,1/fe)
signal=asin(2pift)
bytes_wav = byte()
byte_io = io.BytesIO(bytes_wav)
wav.write(byte_io, fe, around(signal) )
st.audio(byte_io)

We DO hear sthg, which changes if you change the freq of the sine
It lasts 1 second as expected
But the sound, although sine-rleated, is corrupted, as if the format of the data was not correctly interpreted.
Strangely enough, if I remove the β€œaround”, then the silder still lasts one second, but it is silent.

Summarizing I am this close to the solution but stucked.

Thierry

import streamlit as st
import numpy as np
import soundfile
import io

# frequency
f = 5000

# sample_rate
sr = 16000

# time (1 sec)
t = np.arange(0.0, 1, 1/sr)

signal = np.sin(2 * np.pi * f * t)

byte_io = io.BytesIO()

sub = 'PCM_24'  # could be 'PCM_32' or 'FLOAT'
soundfile.write(byte_io, signal, sr, subtype=sub, format='WAV')

st.audio(byte_io)

THANKS A LOT!
It works with β€˜FLOAT’
I think this will help other poeple too (I had first checked on the forum and found non answer to this issue).

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.