Guest User

Untitled

a guest
Nov 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. import pyaudio
  2. import wave
  3.  
  4. form_1 = pyaudio.paInt16 # 16-bit resolution
  5. chans = 1 # 1 channel
  6. samp_rate = 44100 # 44.1kHz sampling rate
  7. chunk = 4096 # 2^12 samples for buffer
  8. record_secs = 3 # seconds to record
  9. dev_index = 2 # device index found by p.get_device_info_by_index(ii)
  10. wav_output_filename = 'test1.wav' # name of .wav file
  11.  
  12. audio = pyaudio.PyAudio() # create pyaudio instantiation
  13.  
  14. # create pyaudio stream
  15. stream = audio.open(format = form_1,rate = samp_rate,channels = chans, \
  16. input_device_index = dev_index,input = True, \
  17. frames_per_buffer=chunk)
  18. print("recording")
  19. frames = []
  20.  
  21. # loop through stream and append audio chunks to frame array
  22. for ii in range(0,int((samp_rate/chunk)*record_secs)):
  23. data = stream.read(chunk)
  24. frames.append(data)
  25.  
  26. print("finished recording")
  27.  
  28. # stop the stream, close it, and terminate the pyaudio instantiation
  29. stream.stop_stream()
  30. stream.close()
  31. audio.terminate()
  32.  
  33. # save the audio frames as .wav file
  34. wavefile = wave.open(wav_output_filename,'wb')
  35. wavefile.setnchannels(chans)
  36. wavefile.setsampwidth(audio.get_sample_size(form_1))
  37. wavefile.setframerate(samp_rate)
  38. wavefile.writeframes(b''.join(frames))
  39. wavefile.close()
Advertisement
Add Comment
Please, Sign In to add comment