From 2d0e0911771bf73fab7224fbc23d50166e8f0d69 Mon Sep 17 00:00:00 2001 From: phatnguyen Date: Mon, 18 Sep 2023 11:55:05 +0200 Subject: [PATCH 1/3] feat: preset --- src/backend/api/api.yaml | 10 +++ src/backend/api/audio.py | 8 +- src/backend/api/project.py | 5 +- src/frontend/components/BaseSelect.vue | 12 ++- src/frontend/components/ProjectItem.vue | 77 +++++++++++-------- .../composables/useConvertSecToMin.ts | 4 +- src/frontend/locales/en.json | 3 +- src/frontend/models/api.ts | 14 +++- src/frontend/models/types.ts | 3 +- src/frontend/pages/project/[id].vue | 1 + 10 files changed, 88 insertions(+), 49 deletions(-) diff --git a/src/backend/api/api.yaml b/src/backend/api/api.yaml index 807a73e..8db7123 100644 --- a/src/backend/api/api.yaml +++ b/src/backend/api/api.yaml @@ -13,9 +13,19 @@ paths: application/json: schema: type: object + required: + - filePath properties: filePath: type: string + presetName: + type: string + enum: + - EXTRA_STRICT + - STRICT + - NORMAL + - LENIENT + - EXTRA_LENIENT responses: "200": description: The segments of the file and potential metadata. diff --git a/src/backend/api/audio.py b/src/backend/api/audio.py index c66f17a..f3430c7 100644 --- a/src/backend/api/audio.py +++ b/src/backend/api/audio.py @@ -5,7 +5,7 @@ from flask import Blueprint, Response, jsonify, request, send_file from modules.api_service import ApiService from modules.audio_stream_io import read_audio_file_to_numpy, save_numpy_as_audio_file -from modules.segmentation import segment_file +from modules.segmentation import Preset, segment_file from pathvalidate import sanitize_filename from utils.env import get_env from utils.file_name_formatter import format_file_name @@ -22,9 +22,13 @@ def split(): """ data = request.json file_path = data["filePath"] + preset_name = data["presetName"] + preset = getattr(Preset, preset_name, Preset.NORMAL) + if not os.path.exists(file_path): return "File does not exist!", 400 - generator = segment_file(file_path) + + generator = segment_file(file_path, preset) segments, mismatch_offsets = ApiService().identify_all_from_generator( generator, file_path ) diff --git a/src/backend/api/project.py b/src/backend/api/project.py index 3f2dae9..efcd3a9 100644 --- a/src/backend/api/project.py +++ b/src/backend/api/project.py @@ -62,10 +62,11 @@ def create(): project["files"].append( { - "fileName": file_name, - "filePath": file_path, "name": name, "fileType": file_type, + "fileName": file_name, + "filePath": file_path, + "presetName": "NORMAL", } ) diff --git a/src/frontend/components/BaseSelect.vue b/src/frontend/components/BaseSelect.vue index 10d5306..0e843be 100644 --- a/src/frontend/components/BaseSelect.vue +++ b/src/frontend/components/BaseSelect.vue @@ -12,6 +12,10 @@ defineProps<{ * Array of options for select */ options: Option[] + /** + * Is select disabled + */ + disabled?: boolean }>() const modelValue = defineModel({ default: '' }) @@ -34,6 +38,7 @@ function handleChooseOption(v: string) { type="button" role="combobox" :aria-expanded="isExpanded" + :disabled="disabled" class="h-10 w-full flex items-center justify-between border border-input rounded-md bg-transparent px-3 py-2 text-sm ring-offset-background disabled:cursor-not-allowed placeholder:text-muted-foreground disabled:opacity-50 focus:ring-2 focus:ring-offset-2 focus:ring-ring" @click="isExpanded = !isExpanded" > @@ -46,13 +51,6 @@ function handleChooseOption(v: string) { - - () const { postAudioSplit, postAudioStore } = getAudioStreamSplittingAPI() @@ -50,34 +51,37 @@ onMounted(() => { }) }) +const presetNameOpts = [ + { value: 'EXTRA_STRICT', label: 'EXTRA_STRICT' }, + { value: 'STRICT', label: 'STRICT' }, + { value: 'NORMAL', label: 'NORMAL' }, + { value: 'LENIENT', label: 'LENIENT' }, + { value: 'EXTRA_LENIENT', label: 'EXTRA_LENIENT' }, +] const isProcessing = ref(false) +const presetName = ref(props.file.presetName) +watch(presetName, () => emits('changePresetName', presetName.value)) async function handleProcess() { isProcessing.value = true - try { - let segments: ProjectFileSegment[] - if (props.file.segments) { - segments = props.file.segments - } - else { - toast({ content: t('toast.long_process') }) - const { data } = await postAudioSplit({ filePath: props.file.filePath }) - - if (!data.segments || !data.segments?.length) - throw new Error('This audio cannot be split') - - data.segments = data.segments - .map((s) => { - const notNullOpts = s.metadataOptions?.filter(o => o.album || o.artist || o.title || o.year) - const notDuplicatedOpts = [...new Set((notNullOpts ?? []).map(o => JSON.stringify(o)))] - .map(o => JSON.parse(o)) - return { ...s, metadataOptions: notDuplicatedOpts } - }) - - segments = data.segments.map(s => ({ ...s, metaIndex: 0 })) - - emits('succeedProcess', segments) - } + regions.value?.clearRegions() + try { + toast({ content: t('toast.long_process') }) + const { data } = await postAudioSplit({ filePath: props.file.filePath, presetName: presetName.value }) + + if (!data.segments || !data.segments?.length) + throw new Error('This audio cannot be split') + + data.segments = data.segments + .map((s) => { + const notNullOpts = s.metadataOptions?.filter(o => o.album || o.artist || o.title || o.year) + const notDuplicatedOpts = [...new Set((notNullOpts ?? []).map(o => JSON.stringify(o)))] + .map(o => JSON.parse(o)) + return { ...s, metadataOptions: notDuplicatedOpts } + }) + + const segments = data.segments.map(s => ({ ...s, metaIndex: 0 })) + emits('succeedProcess', segments) addRegion(segments) } catch (e) { @@ -189,15 +193,22 @@ function handleEdit(songIndex: number) { -

- {{ t('song.no_webm') }} -

+
+
+ {{ t('song.preset') }} + + + +
-
- +
From 54d6fb72e4d5125d2d8987be1913e5434deaec1a Mon Sep 17 00:00:00 2001 From: phatnguyen Date: Tue, 19 Sep 2023 21:06:29 +0200 Subject: [PATCH 2/3] fix: text --- src/frontend/components/BaseSelect.vue | 22 +++++++++++++--------- src/frontend/components/ProjectItem.vue | 20 ++++++++------------ src/frontend/locales/de.json | 10 +++++++++- src/frontend/locales/en.json | 9 ++++++++- src/frontend/pages/settings.vue | 6 +----- 5 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/frontend/components/BaseSelect.vue b/src/frontend/components/BaseSelect.vue index 0e843be..64ab53e 100644 --- a/src/frontend/components/BaseSelect.vue +++ b/src/frontend/components/BaseSelect.vue @@ -3,7 +3,7 @@ import { onClickOutside } from '@vueuse/core' interface Option { label: string; value: string } -defineProps<{ +const props = defineProps<{ /** * Placeholder for select */ @@ -19,13 +19,15 @@ defineProps<{ }>() const modelValue = defineModel({ default: '' }) +const index = ref(props.options.findIndex(o => o.value === modelValue.value) ?? 0) const isExpanded = ref(false) const isMouseOverOpts = ref(false) const button = ref() onClickOutside(button, () => isExpanded.value = false) -function handleChooseOption(v: string) { +function handleChooseOption(v: string, i: number) { modelValue.value = v + index.value = i isExpanded.value = false button.value?.focus() } @@ -43,9 +45,11 @@ function handleChooseOption(v: string) { @click="isExpanded = !isExpanded" > - - - {{ modelValue ?? placeholder }} + + + {{ options[index].label }} @@ -64,14 +68,14 @@ function handleChooseOption(v: string) { @mouseleave="isMouseOverOpts = false" >
  • { }) const presetNameOpts = [ - { value: 'EXTRA_STRICT', label: 'EXTRA_STRICT' }, - { value: 'STRICT', label: 'STRICT' }, - { value: 'NORMAL', label: 'NORMAL' }, - { value: 'LENIENT', label: 'LENIENT' }, - { value: 'EXTRA_LENIENT', label: 'EXTRA_LENIENT' }, + { value: 'EXTRA_STRICT', label: t('song.preset.extra_strict') }, + { value: 'STRICT', label: t('song.preset.strict') }, + { value: 'NORMAL', label: t('song.preset.normal') }, + { value: 'LENIENT', label: t('song.preset.lenient') }, + { value: 'EXTRA_LENIENT', label: t('song.preset.extra_lenient') }, ] const isProcessing = ref(false) -const presetName = ref(props.file.presetName) +const presetName = ref(props.file.presetName ?? 'EXTRA_STRICT') watch(presetName, () => emits('changePresetName', presetName.value)) async function handleProcess() { isProcessing.value = true @@ -195,17 +195,13 @@ function handleEdit(songIndex: number) {
    - {{ t('song.preset') }} + {{ t('song.preset.index') }} - - + />
    diff --git a/src/frontend/locales/de.json b/src/frontend/locales/de.json index 838f0c4..bcb99a8 100644 --- a/src/frontend/locales/de.json +++ b/src/frontend/locales/de.json @@ -76,7 +76,15 @@ "duration": "Dauer", "no_webm": ".webm-Dateien können nicht verarbeitet werden!", "no_meta": "Keine Information", - "list_caption": "Gefundene Lieder" + "list_caption": "Gefundene Lieder", + "preset": { + "extra_lenient": "Besonders nachsichtig", + "extra_strict": "Extra streng", + "index": "Voreingestellt", + "lenient": "Nachsichtig", + "normal": "Normal", + "strict": "Strikt" + } }, "toast": { "empty_field": "Bitte geben Sie einen gültigen Wert ein!", diff --git a/src/frontend/locales/en.json b/src/frontend/locales/en.json index ca1c669..20fd996 100644 --- a/src/frontend/locales/en.json +++ b/src/frontend/locales/en.json @@ -77,7 +77,14 @@ "no_webm": ".webm cannot be processed at the moment!", "no_meta": "No information", "list_caption": "Found songs", - "preset": "Preset" + "preset": { + "index": "Preset", + "extra_strict": "Extra Strict", + "strict": "Strict", + "normal": "Normal", + "lenient": "Lenient", + "extra_lenient": "Extra Lenient" + } }, "toast": { "empty_field": "Please enter a valid value!", diff --git a/src/frontend/pages/settings.vue b/src/frontend/pages/settings.vue index e5cfd71..d01b3cc 100644 --- a/src/frontend/pages/settings.vue +++ b/src/frontend/pages/settings.vue @@ -36,11 +36,7 @@ function handleToggleDark() { v-model="currentLocal" :options="localOpts" class="w-200px" - > - - + />
    From 856cacd3b5308593fccf8bc7331b13737a0c0750 Mon Sep 17 00:00:00 2001 From: phatnguyen Date: Tue, 19 Sep 2023 21:49:02 +0200 Subject: [PATCH 3/3] fix: notes --- src/frontend/composables/useConvertSecToMin.ts | 5 +++-- src/frontend/locales/de.json | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/frontend/composables/useConvertSecToMin.ts b/src/frontend/composables/useConvertSecToMin.ts index f816d62..f5dae89 100644 --- a/src/frontend/composables/useConvertSecToMin.ts +++ b/src/frontend/composables/useConvertSecToMin.ts @@ -1,5 +1,6 @@ export function useConvertSecToMin(secs: number) { - const rest = Math.round((secs % 60)) - const min = Math.round((secs - rest) / 60) + const _secs = Math.floor(secs) + const rest = _secs % 60 + const min = (_secs - rest) / 60 return rest ? `${min}m ${rest}s` : `${min}m` } diff --git a/src/frontend/locales/de.json b/src/frontend/locales/de.json index bcb99a8..e728585 100644 --- a/src/frontend/locales/de.json +++ b/src/frontend/locales/de.json @@ -78,10 +78,10 @@ "no_meta": "Keine Information", "list_caption": "Gefundene Lieder", "preset": { - "extra_lenient": "Besonders nachsichtig", - "extra_strict": "Extra streng", + "extra_lenient": "Extra locker", + "extra_strict": "Extra strikt", "index": "Voreingestellt", - "lenient": "Nachsichtig", + "lenient": "Locker", "normal": "Normal", "strict": "Strikt" }