|
import pyaudio |
|
import wave |
|
|
|
|
|
FORMAT = pyaudio.paInt16 |
|
CHANNELS = 1 |
|
RATE = 44100 |
|
CHUNK = 1024 |
|
RECORD_SECONDS = 5 |
|
WAVE_OUTPUT_FILENAME = "sample1.wav" |
|
|
|
audio = pyaudio.PyAudio() |
|
|
|
|
|
stream = audio.open(format=FORMAT, channels=CHANNELS, |
|
rate=RATE, input=True, |
|
frames_per_buffer=CHUNK) |
|
print("Nahrávám...") |
|
|
|
frames = [] |
|
|
|
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): |
|
data = stream.read(CHUNK) |
|
frames.append(data) |
|
|
|
print("Nahrávání dokončeno") |
|
|
|
|
|
with wave.open(WAVE_OUTPUT_FILENAME, 'wb') as wf: |
|
wf.setnchannels(CHANNELS) |
|
wf.setsampwidth(audio.get_sample_size(FORMAT)) |
|
wf.setframerate(RATE) |
|
wf.writeframes(b''.join(frames)) |
|
|
|
stream.stop_stream() |
|
stream.close() |
|
audio.terminate() |
|
|