AudioProcessingEvent

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.

Giao diện AudioProcessingEvent của Web Audio API biểu diễn các sự kiện xảy ra khi bộ đệm đầu vào của một ScriptProcessorNode sẵn sàng để được xử lý.

Một sự kiện audioprocess với giao diện này sẽ được kích hoạt trên một ScriptProcessorNode khi cần xử lý âm thanh. Trong quá trình xử lý âm thanh, bộ đệm đầu vào được đọc và xử lý để tạo ra dữ liệu âm thanh đầu ra, sau đó dữ liệu này được ghi vào bộ đệm đầu ra.

Warning: Tính năng này đã bị loại bỏ và nên được thay thế bằng AudioWorklet.

Event AudioProcessingEvent

Bộ khởi tạo

AudioProcessingEvent() Deprecated

Tạo một đối tượng AudioProcessingEvent mới.

Thuộc tính thể hiện

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

playbackTime Read only Deprecated

Một số thực kép biểu diễn thời điểm âm thanh sẽ được phát, như được xác định bởi thời gian của AudioContext.currentTime.

inputBuffer Read only Deprecated

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 bởi tham số numberOfInputChannels của phương thức factory 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 Deprecated

Một AudioBuffer là bộ đệm nơi dữ liệu âm thanh đầu ra cần được ghi vào. Số lượng kênh được xác định bởi tham số numberOfOutputChannels của phương thức factory 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ụ

Thêm nhiễu trắng bằng script processor

Ví dụ sau cho thấy cách dùng ScriptProcessorNode để lấy một track được tải bằng AudioContext.decodeAudioData(), xử lý nó bằng cách thêm một ít nhiễu trắng vào từng mẫu âm thanh của track đầu vào (bộ đệm) và phát nó qua AudioDestinationNode. Với mỗi kênh và mỗi khung mẫu, hàm scriptNode.onaudioprocess nhận audioProcessingEvent tương ứng và dùng nó để lặp qua từng kênh của bộ đệm đầu vào, và từng mẫu trong mỗi kênh, rồi thêm một lượng nhỏ nhiễu trắng, trước khi đặt kết quả đó làm mẫu đầu ra trong từng trường hợp.

Note: Để xem ví dụ đầy đủ có thể chạy được, hãy xem kho GitHub script-processor-node của chúng tôi. (Bạn cũng có thể truy cập mã nguồn.)

js
const myScript = document.querySelector("script");
const myPre = document.querySelector("pre");
const playButton = document.querySelector("button");

// Tạo AudioContext và nguồn bộ đệm
let audioCtx;

async function init() {
  audioCtx = new AudioContext();
  const source = audioCtx.createBufferSource();

  // Tạo một ScriptProcessorNode với bufferSize là 4096 và
  // một kênh đầu vào và một kênh đầu ra
  const scriptNode = audioCtx.createScriptProcessor(4096, 1, 1);

  // Tải một track âm thanh bằng fetch() và decodeAudioData()
  try {
    const response = await fetch("viper.ogg");
    const arrayBuffer = await response.arrayBuffer();
    source.buffer = await audioCtx.decodeAudioData(arrayBuffer);
  } catch (err) {
    console.error(
      `Unable to fetch the audio file: ${name} Error: ${err.message}`,
    );
  }

  // Gán cho node một hàm để xử lý các sự kiện âm thanh
  scriptNode.addEventListener("audioprocess", (audioProcessingEvent) => {
    // Bộ đệm đầu vào là bài hát chúng ta đã tải trước đó
    let inputBuffer = audioProcessingEvent.inputBuffer;

    // Bộ đệm đầu ra chứa các mẫu sẽ được chỉnh sửa
    // và phát
    let 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++) {
      let inputData = inputBuffer.getChannelData(channel);
      let outputData = outputBuffer.getChannelData(channel);

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

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

  source.connect(scriptNode);
  scriptNode.connect(audioCtx.destination);
  source.start();

  // Khi nguồn bộ đệm dừng phát, ngắt kết nối mọi thứ
  source.addEventListener("ended", () => {
    source.disconnect(scriptNode);
    scriptNode.disconnect(audioCtx.destination);
  });
}

// nối nút phát
playButton.addEventListener("click", () => {
  if (!audioCtx) {
    init();
  }
});

Thông số kỹ thuật

Specification
Web Audio API
# dom-audioprocessingevent-outputbuffer
Web Audio API
# dom-audioprocessingevent-audioprocessingevent
Web Audio API
# dom-audioprocessingevent-inputbuffer
Web Audio API
# dom-audioprocessingevent-playbacktime

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

Xem thêm