diff --git a/src/components/App.vue b/src/components/App.vue index e1cc5c9a42..6718bdf19b 100644 --- a/src/components/App.vue +++ b/src/components/App.vue @@ -31,6 +31,7 @@ import SingEditor from "@/components/Sing/SingEditor.vue"; import { EngineId } from "@/type/preload"; import ErrorBoundary from "@/components/ErrorBoundary.vue"; import { useStore } from "@/store"; +import { applyDeviceId } from "@/store/singing"; import { useHotkeyManager } from "@/plugins/hotkeyPlugin"; import AllDialog from "@/components/Dialog/AllDialog.vue"; import MenuBar from "@/components/Menu/MenuBar/MenuBar.vue"; @@ -97,6 +98,13 @@ watchEffect(() => { setThemeToCss(theme); }); +// 再生デバイスの初期化と変更の監視 +watchEffect(() => { + applyDeviceId(store.state.savingSetting.audioOutputDevice).catch((e) => { + console.error(e); + }); +}); + // ソフトウェアを初期化 const { hotkeyManager } = useHotkeyManager(); const isEnginesReady = ref(false); diff --git a/src/store/singing.ts b/src/store/singing.ts index 611fe2ce22..8e0f9a96c6 100644 --- a/src/store/singing.ts +++ b/src/store/singing.ts @@ -106,6 +106,7 @@ import { uuid4 } from "@/helpers/random"; import { convertToWavFileData } from "@/sing/convertToWavFileData"; import { generateWriteErrorMessage } from "@/helpers/fileHelper"; import path from "@/helpers/path"; +import { showAlertDialog } from "@/components/Dialog/Dialog"; const logger = createLogger("store/singing"); @@ -753,6 +754,21 @@ const getSelectedTrackWithFallback = (partialState: { return getOrThrow(partialState.tracks, partialState._selectedTrackId); }; +// AudioContextに再生デバイスのIDを設定 +export const applyDeviceId = async (device: string) => { + if (audioContext) { + const sinkId = device === "default" ? "" : device; + audioContext.setSinkId(sinkId).catch((err: unknown) => { + void showAlertDialog({ + type: "error", + title: "エラー", + message: "再生デバイスが見つかりません", + }); + throw err; + }); + } +}; + export const singingStoreState: SingingStoreState = { tpqn: DEFAULT_TPQN, tempos: [createDefaultTempo(0)], diff --git a/src/type/globals.d.ts b/src/type/globals.d.ts index 6c761fa17f..9708bb15e5 100644 --- a/src/type/globals.d.ts +++ b/src/type/globals.d.ts @@ -9,6 +9,10 @@ declare global { setSinkId(deviceID: string): Promise; // setSinkIdを認識してくれないため } + interface AudioContext { + setSinkId: (sinkId: string) => Promise; + } + interface Window { readonly [SandboxKey]: import("./preload").Sandbox; }