Created
May 8, 2019 11:52
-
-
Save navinthenapster/292855027b4990ce7b0250b55c24c276 to your computer and use it in GitHub Desktop.
Live streaming the speech input and saving the output file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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