-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture_audio.py
91 lines (64 loc) · 2.48 KB
/
capture_audio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import pyaudio
import wave
import datetime
import time
from pathlib import Path
from log_config import init_logger
import logging
# initaite logger
init_logger()
logger = logging.getLogger('audio-to-text')
def start_recording(*, recording_duration=10, interval_duration=0, total_duration=2):
# save audio to folder
now = datetime.datetime.now()
file_name = now.strftime("%Y-%m-%d_%H-%M")
base = Path(__file__).parent
path_audio_files = base / 'audio_files' / file_name
path_audio_files.mkdir(exist_ok=True, parents=True)
# Set the duration of each recording in seconds
# recording_duration = 10
# Set the duration of the interval between recordings in seconds
# interval_duration = 0
# Set the format and number of channels for the audio recording
audio_format = pyaudio.paInt16
channels = 2
sample_rate = 44100
chunk_size = 1024
# Initialize PyAudio
audio = pyaudio.PyAudio()
# Open a new stream for recording
stream = audio.open(format=audio_format, channels=channels, rate=sample_rate, input=True, frames_per_buffer=chunk_size)
# Starting Podcast time
start_podacst = datetime.datetime.now()
# End Podcast time
end_podcast = start_podacst + datetime.timedelta(minutes=total_duration)
# Record and save audio every interval_duration seconds
while True:
# Get the current time for the filename
now = datetime.datetime.now()
file_name = path_audio_files / now.strftime("%Y-%m-%d_%H-%M-%S.wav")
file_name.touch()
with open(file_name, 'wb') as f:
# Open a new wave file for writing
wave_file = wave.open(f)
wave_file.setnchannels(channels)
wave_file.setsampwidth(audio.get_sample_size(audio_format))
wave_file.setframerate(sample_rate)
# Record audio for recording_duration seconds
for i in range(0, int(sample_rate / chunk_size * recording_duration)):
data = stream.read(chunk_size)
wave_file.writeframes(data)
# Close the wave file
wave_file.close()
# Terminate if end podcast time
if datetime.datetime.now() >= end_podcast:
break
# Wait for the next interval_duration seconds
time.sleep(interval_duration)
# Stop and close the audio stream
stream.stop_stream()
stream.close()
audio.terminate()
return path_audio_files.name
if __name__ == "__main__":
start_recording()