Skip to content

Commit

Permalink
audio: visualize audio level through getSynchronizationSources
Browse files Browse the repository at this point in the history
  • Loading branch information
fippo committed Oct 7, 2020
1 parent ef05891 commit 0c541c0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/content/peerconnection/audio/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ <h1><a href="//webrtc.github.io/samples/" title="WebRTC samples homepage">WebRTC
<div>Packets sent per second</div>
<canvas id="packetCanvas"></canvas>
</div>
<div class="graph-container" id="audioLevelGraph">
<div>average audio level ([0..1])</div>
<canvas id="audioLevelCanvas"></canvas>
</div>

<a href="https://github.com/webrtc/samples/tree/gh-pages/src/content/peerconnection/audio"
title="View source for this page on GitHub" id="viewSource">View source on GitHub</a>
Expand Down
38 changes: 38 additions & 0 deletions src/content/peerconnection/audio/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const offerOptions = {
voiceActivityDetection: false
};

const audioLevels = [];
let audioLevelGraph;
let audioLevelSeries;

// Enabling opus DTX is an expert option without GUI.
// eslint-disable-next-line prefer-const
let useDtx = false;
Expand Down Expand Up @@ -123,6 +127,10 @@ function gotStream(stream) {
packetSeries = new TimelineDataSeries();
packetGraph = new TimelineGraphView('packetGraph', 'packetCanvas');
packetGraph.updateEndDate();

audioLevelSeries = new TimelineDataSeries();
audioLevelGraph = new TimelineGraphView('audioLevelGraph', 'audioLevelCanvas');
audioLevelGraph.updateEndDate();
}

function onCreateSessionDescriptionError(error) {
Expand Down Expand Up @@ -359,3 +367,33 @@ window.setInterval(() => {
lastResult = res;
});
}, 1000);

if (window.RTCRtpReceiver && ('getSynchronizationSources' in window.RTCRtpReceiver.prototype)) {
let lastTime;
const getAudioLevel = (timestamp) => {
window.requestAnimationFrame(getAudioLevel);
if (!pc2) {
return;
}
const receiver = pc2.getReceivers().find(r => r.track.kind === 'audio');
if (!receiver) {
return;
}
const sources = receiver.getSynchronizationSources();
sources.forEach(source => {
audioLevels.push(source.audioLevel);
});
if (!lastTime) {
lastTime = timestamp;
} else if (timestamp - lastTime > 500 && audioLevels.length > 0) {
// Update graph every 500ms.
const maxAudioLevel = Math.max.apply(null, audioLevels);
audioLevelSeries.addPoint(Date.now(), maxAudioLevel);
audioLevelGraph.setDataSeries([audioLevelSeries]);
audioLevelGraph.updateEndDate();
audioLevels.length = 0;
lastTime = timestamp;
}
};
window.requestAnimationFrame(getAudioLevel);
}

0 comments on commit 0c541c0

Please sign in to comment.