Skip to content

Commit

Permalink
ソング:シーケンスを作成する処理を関数化してRENDER関数の外に出す (#2275)
Browse files Browse the repository at this point in the history
シーケンスの生成処理をrender関数の外に出した
  • Loading branch information
sigprogramming authored Sep 30, 2024
1 parent 09958a6 commit f8d966f
Showing 1 changed file with 54 additions and 26 deletions.
80 changes: 54 additions & 26 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
Clipper,
Limiter,
NoteEvent,
NoteSequence,
OfflineTransport,
PolySynth,
Sequence,
Expand Down Expand Up @@ -329,6 +330,49 @@ const deleteSequence = (sequenceId: SequenceId) => {
}
};

/**
* ノートシーケンスを生成する。
*/
const generateNoteSequence = (
notes: Note[],
tempos: Tempo[],
tpqn: number,
trackId: TrackId,
): NoteSequence & { trackId: TrackId } => {
if (!audioContext) {
throw new Error("audioContext is undefined.");
}
const noteEvents = generateNoteEvents(notes, tempos, tpqn);
const polySynth = new PolySynth(audioContext);
return {
type: "note",
instrument: polySynth,
noteEvents,
trackId,
};
};

/**
* オーディオシーケンスを生成する。
*/
const generateAudioSequence = async (
startTime: number,
blob: Blob,
trackId: TrackId,
): Promise<AudioSequence & { trackId: TrackId }> => {
if (!audioContext) {
throw new Error("audioContext is undefined.");
}
const audioEvents = await generateAudioEvents(audioContext, startTime, blob);
const audioPlayer = new AudioPlayer(audioContext);
return {
type: "audio",
audioPlayer,
audioEvents,
trackId,
};
};

/**
* `tracks`と`trackChannelStrips`を同期する。
* シーケンスが存在する場合は、ChannelStripとシーケンスの接続・接続の解除を行う。
Expand Down Expand Up @@ -1535,11 +1579,6 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
};

const render = async () => {
if (!audioContext) {
throw new Error("audioContext is undefined.");
}
const audioContextRef = audioContext;

const firstRestMinDurationSeconds = 0.12;

// レンダリング中に変更される可能性のあるデータのコピー
Expand Down Expand Up @@ -1735,20 +1774,15 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
});
}

// ノートシーケンスを作成して登録し、プレビュー音が鳴るようにする
const noteEvents = generateNoteEvents(
// ノートシーケンスを生成して登録し、プレビュー音が鳴るようにする
const sequenceId = SequenceId(uuid4());
const noteSequence = generateNoteSequence(
phrase.notes,
snapshot.tempos,
snapshot.tpqn,
phrase.trackId,
);
const polySynth = new PolySynth(audioContextRef);
const sequenceId = SequenceId(uuid4());
registerSequence(sequenceId, {
type: "note",
instrument: polySynth,
noteEvents,
trackId: phrase.trackId,
});
registerSequence(sequenceId, noteSequence);
mutations.SET_SEQUENCE_ID_TO_PHRASE({ phraseKey, sequenceId });
}
}
Expand Down Expand Up @@ -1791,7 +1825,7 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
});
}

// オーディオシーケンスを作成して登録する
// オーディオシーケンスを生成して登録する
const singingVoiceKey = getPhraseSingingVoiceKey(phraseKey);
if (singingVoiceKey == undefined) {
throw new Error("singingVoiceKey is undefined.");
Expand All @@ -1800,19 +1834,13 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
phraseSingingVoices,
singingVoiceKey,
);
const audioEvents = await generateAudioEvents(
audioContextRef,
const sequenceId = SequenceId(uuid4());
const audioSequence = await generateAudioSequence(
phrase.startTime,
singingVoice,
phrase.trackId,
);
const audioPlayer = new AudioPlayer(audioContext);
const sequenceId = SequenceId(uuid4());
registerSequence(sequenceId, {
type: "audio",
audioPlayer,
audioEvents,
trackId: phrase.trackId,
});
registerSequence(sequenceId, audioSequence);
mutations.SET_SEQUENCE_ID_TO_PHRASE({ phraseKey, sequenceId });

mutations.SET_STATE_TO_PHRASE({
Expand Down

0 comments on commit f8d966f

Please sign in to comment.