-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add getPeaks and cleanup methods (#50)
- Loading branch information
1 parent
759c119
commit 1a9e835
Showing
18 changed files
with
455 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Dynamic creation and cleanup</title> | ||
<link href="/web-audio-peak-meter/docs.css" rel="stylesheet"> | ||
</head> | ||
<body> | ||
<h1 id="dynamic-creation-and-cleanup">Dynamic creation and cleanup</h1> | ||
<p>Sometimes You only need a peak meter for some of the time that the page is loaded. For those cases, a peak meter instance can by dynamically created and cleaned up.</p> | ||
|
||
<h2>Working Example</h2> | ||
<p> | ||
The web audio API context is <span id="ctx-status">loading</span>. | ||
<button id="ctx-button">Loading</button> | ||
</p> | ||
|
||
<audio id="the-audio" preload="metadata" crossorigin="anonymous" controls="controls"> | ||
<source src="https://assets.rpy.xyz/testmedia/semper_fidelis.mp3" type="audio/mpeg" /> | ||
</audio> | ||
<div> | ||
<button id="meter-toggle">Create meter</button> | ||
</div> | ||
<div id="peak-meter" style="height: 80px"></div> | ||
|
||
<h2>HTML code</h2> | ||
<pre class="code-block"><code><p> | ||
The web audio API context is <span id="ctx-status">loading</span>. | ||
<button id="ctx-button">Loading</button> | ||
</p> | ||
|
||
<audio id="the-audio" preload="metadata" crossorigin="anonymous" controls="controls"> | ||
<source src="https://assets.rpy.xyz/testmedia/semper_fidelis.mp3" type="audio/mpeg" /> | ||
</audio> | ||
<div> | ||
<button id="meter-toggle">Create meter</button> | ||
</div> | ||
<div id="peak-meter" style="height: 80px"></div> | ||
</code></pre> | ||
<h2>Javascript code</h2> | ||
<pre class="code-block"><code>const audioCtx = new AudioContext(); | ||
const audioElement = document.getElementById('the-audio'); | ||
const meterElement = document.getElementById('peak-meter'); | ||
const meterToggle = document.getElementById('meter-toggle'); | ||
|
||
const sourceNode = audioCtx.createMediaElementSource(audioElement); | ||
sourceNode.connect(audioCtx.destination); | ||
|
||
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(() => { | ||
ctxStatus.innerText = audioCtx.state; | ||
if (audioCtx.state === 'suspended') { | ||
buttonElement.innerText = 'Resume'; | ||
} else { | ||
buttonElement.innerText = 'Suspend'; | ||
} | ||
}, 100); | ||
|
||
let meterInstance = null; | ||
|
||
meterToggle.addEventListener('click', () => { | ||
if (meterInstance) { | ||
meterInstance.cleanup(); | ||
meterInstance = null; | ||
meterToggle.innerText = 'Create meter'; | ||
} else { | ||
meterInstance = new webAudioPeakMeter.WebAudioPeakMeter(sourceNode, meterElement); | ||
meterToggle.innerText = 'Delete meter'; | ||
} | ||
}); | ||
</code></pre> | ||
<script src="/web-audio-peak-meter/web-audio-peak-meter-3.1.0.min.js"></script> | ||
<script> | ||
const audioCtx = new AudioContext(); | ||
const audioElement = document.getElementById('the-audio'); | ||
const meterElement = document.getElementById('peak-meter'); | ||
const meterToggle = document.getElementById('meter-toggle'); | ||
|
||
const sourceNode = audioCtx.createMediaElementSource(audioElement); | ||
sourceNode.connect(audioCtx.destination); | ||
|
||
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(() => { | ||
ctxStatus.innerText = audioCtx.state; | ||
if (audioCtx.state === 'suspended') { | ||
buttonElement.innerText = 'Resume'; | ||
} else { | ||
buttonElement.innerText = 'Suspend'; | ||
} | ||
}, 100); | ||
|
||
let meterInstance = null; | ||
|
||
meterToggle.addEventListener('click', () => { | ||
if (meterInstance) { | ||
meterInstance.cleanup(); | ||
meterInstance = null; | ||
meterToggle.innerText = 'Create meter'; | ||
} else { | ||
meterInstance = new webAudioPeakMeter.WebAudioPeakMeter(sourceNode, meterElement); | ||
meterToggle.innerText = 'Delete meter'; | ||
} | ||
}); | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Usage without a DOM node</title> | ||
<link href="/web-audio-peak-meter/docs.css" rel="stylesheet"> | ||
</head> | ||
<body> | ||
<h1 id="usage-without-a-dom-node">Usage without a DOM node</h1> | ||
<p>For users who desire the metering functionality of this library but not the visual presentation, it’s possible to create an instance without providing a DOM node as a second argument.</p> | ||
<p>To get the peaks at the current moment, as well as the maximum values for each channel, since the last time <code>clearPeaks()</code> was called, call the instance’s <code>getPeaks()</code> method.</p> | ||
|
||
<h2>Working Example</h2> | ||
<p> | ||
The web audio API context is <span id="ctx-status">loading</span>. | ||
<button id="ctx-button">Loading</button> | ||
</p> | ||
|
||
<audio id="the-audio" preload="metadata" crossorigin="anonymous" controls="controls"> | ||
<source src="https://assets.rpy.xyz/testmedia/semper_fidelis.mp3" type="audio/mpeg" /> | ||
</audio> | ||
<div> | ||
<button id="clear-peaks">Clear Peaks</button> | ||
<button id="get-peaks">Get Peaks</button> | ||
</div> | ||
<ul> | ||
<li>Current peaks (floating point): <span id="current-float"></span></li> | ||
<li>Current peaks (decibels): <span id="current-db"></span></li> | ||
<li>Channel maxes (floating point): <span id="maxes-float"></span></li> | ||
<li>Channel maxes (decibels): <span id="maxes-db"></span></li> | ||
</ul> | ||
|
||
<h2>HTML code</h2> | ||
<pre class="code-block"><code><p> | ||
The web audio API context is <span id="ctx-status">loading</span>. | ||
<button id="ctx-button">Loading</button> | ||
</p> | ||
|
||
<audio id="the-audio" preload="metadata" crossorigin="anonymous" controls="controls"> | ||
<source src="https://assets.rpy.xyz/testmedia/semper_fidelis.mp3" type="audio/mpeg" /> | ||
</audio> | ||
<div> | ||
<button id="clear-peaks">Clear Peaks</button> | ||
<button id="get-peaks">Get Peaks</button> | ||
</div> | ||
<ul> | ||
<li>Current peaks (floating point): <span id="current-float"></span></li> | ||
<li>Current peaks (decibels): <span id="current-db"></span></li> | ||
<li>Channel maxes (floating point): <span id="maxes-float"></span></li> | ||
<li>Channel maxes (decibels): <span id="maxes-db"></span></li> | ||
</ul> | ||
</code></pre> | ||
<h2>Javascript code</h2> | ||
<pre class="code-block"><code>const audioCtx = new AudioContext(); | ||
const audioElement = document.getElementById('the-audio'); | ||
const clearPeaks = document.getElementById('clear-peaks'); | ||
const getPeaks = document.getElementById('get-peaks'); | ||
const currentFloat = document.getElementById('current-float'); | ||
const currentDB = document.getElementById('current-db'); | ||
const maxesFloat = document.getElementById('maxes-float'); | ||
const maxesDB = document.getElementById('maxes-db'); | ||
|
||
const sourceNode = audioCtx.createMediaElementSource(audioElement); | ||
sourceNode.connect(audioCtx.destination); | ||
|
||
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(() => { | ||
ctxStatus.innerText = audioCtx.state; | ||
if (audioCtx.state === 'suspended') { | ||
buttonElement.innerText = 'Resume'; | ||
} else { | ||
buttonElement.innerText = 'Suspend'; | ||
} | ||
}, 100); | ||
|
||
const meterInstance = new webAudioPeakMeter.WebAudioPeakMeter(sourceNode); | ||
|
||
clearPeaks.addEventListener('click', () => { | ||
meterInstance.clearPeaks(); | ||
}); | ||
|
||
const displayFloatArray = (arr) => arr.map((val) => val.toFixed(2)).join(', '); | ||
|
||
getPeaks.addEventListener('click', () => { | ||
const peaks = meterInstance.getPeaks(); | ||
currentFloat.innerText = displayFloatArray(peaks.current); | ||
currentDB.innerText = displayFloatArray(peaks.currentDB); | ||
maxesFloat.innerText = displayFloatArray(peaks.maxes); | ||
maxesDB.innerText = displayFloatArray(peaks.maxesDB); | ||
}); | ||
</code></pre> | ||
<script src="/web-audio-peak-meter/web-audio-peak-meter-3.1.0.min.js"></script> | ||
<script> | ||
const audioCtx = new AudioContext(); | ||
const audioElement = document.getElementById('the-audio'); | ||
const clearPeaks = document.getElementById('clear-peaks'); | ||
const getPeaks = document.getElementById('get-peaks'); | ||
const currentFloat = document.getElementById('current-float'); | ||
const currentDB = document.getElementById('current-db'); | ||
const maxesFloat = document.getElementById('maxes-float'); | ||
const maxesDB = document.getElementById('maxes-db'); | ||
|
||
const sourceNode = audioCtx.createMediaElementSource(audioElement); | ||
sourceNode.connect(audioCtx.destination); | ||
|
||
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(() => { | ||
ctxStatus.innerText = audioCtx.state; | ||
if (audioCtx.state === 'suspended') { | ||
buttonElement.innerText = 'Resume'; | ||
} else { | ||
buttonElement.innerText = 'Suspend'; | ||
} | ||
}, 100); | ||
|
||
const meterInstance = new webAudioPeakMeter.WebAudioPeakMeter(sourceNode); | ||
|
||
clearPeaks.addEventListener('click', () => { | ||
meterInstance.clearPeaks(); | ||
}); | ||
|
||
const displayFloatArray = (arr) => arr.map((val) => val.toFixed(2)).join(', '); | ||
|
||
getPeaks.addEventListener('click', () => { | ||
const peaks = meterInstance.getPeaks(); | ||
currentFloat.innerText = displayFloatArray(peaks.current); | ||
currentDB.innerText = displayFloatArray(peaks.currentDB); | ||
maxesFloat.innerText = displayFloatArray(peaks.maxes); | ||
maxesDB.innerText = displayFloatArray(peaks.maxesDB); | ||
}); | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.