Skip to content

Add SpeechRecognitionResult Timestamps #191

Description

@alan33d

Description

The Web Speech API currently does not expose the start and end timestamps of the source audio corresponding to a given transcription result (SpeechRecognitionResult). This limitation creates two major challenges for API clients and end users:

  • Timeline Association: Developers cannot readily associate transcribed text with specific segments of the source audio stream, making it difficult to map generated captions to media timelines, audio tracks, or live video frames.
  • Latency Tracking & Cloud Failover: With the adoption of on-device Automatic Speech Recognition (ASR) to improve privacy and reduce server costs, processing performance becomes heavily dependent on local client hardware resources. The Web Speech API acts as a "black box" regarding local processing delays. Developers cannot programmatically calculate transcription latency or detect when on-device models fall behind real-time. This leads to poor user experiences (e.g. caption lag during live video conferencing) and deprives applications of the signal needed to seamlessly fail over to a cloud ASR backend provider.

Specification

Expose source audio timestamps on the SpeechRecognitionResult interface in seconds (relative to the start of the audio stream consumed by the recognizer, starting at $0.0\text{s}$).

Web IDL

partial interface SpeechRecognitionResult {
    // Start timestamp of the audio segment in seconds relative to the start of the audio stream (0.0s).
    readonly attribute double audioStartTime;

    // End timestamp of the audio segment in seconds relative to the start of the audio stream.
    readonly attribute double audioEndTime;
};

Usage Example

Developers can compute real-time processing latency by mapping the stream timestamp to the document timeline and comparing with Event.timeStamp:

const recognition = new SpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;

let audioOriginMs = 0;

// 1. Capture stream start timestamp on the document timeline
recognition.onaudiostart = (event) => {
  audioOriginMs = event.timeStamp;
};

recognition.onresult = (event) => {
  const result = event.results[event.resultIndex];

  // 2. Convert stream-relative seconds to document timeline milliseconds
  const absoluteAudioEndMs = audioOriginMs + (result.audioEndTime * 1000);
  const latencyMs = event.timeStamp - absoluteAudioEndMs;

  // 3. If local processing latency exceeds acceptable threshold, switch to cloud provider
  if (latencyMs > LATENCY_THRESHOLD_MS) {
    console.warn(`ASR processing lag detected (${latencyMs.toFixed(0)}ms). Transitioning to cloud provider.`);
    switchToCloudBackend();
  }
};

recognition.start();

Security & Privacy Considerations

  • Fingerprinting Risk: High-precision timing can expose micro-architectural hardware and CPU performance characteristics for device fingerprinting.
  • Mitigation: In accordance with W3C High Resolution Time Level 3 and HTMLMediaElement.currentTime, user agents may coarsen timer resolution or introduce jitter to mitigate side-channel risks in non-isolated contexts.

Alternatives Considered

  • Browser Warning Events (e.g. onprocessinglag): Difficult to standardize a single threshold across applications with varying latency tolerances (e.g., real-time captions require <200ms, while dictation tolerates multiple seconds).
  • Internal Queue Metric (e.g. queueDepth): Difficult to standardize across fragmented engine architectures, model types, and buffering models.
  • Binary Status Flag (e.g. isRealTime): Lacks numerical flexibility for applications to measure progressive degradation trends.
  • Existing Events (speechstart / speechend): Fire only once per continuous session, cannot be correlated to ongoing stream results, and event.timeStamp reflects DOM dispatch rather than acoustic audio boundaries.

Links & References

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions