Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
X-20A committed Nov 22, 2024
1 parent 6f91632 commit e9cfec3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
37 changes: 34 additions & 3 deletions src/sing/audioRendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 @@ -196,11 +197,26 @@ export class Transport {

/**
* 再生を開始します。すでに再生中の場合は何も行いません。
* @param device 再生させるデバイスのID、指定が無ければデフォルトで再生
*/
start() {
start(device?: string) {
if (this._state === "started") return;
const contextTime = this.audioContext.currentTime;

device = device ? device : "";
if (this.audioContext.setSinkId) {
this.audioContext
.setSinkId(device === "default" ? "" : device)
.catch((err: unknown) => {
void showAlertDialog({
type: "error",
title: "エラー",
message: "再生デバイスが見つかりません",
});
throw err;
});
}

this._state = "started";

this.startContextTime = contextTime;
Expand Down Expand Up @@ -766,7 +782,7 @@ export type PolySynthOptions = {
* ポリフォニックシンセサイザーです。
*/
export class PolySynth implements Instrument {
private readonly audioContext: BaseAudioContext;
private readonly audioContext: AudioContext;
private readonly gainNode: GainNode;
private readonly oscParams: SynthOscParams;
private readonly filterParams: SynthFilterParams;
Expand All @@ -778,7 +794,7 @@ export class PolySynth implements Instrument {
return this.gainNode;
}

constructor(audioContext: BaseAudioContext, options?: PolySynthOptions) {
constructor(audioContext: AudioContext, options?: PolySynthOptions) {
this.audioContext = audioContext;
this.oscParams = options?.osc ?? {
type: "square",
Expand Down Expand Up @@ -806,12 +822,27 @@ export class PolySynth implements Instrument {
* @param contextTime ノートオンを行う時刻(コンテキスト時刻)
* @param noteNumber MIDIノート番号
* @param duration ノートの長さ(秒)
* @param device 再生させるデバイスのID、指定が無ければデフォルトで再生
*/
noteOn(
contextTime: number | "immediately",
noteNumber: number,
duration?: number,
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;
});
}
let voice = this.voices.find((value) => {
return value.isActive && value.noteNumber === noteNumber;
});
Expand Down
11 changes: 8 additions & 3 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
}
mutations.SET_PLAYBACK_STATE({ nowPlaying: true });

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

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

Expand Down
5 changes: 5 additions & 0 deletions src/type/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ declare global {
setSinkId(deviceID: string): Promise<undefined>; // setSinkIdを認識してくれないため
}

interface AudioContext {
sinkId: string;
setSinkId: (sinkId: string) => Promise<void>;
}

interface Window {
readonly [SandboxKey]: import("./preload").Sandbox;
}
Expand Down

0 comments on commit e9cfec3

Please sign in to comment.