Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Javascript

สร้างเสียงได้ทันทีด้วย JavaScript/ HTML5


Web Audio API ใช้เพื่อควบคุมเสียง ซึ่งช่วยให้คุณสามารถเลือกแหล่งที่มาของเสียงได้ คุณยังสามารถเพิ่มเอฟเฟกต์ สร้างภาพและเสียง การแพนกล้อง ฯลฯ

ตัวอย่าง

คุณสามารถลองเรียกใช้ข้อมูลโค้ดต่อไปนี้เพื่อสร้างเสียง -

// use one context per document. Here we are creating one context for one document. You can create for other documents also
var context = new (window.AudioContext || window.webkitAudioContext)();

// oscillator
var os = context.createOscillator();  
os.type = 'sine'; // sine is the default. So you can also use square, saw tooth, triangle
os.frequency.value = 500; // setting the frequency Hz
os.connect(context.destination); // connecting  to the destination

// starting the oscillator
os.start();  
os.stop(context.currentTime + 5); // stop 5 seconds after the current time