AudioParam: phương thức exponentialRampToValueAtTime()
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Phương thức exponentialRampToValueAtTime() của giao diện AudioParam lên lịch một thay đổi dần dần theo hàm mũ đối với 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 hàm mũ đế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.
Note: Các đường dốc hàm mũ thường được xem là hữu ích hơn khi thay đổi tần số hoặc tốc độ phát so với đường dốc tuyến tính do cách tai người hoạt động.
Cú pháp
exponentialRampToValueAtTime(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, exponentialRampToValueAtTime() đượ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:
// create audio context
const audioCtx = new AudioContext();
// set basic variables for example
const myAudio = document.querySelector("audio");
const expRampPlus = document.querySelector(".exp-ramp-plus");
const expRampMinus = document.querySelector(".exp-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
expRampPlus.onclick = () => {
gainNode.gain.exponentialRampToValueAtTime(1.0, audioCtx.currentTime + 2);
};
expRampMinus.onclick = () => {
gainNode.gain.exponentialRampToValueAtTime(0.01, audioCtx.currentTime + 2);
};
Note: Giá trị 0.01 được dùng làm giá trị giảm dần tới trong hàm cuối thay vì 0, vì nếu dùng 0 thì lỗi invalid or illegal string sẽ được ném ra; giá trị này cần là số dương.
Thông số kỹ thuật
| Specification |
|---|
| Web Audio API> # dom-audioparam-exponentialramptovalueattime> |