SpeechSynthesisUtterance: pitch property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2018.

Thuộc tính pitch của giao diện SpeechSynthesisUtterance lấy và đặt cao độ mà yêu cầu phát âm sẽ được nói.

Nếu không được đặt, giá trị mặc định là 1 sẽ được dùng.

Giá trị

Một số thực đại diện cho giá trị cao độ. Nó có thể nằm trong khoảng từ 0 (thấp nhất) đến 2 (cao nhất), với 1 là cao độ mặc định cho nền tảng hoặc giọng nói hiện tại. Một số engine tổng hợp giọng nói hoặc giọng nói có thể giới hạn thêm giá trị tối thiểu và tối đa. Nếu SSML được sử dụng, giá trị này sẽ bị ghi đè bởi các thẻ prosody trong markup.

Ví dụ

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();
};

Thông số kỹ thuật

Specification
Web Speech API
# dom-speechsynthesisutterance-pitch

Tương thích trình duyệt

Xem thêm