Skip to content

Commit

Permalink
Fix:フレーズの更新を行っているところの処理に問題があったので修正 (#2188)
Browse files Browse the repository at this point in the history
* singingGuides、singingVoices、phrases、sequencesの更新を同じタイミングで行うように修正

* 修正
  • Loading branch information
sigprogramming authored Jul 30, 2024
1 parent 7fbfde0 commit 6cc30fd
Showing 1 changed file with 69 additions and 60 deletions.
129 changes: 69 additions & 60 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1565,36 +1565,20 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
}
}

for (const [phraseKey, phrase] of state.phrases) {
const notesHash = phraseKey;
if (!foundPhrases.has(notesHash)) {
// 歌い方と歌声を削除する
if (phrase.singingGuideKey != undefined) {
commit("DELETE_SINGING_GUIDE", {
singingGuideKey: phrase.singingGuideKey,
});
}
if (phrase.singingVoiceKey != undefined) {
singingVoices.delete(phrase.singingVoiceKey);
}
const phrases = new Map<PhraseSourceHash, Phrase>();
const disappearedPhraseKeys = new Set<PhraseSourceHash>();

// 音源とシーケンスの接続を解除して削除する
const sequence = sequences.get(phraseKey);
if (sequence) {
getAudioSourceNode(sequence).disconnect();
transportRef.removeSequence(sequence);
sequences.delete(phraseKey);
}
for (const phraseKey of state.phrases.keys()) {
if (!foundPhrases.has(phraseKey)) {
// 無くなったフレーズの場合
disappearedPhraseKeys.add(phraseKey);
}
}

const newPhrases = new Map<PhraseSourceHash, Phrase>();

for (const [phraseKey, foundPhrase] of foundPhrases) {
const existingPhrase = state.phrases.get(phraseKey);
if (!existingPhrase) {
// 新しいフレーズの場合
newPhrases.set(phraseKey, foundPhrase);
phrases.set(phraseKey, foundPhrase);
continue;
}

Expand All @@ -1608,50 +1592,36 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
// すでに存在するフレーズの場合
// 再レンダリングする必要があるかどうかをチェックする
// シンガーが未設定の場合、とりあえず常に再レンダリングする
// 音声合成を行う必要がある場合、現在フレーズに設定されている歌声を削除する
// 歌い方の推論も行う必要がある場合、現在フレーズに設定されている歌い方を削除する
// 音声合成を行う必要がある場合、singingVoiceKeyをundefinedにする
// 歌い方の推論も行う必要がある場合、singingGuideKeyとsingingVoiceKeyをundefinedにする
// TODO: リファクタリングする
const phrase = { ...existingPhrase };
if (!singerAndFrameRate || phrase.state === "COULD_NOT_RENDER") {
if (phrase.singingGuideKey != undefined) {
commit("DELETE_SINGING_GUIDE", {
singingGuideKey: phrase.singingGuideKey,
});
phrase.singingGuideKey = undefined;
}
if (phrase.singingVoiceKey != undefined) {
singingVoices.delete(phrase.singingVoiceKey);
phrase.singingVoiceKey = undefined;
}
} else {
if (phrase.singingGuideKey != undefined) {
const calculatedHash = await calculateSingingGuideSourceHash({
engineId: singerAndFrameRate.singer.engineId,
tpqn,
tempos,
firstRestDuration: phrase.firstRestDuration,
lastRestDurationSeconds,
notes: phrase.notes,
keyRangeAdjustment: track.keyRangeAdjustment,
volumeRangeAdjustment: track.volumeRangeAdjustment,
frameRate: singerAndFrameRate.frameRate,
});
const hash = phrase.singingGuideKey;
if (hash !== calculatedHash) {
commit("DELETE_SINGING_GUIDE", {
singingGuideKey: phrase.singingGuideKey,
});
phrase.singingGuideKey = undefined;
if (phrase.singingVoiceKey != undefined) {
singingVoices.delete(phrase.singingVoiceKey);
phrase.singingVoiceKey = undefined;
}
} else if (phrase.singingGuideKey != undefined) {
const calculatedHash = await calculateSingingGuideSourceHash({
engineId: singerAndFrameRate.singer.engineId,
tpqn,
tempos,
firstRestDuration: phrase.firstRestDuration,
lastRestDurationSeconds,
notes: phrase.notes,
keyRangeAdjustment: track.keyRangeAdjustment,
volumeRangeAdjustment: track.volumeRangeAdjustment,
frameRate: singerAndFrameRate.frameRate,
});
const hash = phrase.singingGuideKey;
if (hash !== calculatedHash) {
phrase.singingGuideKey = undefined;
if (phrase.singingVoiceKey != undefined) {
phrase.singingVoiceKey = undefined;
}
}
if (
phrase.singingGuideKey != undefined &&
phrase.singingVoiceKey != undefined
) {
} else if (phrase.singingVoiceKey != undefined) {
let singingGuide = getOrThrow(
state.singingGuides,
phrase.singingGuideKey,
Expand All @@ -1667,22 +1637,61 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
});
const hash = phrase.singingVoiceKey;
if (hash !== calculatedHash) {
singingVoices.delete(phrase.singingVoiceKey);
phrase.singingVoiceKey = undefined;
}
}
}

phrases.set(phraseKey, phrase);
}

// フレーズのstateを更新する
for (const phrase of phrases.values()) {
if (
phrase.singingGuideKey == undefined ||
phrase.singingVoiceKey == undefined
) {
phrase.state = "WAITING_TO_BE_RENDERED";
}
}

// 無くなったフレーズの音源とシーケンスの接続を解除して削除する
for (const phraseKey of disappearedPhraseKeys) {
const sequence = sequences.get(phraseKey);
if (sequence) {
getAudioSourceNode(sequence).disconnect();
transportRef.removeSequence(sequence);
sequences.delete(phraseKey);
}
}

newPhrases.set(phraseKey, phrase);
// 使われていない歌い方と歌声を削除する
const singingGuideKeysInUse = new Set(
[...phrases.values()]
.map((value) => value.singingGuideKey)
.filter((value) => value != undefined),
);
const singingVoiceKeysInUse = new Set(
[...phrases.values()]
.map((value) => value.singingVoiceKey)
.filter((value) => value != undefined),
);
const existingSingingGuideKeys = new Set(state.singingGuides.keys());
const existingSingingVoiceKeys = new Set(singingVoices.keys());
const singingGuideKeysToDelete = existingSingingGuideKeys.difference(
singingGuideKeysInUse,
);
const singingVoiceKeysToDelete = existingSingingVoiceKeys.difference(
singingVoiceKeysInUse,
);
for (const singingGuideKey of singingGuideKeysToDelete) {
commit("DELETE_SINGING_GUIDE", { singingGuideKey });
}
for (const singingVoiceKey of singingVoiceKeysToDelete) {
singingVoices.delete(singingVoiceKey);
}

commit("SET_PHRASES", { phrases: newPhrases });
commit("SET_PHRASES", { phrases });

logger.info("Phrases updated.");

Expand Down

0 comments on commit 6cc30fd

Please sign in to comment.