Skip to content

Commit

Permalink
Patch music builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
c-bertran committed Sep 20, 2024
1 parent 238e028 commit 2dd5ce6
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 25 deletions.
184 changes: 182 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"codemirror": "^6.0.1",
"mapcraft-api": "^2.4.7",
"markdown-it": "^14.1.0",
"music-metadata": "^10.5.0",
"pinia": "^2.1.7",
"quasar": "^2.15.1",
"vue": "^3.4.21",
Expand Down
18 changes: 2 additions & 16 deletions src/builtin/music/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,10 @@ import { basename, resolve } from 'path';
import { minecraft } from 'mapcraft-api/frontend';
import { fs } from 'mapcraft-api/backend';
import { mapEngineInstance } from '@/electron/preload/engine';
import ipc from '@/electron/ipc/render';
import { sounds, sound, category } from './interface';
import { envInterface } from '../interface';

const analyzeAudio = async (src: string): Promise<number> => {
return new Promise((res) => {
const audio = new Audio(src);
audio.addEventListener('loadedmetadata', () => {
if (audio.duration !== Infinity)
return res(Math.floor(audio.duration));
audio.currentTime = 10000000;
setTimeout(() => {
audio.currentTime = 0;
res(Math.floor(audio.duration));
}, 1000);
}, { once: true });
});
};

class music {
private env: envInterface;
private id: number;
Expand Down Expand Up @@ -217,7 +203,7 @@ exposeInMainWorld('music', {
) => __instance__.datapackCreateMusic(d),
delete: (id: number, index: number) => __instance__.datapackDeleteMusic(id, index)
},
analyze: (src: string) => analyzeAudio(src),
analyze: async (src: string) => await ipc.invoke('file::get-duration', src),
music: {
add: (sound: sound) => __instance__.addMusic(sound),
remove: (name: string) => __instance__.removeMusic(name),
Expand Down
7 changes: 1 addition & 6 deletions src/builtin/music/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,9 @@ export default defineComponent({
} as sound;
for (const id in soundList.value[key].sounds) {
const toto = await window.music.analyze(
path(window.music.sound.get(soundList.value[key].sounds[id].name))
);
console.log(toto, toto * 20)
window.music.datapack.create({
id:soundList.value[key].id,
name: soundList.value[key].sounds[id].name,
name: soundList.value[key].sounds[id].name.slice(0, soundList.value[key].sounds[id].name.lastIndexOf('/')),
index: Number(id),
category: soundList.value[key].category,
duration: (await window.music.analyze(
Expand Down
11 changes: 11 additions & 0 deletions src/electron/ipc/channels/file/definitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ipcDefinition, ipcType } from '@/main/src/electron/ipc/type';

export default {
channel: 'file',
channels: [
{
name: 'get-duration',
type: ipcType.INVOKE,
}
]
} as ipcDefinition;
7 changes: 7 additions & 0 deletions src/electron/ipc/channels/file/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ipcFunctions } from '@/main/src/electron/ipc/type';
import { parseFile } from 'node_modules/music-metadata/lib/index';
import type Electron from 'electron';

export default [
async (_event: Electron.IpcMainEvent, pathToFile: string): Promise<number> => Math.floor((await parseFile(pathToFile.replace('app:///', ''))).format.duration ?? 1),
] as ipcFunctions;
4 changes: 4 additions & 0 deletions src/electron/ipc/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import dialogFunctions from './channels/dialog/functions';
import editorDefinitions from './channels/editor/definitions';
import editorFunctions from './channels/editor/functions';

import fileDefinitions from './channels/file/definitions';
import fileFunctions from './channels/file/functions';

import notificationDefinitions from './channels/notification/definitions';
import notificationFunctions from './channels/notification/functions';

Expand All @@ -32,6 +35,7 @@ const importInList = (definition: ipcDefinition, functions: ipcFunctions): void

importInList(dialogDefinitions, dialogFunctions);
importInList(editorDefinitions, editorFunctions);
importInList(fileDefinitions, fileFunctions);
importInList(notificationDefinitions, notificationFunctions);
importInList(shellDefinitions, shellFunctions);
importInList(updateDefinitions, updateFunctions);
Expand Down
2 changes: 2 additions & 0 deletions src/electron/ipc/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ipcNaming, { ipcDefinition, ipcType } from './type';

import dialogDefinitions from './channels/dialog/definitions';
import editorDefinitions from './channels/editor/definitions';
import fileDefinitions from './channels/file/definitions';
import notificationDefinitions from './channels/notification/definitions';
import shellDefinitions from './channels/shell/definitions';
import updateDefitions from './channels/update/definitions';
Expand Down Expand Up @@ -39,6 +40,7 @@ const pushImport = (def: ipcDefinition): void => {

pushImport(dialogDefinitions);
pushImport(editorDefinitions);
pushImport(fileDefinitions);
pushImport(notificationDefinitions);
pushImport(shellDefinitions);
pushImport(updateDefitions);
Expand Down
Loading

0 comments on commit 2dd5ce6

Please sign in to comment.