Speech

Read a passage, announce an app event, or offer spoken playback of user-provided text. day-part-speech uses the operating system’s speech engine. For short interface feedback, also consider sound and accessibility.

Add to your app

Add day-part-speech to your app’s Cargo dependencies:

[dependencies]
day-part-speech = "0.4.2"

Build with the Day CLI so its native bridge code is packaged. This part needs no toolkit feature or extra Day.toml entry. API reference · Source.

Speak and observe completion

This helper accepts text from the caller and returns the utterance’s outcome. Call it from a UI action under day::task. For app-authored announcements, format a generated res::str message before passing it in; user-supplied text can be passed directly.

use day_part_speech::{Error, SpeechEnd, speak_future};

pub async fn read_aloud(text: String) -> Result<SpeechEnd, Error> {
    speak_future(&text).await
}
Entry pointBehavior
available()Reports Native, Emulated, or Unsupported for the host.
speak(text)Accepts an utterance and interrupts the previous one; success does not mean playback finished.
speak_future(text)Resolves when the engine reports completion, interruption, or failure.
speak_until_done(text, callback)Calls the callback once, including when the initial request fails. The callback may run on an engine thread.
stop()Stops the current utterance; does nothing when speech is unavailable.

SpeechEnd distinguishes finished playback, interruption, and an accepted utterance whose completion the engine cannot observe. Starting another utterance replaces the current one; this API does not provide an app-owned queue. Dropping the future stops observing it but does not stop the voice. Stop playback explicitly when that is the intended behavior on navigation or cancellation.

Prefer the future form for UI code. If using the callback form, send data through a Setter and format localized messages on the UI thread. See async ownership.

Platform support

PlatformEngine and limits
macOS / iOSAVSpeechSynthesizer; uses the system voice.
AndroidTextToSpeech; engine initialization can delay the first utterance.
WindowsSpeech API; uses an installed SAPI voice.
Linux / GTK / Qtspeech-dispatcher must be installed and available. Completion returns Unobserved; it does not track the end of audible speech.
HarmonyOSCore Speech Kit integration is reported as emulated and uses a Chinese voice configuration.
WebSpeechSynthesis; installed voices and playback policy depend on the browser.

Handle unsupported and engine-error results even after checking availability. The API does not expose voice selection, rate, pitch, or a per-utterance language. When language choice is essential, verify that the system voice suits the text before offering this feature.

  • Sound for short system sounds and haptics.
  • Media playback for recorded audio and longer playback sessions.
  • Localization for app-authored spoken messages.