Skip to content

Commit

Permalink
audioItem, presetItemを渡してaudioItemを返す関数に切り出す
Browse files Browse the repository at this point in the history
  • Loading branch information
k-chop committed Apr 1, 2023
1 parent ddd2fdd commit 92bf9ad
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 52 deletions.
113 changes: 66 additions & 47 deletions src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
EngineId,
MoraDataType,
MorphingInfo,
Preset,
PresetKey,
SpeakerId,
StyleId,
Expand Down Expand Up @@ -253,6 +254,38 @@ export function determineNextPresetKey(
};
}

/**
* 与えたAudioItemを元に、Presetを適用した新しいAudioItemを返す
*/
export function applyAudioPresetToAudioItem(
audioItem: AudioItem | undefined,
presetItem: Preset | undefined
) {
if (
audioItem == undefined ||
audioItem.presetKey == undefined ||
audioItem.query == undefined
)
return;
if (presetItem == undefined) return;

const newAudioItem = { ...audioItem };

// Filter name property from presetItem in order to extract audioInfos.
const { name: _, morphingInfo, ...presetAudioInfos } = presetItem;

// Type Assertion
const audioInfos: Omit<
AudioQuery,
"accentPhrases" | "outputSamplingRate" | "outputStereo" | "kana"
> = presetAudioInfos;

newAudioItem.query = { ...audioItem.query, ...audioInfos };
newAudioItem.morphingInfo = morphingInfo;

return newAudioItem;
}

const audioBlobCache: Record<string, Blob> = {};
const audioElements: Record<AudioKey, HTMLAudioElement> = {};

Expand Down Expand Up @@ -603,9 +636,9 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
}).catch(() => undefined)
: undefined;

const audioItem: AudioItem = { text, voice };
let newAudioItem: AudioItem = { text, voice };
if (query != undefined) {
audioItem.query = query;
newAudioItem.query = query;
}

const presetKeyCandidate = payload.baseAudioItem?.presetKey;
Expand All @@ -616,35 +649,44 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
presetKeyCandidate,
state.inheritAudioInfo && baseAudioItem !== undefined
);
audioItem.presetKey = nextPresetKey;
newAudioItem.presetKey = nextPresetKey;

if (
state.inheritAudioInfo &&
baseAudioItem &&
baseAudioItem.query &&
audioItem.query
newAudioItem.query
) {
//引数にbaseAudioItemがある場合、話速等のパラメータを引き継いだAudioItemを返す
//baseAudioItem.queryが未設定の場合は引き継がない(起動直後等?)
audioItem.query.speedScale = baseAudioItem.query.speedScale;
audioItem.query.pitchScale = baseAudioItem.query.pitchScale;
audioItem.query.intonationScale = baseAudioItem.query.intonationScale;
audioItem.query.volumeScale = baseAudioItem.query.volumeScale;
audioItem.query.prePhonemeLength = baseAudioItem.query.prePhonemeLength;
audioItem.query.postPhonemeLength =
newAudioItem.query.speedScale = baseAudioItem.query.speedScale;
newAudioItem.query.pitchScale = baseAudioItem.query.pitchScale;
newAudioItem.query.intonationScale =
baseAudioItem.query.intonationScale;
newAudioItem.query.volumeScale = baseAudioItem.query.volumeScale;
newAudioItem.query.prePhonemeLength =
baseAudioItem.query.prePhonemeLength;
newAudioItem.query.postPhonemeLength =
baseAudioItem.query.postPhonemeLength;
audioItem.query.outputSamplingRate =
newAudioItem.query.outputSamplingRate =
baseAudioItem.query.outputSamplingRate;
audioItem.query.outputStereo = baseAudioItem.query.outputStereo;
audioItem.morphingInfo = baseAudioItem.morphingInfo;
newAudioItem.query.outputStereo = baseAudioItem.query.outputStereo;
newAudioItem.morphingInfo = baseAudioItem.morphingInfo;
}

// audioItemに対してプリセットを適用する
if (shouldApplyPreset) {
await dispatch("APPLY_AUDIO_PRESET_TO_AUDIO_ITEM", { audioItem });
if (nextPresetKey) {
const preset = state.presetItems[nextPresetKey];
const result = applyAudioPresetToAudioItem(newAudioItem, preset);

if (result) {
newAudioItem = result;
}
}
}

return audioItem;
return newAudioItem;
},
},

Expand Down Expand Up @@ -1045,41 +1087,18 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
},
},

APPLY_AUDIO_PRESET_TO_AUDIO_ITEM: {
// FIXME: audioItemを直接書き換えないようにするか、関数化する
mutation(state, { audioItem }) {
if (
audioItem == undefined ||
audioItem.presetKey == undefined ||
audioItem.query == undefined
)
return;
const presetItem = state.presetItems[audioItem.presetKey];
if (presetItem == undefined) return;

// Filter name property from presetItem in order to extract audioInfos.
const { name: _, morphingInfo, ...presetAudioInfos } = presetItem;

// Type Assertion
const audioInfos: Omit<
AudioQuery,
"accentPhrases" | "outputSamplingRate" | "outputStereo" | "kana"
> = presetAudioInfos;
APPLY_AUDIO_PRESET: {
mutation(state, { audioKey }: { audioKey: AudioKey }) {
const audioItem = state.audioItems[audioKey];

audioItem.query = { ...audioItem.query, ...audioInfos };
if (!audioItem || !audioItem.presetKey) return;

audioItem.morphingInfo = morphingInfo;
},
action({ commit }, { audioItem }) {
commit("APPLY_AUDIO_PRESET_TO_AUDIO_ITEM", { audioItem });
},
},
const presetItem = state.presetItems[audioItem.presetKey];
const newAudioItem = applyAudioPresetToAudioItem(audioItem, presetItem);

APPLY_AUDIO_PRESET: {
mutation(state, { audioKey }: { audioKey: AudioKey }) {
audioStore.mutations.APPLY_AUDIO_PRESET_TO_AUDIO_ITEM(state, {
audioItem: state.audioItems[audioKey],
});
if (newAudioItem) {
state.audioItems[audioKey] = newAudioItem;
}
},
},

Expand Down
5 changes: 0 additions & 5 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,6 @@ export type AudioStoreTypes = {
};
};

APPLY_AUDIO_PRESET_TO_AUDIO_ITEM: {
mutation: { audioItem: AudioItem };
action(payload: { audioItem: AudioItem }): void;
};

APPLY_AUDIO_PRESET: {
mutation: { audioKey: AudioKey };
};
Expand Down

0 comments on commit 92bf9ad

Please sign in to comment.