Skip to content

Commit

Permalink
add words for correction
Browse files Browse the repository at this point in the history
Signed-off-by: mspierg <[email protected]>
  • Loading branch information
mspierg committed Jan 19, 2025
1 parent a456c7f commit b710b93
Showing 1 changed file with 100 additions and 46 deletions.
146 changes: 100 additions & 46 deletions src/module_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@ def __init__(self, control='Master'):

def get_volume(self):
try:
# Executes the amixer command to get volume information
output = subprocess.check_output(
['amixer', 'get', self.control],
stderr=subprocess.STDOUT
).decode('utf-8')
match = re.search(r'\[(\d+)%\]', output)
if match:
return int(match.group(1))

# Search for volume percentages in the output for both channels
left_match = re.search(r'Front Left: Playback \d+ \[(\d+)%\]', output)
right_match = re.search(r'Front Right: Playback \d+ \[(\d+)%\]', output)

if left_match and right_match:
# Calculate the average volume of both channels
left_volume = int(left_match.group(1))
right_volume = int(right_match.group(1))
return (left_volume + right_volume) // 2

elif left_match: # If only the left channel is present
return int(left_match.group(1))

elif right_match: # If only the right channel is present
return int(right_match.group(1))

raise RuntimeError("Volume percentage not found in amixer output.")
except subprocess.CalledProcessError as e:
print(f"Error getting volume: {e}")
Expand All @@ -28,10 +43,42 @@ def set_volume(self, percent):
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT
)
print(f"Volume set to {percent}%.")
# Verify the current volume after setting it
current_volume = self.get_volume()
print(f"Volume set to {percent}%. Current volume is {current_volume}%.")
except subprocess.CalledProcessError as e:
print(f"Error setting volume: {e}")


def correct_transcription(transcribed_text):
"""
Corrects common misinterpretations of volume commands by the STT module.
Parameters:
transcribed_text (str): The transcribed user input.
Returns:
str: The corrected user input.
"""
corrections = {
"the grease volume": "decrease volume",
"degrees volume": "decrease volume",
"the greek volume": "decrease volume",
"the great volume": "decrease volume",
"the greece volume": "decrease volume",
"the brief volume": "decrease volume",
"the grief volume": "decrease volume",
"increase volume": "increase volume",
"reduce volume": "decrease volume",
}
for wrong, correct in corrections.items():
if wrong in transcribed_text.lower():
suggestion = "increase" if "increase" in correct else "decrease"
print(f"I think I heard {suggestion}. Proceeding as '{correct}'.")
return transcribed_text.lower().replace(wrong, correct)
return transcribed_text


def handle_volume_command(user_input):
"""
Interprets and handles volume-related commands from the user input.
Expand All @@ -42,84 +89,91 @@ def handle_volume_command(user_input):
Returns:
str: A response describing the result of the volume action.
"""
# Correct the input text based on common misinterpretations
user_input = correct_transcription(user_input)

volume_manager = RaspbianVolumeManager() # Create volume manager instance

current_volume = volume_manager.get_volume()

if current_volume is None:
return "Unable to retrieve the current volume level. Please try again."

print(f"TOOL: Using Tool Volume at {current_volume / 100:.2f}") # Report the correct volume value

# Handle specific volume commands
if "increase" in user_input.lower() or "raise" in user_input.lower():
current_volume = volume_manager.get_volume()
if current_volume is not None:
increment = 10
increment = 10
if "by" in user_input.lower():
match = re.search(r'by (\d+)', user_input.lower())
if match:
increment = int(match.group(1))
new_volume = min(current_volume + increment, 100)
volume_manager.set_volume(new_volume)
current_volume = volume_manager.get_volume() # Ensure updated value is fetched
return f"Volume increased by {increment}%. Current volume is {current_volume}%."

elif "decrease" in user_input.lower() or "lower" in user_input.lower():
decrement = 10
if "by" in user_input.lower():
match = re.search(r'by (\d+)', user_input.lower())
if match:
decrement = int(match.group(1))
new_volume = max(current_volume - decrement, 0)
volume_manager.set_volume(new_volume)
current_volume = volume_manager.get_volume() # Ensure updated value is fetched
return f"Volume decreased by {decrement}%. Current volume is {current_volume}%."

elif "adjust" in user_input.lower():
if "up" in user_input.lower():
increment = 5
if "by" in user_input.lower():
match = re.search(r'by (\d+)', user_input.lower())
if match:
increment = int(match.group(1))
new_volume = min(current_volume + increment, 100)
volume_manager.set_volume(new_volume)
return f"Volume increased by {increment}%. Current volume is {new_volume}%."
current_volume = volume_manager.get_volume() # Ensure updated value is fetched
return f"Volume adjusted up by {increment}%. Current volume is {current_volume}%."

elif "decrease" in user_input.lower() or "lower" in user_input.lower():
current_volume = volume_manager.get_volume()
if current_volume is not None:
decrement = 10
elif "down" in user_input.lower():
decrement = 5
if "by" in user_input.lower():
match = re.search(r'by (\d+)', user_input.lower())
if match:
decrement = int(match.group(1))
new_volume = max(current_volume - decrement, 0)
volume_manager.set_volume(new_volume)
return f"Volume decreased by {decrement}%. Current volume is {new_volume}%."

elif "adjust" in user_input.lower():
current_volume = volume_manager.get_volume()
if current_volume is not None:
if "up" in user_input.lower():
increment = 5
if "by" in user_input.lower():
match = re.search(r'by (\d+)', user_input.lower())
if match:
increment = int(match.group(1))
new_volume = min(current_volume + increment, 100)
volume_manager.set_volume(new_volume)
return f"Volume adjusted up by {increment}%. Current volume is {new_volume}%."

elif "down" in user_input.lower():
decrement = 5
if "by" in user_input.lower():
match = re.search(r'by (\d+)', user_input.lower())
if match:
decrement = int(match.group(1))
new_volume = max(current_volume - decrement, 0)
volume_manager.set_volume(new_volume)
return f"Volume adjusted down by {decrement}%. Current volume is {new_volume}%."
else:
return "Please specify 'up' or 'down' when using 'adjust'."
current_volume = volume_manager.get_volume() # Ensure updated value is fetched
return f"Volume adjusted down by {decrement}%. Current volume is {current_volume}%."
else:
return "Please specify 'up' or 'down' when using 'adjust'."

elif "set" in user_input.lower():
match = re.search(r'(\d{1,3})%', user_input)
if match:
volume = int(match.group(1))
if 0 <= volume <= 100:
volume_manager.set_volume(volume)
return f"Volume set to {volume}%."
current_volume = volume_manager.get_volume() # Ensure updated value is fetched
return f"Volume set to {volume}%. Current volume is {current_volume}%."
else:
return "Please provide a valid volume between 0 and 100."
else:
return "Please specify the volume percentage."

elif "mute" in user_input.lower():
volume_manager.set_volume(0)
return "Volume has been muted."
current_volume = volume_manager.get_volume() # Ensure updated value is fetched
return "Volume has been muted. Current volume is 0%."

elif "unmute" in user_input.lower() or "activate sound" in user_input.lower():
default_volume = 50 # Default volume level when unmuting
volume_manager.set_volume(default_volume)
return f"Volume has been unmuted. Current volume is {default_volume}%."
current_volume = volume_manager.get_volume() # Ensure updated value is fetched
return f"Volume has been unmuted. Current volume is {current_volume}%."

elif "check volume" in user_input.lower() or "current volume" in user_input.lower():
current_volume = volume_manager.get_volume()
if current_volume is not None:
return f"The current volume is {current_volume}%."
else:
return "Unable to retrieve the current volume level."
return f"The current volume is {current_volume}%."

return "Volume control command not recognized. Please specify a valid action (e.g., increase, decrease, adjust, mute, unmute, set)."

0 comments on commit b710b93

Please sign in to comment.