AudioParam: phương thức linearRampToValueAtTime()
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Phương thức linearRampToValueAtTime() của giao diện AudioParam lên lịch một thay đổi tuyến tính dần dần cho giá trị của AudioParam. Thay đổi bắt đầu tại thời điểm được chỉ định cho sự kiện trước đó, đi theo một đường dốc tuyến tính đến giá trị mới được cung cấp trong tham số value, và đạt đến giá trị mới tại thời điểm được cung cấp trong tham số endTime.
Cú pháp
linearRampToValueAtTime(value, endTime)
Tham số
Giá trị trả về
Tham chiếu đến đối tượng AudioParam này. Trong một số trình duyệt, các cách triển khai cũ hơn của giao diện này trả về undefined.
Ví dụ
Trong ví dụ này, chúng ta có một nguồn phương tiện với hai nút điều khiển (xem repo audio-param để xem mã nguồn, hoặc xem ví dụ trực tiếp.) Khi nhấn các nút này, linearRampToValueAtTime() được dùng để tăng dần giá trị gain lên 1.0 và giảm dần xuống 0 tương ứng. Cách này khá hữu ích cho các hiệu ứng fade in/fade out, dù AudioParam.exponentialRampToValueAtTime() thường được cho là tự nhiên hơn đôi chút.
// create audio context
const audioCtx = new AudioContext();
// set basic variables for example
const myAudio = document.querySelector("audio");
const linearRampPlus = document.querySelector(".linear-ramp-plus");
const linearRampMinus = document.querySelector(".linear-ramp-minus");
// Create a MediaElementAudioSourceNode
// Feed the HTMLMediaElement into it
const source = audioCtx.createMediaElementSource(myAudio);
// Create a gain node and set its gain value to 0.5
const gainNode = audioCtx.createGain();
// connect the AudioBufferSourceNode to the gainNode
// and the gainNode to the destination
gainNode.gain.setValueAtTime(0, audioCtx.currentTime);
source.connect(gainNode);
gainNode.connect(audioCtx.destination);
// set buttons to do something onclick
linearRampPlus.onclick = () => {
gainNode.gain.linearRampToValueAtTime(1.0, audioCtx.currentTime + 2);
};
linearRampMinus.onclick = () => {
gainNode.gain.linearRampToValueAtTime(0, audioCtx.currentTime + 2);
};
Thông số kỹ thuật
| Specification |
|---|
| Web Audio API> # dom-audioparam-linearramptovalueattime> |