select input
[scannr.git] / test.py
blob:a/test.py -> blob:b/test.py
"""PyAudio example: Record a few seconds of audio and save to a WAVE file.""" """PyAudio example: Record a few seconds of audio and save to a WAVE file."""
   
import pyaudio import pyaudio
import wave import wave
   
CHUNK = 1024 CHUNK = 1024
FORMAT = pyaudio.paInt16 FORMAT = pyaudio.paInt16
CHANNELS = 2 CHANNELS = 2
RATE = 44100 RATE = 44100
RECORD_SECONDS = 5 RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav" WAVE_OUTPUT_FILENAME = "output.wav"
   
p = pyaudio.PyAudio() p = pyaudio.PyAudio()
  device_idx = 0;
  for i in range (0, p.get_device_count()):
  print(p.get_device_info_by_index(i))
  if p.get_device_info_by_index(i)['name'] == 'Built-in Input':
  device_idx = i
  print i
stream = p.open(format=FORMAT, stream = p.open(format=FORMAT,
channels=CHANNELS, channels=CHANNELS,
rate=RATE, rate=RATE,
input=True, input=True,
  input_device_index=device_idx,
frames_per_buffer=CHUNK) frames_per_buffer=CHUNK)
   
print("* recording") print("* recording")
   
frames = [] frames = []
   
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK) data = stream.read(CHUNK)
frames.append(data) frames.append(data)
   
print("* done recording") print("* done recording")
   
stream.stop_stream() stream.stop_stream()
stream.close() stream.close()
p.terminate() p.terminate()
   
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS) wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE) wf.setframerate(RATE)
wf.writeframes(b''.join(frames)) wf.writeframes(b''.join(frames))
wf.close() wf.close()