[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/API/Web_Audio_API/Using_Web_Audio_API [Back]  [Original]

Web Audio API - Web API | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Web Audio API

Web Audio API . , , , .

<canvas> <img> , Web Audio API <audio> . . <audio> Web Audio API . , Web Audio API .

Web Audio API " (sound call limitation)" . , 32 64 . 1,000 .

In this article

:

, ,      [, , ]

, . , .

Codepen , GitHub .

Web Audio API . API , .

Web Audio API , .

Web Audio API , (modular routing) . , . , , () .

. , .

Web Audio API . API .

js
//   
const AudioContext = window.AudioContext || window.webkitAudioContext;

const audioContext = new AudioContext();

? BaseAudioContext . .

, . API . , . , , DOM . <audio> .

html
<audio src="myCoolTrack.mp3"></audio>

Web Audio API . AudioContext.createMediaElementSource:

js
//   
const audioElement = document.querySelector("audio");

//     
const track = audioContext.createMediaElementSource(audioElement);

:

<audio> HTMLMediaElement DOM , .

. Web Audio API .

, . , , /, , .

JavaScript , ( ) . .

, . Web Audio API .

( ) , . , . :

html
<button data-playing="false" role="switch" aria-checked="false">
  <span>Play/Pause</span>
</button>

/ .

API . , , BaseAudioContext.destination .

js
track.connect(audioContext.destination);

. :

        [ ]

.

js
//   
const playButton = document.querySelector("button");

playButton.addEventListener(
  "click",
  function () {
    //  (suspended)    (  )
    if (audioContext.state === "suspended") {
      audioContext.resume();
    }

    //     
    if (this.dataset.playing === "false") {
      audioElement.play();
      this.dataset.playing = "true";
    } else if (this.dataset.playing === "true") {
      audioElement.pause();
      this.dataset.playing = "false";
    }
  },
  false,
);

. HTMLMediaElement ended , .

js
audioElement.addEventListener(
  "ended",
  () => {
    playButton.dataset.playing = "false";
  },
  false,
);

, . Web Audio API . , . GainNode , .

Web Audio API . (: audioContext.createGain()) (constructor) (: new GainNode()). .

js
const gainNode = audioContext.createGain();

gain , gain :

js
track.connect(gainNode).connect(audioContext.destination);

:

   gain  ,           [ gain , ]

gain 1; . gain -3.4 3.4 . gain 2 ( 2) 0 ( ) .

:

html
<input type="range" id="volume" min="0" max="2" value="1" step="0.01" />

: . .

gain :

js
const volumeControl = document.querySelector("#volume");

volumeControl.addEventListener(
  "input",
  function () {
    gainNode.gain.value = this.value;
  },
  false,
);

: (: GainNode.gain) . AudioParam . gain , GainNode.gain value . , .

, ! gain .

.

StereoPannerNode , , .

:

StereoPannerNode . PannerNode , , 3D , . 3D .

, .

 ,     (gain  stereo panner )        [ , (gain stereo panner ) ]

. , .

js
const pannerOptions = { pan: 0 };
const panner = new StereoPannerNode(audioContext, pannerOptions);

:

. .

-1 () 1 (). :

html
<input type="range" id="panner" min="-1" max="1" value="0" step="0.01" />

:

js
const pannerControl = document.querySelector("#panner");

pannerControl.addEventListener(
  "input",
  function () {
    panner.pan.value = this.value;
  },
  false,
);

, :

js
track.connect(gainNode).connect(panner).connect(audioContext.destination);

. Codepen .

! '' , , .

. Web Audio API , , .

Web Audio API .

Voice-change-O-matic . , Web Audio API . (Voice-change-O-matic ).

,         UI [, UI]

Web Audio API Violent Theremin, (pitch) . (Violent Theremin ).

       ,      [ , ]

webaudio-examples repo .


Web Proxy Viewer  |  New URL  |  Original Page