Skip to content

Instantly share code, notes, and snippets.

@gose
Last active May 11, 2021 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gose/43e45cc6c8e56903cd0f3821298f0a06 to your computer and use it in GitHub Desktop.
Save gose/43e45cc6c8e56903cd0f3821298f0a06 to your computer and use it in GitHub Desktop.
from datetime import datetime
import pyaudio
import numpy
import wave
CHUNK = 4000
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 8000
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
log = open("/var/log/sounds.log", "a")
# Use a Blackman window
window = numpy.blackman(CHUNK)
while True:
data = stream.read(CHUNK, exception_on_overflow=False)
waveData = wave.struct.unpack("%dh"%(CHUNK), data)
npArrayData = numpy.array(waveData)
indata = npArrayData * window
for i in indata:
log.write(datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f000Z') + ";" + str(i) + "\n")
log.close()
stream.stop_stream()
stream.close()
audio.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment