Ladies and Gentlemen, the question arose, is it possible to create sounds on JS? I remember as a child, I wrote code on the basic-ке , when IBM 386 or IBM 486 was still a dream of my generation)))) .... So the basic operator was BEEP . Question: is there any analogue of this operator on JS?

  • About 80386 and 80486, a classic. - LFC
  • YES, YES_YES ....)))) - Air
  • Yes, as time flies, just yesterday, 80386 was a dream, and now quantum processors are already testing. - LFC
  • I'm afraid to imagine the possibilities)))) - Air

2 answers 2

I see two options

  1. Add <audio>
  2. Use the Web Audio API

The first option will allow you to play pre-recorded tracks. The second will also allow you to generate sounds.

Example from MDN:

 // создаем аудио контекст var audioCtx = new(window.AudioContext || window.webkitAudioContext)(); // создаем OscillatorNode - генератор var oscillator = audioCtx.createOscillator(); oscillator.type = 'square'; // задаем частоту в герцах oscillator.frequency.setValueAtTime(440, audioCtx.currentTime); oscillator.connect(audioCtx.destination); // запускаем пищалку oscillator.start(); // говорим "горшочек не вари" через 300 мс setTimeout(e => oscillator.stop(), 300); 

See also:

  1. Making Music in the Browser - Web Audio API, Part 1 (in English)
  2. Violent Theremin demo (demo)
  3. OscillatorNode (in English)
  • tutankhamun, searched, did not find, be a friend or a link or a piece of code - Air
  • not to reproduce, namely to create - Air
  • Then the first option will not work. - tutankhamun
  • Well, then just create on the basis of existing files. - nick_n_a pm
  • tutankhamun, it does not matter, but what about the 2nd option? - Air

So:

 <audio id="beep" src="https://www.soundjay.com/button/beep-01a.wav" autostart="false" > </audio> <a onclick="playSound();">Click me to hear a beep</a> <script> function playSound() { var sound = document.getElementById("beep"); sound.play(); } </script> 

  • Read the question carefully ..... - Air
  • one
    And yet, who taught you, hang a link on a link?)))))) Remember, the <a> tag is a link ... And use it as a link .... - Air