2021-05-28 15:48:58 +02:00
|
|
|
<script lang="typescript">
|
2021-06-02 17:58:58 +02:00
|
|
|
import { AudioContext } from 'standardized-audio-context';
|
2021-05-28 15:48:58 +02:00
|
|
|
import {SoundMeter} from "../Phaser/Components/SoundMeter";
|
|
|
|
import {onDestroy} from "svelte";
|
|
|
|
|
2021-06-01 16:17:36 +02:00
|
|
|
export let stream: MediaStream|null;
|
2021-05-28 15:48:58 +02:00
|
|
|
let volume = 0;
|
|
|
|
|
2021-06-01 16:17:36 +02:00
|
|
|
const NB_BARS = 5;
|
2021-05-28 15:48:58 +02:00
|
|
|
|
2021-06-01 16:17:36 +02:00
|
|
|
let timeout;
|
|
|
|
const soundMeter = new SoundMeter();
|
2021-06-03 10:17:38 +02:00
|
|
|
let display = false;
|
2021-05-28 15:48:58 +02:00
|
|
|
|
2021-06-01 16:17:36 +02:00
|
|
|
$: {
|
|
|
|
if (stream && stream.getAudioTracks().length > 0) {
|
2021-06-03 10:17:38 +02:00
|
|
|
display = true;
|
2021-06-01 16:17:36 +02:00
|
|
|
soundMeter.connectToSource(stream, new AudioContext());
|
2021-05-28 15:48:58 +02:00
|
|
|
|
2021-06-01 16:17:36 +02:00
|
|
|
if (timeout) {
|
|
|
|
clearInterval(timeout);
|
2021-05-28 15:48:58 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 16:17:36 +02:00
|
|
|
timeout = setInterval(() => {
|
|
|
|
try{
|
|
|
|
volume = parseInt((soundMeter.getVolume() / 100 * NB_BARS).toFixed(0));
|
|
|
|
//console.log(volume);
|
|
|
|
}catch(err){
|
|
|
|
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
|
2021-06-03 10:17:38 +02:00
|
|
|
} else {
|
|
|
|
display = false;
|
2021-06-01 16:17:36 +02:00
|
|
|
}
|
2021-05-28 15:48:58 +02:00
|
|
|
}
|
2021-06-01 16:17:36 +02:00
|
|
|
|
|
|
|
onDestroy(() => {
|
|
|
|
soundMeter.stop();
|
|
|
|
if (timeout) {
|
|
|
|
clearInterval(timeout);
|
|
|
|
}
|
|
|
|
})
|
2021-05-28 15:48:58 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
2021-06-03 10:17:38 +02:00
|
|
|
<div class="sound-progress" class:active={display}>
|
2021-05-28 15:48:58 +02:00
|
|
|
<span class:active={volume > 1}></span>
|
|
|
|
<span class:active={volume > 2}></span>
|
|
|
|
<span class:active={volume > 3}></span>
|
|
|
|
<span class:active={volume > 4}></span>
|
|
|
|
<span class:active={volume > 5}></span>
|
|
|
|
</div>
|