Skip to content

Commit

Permalink
Fix examples for Safari and add pre-fader example (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
esonderegger authored Oct 31, 2024
1 parent 1a9e835 commit 896d984
Show file tree
Hide file tree
Showing 18 changed files with 383 additions and 150 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Customizable peak meters, using the web audio API. It can measure peak or true p
- [Single audio element](https://esonderegger.github.io/web-audio-peak-meter/examples/audio.html)
- [Single video element](https://esonderegger.github.io/web-audio-peak-meter/examples/video.html)
- [An oscillator node](https://esonderegger.github.io/web-audio-peak-meter/examples/osc.html)
- [Pre-fader metering](https://esonderegger.github.io/web-audio-peak-meter/examples/pre-fader-video.html)
- [Variations using configuration](https://esonderegger.github.io/web-audio-peak-meter/examples/variations.html)
- [Dynamic creation and cleanup](https://esonderegger.github.io/web-audio-peak-meter/examples/cleanup.html)
- [Usage without a DOM node](https://esonderegger.github.io/web-audio-peak-meter/examples/nodom.html)
Expand Down
46 changes: 26 additions & 20 deletions docs/examples/audio.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,27 @@ <h2>Javascript code</h2>
const sourceNode = audioCtx.createMediaElementSource(audioElement);
sourceNode.connect(audioCtx.destination);

const ctxStatus = document.getElementById(&#039;ctx-status&#039;);
const buttonElement = document.getElementById(&#039;ctx-button&#039;);
buttonElement.addEventListener(&#039;click&#039;, () =&gt; {
if (audioCtx.state === &#039;suspended&#039;) {
audioCtx.resume();
} else {
audioCtx.suspend();
}
});

const ctxStatus = document.getElementById(&#039;ctx-status&#039;);
setInterval(() =&gt; {
function updateAudioCtxStatus() {
ctxStatus.innerText = audioCtx.state;
if (audioCtx.state === &#039;suspended&#039;) {
buttonElement.innerText = &#039;Resume&#039;;
} else {
buttonElement.innerText = &#039;Suspend&#039;;
}
}, 100);
}

setInterval(updateAudioCtxStatus, 1000);

buttonElement.addEventListener(&#039;click&#039;, () =&gt; {
if (audioCtx.state === &#039;suspended&#039;) {
audioCtx.resume().then(updateAudioCtxStatus);
} else {
audioCtx.suspend().then(updateAudioCtxStatus);
}
});

const test = new webAudioPeakMeter.WebAudioPeakMeter(sourceNode, meterElement);
</code></pre>
Expand All @@ -63,24 +66,27 @@ <h2>Javascript code</h2>
const sourceNode = audioCtx.createMediaElementSource(audioElement);
sourceNode.connect(audioCtx.destination);

const ctxStatus = document.getElementById('ctx-status');
const buttonElement = document.getElementById('ctx-button');
buttonElement.addEventListener('click', () => {
if (audioCtx.state === 'suspended') {
audioCtx.resume();
} else {
audioCtx.suspend();
}
});

const ctxStatus = document.getElementById('ctx-status');
setInterval(() => {
function updateAudioCtxStatus() {
ctxStatus.innerText = audioCtx.state;
if (audioCtx.state === 'suspended') {
buttonElement.innerText = 'Resume';
} else {
buttonElement.innerText = 'Suspend';
}
}, 100);
}

setInterval(updateAudioCtxStatus, 1000);

buttonElement.addEventListener('click', () => {
if (audioCtx.state === 'suspended') {
audioCtx.resume().then(updateAudioCtxStatus);
} else {
audioCtx.suspend().then(updateAudioCtxStatus);
}
});

const test = new webAudioPeakMeter.WebAudioPeakMeter(sourceNode, meterElement);

Expand Down
46 changes: 26 additions & 20 deletions docs/examples/cleanup.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,27 @@ <h2>Javascript code</h2>
const sourceNode = audioCtx.createMediaElementSource(audioElement);
sourceNode.connect(audioCtx.destination);

const ctxStatus = document.getElementById(&#039;ctx-status&#039;);
const buttonElement = document.getElementById(&#039;ctx-button&#039;);
buttonElement.addEventListener(&#039;click&#039;, () =&gt; {
if (audioCtx.state === &#039;suspended&#039;) {
audioCtx.resume();
} else {
audioCtx.suspend();
}
});

const ctxStatus = document.getElementById(&#039;ctx-status&#039;);
setInterval(() =&gt; {
function updateAudioCtxStatus() {
ctxStatus.innerText = audioCtx.state;
if (audioCtx.state === &#039;suspended&#039;) {
buttonElement.innerText = &#039;Resume&#039;;
} else {
buttonElement.innerText = &#039;Suspend&#039;;
}
}, 100);
}

setInterval(updateAudioCtxStatus, 1000);

buttonElement.addEventListener(&#039;click&#039;, () =&gt; {
if (audioCtx.state === &#039;suspended&#039;) {
audioCtx.resume().then(updateAudioCtxStatus);
} else {
audioCtx.suspend().then(updateAudioCtxStatus);
}
});

let meterInstance = null;

Expand All @@ -88,24 +91,27 @@ <h2>Javascript code</h2>
const sourceNode = audioCtx.createMediaElementSource(audioElement);
sourceNode.connect(audioCtx.destination);

const ctxStatus = document.getElementById('ctx-status');
const buttonElement = document.getElementById('ctx-button');
buttonElement.addEventListener('click', () => {
if (audioCtx.state === 'suspended') {
audioCtx.resume();
} else {
audioCtx.suspend();
}
});

const ctxStatus = document.getElementById('ctx-status');
setInterval(() => {
function updateAudioCtxStatus() {
ctxStatus.innerText = audioCtx.state;
if (audioCtx.state === 'suspended') {
buttonElement.innerText = 'Resume';
} else {
buttonElement.innerText = 'Suspend';
}
}, 100);
}

setInterval(updateAudioCtxStatus, 1000);

buttonElement.addEventListener('click', () => {
if (audioCtx.state === 'suspended') {
audioCtx.resume().then(updateAudioCtxStatus);
} else {
audioCtx.suspend().then(updateAudioCtxStatus);
}
});

let meterInstance = null;

Expand Down
46 changes: 26 additions & 20 deletions docs/examples/nodom.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,27 @@ <h2>Javascript code</h2>
const sourceNode = audioCtx.createMediaElementSource(audioElement);
sourceNode.connect(audioCtx.destination);

const ctxStatus = document.getElementById(&#039;ctx-status&#039;);
const buttonElement = document.getElementById(&#039;ctx-button&#039;);
buttonElement.addEventListener(&#039;click&#039;, () =&gt; {
if (audioCtx.state === &#039;suspended&#039;) {
audioCtx.resume();
} else {
audioCtx.suspend();
}
});

const ctxStatus = document.getElementById(&#039;ctx-status&#039;);
setInterval(() =&gt; {
function updateAudioCtxStatus() {
ctxStatus.innerText = audioCtx.state;
if (audioCtx.state === &#039;suspended&#039;) {
buttonElement.innerText = &#039;Resume&#039;;
} else {
buttonElement.innerText = &#039;Suspend&#039;;
}
}, 100);
}

setInterval(updateAudioCtxStatus, 1000);

buttonElement.addEventListener(&#039;click&#039;, () =&gt; {
if (audioCtx.state === &#039;suspended&#039;) {
audioCtx.resume().then(updateAudioCtxStatus);
} else {
audioCtx.suspend().then(updateAudioCtxStatus);
}
});

const meterInstance = new webAudioPeakMeter.WebAudioPeakMeter(sourceNode);

Expand Down Expand Up @@ -112,24 +115,27 @@ <h2>Javascript code</h2>
const sourceNode = audioCtx.createMediaElementSource(audioElement);
sourceNode.connect(audioCtx.destination);

const ctxStatus = document.getElementById('ctx-status');
const buttonElement = document.getElementById('ctx-button');
buttonElement.addEventListener('click', () => {
if (audioCtx.state === 'suspended') {
audioCtx.resume();
} else {
audioCtx.suspend();
}
});

const ctxStatus = document.getElementById('ctx-status');
setInterval(() => {
function updateAudioCtxStatus() {
ctxStatus.innerText = audioCtx.state;
if (audioCtx.state === 'suspended') {
buttonElement.innerText = 'Resume';
} else {
buttonElement.innerText = 'Suspend';
}
}, 100);
}

setInterval(updateAudioCtxStatus, 1000);

buttonElement.addEventListener('click', () => {
if (audioCtx.state === 'suspended') {
audioCtx.resume().then(updateAudioCtxStatus);
} else {
audioCtx.suspend().then(updateAudioCtxStatus);
}
});

const meterInstance = new webAudioPeakMeter.WebAudioPeakMeter(sourceNode);

Expand Down
26 changes: 16 additions & 10 deletions docs/examples/osc.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,24 @@ <h2>Javascript code</h2>
<pre class="code-block"><code>const audioCtx = new AudioContext();

const ctxStatus = document.getElementById(&#039;ctx-status&#039;);
setInterval(() =&gt; {
const buttonElement = document.getElementById(&#039;ctx-button&#039;);

function updateAudioCtxStatus() {
ctxStatus.innerText = audioCtx.state;
if (audioCtx.state === &#039;suspended&#039;) {
buttonElement.innerText = &#039;Resume&#039;;
} else {
buttonElement.innerText = &#039;Suspend&#039;;
}
}, 100);
}

setInterval(updateAudioCtxStatus, 1000);

const buttonElement = document.getElementById(&#039;ctx-button&#039;);
buttonElement.addEventListener(&#039;click&#039;, () =&gt; {
if (audioCtx.state === &#039;suspended&#039;) {
audioCtx.resume();
audioCtx.resume().then(updateAudioCtxStatus);
} else {
audioCtx.suspend();
audioCtx.suspend().then(updateAudioCtxStatus);
}
});

Expand Down Expand Up @@ -90,21 +93,24 @@ <h2>Javascript code</h2>
const audioCtx = new AudioContext();

const ctxStatus = document.getElementById('ctx-status');
setInterval(() => {
const buttonElement = document.getElementById('ctx-button');

function updateAudioCtxStatus() {
ctxStatus.innerText = audioCtx.state;
if (audioCtx.state === 'suspended') {
buttonElement.innerText = 'Resume';
} else {
buttonElement.innerText = 'Suspend';
}
}, 100);
}

setInterval(updateAudioCtxStatus, 1000);

const buttonElement = document.getElementById('ctx-button');
buttonElement.addEventListener('click', () => {
if (audioCtx.state === 'suspended') {
audioCtx.resume();
audioCtx.resume().then(updateAudioCtxStatus);
} else {
audioCtx.suspend();
audioCtx.suspend().then(updateAudioCtxStatus);
}
});

Expand Down
Loading

0 comments on commit 896d984

Please sign in to comment.