Skip to content

Commit

Permalink
fix: アクセント区間の数が選択インデックスを下回る可能性があるバグを修正 (#1575)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroshiba <[email protected]>
  • Loading branch information
thiramisu and Hiroshiba authored Sep 27, 2023
1 parent 490952b commit f4f42d0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/components/AudioDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ const hotkeyMap = new Map<HotkeyAction, () => HotkeyReturnType>([
if (
!uiLocked.value &&
store.getters.ACTIVE_AUDIO_KEY &&
store.state.audioPlayStartPoint !== undefined
store.getters.AUDIO_PLAY_START_POINT !== undefined
) {
store.dispatch("COMMAND_RESET_SELECTED_MORA_PITCH_AND_LENGTH", {
audioKey: store.getters.ACTIVE_AUDIO_KEY,
accentPhraseIndex: store.state.audioPlayStartPoint,
accentPhraseIndex: store.getters.AUDIO_PLAY_START_POINT,
});
}
},
Expand Down Expand Up @@ -221,7 +221,7 @@ const activePointScrollMode = computed(() => store.state.activePointScrollMode);
// 再生開始アクセント句
const startPoint = computed({
get: () => {
return store.state.audioPlayStartPoint;
return store.getters.AUDIO_PLAY_START_POINT;
},
set: (startPoint) => {
store.dispatch("SET_AUDIO_PLAY_START_POINT", { startPoint });
Expand Down
36 changes: 29 additions & 7 deletions src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,6 @@ export const audioStoreState: AudioStoreState = {
audioItems: {},
audioKeys: [],
audioStates: {},
// audio elementの再生オフセット
audioPlayStartPoint: undefined,
nowPlayingContinuously: false,
};

Expand Down Expand Up @@ -294,6 +292,29 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
},
},

/**
* audio elementの再生オフセット。
* 選択+削除 や 挿入+選択+元に戻す などを行った場合でも範囲外にならないようにクランプする。
* ACTIVE_AUDIO_KEYがundefinedのときはundefinedを返す。
*/
AUDIO_PLAY_START_POINT: {
getter(state, getters) {
const audioPlayStartPoint = state._audioPlayStartPoint;
if (
audioPlayStartPoint == undefined ||
getters.ACTIVE_AUDIO_KEY == undefined
) {
return undefined;
}
const length =
state.audioItems[getters.ACTIVE_AUDIO_KEY].query?.accentPhrases.length;
if (length == undefined) {
return undefined;
}
return Math.max(0, Math.min(length - 1, audioPlayStartPoint));
},
},

LOAD_CHARACTER: {
action: createUILockAction(async ({ commit, dispatch }, { engineId }) => {
const speakers = await dispatch("INSTANTIATE_ENGINE_CONNECTOR", {
Expand Down Expand Up @@ -538,7 +559,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({

SET_AUDIO_PLAY_START_POINT: {
mutation(state, { startPoint }: { startPoint?: number }) {
state.audioPlayStartPoint = startPoint;
state._audioPlayStartPoint = startPoint;
},
action({ commit }, { startPoint }: { startPoint?: number }) {
commit("SET_AUDIO_PLAY_START_POINT", { startPoint });
Expand Down Expand Up @@ -1762,7 +1783,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
PLAY_AUDIO_BLOB: {
action: createUILockAction(
async (
{ state, commit, dispatch },
{ getters, commit, dispatch },
{ audioBlob, audioKey }: { audioBlob: Blob; audioKey?: AudioKey }
) => {
commit("SET_AUDIO_SOURCE", { audioBlob });
Expand All @@ -1774,7 +1795,8 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
});
if (accentPhraseOffsets.length === 0)
throw new Error("accentPhraseOffsets.length === 0");
const startTime = accentPhraseOffsets[state.audioPlayStartPoint ?? 0];
const startTime =
accentPhraseOffsets[getters.AUDIO_PLAY_START_POINT ?? 0];
if (startTime === undefined) throw Error("startTime === undefined");
// 小さい値が切り捨てられることでフォーカスされるアクセントフレーズが一瞬元に戻るので、
// 再生に影響のない程度かつ切り捨てられない値を加算する
Expand Down Expand Up @@ -1803,9 +1825,9 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
},

PLAY_CONTINUOUSLY_AUDIO: {
action: createUILockAction(async ({ state, commit, dispatch }) => {
action: createUILockAction(async ({ state, getters, commit, dispatch }) => {
const currentAudioKey = state._activeAudioKey;
const currentAudioPlayStartPoint = state.audioPlayStartPoint;
const currentAudioPlayStartPoint = getters.AUDIO_PLAY_START_POINT;

let index = 0;
if (currentAudioKey !== undefined) {
Expand Down
6 changes: 5 additions & 1 deletion src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export type AudioStoreState = {
audioStates: Record<AudioKey, AudioState>;
_activeAudioKey?: AudioKey;
_selectedAudioKeys?: AudioKey[];
audioPlayStartPoint?: number;
_audioPlayStartPoint?: number;
nowPlayingContinuously: boolean;
};

Expand All @@ -151,6 +151,10 @@ export type AudioStoreTypes = {
getter(audioKey: AudioKey): boolean;
};

AUDIO_PLAY_START_POINT: {
getter: number | undefined;
};

LOAD_CHARACTER: {
action(payload: { engineId: EngineId }): void;
};
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/store/Vuex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ describe("store/vuex.js test", () => {
audioItems: {},
audioKeys: [],
audioStates: {},
audioPlayStartPoint: 0,
uiLockCount: 0,
dialogLockCount: 0,
reloadingLock: false,
Expand Down Expand Up @@ -209,7 +208,6 @@ describe("store/vuex.js test", () => {
assert.isEmpty(store.state.audioKeys);
assert.isObject(store.state.audioStates);
assert.isEmpty(store.state.audioStates);
assert.equal(store.state.audioPlayStartPoint, 0);
assert.equal(store.state.uiLockCount, 0);
assert.equal(store.state.nowPlayingContinuously, false);
assert.isArray(store.state.undoCommands);
Expand Down

0 comments on commit f4f42d0

Please sign in to comment.