Fix extra button sound playback and add on-hook restriction
Bug Fixes: - Extra button now plays sound completely (was stopping immediately) - Added check_hook_status parameter to play_sound_file() - Button sounds play with check_hook_status=False (always complete) - Greeting sounds play with check_hook_status=True (stop if hung up) Behavior Changes: - Extra button ONLY works when phone is on-hook (not in use) - Prevents button from interfering with active calls - Clear console message when button pressed during call - Sound plays completely without hook status interruption Technical Details: - Modified play_sound_file() to accept check_hook_status parameter - Changed while loop condition from "and" to conditional break - Added on-hook check in play_extra_button_sound() - Greeting playback still stops if handset hung up mid-message This ensures: ✓ Button sounds always play completely ✓ Button only works when phone not in use ✓ Greeting playback can still be interrupted by hanging up ✓ No audio conflicts between button and phone calls 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user