Created
October 2, 2019 15:36
-
-
Save epietrowicz/efa39569799bf19da1aaccedbb43353b to your computer and use it in GitHub Desktop.
audio_capture
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 pyaudio | |
import wave | |
import os | |
FORMAT = pyaudio.paInt16 | |
CHANNELS = 2 | |
RATE = 44100 | |
CHUNK = 1024 | |
RECORD_SECONDS = 2 | |
cat_str = os.getcwd() + '/audio/noise_' | |
sample_number = 0 | |
def capture_audio(test): | |
audio = pyaudio.PyAudio() | |
# start Recording | |
stream = audio.open(format=FORMAT, channels=CHANNELS, | |
rate=RATE, input=True, | |
frames_per_buffer=CHUNK) | |
print ("recording...") | |
frames = [] | |
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): | |
data = stream.read(CHUNK) | |
frames.append(data) | |
print ("finished recording") | |
# stop Recording | |
stream.stop_stream() | |
stream.close() | |
audio.terminate() | |
waveFile = wave.open(test, 'wb') | |
waveFile.setnchannels(CHANNELS) | |
waveFile.setsampwidth(audio.get_sample_size(FORMAT)) | |
waveFile.setframerate(RATE) | |
waveFile.writeframes(b''.join(frames)) | |
waveFile.close() | |
while sample_number < 26: | |
WAVE_OUTPUT_FILENAME = cat_str + str(sample_number) + ".wav" | |
print(WAVE_OUTPUT_FILENAME) | |
capture_audio(WAVE_OUTPUT_FILENAME) | |
sample_number += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment