From bb50bf84779e58bf178d826b744320c54f5f8a38 Mon Sep 17 00:00:00 2001 From: Xmader Date: Tue, 20 Oct 2020 01:58:39 -0400 Subject: [PATCH] feat: download audio --- src/main.ts | 23 +++++++++++++++++------ src/mscore.ts | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/main.ts b/src/main.ts index fbba643..60e09cc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,6 +4,7 @@ import { waitForDocumentLoaded, saveAs } from './utils' import { downloadPDF } from './pdf' import { downloadMscz } from './mscz' import { getFileUrl } from './file' +import { WebMscore, loadSoundFont } from './mscore' import { getDownloadBtn, BtnList, BtnAction } from './btn' import * as recaptcha from './recaptcha' import scoreinfo from './scoreinfo' @@ -67,7 +68,7 @@ const main = (): void => { interface IndividualDownload { name: string; fileExt: string; - action (score: import('webmscore').default): Promise; + action (score: WebMscore): Promise; } const downloads: IndividualDownload[] = [ @@ -76,11 +77,6 @@ const main = (): void => { fileExt: 'pdf', action: (score) => score.savePdf(), }, - { - name: 'Download MIDI', - fileExt: 'mid', - action: (score) => score.saveMidi(true, true), - }, { name: 'Download Part MSCZ', fileExt: 'mscz', @@ -91,6 +87,21 @@ const main = (): void => { fileExt: 'mxl', action: (score) => score.saveMxl(), }, + { + name: 'Download MIDI', + fileExt: 'mid', + action: (score) => score.saveMidi(true, true), + }, + { + name: 'Download FLAC Audio', + fileExt: 'flac', + action: (score) => loadSoundFont(score).then(() => score.saveAudio('flac')), + }, + { + name: 'Download OGG Audio', + fileExt: 'ogg', + action: (score) => loadSoundFont(score).then(() => score.saveAudio('ogg')), + }, ] // part selection diff --git a/src/mscore.ts b/src/mscore.ts index 7ef0134..9e0be1a 100644 --- a/src/mscore.ts +++ b/src/mscore.ts @@ -8,6 +8,9 @@ const WEBMSCORE_URL = 'https://cdn.jsdelivr.net/npm/webmscore@0.10/webmscore.js' // JP characters are included in the CN font const FONT_URLS = ['CN', 'KR'].map(l => `https://cdn.jsdelivr.net/npm/@librescore/fonts/SourceHanSans${l}-Regular.woff2`) +const SF3_URL = 'https://cdn.jsdelivr.net/npm/@librescore/sf3/FluidR3Mono_GM.sf3' +const SOUND_FONT_LOADED = Symbol('SoundFont loaded') + export type WebMscore = import('webmscore').default const initMscore = async (w: Window) => { @@ -31,6 +34,18 @@ const initFonts = () => { } } +export const loadSoundFont = (score: WebMscore): Promise => { + if (!score[SOUND_FONT_LOADED]) { + const loadPromise = (async () => { + await score.setSoundFont( + await fetchData(SF3_URL), + ) + })() + score[SOUND_FONT_LOADED] = loadPromise + } + return score[SOUND_FONT_LOADED] as Promise +} + export const loadMscore = async (w: Window): Promise => { initFonts() await initMscore(w)