diff --git a/rotary_phone_web.py b/rotary_phone_web.py index 27751c8..89b1cea 100644 --- a/rotary_phone_web.py +++ b/rotary_phone_web.py @@ -181,8 +181,14 @@ class RotaryPhone: wf.close() print(f"Default dial tone saved to {DIALTONE_FILE}") - def play_sound_file(self, filepath): - """Play a WAV file with volume control""" + def play_sound_file(self, filepath, check_hook_status=True): + """Play a WAV file with volume control + + Args: + filepath: Path to WAV file + check_hook_status: If True, only play while off-hook (for greeting). + If False, play completely regardless of hook (for button) + """ if not os.path.exists(filepath): print(f"Sound file not found: {filepath}") return False @@ -207,7 +213,11 @@ class RotaryPhone: # Play the sound with volume control data = wf.readframes(CHUNK) - while data and self.phone_status == "off_hook": + while data: + # For greeting sounds, stop if handset is hung up + if check_hook_status and self.phone_status != "off_hook": + break + # Apply volume by converting to numpy array and scaling if volume < 1.0: audio_data = np.frombuffer(data, dtype=np.int16) @@ -295,10 +305,15 @@ class RotaryPhone: } def play_extra_button_sound(self): - """Play sound when extra button is pressed""" + """Play sound when extra button is pressed (only when on-hook)""" if not EXTRA_BUTTON_ENABLED: return + # Only play if phone is on hook (not in use) + if self.phone_status != "on_hook": + print("Extra button ignored - phone is off hook") + return + button_sound = self.get_extra_button_sound_path() if not os.path.exists(button_sound): print(f"Extra button sound not found: {button_sound}") @@ -306,7 +321,8 @@ class RotaryPhone: print(f"\n=== Extra button pressed ===") self.extra_button_playing = True - self.play_sound_file(button_sound) + # Play without checking hook status - play entire sound + self.play_sound_file(button_sound, check_hook_status=False) self.extra_button_playing = False def phone_loop(self):