Skip to content

Instantly share code, notes, and snippets.

@navinthenapster
Created May 8, 2019 11:52
Show Gist options
  • Save navinthenapster/292855027b4990ce7b0250b55c24c276 to your computer and use it in GitHub Desktop.
Save navinthenapster/292855027b4990ce7b0250b55c24c276 to your computer and use it in GitHub Desktop.
Live streaming the speech input and saving the output file
import sounddevice as sd
duration = 5 # seconds
import wave
from scipy.io import wavfile
def writeWavFile(frames,width=2L,fileName="test.wav"):
#writing the wave file
wf = wave.open(fileName, 'wb')
wf.setnchannels(1)
wf.setsampwidth(2L)
wf.setframerate(48000)
wf.writeframes(b''.join(frames))
wf.close()
frames=[]
def callback(indata, outdata, callframes, calltime, status):
if status:
print(status)
outdata[:] = indata
frames.append(indata.tobytes())
with sd.Stream(channels=1, callback=callback):
try:
sd.sleep(int(duration * 1000))
except KeyError:
print "done"
writeWavFile(frames)
print "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment