|
import streamlit as st |
|
import pyaudio |
|
import wave |
|
|
|
|
|
FORMAT = pyaudio.paInt16 |
|
CHANNELS = 2 |
|
RATE = 44100 |
|
CHUNK = 1024 |
|
|
|
|
|
def record_audio(duration, output_file="output.wav"): |
|
audio = pyaudio.PyAudio() |
|
stream = audio.open(format=FORMAT, channels=CHANNELS, |
|
rate=RATE, input=True, |
|
frames_per_buffer=CHUNK) |
|
st.write("🎙️ Запись...") |
|
frames = [] |
|
|
|
for _ in range(0, int(RATE / CHUNK * duration)): |
|
data = stream.read(CHUNK) |
|
frames.append(data) |
|
|
|
st.write("✅ Запись завершена!") |
|
|
|
|
|
stream.stop_stream() |
|
stream.close() |
|
audio.terminate() |
|
|
|
|
|
with wave.open(output_file, 'wb') as wf: |
|
wf.setnchannels(CHANNELS) |
|
wf.setsampwidth(audio.get_sample_size(FORMAT)) |
|
wf.setframerate(RATE) |
|
wf.writeframes(b''.join(frames)) |
|
|
|
return output_file |
|
|
|
|
|
st.title("🎙️ Простой диктофон на PyAudio") |
|
duration = st.slider("Длительность записи (секунды)", 1, 60, 5) |
|
|
|
if st.button("Начать запись"): |
|
output_file = record_audio(duration) |
|
st.audio(output_file, format="audio/wav") |
|
st.success(f"Файл сохранён: {output_file}") |