Skip to content

Commit ae964d4

Browse files
committed
Fixed bug to do with self.recording flag in recording code
1 parent f76c273 commit ae964d4

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

audio_recorder.py

+26-27
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,32 @@ def start_recording(self):
5454
This method starts the recording thread and the audio stream.
5555
It uses the system default microphone as the input device.
5656
"""
57-
if self.recording:
58-
print("Recording is already in progress.")
59-
return
60-
self.frames.clear()
61-
self.start_time = time.time()
62-
try:
63-
mic_index = self.get_default_mic_index()
64-
if mic_index is None:
65-
raise IOError("No default microphone found.")
66-
self.stream = self.audio.open(format=pyaudio.paInt16, channels=1,
67-
rate=self.FS, input=True,
68-
frames_per_buffer=512, start=False,
69-
input_device_index=mic_index)
70-
self.stream.start_stream()
71-
self.record_thread = threading.Thread(target=self.record_audio, daemon=True)
72-
self.record_thread.start()
73-
self.recording = True
74-
if self.verbose:
75-
print("Recording started...")
76-
except Exception as e:
77-
self.recording = False
78-
self.record_thread = None # Ensure the thread is reset
79-
if self.verbose:
80-
import traceback
81-
traceback.print_exc()
82-
else:
83-
print(f"Failed to start recording: {e}")
57+
if not self.recording:
58+
self.frames.clear()
59+
self.start_time = time.time()
60+
try:
61+
mic_index = self.get_default_mic_index()
62+
if mic_index is not None:
63+
self.stream = self.audio.open(format=pyaudio.paInt16, channels=1,
64+
rate=self.FS, input=True,
65+
frames_per_buffer=512, start=False,
66+
input_device_index=mic_index)
67+
self.recording = True # Set this before starting the thread
68+
self.record_thread = threading.Thread(target=self.record_audio, daemon=True)
69+
self.stream.start_stream()
70+
self.record_thread.start()
71+
if self.verbose:
72+
print("Recording started...")
73+
else:
74+
print("No default microphone found.")
75+
except Exception as e:
76+
self.recording = False
77+
self.record_thread = None # Ensure the thread is reset
78+
if self.verbose:
79+
import traceback
80+
traceback.print_exc()
81+
else:
82+
print(f"Failed to start recording: {e}")
8483

8584
@property
8685
def duration(self):

0 commit comments

Comments
 (0)