@dsebastien/music-notation 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -13
- package/dist/index.js +12 -4
- package/dist/index.js.map +3 -3
- package/dist/music-notation.css +10 -2
- package/dist/music-notation.publish.min.js +14 -6
- package/dist/music-notation.tab.min.js +14 -6
- package/dist/playback/controls.d.ts +3 -2
- package/dist/publish/index.js +2 -2
- package/dist/publish/index.js.map +3 -3
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/*! @dsebastien/music-notation 0.5.
|
|
2
|
-
(()=>{var j=(t,e)=>{for(let[a,r]of Object.entries(e))if(r!==void 0)t.setAttribute(a,String(r))};function C(t,e,a={},r=[]){let o=t.createElementNS("http://www.w3.org/2000/svg",e);j(o,a);for(let n of r)if(n)o.appendChild(n);return o}function k(t,e,a,r){let o=t.createElement(e);if(a)o.className=a;if(r!==void 0)o.textContent=r;return o}var q="1.8.4",F=(t=q)=>{let e=`https://cdn.jsdelivr.net/npm/@coderline/alphatab@${t}/dist`;return{script:`${e}/alphaTab.min.js`,font:`${e}/font/Bravura.woff2`,soundFont:`${e}/soundfont/sonivox.sf3`}},
|
|
3
|
-
`).trimEnd(),!r.file&&!r.tex)throw Error("Write the music in alphaTex, or give a file: [[Song.gp]]");return r}function Y(t,e){let a=t?.cause??t;for(let r of["lexerDiagnostics","parserDiagnostics","semanticDiagnostics"]){let o=a?.[r]?.items;if(!Array.isArray(o))continue;let n=o.find((
|
|
1
|
+
/*! @dsebastien/music-notation 0.5.1 tab bundle | MIT | loads alphaTab (MPL-2.0) from jsDelivr */
|
|
2
|
+
(()=>{var j=(t,e)=>{for(let[a,r]of Object.entries(e))if(r!==void 0)t.setAttribute(a,String(r))};function C(t,e,a={},r=[]){let o=t.createElementNS("http://www.w3.org/2000/svg",e);j(o,a);for(let n of r)if(n)o.appendChild(n);return o}function k(t,e,a,r){let o=t.createElement(e);if(a)o.className=a;if(r!==void 0)o.textContent=r;return o}var q="1.8.4",F=(t=q)=>{let e=`https://cdn.jsdelivr.net/npm/@coderline/alphatab@${t}/dist`;return{script:`${e}/alphaTab.min.js`,font:`${e}/font/Bravura.woff2`,soundFont:`${e}/soundfont/sonivox.sf3`}},_=/^\s*(staves|file|tracks?)\s*:\s*(.*)$/i;function K(t,e){let a=e.split(/\r?\n/),r={staves:t,file:null,tracks:null,tex:"",texLine:0},o=0;for(;o<a.length;o++){let n=a[o];if(!n.trim()||/^\s*\/\//.test(n))continue;let s=_.exec(n);if(!s)break;let c=s[1].toLowerCase(),d=s[2].trim();if(c==="staves"){let i=d.toLowerCase();if(i!=="tab"&&i!=="score"&&i!=="both")throw Error(`staves: use tab, score or both, got "${d}"`);r.staves=i}else if(c==="file")r.file=d.replace(/^!?\[\[|\]\]$/g,"").split(/[|#]/)[0].trim()||null;else{let i=d.split(/[\s,]+/).filter(Boolean).map(Number);if(!i.length||i.some((u)=>!Number.isInteger(u)||u<1))throw Error(`tracks: use track numbers from 1, got "${d}"`);r.tracks=i}}while(o<a.length&&!a[o].trim())o++;if(r.texLine=o,r.tex=a.slice(o).join(`
|
|
3
|
+
`).trimEnd(),!r.file&&!r.tex)throw Error("Write the music in alphaTex, or give a file: [[Song.gp]]");return r}function Y(t,e){let a=t?.cause??t;for(let r of["lexerDiagnostics","parserDiagnostics","semanticDiagnostics"]){let o=a?.[r]?.items;if(!Array.isArray(o))continue;let n=o.find((s)=>s.severity===2&&s.message);if(n)return`${n.start?`line ${n.start.line+e}, column ${n.start.col}: `:""}${n.message}`}return t instanceof Error?t.message:String(t)}var J={tab:"Tab",score:"Score",both:"ScoreTab"};function L(t,e,a=1){let r=t.createElement("canvas");r.width=1,r.height=1;let o=r.getContext("2d",{willReadFrequently:!0});if(!o)return null;o.fillStyle="#000",o.fillStyle=e,o.fillRect(0,0,1,1);let n=o.getImageData(0,0,1,1).data;if(n.length<4)return null;return`rgba(${n[0]}, ${n[1]}, ${n[2]}, ${(n[3]/255*a).toFixed(3)})`}function Q(t){let e=t.ownerDocument,a=e.defaultView?.getComputedStyle(t).color||"",r=a&&L(e,a)||(/^rgba?\(/.test(a)?a:"rgba(0, 0, 0, 1)"),o=a&&L(e,a,0.6)||r;return{mainGlyphColor:r,secondaryGlyphColor:o,scoreInfoColor:r,staffLineColor:o,barSeparatorColor:r,barNumberColor:o}}var P=(t)=>{let e=t.defaultView??globalThis;return{map:(a)=>new e.Map(a),bytes:(a)=>{let r=new e.Uint8Array(a.length);return r.set(a),r}}},X={play:"M4 2.5v11l9-5.5z",pause:"M4 3h3v10H4zM9 3h3v10H9z",stop:"M4 4h8v8H4z",loop:"M3 8a5 5 0 0 1 8.5-3.5L13 6V2.5M13 8a5 5 0 0 1-8.5 3.5L3 10v3.5",metronome:"M6 2h4l3 12H3zM8 11l3-7",countIn:"M3 13h10M5 13V9M8 13V6M11 13V3",volume:"M2 6h3l4-3v10l-4-3H2zM11 5.5a3.5 3.5 0 0 1 0 5"};function E(t,e){let r=C(t,"svg",{class:`mn-play-icon${e==="play"||e==="pause"||e==="stop"?"":" mn-play-icon-loop"}`,viewBox:"0 0 16 16",width:12,height:12,"aria-hidden":"true"});return r.appendChild(C(t,"path",{d:X[e]})),r}var V=(t)=>{let e=Math.max(0,Math.round(t/1000));return`${Math.floor(e/60)}:${String(e%60).padStart(2,"0")}`};function Z(t,e,a,r){let o=k(t,"div","mn-play mn-tab-bar");o.setAttribute("role","group"),o.setAttribute("aria-label","Playback");let n={loop:!1,metronome:!1,countIn:!1,speed:1,volume:1},s=null,c=null,d=()=>(s??=new Promise((l)=>{if(e.playerReady.on(()=>l(!0)),c=()=>l(!1),e.settings.player.playerMode=a.PlayerMode.EnabledSynthesizer,e.settings.player.enableCursor=!0,typeof r.soundFont==="string")e.settings.player.soundFont=r.soundFont;if(e.updateSettings(),r.soundFont instanceof Uint8Array)e.loadSoundFont(P(t).bytes(r.soundFont),!1)}).then((l)=>{if(l)e.isLooping=n.loop,e.metronomeVolume=n.metronome?1:0,e.countInVolume=n.countIn?1:0,e.playbackSpeed=n.speed,e.masterVolume=n.volume;return l}),s),i=(l,h,y)=>{let p=k(t,"button");return p.type="button",p.title=h,p.setAttribute("aria-label",h),p.appendChild(E(t,l)),p.addEventListener("click",y),o.appendChild(p),p},u=(l)=>{if(e.player)l()},f=(l,h,y)=>{let p=i(l,h,()=>{n[y]=!n[y],u(()=>{e.isLooping=n.loop,e.metronomeVolume=n.metronome?1:0,e.countInVolume=n.countIn?1:0}),p.classList.toggle("mn-on",n[y]),p.setAttribute("aria-pressed",String(n[y]))});p.setAttribute("aria-pressed","false")},m=i("play","Play",()=>{d().then((l)=>{if(l)e.playPause()})});i("stop","Stop",()=>u(()=>e.stop())),f("loop","Loop","loop"),f("metronome","Metronome","metronome"),f("countIn","Count-in","countIn");let b=(l,h,y,p,v,w,x="")=>{let T=k(t,"label","mn-play-field"),S=k(t,"span","mn-play-label");S.setAttribute("aria-hidden","true"),S.append(h);let A=k(t,"input");A.type="range",A.min=String(y),A.max=String(p),A.value=String(v),A.setAttribute("aria-label",l),T.title=`${l}: ${v}${x}`,A.addEventListener("input",()=>{w(Number(A.value)),T.title=`${l}: ${A.value}${x}`}),T.append(S,A),o.appendChild(T)};b("Speed","%",25,150,100,(l)=>{n.speed=l/100,u(()=>e.playbackSpeed=n.speed)},"%"),b("Volume",E(t,"volume"),0,100,100,(l)=>{n.volume=l/100,u(()=>e.masterVolume=n.volume)});let g=k(t,"span","mn-tab-time","");return o.appendChild(g),e.playerStateChanged.on((l)=>{let h=Number(l.state)===Number(a.synth.PlayerState.Playing);m.replaceChildren(E(t,h?"pause":"play")),m.title=h?"Pause":"Play",m.setAttribute("aria-label",m.title)}),e.playerPositionChanged.on((l)=>{g.textContent=`${V(l.currentTime)} / ${V(l.endTime)}`}),{bar:o,failed:(l)=>{c?.(),m.disabled=!0,m.title=`Can't play: ${l}`,m.setAttribute("aria-label",m.title)}}}function O(t,e,a,r){let o=a.ownerDocument,n=k(o,"div",`mn-block mn-block-${t} mn-tab`);a.appendChild(n);let s=null,c=!1,d=!1,i=()=>{},u=new Promise((m)=>{i=(b)=>{if(d)return;d=!0,m(b)}}),f=(m)=>{n.replaceChildren(k(o,"div","mn-error-message",`${t}: ${m}`)),n.classList.add("mn-error"),i({ok:!1,error:m})};if(!e.trim())n.classList.add("mn-empty"),i({ok:!0,error:null});else(async()=>{await Promise.resolve();let m=K(t,e),b=r.alphaTab;if(!b?.AlphaTabApi)throw Error("alphaTab isn't loaded");let g=k(o,"div","mn-tab-surface");n.appendChild(g);let{assets:l}=r,h=P(o),y={core:{useWorkers:Boolean(l.scriptFile),...l.scriptFile?{scriptFile:l.scriptFile}:{},smuflFontSources:h.map([[b.FontFileFormat.Woff2,l.font]]),logLevel:b.LogLevel.Warning},display:{staveProfile:J[m.staves],resources:Q(n)},player:{playerMode:b.PlayerMode.Disabled,enableUserInteraction:!0}},p=new b.AlphaTabApi(g,y);if(s=p,c){p.destroy();return}r.onApi?.(p);let v=null;p.renderFinished.on(()=>{if(d||c)return;if(r.player!==!1)v=Z(o,p,b,l),n.appendChild(v.bar),n.addEventListener("pointerdown",(x)=>{if(x.pointerType!=="touch"||v?.bar.contains(x.target))return;n.classList.toggle("mn-play-open")});i({ok:!0,error:null})}),p.error.on((x)=>{let T=Y(x,m.texLine);if(d)v?.failed(T);else f(T)});let w=m.tracks?.map((x)=>x-1);if(m.file){let x;try{x=r.resolveFile?await r.resolveFile(m.file):null}catch(S){throw Error(`Couldn't read "${m.file}": ${S instanceof Error?S.message:String(S)}`)}if(c)return;if(!x)throw Error(`Couldn't find the file "${m.file}"`);if(!p.load(h.bytes(x),w))throw Error(`Couldn't read "${m.file}": not a score file alphaTab knows`)}else p.tex(m.tex,w??"all")})().catch((m)=>{if(!c)f(m instanceof Error?m.message:String(m))});return{el:n,ready:u,get api(){return s},destroy(){c=!0;try{s?.destroy()}catch{}n.remove(),i({ok:!1,error:null})}}}var ee={guitar:25,piano:0};function te(t,e){return{play(a,{sound:r,volume:o,onEnd:n}){let s=e(),c=s?.player;if(!s||!c)throw Error("The soundfont player is not ready");let d=new t.midi.MidiFile,i=new t.midi.AlphaSynthMidiFileHandler(d,!0),u=(g)=>Math.round(g*d.division);i.addTempo(0,60),i.addProgramChange(0,0,0,ee[r]);let f=Number.isFinite(o)?Math.min(1,Math.max(0,o)):0.8;s.masterVolume=f;for(let g of a)i.addNote(0,u(g.time),Math.max(1,u(g.duration)),g.midi,100,0);let m=Math.max(...a.map((g)=>g.time+g.duration),0);s.playbackSpeed=1,c.playOneTimeMidiFile(d);let b=setTimeout(()=>n?.(),(m+0.2)*1000);return{stop(){clearTimeout(b),c.stop()}}}}}function R(t,e,a,r){let o=null,n=null,s=null,c=()=>{if(s)return s;n=k(a,"div","mn-tab-hidden"),n.setAttribute("aria-hidden","true"),a.body.appendChild(n);let i=P(a);o=new t.AlphaTabApi(n,{core:{useWorkers:Boolean(e.scriptFile),...e.scriptFile?{scriptFile:e.scriptFile}:{},smuflFontSources:i.map([[t.FontFileFormat.Woff2,e.font]])},player:{playerMode:t.PlayerMode.EnabledSynthesizer,...typeof e.soundFont==="string"?{soundFont:e.soundFont}:{}}});let u=o;if(s=new Promise((f)=>{u.soundFontLoaded.on(()=>f(!0)),u.error.on(()=>f(!1))}),e.soundFont instanceof Uint8Array)u.loadSoundFont(i.bytes(e.soundFont),!1);return u.tex("r"),s},d=te(t,()=>o);return{play(i,u){let f=null,m=!1;return c().then((b)=>{if(m)return;try{if(b){f=d.play(i,u);return}}catch{}if(r)f=r.play(i,u)}),{stop(){m=!0,f?.stop()}}},destroy(){try{o?.destroy()}catch{}n?.remove(),o=null,s=null}}}var I=`/* Music notation. Colors follow Obsidian's theme variables when present, with fallbacks for any page. */
|
|
4
4
|
.mn-block,
|
|
5
5
|
.mn-svg,
|
|
6
6
|
.mn-chart {
|
|
@@ -163,7 +163,7 @@
|
|
|
163
163
|
all: unset;
|
|
164
164
|
appearance: none;
|
|
165
165
|
width: 44px;
|
|
166
|
-
/* The input is the track
|
|
166
|
+
/* The input is the track (3px); the thumb is pulled up to its middle below. */
|
|
167
167
|
height: 3px;
|
|
168
168
|
margin: 0;
|
|
169
169
|
border-radius: 2px;
|
|
@@ -181,13 +181,21 @@
|
|
|
181
181
|
appearance: none;
|
|
182
182
|
width: 9px;
|
|
183
183
|
height: 9px;
|
|
184
|
-
|
|
184
|
+
/* Centered on the 3px track. Hosts may move thumbs (Obsidian: relative, top -6px). */
|
|
185
|
+
position: static;
|
|
186
|
+
top: 0;
|
|
187
|
+
margin-top: -3px;
|
|
185
188
|
border: none;
|
|
186
189
|
border-radius: 50%;
|
|
187
190
|
background: var(--mn-accent);
|
|
188
191
|
box-shadow: none;
|
|
189
192
|
}
|
|
190
193
|
|
|
194
|
+
/* Hosts ring a focused thumb (Obsidian): only for keyboard focus here, not after a click. */
|
|
195
|
+
.mn-play input[type='range']:focus:not(:focus-visible)::-webkit-slider-thumb {
|
|
196
|
+
box-shadow: none;
|
|
197
|
+
}
|
|
198
|
+
|
|
191
199
|
.mn-play input[type='range']::-moz-range-track {
|
|
192
200
|
height: 3px;
|
|
193
201
|
border: none;
|
|
@@ -524,4 +532,4 @@
|
|
|
524
532
|
color: var(--text-error, #c92a2a);
|
|
525
533
|
font-size: 0.9em;
|
|
526
534
|
}
|
|
527
|
-
`;var oe=I,D="music-notation-styles";function G(t){if(t.getElementById(D))return;let e=t.createElement("style");e.id=D,e.textContent=oe,t.head.appendChild(e)}var re=(t)=>t.reduce((e,a)=>Math.max(e,a.time+a.duration),0),B=(t)=>440*Math.pow(2,(t-69)/12);function ae(t,e,a,r=Math.random){let o=new Float32Array(Math.round(e*a)),n=Math.max(2,Math.round(e/B(t))),
|
|
535
|
+
`;var oe=I,D="music-notation-styles";function G(t){if(t.getElementById(D))return;let e=t.createElement("style");e.id=D,e.textContent=oe,t.head.appendChild(e)}var re=(t)=>t.reduce((e,a)=>Math.max(e,a.time+a.duration),0),B=(t)=>440*Math.pow(2,(t-69)/12);function ae(t,e,a,r=Math.random){let o=new Float32Array(Math.round(e*a)),n=Math.max(2,Math.round(e/B(t))),s=new Float32Array(n);for(let i=0;i<n;i++)s[i]=r()*2-1;let c=0.996+Math.min(0.0035,0.00004*(90-t)),d=0;for(let i=0;i<o.length;i++){let u=(d+1)%n,f=s[d];s[d]=c*0.5*(f+s[u]),o[i]=f*0.5,d=u}return o}var H=new WeakMap;function ie(t){let e=H.get(t);if(e)return e;let a=t.AudioContext;if(!a)return null;let r=new a;return H.set(t,r),r}var se=4,z=[0,1,0.5,0.3,0.15,0.08,0.04];function U(t){let e=new Map,a=new WeakMap,r=(n,s)=>{let c=e.get(s);if(!c){let d=ae(s,n.sampleRate,se);c=n.createBuffer(1,d.length,n.sampleRate),c.getChannelData(0).set(d),e.set(s,c)}return c},o=(n)=>{let s=a.get(n);if(!s){let c=new Float32Array(z.length);s=n.createPeriodicWave(c,Float32Array.from(z)),a.set(n,s)}return s};return{play(n,{sound:s,volume:c,onEnd:d}){let i=ie(t);if(!i||!n.length)return{stop(){}};if(i.resume(),s==="guitar")for(let l of n)r(i,l.midi);let u=i.createGain();u.gain.value=Number.isFinite(c)?Math.min(1,Math.max(0,c)):0.8,u.connect(i.destination);let f=i.currentTime+0.05,m=n.map((l)=>{let h=f+l.time,y=h+l.duration,p=i.createGain();p.connect(u);let v;if(s==="guitar"){let w=i.createBufferSource();w.buffer=r(i,l.midi),v=w,p.gain.setValueAtTime(1,y)}else{let w=i.createOscillator();w.setPeriodicWave(o(i)),w.frequency.value=B(l.midi),v=w,p.gain.setValueAtTime(0,h),p.gain.linearRampToValueAtTime(0.35,h+0.005),p.gain.exponentialRampToValueAtTime(0.02,Math.max(h+0.01,y))}return p.gain.linearRampToValueAtTime(0,y+0.08),v.connect(p),v.start(h),v.stop(y+0.1),v}),b=()=>u.disconnect(),g=t.setTimeout(()=>{b(),d?.()},(re(n)+0.15)*1000);return{stop(){t.clearTimeout(g);for(let l of m)try{l.stop()}catch{}b()}}}}}var le="pre > code.language-tab, pre > code.language-score",M=new Map,N=null;function me(t){let e=t.defaultView;if(e.alphaTab)return Promise.resolve(e.alphaTab);return N??=new Promise((a,r)=>{let o=t.createElement("script");o.src=F().script,o.onload=()=>e.alphaTab?a(e.alphaTab):r(Error("alphaTab")),o.onerror=()=>{N=null,r(Error("Couldn't load alphaTab"))},t.head.appendChild(o)}),N}function W(t){for(let[a,{result:r,holder:o}]of M)if(!a.isConnected||!r.el.isConnected){if(r.destroy(),o.remove(),M.delete(a),a.isConnected)a.style.removeProperty("display")}let e=[...t.querySelectorAll(le)].filter((a)=>{let r=a.parentElement,o=M.get(r);if(!o)return r.dataset.mpRendered!=="pending";if(o.source===(a.textContent??""))return!1;return o.result.destroy(),o.holder.remove(),M.delete(r),!0});if(!e.length)return;for(let a of e)a.parentElement.dataset.mpRendered="pending";me(t).then((a)=>{let r=F(),o={scriptFile:null,font:r.font,soundFont:r.soundFont},n=t.defaultView;n.musicNotationSoundfontPlayer??=R(a,o,t,U(n));for(let s of e){let c=s.parentElement;if(!c.parentElement)continue;let d=s.classList.contains("language-score")?"score":"tab",i=s.textContent??"",u=t.createElement("div");u.className="mn-tab-holder",c.insertAdjacentElement("afterend",u);let f=O(d,i,u,{alphaTab:a,assets:o});M.set(c,{result:f,source:i,holder:u}),f.ready.then(({ok:m})=>{if(c.dataset.mpRendered=m?"done":"error",m&&f.el.isConnected)c.style.display="none"})}}).catch(()=>{for(let a of e)a.parentElement.dataset.mpRendered="error"})}if(!window.musicNotationTabLoaded){window.musicNotationTabLoaded=!0;let t=()=>{G(document),W(document);let e=!1;new MutationObserver(()=>{if(e)return;e=!0,requestAnimationFrame(()=>{e=!1,W(document)})}).observe(document.body,{childList:!0,subtree:!0,characterData:!0})};if(document.readyState==="loading")document.addEventListener("DOMContentLoaded",t);else t()}})();
|
|
@@ -2,9 +2,10 @@ import { type NotePlayer, type Sound } from './player.js';
|
|
|
2
2
|
export interface PlayOptions {
|
|
3
3
|
/**
|
|
4
4
|
* Plays the notes: a player, or a function returning one at each play (null for the default),
|
|
5
|
-
* so a soundfont loaded later is used once it's there. The
|
|
5
|
+
* so a soundfont loaded later is used once it's there. The function gets the diagram's window
|
|
6
|
+
* (popouts have their own). The light synthesizer by default.
|
|
6
7
|
*/
|
|
7
|
-
player?: NotePlayer | (() => NotePlayer | null | undefined);
|
|
8
|
+
player?: NotePlayer | ((win: Window) => NotePlayer | null | undefined);
|
|
8
9
|
/** Guitar or piano; by default the diagram's (piano for keyboards and piano chords). */
|
|
9
10
|
sound?: Sound;
|
|
10
11
|
/** 0 to 1, 0.8 by default. */
|
package/dist/publish/index.js
CHANGED
|
@@ -1972,7 +1972,7 @@ function lightFor(win) {
|
|
|
1972
1972
|
return player;
|
|
1973
1973
|
}
|
|
1974
1974
|
function playerFor(win, options) {
|
|
1975
|
-
const given = typeof options.player === "function" ? options.player() : options.player;
|
|
1975
|
+
const given = typeof options.player === "function" ? options.player(win) : options.player;
|
|
1976
1976
|
return given ?? lightFor(win);
|
|
1977
1977
|
}
|
|
1978
1978
|
function dataOf(diagram) {
|
|
@@ -3593,5 +3593,5 @@ export {
|
|
|
3593
3593
|
renderAll
|
|
3594
3594
|
};
|
|
3595
3595
|
|
|
3596
|
-
//# debugId=
|
|
3596
|
+
//# debugId=D4DE4A0A3909210364756E2164756E21
|
|
3597
3597
|
//# sourceMappingURL=index.js.map
|