Skip to content

Commit

Permalink
タイミング再調整
Browse files Browse the repository at this point in the history
  • Loading branch information
X-20A committed Nov 24, 2024
1 parent 1f1e902 commit ded8c10
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
8 changes: 8 additions & 0 deletions src/components/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -97,6 +98,13 @@ watchEffect(() => {
setThemeToCss(theme);
});
// 再生デバイスの初期化と変更の監視
watchEffect(() => {
applyDeviceId(store.state.savingSetting.audioOutputDevice).catch((e) => {
console.error(e);

Check warning on line 104 in src/components/App.vue

View workflow job for this annotation

GitHub Actions / build-test

Unexpected console statement
});
});
// ソフトウェアを初期化
const { hotkeyManager } = useHotkeyManager();
const isEnginesReady = ref(false);
Expand Down
35 changes: 2 additions & 33 deletions src/sing/audioRendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
linearToDecibel,
} from "@/sing/domain";
import { Timer } from "@/sing/utility";
import { showAlertDialog } from "@/components/Dialog/Dialog";

const getEarliestSchedulableContextTime = (audioContext: BaseAudioContext) => {
const renderQuantumSize = 128;
Expand Down Expand Up @@ -94,21 +93,6 @@ export class Transport {
this._time = value;
}
}
set sinkId(device: string) {
device = device ? device : "";
if (this.audioContext.setSinkId) {
this.audioContext
.setSinkId(device === "default" ? "" : device)
.catch((err: unknown) => {
void showAlertDialog({
type: "error",
title: "エラー",
message: "再生デバイスが見つかりません",
});
throw err;
});
}
}

/**
* @param audioContext 音声コンテキスト
Expand Down Expand Up @@ -782,7 +766,7 @@ export type PolySynthOptions = {
* ポリフォニックシンセサイザーです。
*/
export class PolySynth implements Instrument {
private readonly audioContext: AudioContext;
private readonly audioContext: BaseAudioContext;
private readonly gainNode: GainNode;
private readonly oscParams: SynthOscParams;
private readonly filterParams: SynthFilterParams;
Expand All @@ -793,23 +777,8 @@ export class PolySynth implements Instrument {
get output(): AudioNode {
return this.gainNode;
}
set sinkId(device: string) {
device = device ? device : "";
if (this.audioContext.setSinkId) {
this.audioContext
.setSinkId(device === "default" ? "" : device)
.catch((err: unknown) => {
void showAlertDialog({
type: "error",
title: "エラー",
message: "再生デバイスが見つかりません",
});
throw err;
});
}
}

constructor(audioContext: AudioContext, options?: PolySynthOptions) {
constructor(audioContext: BaseAudioContext, options?: PolySynthOptions) {
this.audioContext = audioContext;
this.oscParams = options?.osc ?? {
type: "square",
Expand Down
25 changes: 15 additions & 10 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -753,10 +754,18 @@ const getSelectedTrackWithFallback = (partialState: {
return getOrThrow(partialState.tracks, partialState._selectedTrackId);
};

const applyDeviceId = (device: string) => {
if (transport && previewSynth) {
transport.sinkId = device;
previewSynth.sinkId = device;
// 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;
});
}
};

Expand Down Expand Up @@ -1464,11 +1473,10 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
},

SET_PLAYHEAD_POSITION: {
async action({ state, getters }, { position }: { position: number }) {
async action({ getters }, { position }: { position: number }) {
if (!transport) {
throw new Error("transport is undefined.");
}
applyDeviceId(state.savingSetting.audioOutputDevice);
playheadPosition.value = position;
transport.time = getters.TICK_TO_SECOND(position);
},
Expand All @@ -1490,8 +1498,6 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
}
mutations.SET_PLAYBACK_STATE({ nowPlaying: true });

applyDeviceId(state.savingSetting.audioOutputDevice);

transport.start();
animationTimer.start(() => {
playheadPosition.value = getters.SECOND_TO_TICK(transport.time);
Expand Down Expand Up @@ -1531,7 +1537,7 @@ export const singingStore = createPartialStore<SingingStoreTypes>({

PLAY_PREVIEW_SOUND: {
async action(
{ state },
_,
{ noteNumber, duration }: { noteNumber: number; duration?: number },
) {
if (!audioContext) {
Expand All @@ -1540,7 +1546,6 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
if (!previewSynth) {
throw new Error("previewSynth is undefined.");
}
applyDeviceId(state.savingSetting.audioOutputDevice);
previewSynth.noteOn("immediately", noteNumber, duration);
},
},
Expand Down

0 comments on commit ded8c10

Please sign in to comment.