SpeechSynthesisUtterance: pitch 属性

Baseline 已广泛支持

此功能已成熟,并可在多种设备和浏览器版本上使用。自 2018 年 9 月以来,它已在各种浏览器中推出。

pitch 属性是 SpeechSynthesisUtterance 接口的一部分,用于获取和设置发音时的音高。

如果未设置,将使用默认值 1。

一个表示音高值的浮点数。它的范围可以是从 0(最低)到 2(最高),其中 1 是当前平台或语音的默认音高。某些语音合成引擎或语音可能会进一步限制最小和最大速率。如果使用 SSML,此值将被标记中的 prosody 标签覆盖。

示例

js
const synth = window.speechSynthesis;

const inputForm = document.querySelector("form");
const inputTxt = document.querySelector("input");
const voiceSelect = document.querySelector("select");

const voices = synth.getVoices();

// …

inputForm.onsubmit = (event) => {
  event.preventDefault();

  const utterThis = new SpeechSynthesisUtterance(inputTxt.value);
  const selectedOption =
    voiceSelect.selectedOptions[0].getAttribute("data-name");
  for (const voice of voices) {
    if (voice.name === selectedOption) {
      utterThis.voice = voice;
    }
  }
  utterThis.pitch = 1.5;
  synth.speak(utterThis);
  inputTxt.blur();
};

规范

规范
Web Speech API
# dom-speechsynthesisutterance-pitch

浏览器兼容性

另见