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

瀏覽器相容性

另見