-
Notifications
You must be signed in to change notification settings - Fork 159
Add TTS and STT #753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add TTS and STT #753
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1ca1da3
add tts and stt
paul-nechifor 1b002fb
CI code cleanup
paul-nechifor cde4c75
Merge branch 'dev' into audio-input-output
paul-nechifor bb29586
fix code review comment
paul-nechifor 6dd2349
Merge branch 'dev' into audio-input-output
paul-nechifor 7589514
fix mypy
paul-nechifor 0c9932f
fix mypy
paul-nechifor 8f430f3
Merge branch 'dev' into audio-input-output
paul-nechifor 6a68a95
Merge branch 'dev' into audio-input-output
paul-nechifor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # Copyright 2025 Dimensional Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from threading import Thread | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import reactivex as rx | ||
| import reactivex.operators as ops | ||
|
|
||
| from dimos.core import Module, rpc | ||
| from dimos.core.transport import pLCMTransport | ||
| from dimos.stream.audio.node_normalizer import AudioNormalizer | ||
| from dimos.stream.audio.stt.node_whisper import WhisperNode | ||
| from dimos.utils.logging_config import setup_logger | ||
| from dimos.web.robot_web_interface import RobotWebInterface | ||
|
|
||
| if TYPE_CHECKING: | ||
| from dimos.stream.audio.base import AudioEvent | ||
|
|
||
| logger = setup_logger(__name__) | ||
|
|
||
|
|
||
| class WebInput(Module): | ||
| _web_interface: RobotWebInterface | None = None | ||
| _thread: Thread | None = None | ||
| _human_transport: pLCMTransport[str] | None = None | ||
|
|
||
| @rpc | ||
| def start(self) -> None: | ||
| super().start() | ||
|
|
||
| self._human_transport = pLCMTransport("/human_input") | ||
|
|
||
| audio_subject: rx.subject.Subject[AudioEvent] = rx.subject.Subject() | ||
|
|
||
| self._web_interface = RobotWebInterface( | ||
| port=5555, | ||
| text_streams={"agent_responses": rx.subject.Subject()}, | ||
| audio_subject=audio_subject, | ||
| ) | ||
|
|
||
| normalizer = AudioNormalizer() | ||
| stt_node = WhisperNode() | ||
|
|
||
| # Connect audio pipeline: browser audio → normalizer → whisper | ||
| normalizer.consume_audio(audio_subject.pipe(ops.share())) | ||
| stt_node.consume_audio(normalizer.emit_audio()) | ||
|
|
||
| # Subscribe to both text input sources | ||
| # 1. Direct text from web interface | ||
| unsub = self._web_interface.query_stream.subscribe(self._human_transport.publish) | ||
| self._disposables.add(unsub) | ||
|
|
||
| # 2. Transcribed text from STT | ||
| unsub = stt_node.emit_text().subscribe(self._human_transport.publish) | ||
| self._disposables.add(unsub) | ||
|
|
||
| self._thread = Thread(target=self._web_interface.run, daemon=True) | ||
| self._thread.start() | ||
|
|
||
| logger.info("Web interface started at http://localhost:5555") | ||
|
|
||
| @rpc | ||
| def stop(self) -> None: | ||
| if self._web_interface: | ||
| self._web_interface.shutdown() | ||
| if self._thread: | ||
| self._thread.join(timeout=1.0) | ||
| if self._human_transport: | ||
| self._human_transport.lcm.stop() | ||
| super().stop() | ||
|
|
||
|
|
||
| web_input = WebInput.blueprint | ||
|
|
||
| __all__ = ["WebInput", "web_input"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # Copyright 2025 Dimensional Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import threading | ||
| import time | ||
|
|
||
| from reactivex import Subject | ||
|
|
||
| from dimos.core.core import rpc | ||
| from dimos.core.skill_module import SkillModule | ||
| from dimos.protocol.skill.skill import skill | ||
| from dimos.stream.audio.node_output import SounddeviceAudioOutput | ||
| from dimos.stream.audio.tts.node_openai import OpenAITTSNode, Voice | ||
| from dimos.utils.logging_config import setup_logger | ||
|
|
||
| logger = setup_logger("dimos.agents2.skills.speak_skill") | ||
|
|
||
|
|
||
| class SpeakSkill(SkillModule): | ||
| _tts_node: OpenAITTSNode | None = None | ||
| _audio_output: SounddeviceAudioOutput | None = None | ||
| _audio_lock: threading.Lock = threading.Lock() | ||
|
|
||
| @rpc | ||
| def start(self) -> None: | ||
| super().start() | ||
| self._tts_node = OpenAITTSNode(speed=1.2, voice=Voice.ONYX) | ||
| self._audio_output = SounddeviceAudioOutput(sample_rate=24000) | ||
| self._audio_output.consume_audio(self._tts_node.emit_audio()) | ||
|
|
||
| @rpc | ||
| def stop(self) -> None: | ||
| if self._tts_node: | ||
| self._tts_node.dispose() | ||
| self._tts_node = None | ||
| if self._audio_output: | ||
| self._audio_output.stop() | ||
| self._audio_output = None | ||
| super().stop() | ||
|
|
||
| @skill() | ||
| def speak(self, text: str) -> str: | ||
| """Speak text out loud through the robot's speakers. | ||
|
|
||
| USE THIS TOOL AS OFTEN AS NEEDED. People can't normally see what you say in text, but can hear what you speak. | ||
|
|
||
| Try to be as concise as possible. Remember that speaking takes time, so get to the point quickly. | ||
|
|
||
| Example usage: | ||
|
|
||
| speak("Hello, I am your robot assistant.") | ||
| """ | ||
| if self._tts_node is None: | ||
| return "Error: TTS not initialized" | ||
|
|
||
| # Use lock to prevent simultaneous speech | ||
| with self._audio_lock: | ||
| text_subject: Subject[str] = Subject() | ||
| audio_complete = threading.Event() | ||
| self._tts_node.consume_text(text_subject) | ||
|
|
||
| def set_as_complete(_t: str) -> None: | ||
| audio_complete.set() | ||
|
|
||
| def set_as_complete_e(_e: Exception) -> None: | ||
| audio_complete.set() | ||
|
|
||
| subscription = self._tts_node.emit_text().subscribe( | ||
| on_next=set_as_complete, | ||
| on_error=set_as_complete_e, | ||
| ) | ||
|
|
||
| text_subject.on_next(text) | ||
| text_subject.on_completed() | ||
|
|
||
| timeout = max(5, len(text) * 0.1) | ||
|
|
||
| if not audio_complete.wait(timeout=timeout): | ||
| logger.warning(f"TTS timeout reached for: {text}") | ||
| subscription.dispose() | ||
| return f"Warning: TTS timeout while speaking: {text}" | ||
| else: | ||
| # Small delay to ensure buffers flush | ||
| time.sleep(0.3) | ||
|
|
||
| subscription.dispose() | ||
|
|
||
| return f"Spoke: {text}" | ||
|
|
||
|
|
||
| speak_skill = SpeakSkill.blueprint | ||
|
|
||
| __all__ = ["SpeakSkill", "speak_skill"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.