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

ソング: USTファイルのインポート機能の追加 #1933

Merged
merged 6 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions src/components/Sing/MenuBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const importMusicXMLFile = async () => {
await store.dispatch("IMPORT_MUSICXML_FILE", {});
};

const importUstFile = async () => {
if (uiLocked.value) return;
await store.dispatch("IMPORT_UST_FILE", {});
};

const exportWaveFile = async () => {
if (uiLocked.value) return;
await store.dispatch("EXPORT_WAVE_FILE", {});
Expand Down Expand Up @@ -52,5 +57,13 @@ const fileSubMenuData: MenuItemData[] = [
},
disableWhenUiLocked: true,
},
{
type: "button",
label: "UST読み込み",
onClick: () => {
importUstFile();
},
disableWhenUiLocked: true,
},
];
</script>
122 changes: 122 additions & 0 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,128 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
),
},

IMPORT_UST_FILE: {
action: createUILockAction(
async ({ dispatch }, { filePath }: { filePath?: string }) => {
// USTファイルの読み込み
if (!filePath) {
filePath = await window.backend.showImportFileDialog({
title: "UST読み込み",
name: "UST",
extensions: ["ust"],
});
if (!filePath) return;
}
// ファイルの読み込み
const fileData = getValueOrThrow(
await window.backend.readFile({ filePath })
);

// ファイルフォーマットに応じてエンコーディングを変える
// UTF-8とShiftJISの2種類に対応
let ustData;
try {
ustData = new TextDecoder("utf-8").decode(fileData);
// ShiftJISの場合はShiftJISでデコードし直す
if (ustData.includes("\ufffd")) {
ustData = new TextDecoder("shift-jis").decode(fileData);
}
} catch (error) {
throw new Error("Failed to decode UST file.");
romot-co marked this conversation as resolved.
Show resolved Hide resolved
}
if (!ustData || typeof ustData !== "string") {
throw new Error("Failed to decode UST file.");
}

// 初期化
const tpqn = DEFAULT_TPQN;
const tempos: Tempo[] = [
{
position: 0,
bpm: DEFAULT_BPM,
},
];
const timeSignatures: TimeSignature[] = [
{
measureNumber: 1,
beats: DEFAULT_BEATS,
beatType: DEFAULT_BEAT_TYPE,
},
];
const notes: Note[] = [];

// USTファイルのセクションをパース
const parseSection = (section: string): { [key: string]: string } => {
const sectionNameMatch = section.match(/\[(.+)\]/);
if (!sectionNameMatch) {
throw new Error("Failed to parse UST file.");
}
const params = section.split(/[\r\n]+/).reduce((acc, line) => {
const [key, value] = line.split("=");
if (key && value) {
acc[key] = value;
}
return acc;
}, {} as { [key: string]: string });
return {
...params,
sectionName: sectionNameMatch[1],
};
};

try {
// セクションを分割
const sections = ustData.split(/^(?=\[)/m);
// ポジション
let position = 0;
// セクションごとに処理
sections.forEach((section) => {
const params = parseSection(section);
// SETTINGセクション
if (params.sectionName === "#SETTING") {
const tempo = Number(params["Tempo"]);
if (tempo) tempos[0].bpm = tempo;
}
// ノートセクション
if (params.sectionName.match(/^#\d{4}/)) {
// テンポ変更があれば追加
const tempo = Number(params["Tempo"]);
if (tempo) tempos.push({ position, bpm: tempo });
const noteNumber = Number(params["NoteNum"]);
const duration = Number(params["Length"]);
const lyric = params["Lyric"];
romot-co marked this conversation as resolved.
Show resolved Hide resolved
// 休符であればポジションを進めるのみ
if (lyric === "R") {
position += duration;
} else {
// それ以外の場合はノートを追加
notes.push({
id: uuidv4(),
position,
duration,
noteNumber,
lyric,
});
position += duration;
}
}
});
} catch (error) {
throw new Error("Failed to parse UST file.", { cause: error });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラー起こらない気がしますね・・・!
念のためにという感じでしょうか?

}

await dispatch("SET_SCORE", {
score: {
tpqn,
tempos,
timeSignatures,
notes,
},
});
}
),
},

SET_NOW_AUDIO_EXPORTING: {
mutation(state, { nowAudioExporting }) {
state.nowAudioExporting = nowAudioExporting;
Expand Down
4 changes: 4 additions & 0 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,10 @@ export type SingingStoreTypes = {
action(payload: { filePath?: string }): void;
};

IMPORT_UST_FILE: {
action(payload: { filePath?: string }): void;
};

EXPORT_WAVE_FILE: {
action(payload: { filePath?: string }): SaveResultObject;
};
Expand Down
Loading