UNPKG

@ioris/core

Version:

This package provides the core functionality for the [@ioris](https://www.npmjs.com/search?q=%40ioris) ecosystem for managing music lyrics with time synchronization.

4 lines (3 loc) 12.1 kB
var g={WHITESPACE:"whitespace",ALPHABET:"alphabet",NUMBER:"number",KANJI:"kanji",HIRAGANA:"hiragana",KATAKANA:"katakana",OTHER:"other"};function oe(e){return/^\s+$/.test(e)?g.WHITESPACE:/^[a-zA-Z]+$/.test(e)?g.ALPHABET:/^[0-9]+$/.test(e)?g.NUMBER:/^[\u4E00-\u9FFF]+$/.test(e)?g.KANJI:/^[\u3040-\u309F]+$/.test(e)?g.HIRAGANA:/^[\u30A0-\u30FF]+$/.test(e)?g.KATAKANA:g.OTHER}function z(e){let r=`char-${crypto.randomUUID()}`,n=oe(e.text);return Object.freeze({id:r,wordID:e.wordID,text:e.text,type:n,position:e.position,begin:e.begin,end:e.end})}function se(e){return e.wordID!==void 0}function S(e){let r=se(e.timeline)?e.timeline.wordID:`word-${crypto.randomUUID()}`,n={...e.timeline,wordID:r,hasNewLine:e.timeline.hasNewLine??!1,hasWhitespace:e.timeline.hasWhitespace??!1},t=n.text.split(""),o=(n.end-n.begin)/t.length,a=t.map((s,d)=>{let f=d+1;return z({wordID:r,position:f,text:s,begin:n.begin+d*o,end:n.begin+f*o})});return Object.freeze({id:r,lineID:e.lineID,position:e.position,timeline:n,chars:a})}function ae(e,r,n,t){if(/^\s+$/.test(e.text))return;let o=r?/^\s+$/.test(r.text):!1,a=e.hasWhitespace||o;return t!==!1&&n&&n.timeline.end-e.begin<=.1&&!n.timeline.hasNewLine&&!n.timeline.hasWhitespace?{isJoint:!0,hasWhitespace:a}:{isJoint:!1,hasWhitespace:a}}function H(e){let r=`line-${crypto.randomUUID()}`,n=e.timelines.sort((i,o)=>i.begin-o.begin),t=[];for(let i=0;i<n.length;i++){let o=n[i],a=n[i+1],s=t[t.length-1],d=ae(o,a,s,e.jointNearWord===!0);if(!d)continue;let{isJoint:f,hasWhitespace:m}=d;if(s&&f){let u=S({lineID:r,position:t.length,timeline:{wordID:s.id,begin:s.timeline.begin,end:o.end,text:s.timeline.text+o.text,hasNewLine:o.hasNewLine===!0,hasWhitespace:m}});t[t.length-1]=u}else{let u=S({lineID:r,position:t.length+1,timeline:{...o,hasWhitespace:m}});t.push(u)}}return Object.freeze({id:r,position:e.position,words:t})}async function k(e){let r=`paragraph-${crypto.randomUUID()}`,n=e.timelines;e.paragraphTokenizer&&(n=await e.paragraphTokenizer(n));let i=n.reduce((s,d)=>{let f=s[s.length-1],m=f?f[f.length-1]:null,u=d[0];return u?m&&(m.end>u.begin||m.end===u.begin&&m.text.length<6)?(m.end=u.end,m.text+=` ${u.text}`,s):(s.push(d),s):(console.error("thisFirst is undefined",n,d),s)},[]).map(async(s,d)=>{let f=d+1;return e.lineTokenizer?await e.lineTokenizer({position:f,timelines:s}):new Map([[f,{jointNearWord:!0,position:f,timelines:s}]])}),o=await Promise.all(i),a=[];for(let s of o)for(let[,d]of s){let f=a.length+1,m=H({...d,position:f});a.push(m)}return Object.freeze({id:r,position:e.position,lines:a})}async function U(e){let r=e.id?e.id:e.initID?`lyric-${crypto.randomUUID()}`:"",n=e.timelines.map(async(i,o)=>{let a=o+1;return await k({position:a,timelines:i,lineTokenizer:e.lineTokenizer,paragraphTokenizer:e.paragraphTokenizer})}),t=await Promise.all(n);return Object.freeze({id:r,resourceID:e.resourceID,duration:Number(e.duration.toFixed(2)),offsetSec:e.offsetSec??0,paragraphs:t})}function j(e){return e.paragraphs.flatMap(r=>r.lines.flatMap(n=>n.words))}function x(e){return e.paragraphs.flatMap(r=>r.lines).sort((r,n)=>L(r)-L(n))}function b(e){return[...e.paragraphs].sort((r,n)=>T(r)-T(n))}function h(e){return[...e.chars]}function $e(e){return e.words.flatMap(r=>h(r))}function p(e){return[...e.words]}function W(e){return[...e.lines]}function w(e){return e.begin}function E(e){return e.end}function c(e){return e.timeline.begin}function l(e){return e.timeline.end}function L(e){return e.words[0]?.timeline.begin||0}function P(e){return e.words[e.words.length-1]?.timeline.end||0}function T(e){return e.lines[0]?L(e.lines[0]):0}function C(e){let r=e.lines[e.lines.length-1];return r?P(r):0}function ve(e){let r=w(e),n=E(e);return r>=n&&console.error(`Cannot calculate duration of invalid char: ${e.id} ${r}-${n}`),n-r}function N(e){let r=c(e),n=l(e);return r>=n&&console.error(`Cannot calculate duration of invalid word: ${e.id} ${r}-${n}`),n-r}function M(e){let r=L(e),n=P(e);return r>=n&&console.error(`Cannot calculate duration of invalid line: ${e.id} ${r}-${n}`),n-r}function q(e){let r=T(e),n=C(e);return r>=n&&console.error(`Cannot calculate duration of invalid paragraph: ${e.id} ${r}-${n}`),n-r}function de(e){return e.chars.map(r=>r.text).join("")}function Be(e){return e.words.map(r=>`${de(r)}${!r.timeline.hasNewLine&&r.timeline.hasWhitespace?" ":""}${r.timeline.hasNewLine?` `:""}`).join("")}function I(e,r,n,t={}){let i=t.offset??0;return t.equal??!0?e<=n+i&&n+i<=r:e<n+i&&n+i<r}function Oe(e,r){return e.paragraphs.find(n=>n.position===r)}function Re(e,r){return e.lines.find(n=>n.position===r)}function Fe(e,r){return e.words.find(n=>n.position===r)}function ze(e,r){return e.chars.find(n=>n.position===r)}function $(e,r,n={offset:e.offsetSec,equal:!0}){let t=n.offset??e.offsetSec,i=n.equal??!0;return b(e).find(o=>{let a=T(o),s=C(o);return I(a,s,r,{offset:t,equal:i})})}function A(e,r,n={offset:e.offsetSec,equal:!0}){let t=n.offset??e.offsetSec,i=n.equal??!0,o=$(e,r,{offset:t,equal:i});if(o)return W(o).find(a=>{let s=L(a),d=P(a);return I(s,d,r,{offset:t,equal:i})})}function v(e,r,n={offset:e.offsetSec,equal:!0}){let t=n.offset??e.offsetSec,i=n.equal??!0,o=A(e,r,{offset:t,equal:i});if(o)return p(o).sort((a,s)=>c(s)-c(a)).find(a=>{let s=c(a),d=l(a);return I(s,d,r,{offset:t,equal:i})})}function J(e,r,n={offset:e.offsetSec,equal:!0}){let t=n.offset??e.offsetSec,i=n.equal??!0,o=v(e,r,{offset:t,equal:i});if(o)return h(o).sort((a,s)=>w(s)-w(a)).find(a=>{let s=w(a),d=E(a);return I(s,d,r,{offset:t,equal:i})})}function Ue(e,r){return b(r).find(n=>W(n).some(t=>t.id===e.id))}function je(e,r){return x(r).find(n=>p(n).some(t=>t.id===e.id))}function qe(e,r){for(let n of b(r))for(let t of W(n))for(let i of p(t))if(h(i).some(o=>o.id===e.id))return i}function G(e,r,n={offset:e.offsetSec}){let t=n.offset??0;return b(e).find(i=>T(i)>r+t)}function B(e,r,n={offset:e.offsetSec}){let t=n.offset??0;return x(e).find(i=>L(i)>r+t)}function K(e,r,n={offset:e.offsetSec}){let t=n.offset??0,i=A(e,r);if(i){let a=p(i).sort((s,d)=>c(s)-c(d)).find(s=>c(s)>r+t);if(a)return a}let o=B(e,r,{offset:t});return o?p(o)[0]:void 0}function V(e,r,n={offset:e.offsetSec}){let t=n.offset??0;return b(e).reverse().find(i=>C(i)<r+t)}function O(e,r,n={offset:e.offsetSec}){let t=n.offset??0;return x(e).reverse().find(i=>P(i)<r+t)}function Y(e,r,n={offset:e.offsetSec}){let t=n.offset??0,i=A(e,r);if(i){let a=p(i).sort((s,d)=>c(d)-c(s)).find(s=>l(s)<r+t);if(a)return a}let o=O(e,r,{offset:t});if(o){let a=p(o);return a[a.length-1]}}function Ve(e){return p(e)[0]}function Ye(e){let r=p(e);return r[r.length-1]}function _e(e,r){return x(r).findIndex(n=>n.id===e.id)+1}function Ze(e,r){for(let n of b(r)){let i=W(n).findIndex(o=>o.id===e.id);if(i!==-1)return i+1}}function R(e){let r=e.map(t=>{let i=t.duration();return t.chars?t.chars().reduce((a,s)=>s.type===g.WHITESPACE?a:a+(s.type===g.ALPHABET||s.type===g.NUMBER?.5:1),0)/i:0});r.sort((t,i)=>t-i);let n=Math.floor(r.length/2);return Number.parseFloat((r.length%2?r[n]:(r[n-1]+r[n])/2).toFixed(2))}function tr(e){let r=N(e);return h(e).reduce((n,t)=>t.type===g.WHITESPACE?n:n+(t.type===g.ALPHABET||t.type===g.NUMBER?.5:1),0)/r}function ue(e){let r=p(e).map(n=>({duration:()=>N(n),chars:()=>h(n)}));return R(r)}function fe(e){let r=W(e).map(n=>({duration:()=>M(n),chars:()=>p(n).flatMap(t=>h(t))}));return R(r)}function pe(e){let r=b(e).map(n=>({duration:()=>q(n),chars:()=>W(n).flatMap(t=>p(t).flatMap(i=>h(i)))}));return R(r)}function ce(e){let r=j(e);return r.reduce((n,t,i)=>{let o=i===0,a=i===r.length-1;o&&c(t)>0&&n.push({begin:0,end:c(t),duration:Number(c(t).toFixed(2))}),a&&e.duration-l(t)>0&&n.push({begin:l(t),end:e.duration,duration:Number((e.duration-l(t)).toFixed(2))});let s=r[i-1];return s&&c(t)-l(s)>0&&n.push({begin:l(s),end:c(t),duration:Number((c(t)-l(s)).toFixed(2))}),n},[])}function ir(e,r){return ce(e).some(({begin:t,end:i})=>r>=t&&r<=i)}function me(e){let r=W(e).map(n=>M(n));return r.reduce((n,t)=>n+t,0)/r.length}function or(e,r,n={offset:e.offsetSec}){let t=$(e,r,n),i=G(e,r,n),o=V(e,r,n),a=x(e),s=A(e,r,n),d=t?W(t).length-1:void 0,f=B(e,r,n),m=K(e,r,n),u=O(e,r,n),y=Y(e,r,n),_=v(e,r,n),Z=J(e,r,n),F=s&&u?L(s)-P(u):void 0,Q=(t?me(t)<1.5:void 0)===!0&&F!==void 0&&F<.1,X=t?C(t)<r:!0,ee=f?L(f)-r:void 0,re=a.length-1,ne=pe(e),te=t?fe(t):void 0,ie=s?ue(s):void 0;return{currentChar:Z,currentLine:s,currentParagraph:t,currentWord:_,isConnected:Q,isParagraphFinishMotion:X,lastLineIndex:re,lastLineIndexInParagraph:d,nextLine:f,nextWord:m,nextParagraph:i,nextWaitingTime:ee,prevLine:u,prevWord:y,prevParagraph:o,lyricTextPerSecond:ne,paragraphTextPerSecond:te,lineTextPerSecond:ie}}function D(e){let r=p(e),n=new Map,t=1,i=0;for(let o=0;o<r.length;o++){let a=r[o];r[o-1]?.timeline.hasNewLine?(t++,i=1):i++,n.set(a.id,{row:t,column:i,word:a})}return n}function ge(e){let r=D(e);return Array.from(r.values()).reduce((n,t)=>(n.has(t.row)||n.set(t.row,[]),n.get(t.row)?.push(t.word),n),new Map)}function dr(e,r){return ge(e).get(r)||[]}function ur(e,r){return D(e).get(r)?.row}function fr(e){let r=D(e);return Math.max(...Array.from(r.values()).map(n=>n.row))}function pr(e){let r=p(e).flatMap(o=>h(o)),n=p(e),t=D(e),i=new Map;for(let o of r){let a=n.find(u=>h(u).some(y=>y.id===o.id));if(!a)throw new Error(`word not found for character ID: ${o.id}`);let s=r.findIndex(u=>u.id===o.id)+1,d=t.get(a.id);if(!d)throw new Error(`wordPosition not found for word ID: ${a.id}`);let m=n.filter(u=>t.get(u.id)?.row===d.row&&u.timeline.begin<a.timeline.begin).reduce((u,y)=>u+h(y).length,0)+o.position;i.set(o.id,{row:d.row,column:m,inLinePosition:s})}return i}function cr(e){let r=x(e),n=new Map;for(let t of r){let i=D(t),o=new Map;for(let{row:a,column:s,word:d}of i.values())o.has(a)||o.set(a,new Map),o.get(a)?.set(s,d);n.set(t.id,o)}return n}async function hr(e,r){let n=r.resourceID||e.resourceID,t=r.duration?Number(r.duration.toFixed(2)):e.duration,i=r.offsetSec!==void 0?r.offsetSec:e.offsetSec;return r.timelines===void 0?{...e,resourceID:n,duration:t,offsetSec:i}:await U({id:e.id,resourceID:n,duration:t,timelines:r.timelines,offsetSec:i})}function lr(e){return e.paragraphs.map(r=>r.lines.map(n=>n.words.map(t=>t.timeline)))}function Lr(e){return e.paragraphs.flatMap((r,n)=>r.lines.map((t,i)=>{let o=t.words[0];if(!o)throw new Error(`firstWord is undefined in paragraph ${n}, line ${i}`);let a=t.words[t.words.length-1];if(!a)throw new Error(`lastWord is undefined in paragraph ${n}, line ${i}`);return{wordID:o.id,begin:o.timeline.begin,end:a.timeline.end,text:t.words.map(s=>s.timeline.text).join(""),hasNewLine:a.timeline.hasNewLine,hasWhitespace:a.timeline.hasWhitespace}}))}export{g as CHAR_TYPES,R as calculateSpeed,z as createChar,H as createLine,U as createLyric,k as createParagraph,S as createWord,ze as findCharAt,Re as findLineAt,Oe as findParagraphAt,Fe as findWordAt,w as getCharBegin,ve as getCharDuration,E as getCharEnd,pr as getCharPositions,J as getCurrentChar,A as getCurrentLine,je as getCurrentLineFromWord,$ as getCurrentParagraph,Ue as getCurrentParagraphFromLine,or as getCurrentSummary,v as getCurrentWord,qe as getCurrentWordFromChar,Ve as getFirstWord,Ye as getLastWord,L as getLineBegin,$e as getLineChars,M as getLineDuration,P as getLineEnd,_e as getLinePositionInLyric,Ze as getLinePositionInParagraph,ue as getLineSpeed,Be as getLineText,p as getLineWords,x as getLines,pe as getLyricSpeed,fr as getMaxRowPosition,B as getNextLine,G as getNextParagraph,K as getNextWord,me as getParagraphAverageLineDuration,T as getParagraphBegin,q as getParagraphDuration,C as getParagraphEnd,W as getParagraphLines,fe as getParagraphSpeed,b as getParagraphs,O as getPrevLine,V as getPrevParagraph,Y as getPrevWord,dr as getRowWords,lr as getTimelines,Lr as getTimelinesByLine,ce as getVoidPeriods,c as getWordBegin,h as getWordChars,N as getWordDuration,l as getWordEnd,D as getWordGridPositions,ur as getWordRowPosition,tr as getWordSpeed,de as getWordText,j as getWords,cr as getWordsByLineIDAndRowPosition,ge as getWordsByRow,I as isCurrentTime,ir as isVoidTime,hr as updateLyric}; //# sourceMappingURL=index.mjs.map