partey_workadventure/front/src/Components/SoundMeterWidget.svelte

52 lines
1.3 KiB
Svelte
Raw Normal View History

2021-05-28 15:48:58 +02:00
<script lang="typescript">
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-15 18:34:11 +02:00
let timeout: ReturnType<typeof setTimeout>;
2021-06-01 16:17:36 +02:00
const soundMeter = new SoundMeter();
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) {
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 = soundMeter.getVolume();
2021-06-01 16:17:36 +02:00
//console.log(volume);
}catch(err){
}
}, 100);
} 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>
<div class="sound-progress" class:active={display}>
2021-05-28 15:48:58 +02:00
<span class:active={volume > 5}></span>
<span class:active={volume > 10}></span>
<span class:active={volume > 15}></span>
<span class:active={volume > 40}></span>
<span class:active={volume > 70}></span>
2021-05-28 15:48:58 +02:00
</div>