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-15 18:34:11 +02:00
|
|
|
let timeout: ReturnType<typeof setTimeout>;
|
2021-06-01 16:17:36 +02:00
|
|
|
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{
|
2021-06-11 11:29:36 +02:00
|
|
|
volume = soundMeter.getVolume();
|
2021-06-01 16:17:36 +02:00
|
|
|
//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 > 5}></span>
|
2021-06-11 11:29:36 +02:00
|
|
|
<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>
|