ScriptProcessorNode: sự kiện audioprocess

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Sự kiện audioprocess của giao diện ScriptProcessorNode được kích hoạt khi bộ đệm đầu vào của một bộ xử lý script sẵn sàng để được xử lý.

Note: Tính năng này đã được thay thế bởi AudioWorklets và giao diện AudioWorkletNode.

Sự kiện này không thể hủy và không nổi bong bóng.

Cú pháp

Sử dụng tên sự kiện trong các phương thức như addEventListener(), hoặc đặt thuộc tính trình xử lý sự kiện.

js
addEventListener("audioprocess", (event) => { })

onaudioprocess = (event) => { }

Loại sự kiện

Một AudioProcessingEvent. Kế thừa từ Event.

Event AudioProcessingEvent

Thuộc tính sự kiện

Cũng triển khai các thuộc tính được kế thừa từ cha của nó, Event.

playbackTime Read only

Một số thực đôi đại diện cho thời điểm âm thanh sẽ được phát, được xác định bởi thời gian của AudioContext.currentTime.

inputBuffer Read only

Một AudioBuffer là bộ đệm chứa dữ liệu âm thanh đầu vào cần được xử lý. Số lượng kênh được xác định là tham số numberOfInputChannels, của phương thức nhà máy AudioContext.createScriptProcessor(). Lưu ý rằng AudioBuffer được trả về chỉ hợp lệ trong phạm vi của trình xử lý sự kiện.

outputBuffer Read only

Một AudioBuffer là bộ đệm nơi dữ liệu âm thanh đầu ra nên được ghi. Số lượng kênh được xác định là tham số, numberOfOutputChannels, của phương thức nhà máy AudioContext.createScriptProcessor(). Lưu ý rằng AudioBuffer được trả về chỉ hợp lệ trong phạm vi của trình xử lý sự kiện.

Ví dụ

js
scriptNode.addEventListener("audioprocess", (audioProcessingEvent) => {
  // Bộ đệm đầu vào là một bài hát chúng tôi đã tải trước đó
  const inputBuffer = audioProcessingEvent.inputBuffer;

  // Bộ đệm đầu ra chứa các mẫu sẽ được sửa đổi và phát
  const outputBuffer = audioProcessingEvent.outputBuffer;

  // Lặp qua các kênh đầu ra (trong trường hợp này chỉ có một)
  for (let channel = 0; channel < outputBuffer.numberOfChannels; channel++) {
    const inputData = inputBuffer.getChannelData(channel);
    const outputData = outputBuffer.getChannelData(channel);

    // Lặp qua 4096 mẫu
    for (let sample = 0; sample < inputBuffer.length; sample++) {
      // làm cho đầu ra bằng với đầu vào
      outputData[sample] = inputData[sample];

      // thêm nhiễu vào mỗi mẫu đầu ra
      outputData[sample] += (Math.random() * 2 - 1) * 0.2;
    }
  }
});

Bạn cũng có thể thiết lập trình xử lý sự kiện bằng thuộc tính onaudioprocess:

js
scriptNode.onaudioprocess = (audioProcessingEvent) => {
  // …
};

Thông số kỹ thuật

This feature does not appear to be defined in any specification.

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

Xem thêm