Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

キーを変更する機能を追加 #1842

Merged
merged 5 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 93 additions & 55 deletions src/components/Sing/ToolBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
/>
</div>
</character-menu-button>
<q-input
type="number"
:model-value="keyShiftInputBuffer"
label="トランスポーズ"
dense
hide-bottom-space
class="key-shift"
@update:model-value="setKeyShiftInputBuffer"
@change="setKeyShift"
/>
<q-input
type="number"
:model-value="bpmInputBuffer"
Expand Down Expand Up @@ -125,6 +135,7 @@ import {
isValidBeatType,
isValidBeats,
isValidBpm,
isValidVoiceKeyShift,
} from "@/sing/domain";
import CharacterMenuButton from "@/components/Sing/CharacterMenuButton.vue";
import { getStyleDescription } from "@/sing/viewHelper";
Expand Down Expand Up @@ -163,30 +174,93 @@ const selectedStyleIconPath = computed(() => {
})?.iconPath;
});

const bpmInputBuffer = ref(0);
const beatsInputBuffer = ref(0);
const beatTypeInputBuffer = ref(0);
const tempos = computed(() => store.state.tempos);
const timeSignatures = computed(() => store.state.timeSignatures);
const keyShift = computed(() => store.getters.SELECTED_TRACK.voiceKeyShift);

const bpmInputBuffer = ref(120);
const beatsInputBuffer = ref(4);
const beatTypeInputBuffer = ref(4);
const keyShiftInputBuffer = ref(0);

watch(
tempos,
() => {
bpmInputBuffer.value = tempos.value[0].bpm;
},
{ deep: true }
);

watch(
timeSignatures,
() => {
beatsInputBuffer.value = timeSignatures.value[0].beats;
beatTypeInputBuffer.value = timeSignatures.value[0].beatType;
},
{ deep: true }
);

watch(keyShift, () => {
keyShiftInputBuffer.value = keyShift.value;
});

const setBpmInputBuffer = (bpmStr: string | number | null) => {
const bpm = Number(bpmStr);
if (!isValidBpm(bpm)) {
const bpmValue = Number(bpmStr);
if (!isValidBpm(bpmValue)) {
return;
}
bpmInputBuffer.value = bpm;
bpmInputBuffer.value = bpmValue;
};

const setBeatsInputBuffer = (beatsStr: string | number | null) => {
const beats = Number(beatsStr);
if (!isValidBeats(beats)) {
const beatsValue = Number(beatsStr);
if (!isValidBeats(beatsValue)) {
return;
}
beatsInputBuffer.value = beats;
beatsInputBuffer.value = beatsValue;
};

const setBeatTypeInputBuffer = (beatTypeStr: string | number | null) => {
const beatType = Number(beatTypeStr);
if (!isValidBeatType(beatType)) {
const beatTypeValue = Number(beatTypeStr);
if (!isValidBeatType(beatTypeValue)) {
return;
}
beatTypeInputBuffer.value = beatTypeValue;
};

const setKeyShiftInputBuffer = (keyShiftStr: string | number | null) => {
const keyShiftValue = Number(keyShiftStr);
if (!isValidVoiceKeyShift(keyShiftValue)) {
return;
}
beatTypeInputBuffer.value = beatType;
keyShiftInputBuffer.value = keyShiftValue;
};

const setTempo = () => {
const bpm = bpmInputBuffer.value;
store.dispatch("SET_TEMPO", {
tempo: {
position: 0,
bpm,
},
});
};

const setTimeSignature = () => {
const beats = beatsInputBuffer.value;
const beatType = beatTypeInputBuffer.value;
store.dispatch("SET_TIME_SIGNATURE", {
timeSignature: {
measureNumber: 1,
beats,
beatType,
},
});
};

const setKeyShift = () => {
const voiceKeyShift = keyShiftInputBuffer.value;
store.dispatch("SET_VOICE_KEY_SHIFT", { voiceKeyShift });
};

const playheadTicks = ref(0);
Expand All @@ -213,50 +287,8 @@ const playHeadPositionMilliSecStr = computed(() => {
return milliSecStr;
});

const tempos = computed(() => store.state.tempos);
const timeSignatures = computed(() => store.state.timeSignatures);
const nowPlaying = computed(() => store.state.nowPlaying);

watch(
tempos,
() => {
bpmInputBuffer.value = tempos.value[0].bpm;
},
{ deep: true }
);
watch(
timeSignatures,
() => {
beatsInputBuffer.value = timeSignatures.value[0].beats;
beatTypeInputBuffer.value = timeSignatures.value[0].beatType;
},
{ deep: true }
);

const setTempo = async () => {
const bpm = bpmInputBuffer.value;
if (bpm === 0) return;
await store.dispatch("SET_TEMPO", {
tempo: {
position: 0,
bpm,
},
});
};

const setTimeSignature = async () => {
const beats = beatsInputBuffer.value;
const beatType = beatTypeInputBuffer.value;
if (beats === 0 || beatType === 0) return;
await store.dispatch("SET_TIME_SIGNATURE", {
timeSignature: {
measureNumber: 1,
beats,
beatType,
},
});
};

const play = () => {
store.dispatch("SING_PLAY_AUDIO");
};
Expand Down Expand Up @@ -408,9 +440,15 @@ onUnmounted(() => {
flex: 1;
}

.sing-tempo {
.key-shift {
margin-left: 16px;
margin-right: 4px;
width: 50px;
}

.sing-tempo {
margin-left: 8px;
margin-right: 4px;
width: 72px;
}

Expand Down
8 changes: 8 additions & 0 deletions src/sing/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,11 @@ export function getSnapTypes(tpqn: number) {
export function isValidSnapType(snapType: number, tpqn: number) {
return getSnapTypes(tpqn).some((value) => value === snapType);
}

export function isValidVoiceKeyShift(voiceKeyShift: number) {
return (
Number.isInteger(voiceKeyShift) &&
voiceKeyShift <= 24 &&
voiceKeyShift >= -24
);
}
2 changes: 2 additions & 0 deletions src/sing/storeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export const DEFAULT_BEAT_TYPE = 4;

export const generatePhraseHash = async (obj: {
singer: Singer | undefined;
notesKeyShift: number;
voiceKeyShift: number;
tpqn: number;
tempos: Tempo[];
notes: Note[];
Expand Down
58 changes: 53 additions & 5 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
isValidSnapType,
isValidTempo,
isValidTimeSignature,
isValidVoiceKeyShift,
secondToTick,
tickToSecond,
} from "@/sing/domain";
Expand Down Expand Up @@ -136,6 +137,8 @@ export const singingStoreState: SingingStoreState = {
tracks: [
{
singer: undefined,
notesKeyShift: 0,
voiceKeyShift: 0,
notes: [],
},
],
Expand Down Expand Up @@ -214,6 +217,23 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
},
},

SET_VOICE_KEY_SHIFT: {
mutation(state, { voiceKeyShift }: { voiceKeyShift: number }) {
state.tracks[selectedTrackIndex].voiceKeyShift = voiceKeyShift;
},
async action(
{ dispatch, commit },
{ voiceKeyShift }: { voiceKeyShift: number }
) {
if (!isValidVoiceKeyShift(voiceKeyShift)) {
throw new Error("The voiceKeyShift is invalid.");
}
commit("SET_VOICE_KEY_SHIFT", { voiceKeyShift });

dispatch("RENDER");
},
},

SET_SCORE: {
mutation(state, { score }: { score: Score }) {
overlappingNotesDetector.clear();
Expand Down Expand Up @@ -821,6 +841,8 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
async action({ state, getters, commit, dispatch }) {
const searchPhrases = async (
singer: Singer | undefined,
notesKeyShift: number,
voiceKeyShift: number,
tpqn: number,
tempos: Tempo[],
notes: Note[]
Expand All @@ -840,12 +862,16 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
const phraseLastNote = phraseNotes[phraseNotes.length - 1];
const hash = await generatePhraseHash({
singer,
notesKeyShift,
voiceKeyShift,
tpqn,
tempos,
notes: phraseNotes,
});
foundPhrases.set(hash, {
singer,
notesKeyShift,
voiceKeyShift,
tpqn,
tempos,
notes: phraseNotes,
Expand All @@ -871,6 +897,7 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
notes: Note[],
tempos: Tempo[],
tpqn: number,
notesKeyShift: number,
frameRate: number,
restDurationSeconds: number
) => {
Expand Down Expand Up @@ -903,7 +930,7 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
.replace("は", "ハ")
.replace("へ", "ヘ");
notesForRequestToEngine.push({
key: note.noteNumber,
key: note.noteNumber + notesKeyShift,
frameLength: noteFrameLength,
lyric,
});
Expand Down Expand Up @@ -945,6 +972,15 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
return frameAudioQuery.phonemes.map((value) => value.phoneme).join(" ");
};

const shiftVoiceKey = (
voiceKeyShift: number,
frameAudioQuery: FrameAudioQuery
) => {
frameAudioQuery.f0 = frameAudioQuery.f0.map((value) => {
return value * Math.pow(2, voiceKeyShift / 12);
});
};

const calcStartTime = (
notes: Note[],
tempos: Tempo[],
Expand Down Expand Up @@ -1000,13 +1036,22 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
const tpqn = state.tpqn;
const tempos = state.tempos.map((value) => ({ ...value }));
const track = getters.SELECTED_TRACK;
const singer = track.singer ? { ...track.singer } : undefined;
const notesKeyShift = track.notesKeyShift;
const voiceKeyShift = track.voiceKeyShift;
const notes = track.notes
.map((value) => ({ ...value }))
.filter((value) => !state.overlappingNoteIds.has(value.id));
const singer = track.singer ? { ...track.singer } : undefined;

// フレーズを更新する
const foundPhrases = await searchPhrases(singer, tpqn, tempos, notes);
const foundPhrases = await searchPhrases(
singer,
notesKeyShift,
voiceKeyShift,
tpqn,
tempos,
notes
);
for (const [hash, phrase] of foundPhrases) {
const phraseKey = hash;
if (!state.phrases.has(phraseKey)) {
Expand Down Expand Up @@ -1074,7 +1119,7 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
});
}

// 推論(クエリのフェッチ)とフレーズの開始時刻の算出を行う
// 推論(クエリのフェッチ)、キーシフト、フレーズの開始時刻の計算を行う

if (!phrase.query) {
const engineId = phrase.singer.engineId;
Expand All @@ -1086,6 +1131,7 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
phrase.notes,
phrase.tempos,
phrase.tpqn,
phrase.notesKeyShift,
frameRate,
restDurationSeconds
).catch((error) => {
Expand All @@ -1095,12 +1141,14 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
});
throw error;
});
const phonemes = getPhonemes(frameAudioQuery);

const phonemes = getPhonemes(frameAudioQuery);
window.electron.logInfo(
`Fetched frame audio query. Phonemes are "${phonemes}".`
);

shiftVoiceKey(phrase.voiceKeyShift, frameAudioQuery);

const startTime = calcStartTime(
phrase.notes,
phrase.tempos,
Expand Down
Loading
Loading