# KptSpeechButton (kpt-speech-button), KptSpeechWave (kpt-speech-wave) Dictation with a microphone button. A click turns the icon into "stop", an animated wave slides in next to it and the recognised words appear live in the target: a textarea, an input, a `contenteditable` element, a plain element or a function. Built on the Web Speech API; the session, insertion and wave maths live in `@konce-pt/speech`. Import: `import { KptSpeechButton, KptSpeechWave } from '@konce-pt/angular';` ## The key rule Interim text is visible in the target but never reaches the model. A textarea or input shows it in the native value without an `input` event; only final text goes in as a regular edit (with native undo when the field has focus). Signal Forms (`[formField]`), `[(value)]`, validators and search fields see final text only - no half-words, no query per syllable. ## KptSpeechButton - inputs - `target`: `HTMLElement | KptSpeechTarget | (snapshot) => void | ElementRef | KptSpeechTargetProvider` (default `null` - only `(result)` is emitted). A template reference to a native element is enough. - `lang`: string | null - BCP 47 tag (`pl-PL`); without it from `KptI18n.locale` and the browser language (`defaultSpeechLang`). A change applies from the next session. - `continuous`: boolean = true - listen until "stop"; `false` - one utterance (search fields). - `mode`: `'insert' | 'append' | 'replace'` = `'insert'` - at the caret, at the end, or clear the target the moment dictation starts (undoable with Ctrl+Z). - `interim`: `'inline' | 'ghost' | 'none' | null` - how interim text is shown; `null` = the default for the target kind (`inline` in fields, `ghost` overlay in contenteditable). - `capitalize`: boolean = true - capital letter at the start of a sentence. - `wave`: `'auto' | 'live' | 'synthetic' | 'none'` = `'auto'` - `live` measures the voice level (desktop), `synthetic` is a CSS animation (phones, tablets, or when the meter fails). - `wavePosition`: `'start' | 'end'` = `'start'`; `bars`: number = 5. - `silenceMs`: number = 0 - stop after this much silence; `maxDuration`: number = 0 - hard limit (ms). - `processLocally`: boolean = false - on-device recognition only (Chrome 135+), audio never leaves the computer. - `unsupported`: `'disable' | 'hide'` = `'disable'` - Firefox, `http:`, or a browser without the service (Brave, Opera): a disabled button with the reason in a tooltip, or nothing. - `listeningColor`: `'primary' | 'danger' | 'success' | 'warning' | 'info'` = `'primary'` - colour of "stop" and the wave while listening (the theme colour by default). Any other colour: set `--kpt-speech-color` and `--kpt-speech-on-color` (icon on it) - both inherit, so they also work on fields with a built-in microphone or globally in `:root`. - `disabled`, `size` (`KptButtonSize`), `variant` = `'text'`, `ariaLabel` (default "Dictate" from the dictionary), `tabIndex` (for roving toolbars). ## Outputs and methods - `(started)`, `(finished)` -> `KptSpeechEndReason` (`stopped | silence | max-duration | aborted | ended | error`), `(result)` -> `KptSpeechSnapshot` (`{ finalText, interimText }`), `(failed)` -> `KptSpeechErrorCode`. - `exportAs: 'kptSpeech'`: `status()`, `listening()`, `available()`, `start()`, `stop()` (graceful), `flush()` (interim becomes final and dictation ends at once - call it before sending), `abort()`, `toggle()`. ## Examples Send ## Built into fields The same microphone is built into `kpt-input`, `kpt-textarea` and `kpt-chat-composer` with the `speech` input (`speechLang`, `speechMode`, `speechColor`), into `kpt-rich-text` as the `dictate` tool, and into the search fields: `kpt-autocomplete` (`speech`, default `replace`), `kpt-data-table` (`filterSpeech`) and `kpt-map` / `kpt-map-search` (`searchSpeech` / `speech`) - search fields clear on click and listen for one utterance. Embedded microphones use `unsupported="hide"`, and the field reserves room for the button only when `available()` is true. ## Fields need provideKptSpeech() The microphone in fields is opt-in. Add `provideKptSpeech()` to the app providers - without it the `speech`, `dictate`, `filterSpeech` and `searchSpeech` options show nothing (a dev build warns once), and neither the button nor the `@konce-pt/speech` engine lands in the bundle. A standalone `` works without the provider. Fields place the microphone through the `kptSpeechSlot` directive, which knows `KptSpeechButton` only as a type. bootstrapApplication(App, { providers: [provideKptSpeech()] }); ## Own components as targets A component that knows where text goes (its own selection, its own model) implements `KptSpeechTargetProvider` - `speechTarget()` returns a `KptSpeechTarget` (`begin(mode)`, `interim(text)`, `commit(text)`, `end()`) or an element. `resolveSpeechTarget()` from the core builds the default adapters. ## KptSpeechWave Bars next to the microphone, `aria-hidden`. `levels: readonly number[]` (0..1) draws a live wave; empty `levels` gives the synthetic one, higher while `active` (voice heard). `bars`: number = 5. Colour: `--kpt-speech-wave-color`, otherwise the listening colour of the button (matching "stop"). ## Behaviour - One dictation per page: starting a microphone stops the previous one. - Continuous mode survives Chrome ending recognition after silence or about a minute - the session restarts the engine, and ends as `silence` after several empty restarts. On iOS a restart without a tap is unreliable, so the button stops and announces "Tap the microphone to continue". - The user types or moves the caret mid-dictation: interim text disappears and dictation continues at the new caret. - A hidden tab stops dictation; leaving the page releases the microphone. ## Screen reader and keyboard - The button name stays the same ("Dictate"); the state is carried by `aria-pressed`, the icon swaps between `microphone` and `player-stop`. - A polite live region announces start, stop and errors - never the transcript (a screen reader reading it would be recorded by the microphone). - Clicking the button does not move focus out of the target, so the caret stays where text goes. - Escape on the button or in the target stops dictation. ## Motion The wave slides in with `--kpt-motion-duration-base`. The synthetic wave is a state indicator like the typing dots: with motion off (`provideKptMotion({ enabled: false })`) or "reduce motion" it turns into a slow pulse instead of stopping. The live wave keeps showing the level, without smoothing. ## Browsers and privacy Chrome, Edge and Safari recognise speech; Firefox does not. A secure context (`https:` or `localhost`) is required. Chrome sends audio to a Google service unless `processLocally` is set - mention it in the privacy policy. ## Tokens `--kpt-color-primary` / `--kpt-color-on-primary` (stop and wave, by `listeningColor`; overridden by `--kpt-speech-color` / `--kpt-speech-on-color`), `--kpt-color-text-muted` (interim text in `.kpt-speech-ghost` and `.kpt-speech-interim`), button tokens shared with `kpt-icon-button`.