Skip to content

Commit cecd25a

Browse files
authored
refactor(tts): use bitbots_tts consistently (#529)
2 parents b697c82 + 6735a66 commit cecd25a

File tree

6 files changed

+66
-59
lines changed

6 files changed

+66
-59
lines changed

bitbots_misc/bitbots_tts/bitbots_tts/tts.py

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import rclpy
1010
import requests
11+
from ament_index_python import get_package_prefix
1112
from rcl_interfaces.msg import Parameter, SetParametersResult
1213
from rclpy.node import Node
1314
from rclpy.publisher import Publisher
@@ -24,6 +25,13 @@ def speak(text: str, publisher: Publisher, priority: int = 20, speaking_active:
2425
publisher.publish(msg)
2526

2627

28+
def say(text: str) -> None:
29+
"""Start the shell `say.sh` script to output given text with mimic3. Beware: this is blocking."""
30+
script_path = os.path.join(get_package_prefix("bitbots_tts"), "lib/bitbots_tts/say.sh")
31+
process = subprocess.Popen((script_path, text))
32+
process.wait()
33+
34+
2735
class Speaker(Node):
2836
"""
2937
Uses tts to say messages from the speak topic.
@@ -50,17 +58,6 @@ def __init__(self) -> None:
5058
# Callback for parameter changes
5159
self.add_on_set_parameters_callback(self.on_set_parameters)
5260

53-
# Mapping from robot name to voice name
54-
self.robot_voice_mapping = {
55-
"amy": "en_US/vctk_low",
56-
"donna": "en_US/vctk_low",
57-
"jack": "en_UK/apope_low",
58-
"melody": "en_US/vctk_low",
59-
"rory": "en_UK/apope_low",
60-
}
61-
62-
self.robot_speed_mapping = {"amy": 2.2, "donna": 2.2, "jack": 1.0, "melody": 2.2, "rory": 1.0}
63-
6461
# Subscribe to the speak topic
6562
self.create_subscription(Audio, "speak", self.speak_cb, 10)
6663

@@ -92,30 +89,15 @@ def on_set_parameters(self, parameters: List[Parameter]) -> SetParametersResult:
9289
return SetParametersResult(successful=True)
9390

9491
def run_speaker(self) -> None:
95-
"""Continously checks the queue and speaks the next message."""
92+
"""Continuously checks the queue and speaks the next message."""
9693
# Check if there is a message in the queue
9794
if len(self.prio_queue) > 0:
9895
# Get the next message and speak it
9996
text, _ = self.prio_queue.pop(0)
100-
self.say(text)
101-
102-
def say(self, text: str) -> None:
103-
"""Speak this specific text."""
104-
# Get the voice name from the environment variable ROBOT_NAME or use the default voice if it's not set
105-
voice = self.robot_voice_mapping.get(os.getenv("ROBOT_NAME"), "en_US/vctk_low")
106-
# Get the speed for the given robot or use the default speed if no robot name is set
107-
speed = self.robot_speed_mapping.get(os.getenv("ROBOT_NAME"), 2.2)
108-
try:
109-
# Generate the speech with mimic
110-
mimic_subprocess = subprocess.Popen(
111-
("mimic3", "--remote", "--voice", voice, "--length-scale", str(speed), text), stdout=subprocess.PIPE
112-
)
113-
# Play the audio from the previous process with aplay
114-
aplay_subprocess = subprocess.Popen(("aplay", "-"), stdin=mimic_subprocess.stdout, stdout=subprocess.PIPE)
115-
# Wait for the process to finish
116-
aplay_subprocess.wait()
117-
except OSError:
118-
self.get_logger().error(str(traceback.format_exc()))
97+
try:
98+
say(text)
99+
except OSError:
100+
self.get_logger().error(str(traceback.format_exc()))
119101

120102
def speak_cb(self, msg: Audio) -> None:
121103
"""Handles incoming msg on speak topic."""

bitbots_misc/bitbots_tts/launch/tts.launch

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<launch>
2-
<executable cmd="mimic3-server --preload-voice en_US/vctk_low --cache-dir $(env HOME)/.cache/mimic3/" name="mimic3-server" output="screen"/>
2+
<!-- Do not launch mimic3-server on robot, as it is running as a systemd service -->
3+
<executable unless="$(env IS_ROBOT)" cmd="mimic3-server --preload-voice en_US/vctk_low --preload-voice en_UK/apope_low --cache-dir $(env HOME)/.cache/mimic3/" name="mimic3-server" output="screen"/>
4+
35
<node name="bitbots_tts" pkg="bitbots_tts" exec="tts">
46
<param from="$(find-pkg-share bitbots_tts)/config/tts_config.yaml"/>
57
</node>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
set -eEuo pipefail
3+
4+
ROBOT_NAME="${ROBOT_NAME:-}"
5+
6+
# Mapping robot name to voice and speed
7+
case "$ROBOT_NAME" in
8+
"jack"|"rory")
9+
voice="en_UK/apope_low"
10+
speed=1.0
11+
;;
12+
"amy"|"donna"|"melody"|"rose")
13+
voice="en_US/vctk_low"
14+
speed=1.7
15+
;;
16+
*)
17+
echo "Unknown robot: '$ROBOT_NAME', using default female voice"
18+
voice="en_US/vctk_low"
19+
speed=1.7
20+
;;
21+
esac
22+
23+
text="$1"
24+
if [ -z "$text" ]; then
25+
echo "No text provided!"
26+
exit 1
27+
fi
28+
29+
# Generate the speech with mimic and play it with alsa
30+
mimic3 --remote --voice "$voice" --length-scale "$speed" "$text" | aplay -q -
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
from socket import gethostname
5+
6+
from bitbots_tts.tts import say
7+
from netifaces import AF_INET, ifaddresses, interfaces
8+
9+
ip_address = "not set"
10+
11+
wifi_interface = next(filter(lambda i: i.startswith("wlp"), interfaces()), None)
12+
if wifi_interface and AF_INET in ifaddresses(wifi_interface):
13+
ip_address = ifaddresses(wifi_interface)[AF_INET][0]["addr"]
14+
15+
ip = " dot ".join(ip_address.split("."))
16+
17+
robot_name = os.getenv("ROBOT_NAME") or gethostname()
18+
msg = f"Startup complete: {robot_name}. My wifi IP is {ip}."
19+
say(msg)

bitbots_misc/bitbots_tts/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
install_requires=[
1717
"setuptools",
1818
],
19-
scripts=["scripts/send_text.py"],
19+
scripts=glob.glob("scripts/*"),
2020
entry_points={
2121
"console_scripts": [
2222
"tts = bitbots_tts.tts:main",

bitbots_misc/bitbots_utils/scripts/speak_ip.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)