BaseAudioContext: phương thức createOscillator()

Baseline Widely available

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

Phương thức createOscillator() của BaseAudioContext tạo một OscillatorNode, một nguồn biểu diễn một dạng sóng tuần hoàn. Về cơ bản, nó tạo ra một âm sắc không đổi.

Note: Hàm dựng OscillatorNode() là cách được khuyến nghị để tạo một OscillatorNode; xem Tạo một AudioNode.

Cú pháp

js
createOscillator()

Tham số

Không có.

Giá trị trả về

Một OscillatorNode.

Ví dụ

Ví dụ sau cho thấy cách dùng cơ bản một AudioContext để tạo một oscillator node. Để xem ví dụ/thông tin ứng dụng, hãy xem bản demo Violent Theremin của chúng tôi (xem app.js để biết phần mã liên quan); đồng thời xem trang OscillatorNode để biết thêm thông tin.

js
// tạo ngữ cảnh web audio api
const audioCtx = new AudioContext();

// tạo Oscillator node
const oscillator = audioCtx.createOscillator();

oscillator.type = "square";
oscillator.frequency.setValueAtTime(3000, audioCtx.currentTime); // giá trị tính bằng hertz
oscillator.connect(audioCtx.destination);
oscillator.start();

Thông số kỹ thuật

Specification
Web Audio API
# dom-baseaudiocontext-createoscillator

Khả năng tương thích với trình duyệt

Xem thêm