-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: OBS audio volume widget (#201)
- Loading branch information
Showing
12 changed files
with
162 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ let state = { | |
recording: false, | ||
status: null, | ||
scenes: null, | ||
sources: null, | ||
currentScene: null, | ||
}; | ||
|
||
|
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
32 changes: 32 additions & 0 deletions
32
front-src/client/components/Widgets/OBS/AudioVolume/Settings.svelte
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,32 @@ | ||
<script> | ||
import { _ } from "@/libs/i18next"; | ||
import { state } from "@/stores/obs"; | ||
import { update } from "@/libs/panels"; | ||
import Select from "@/components/UI/Select.svelte"; | ||
export let data; | ||
let sources = []; | ||
let { panel, widget } = data; | ||
let none = _("words.none"); | ||
$: if ($state.sources) { | ||
sources = $state.sources.filter((source) => source.caps.hasAudio); | ||
sources = [none, ...sources.map((source) => source.name)]; | ||
} | ||
$: props = widget.component.props; | ||
function onChange(key, { detail }) { | ||
props[key] = detail === none ? null : detail; | ||
update(panel); | ||
} | ||
</script> | ||
|
||
<div class="p-2 pt-0 space-y-2 flex flex-auto flex-col"> | ||
<Select | ||
items="{sources}" | ||
value="{props.source}" | ||
label="{_('words.source')}" | ||
on:change="{onChange.bind(null, 'source')}" | ||
/> | ||
</div> |
89 changes: 89 additions & 0 deletions
89
front-src/client/components/Widgets/OBS/AudioVolume/Widget.svelte
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,89 @@ | ||
<script> | ||
import obs from "@/api/obs"; | ||
import { onMount } from "svelte"; | ||
import { _ } from "@/libs/i18next"; | ||
import actions from "@/api/actions"; | ||
import MdVolumeUp from "svelte-icons/md/MdVolumeUp.svelte"; | ||
import MdVolumeOff from "svelte-icons/md/MdVolumeOff.svelte"; | ||
import WidgetWrapper from "@/components/Widgets/OBS/WidgetWrapper.svelte"; | ||
export let widget; | ||
let state = { volume: 0.5, muted: false }; | ||
$: source = widget.component.props.source; | ||
$: icon = !state.muted ? MdVolumeUp : MdVolumeOff; | ||
$: if (source) getVolume(); | ||
function getVolume() { | ||
obs.emit("GetVolume", { source }).then((result) => (state = result)); | ||
} | ||
function db(mul) { | ||
return (Math.log10(mul) * 20).toFixed(1); | ||
} | ||
function sendAction(data) { | ||
actions.push({ type: "obs", widget, data }).catch((error) => { | ||
console.log(">>>Error:", error); | ||
}); | ||
} | ||
function onToggleAudio() { | ||
sendAction({ mute: !state.muted }); | ||
} | ||
function onVolumeChange({ target }) { | ||
sendAction({ volume: Math.pow(parseFloat(target.value), 3) }); | ||
} | ||
onMount(async () => { | ||
obs.on(`connected`, () => { | ||
getVolume(); | ||
}); | ||
obs.on(`source.volume`, ({ sourceName, volume }) => { | ||
if (sourceName === source) { | ||
state.volume = volume; | ||
} | ||
}); | ||
obs.on(`source.muted`, ({ sourceName, muted }) => { | ||
if (sourceName === source) { | ||
state.muted = muted; | ||
} | ||
}); | ||
}); | ||
</script> | ||
|
||
<WidgetWrapper widget="{widget}"> | ||
<div class="flex flex-col h-full"> | ||
{#if source} | ||
<div | ||
class="flex flex-col w-full h-full {state.muted ? 'text-red-600' : ''}" | ||
> | ||
<div class="flex p-2 gap-2"> | ||
<div class="break-words flex-auto">{source}</div> | ||
<div class="text-right">{db(state.volume)} dB</div> | ||
</div> | ||
<div class="flex p-2 gap-2 items-center"> | ||
<input | ||
type="range" | ||
min="{0}" | ||
max="1" | ||
step="0.01" | ||
class="flex-auto" | ||
value="{Math.pow(state.volume, 1 / 3)}" | ||
on:input="{onVolumeChange}" | ||
/> | ||
<div class="w-8 h-8 flex-shrink-0" on:click="{onToggleAudio}"> | ||
<svelte:component this="{icon}" /> | ||
</div> | ||
</div> | ||
</div> | ||
{:else} | ||
<div class="p-2 bg-red-600">{_('obs.no-source-selected')}</div> | ||
{/if} | ||
</div> | ||
</WidgetWrapper> |
13 changes: 13 additions & 0 deletions
13
front-src/client/components/Widgets/OBS/AudioVolume/config.js
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,13 @@ | ||
export default { | ||
name: "AudioVolume", | ||
label: "obs.audio-volume", | ||
hasTrigger: true, | ||
hasEvent: true, | ||
minSize: { | ||
w: 4, | ||
h: 3, | ||
}, | ||
props: { | ||
source: null, | ||
}, | ||
}; |
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,3 @@ | ||
export { default as Settings } from "./Settings.svelte"; | ||
export { default as Widget } from "./Widget.svelte"; | ||
export { default as config } from "./config"; |
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