write_audio.py 728 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/env python3
  2. # NOTE: this example requires PyAudio because it uses the Microphone class
  3. import speech_recognition as sr
  4. # obtain audio from the microphone
  5. r = sr.Recognizer()
  6. with sr.Microphone() as source:
  7. print("Say something!")
  8. audio = r.listen(source)
  9. # write audio to a RAW file
  10. with open("microphone-results.raw", "wb") as f:
  11. f.write(audio.get_raw_data())
  12. # write audio to a WAV file
  13. with open("microphone-results.wav", "wb") as f:
  14. f.write(audio.get_wav_data())
  15. # write audio to an AIFF file
  16. with open("microphone-results.aiff", "wb") as f:
  17. f.write(audio.get_aiff_data())
  18. # write audio to a FLAC file
  19. with open("microphone-results.flac", "wb") as f:
  20. f.write(audio.get_flac_data())