Seanitzel/Note-Art

View on GitHub
bundle.js

Summary

Maintainability
A
0 mins
Test Coverage
(()=>{"use strict";var t={d:(e,n)=>{for(var r in n)t.o(n,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:n[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};function n(t){return t.charAt(0).toUpperCase()+t.slice(1)}function r(t){return Number(Number.parseFloat(String(t)).toFixed(2))}function s([...t],e,n){const r=t[e];return t[e]=t[n],t[n]=r,t}function i([...t],e){const n=[];let r=0;for(;r<e;)n.push(t.shift()),++r;return[...t,...n]}function o(t,e,n){for(;t.includes(e);){const r=t.length;for(let s=0;s<r;++s)if(t.substring(s,s+e.length)===e){t=`${t.slice(0,s)}${n}${t.slice(s+e.length)}`;break}}return t}function a(t,e){return t.split(e).length-1}function c([...t],e){for(;t.length<e;)t=t.concat([...t]);return t.slice(0,e)}function u(t){if(!Array.isArray(t)||!t.every((t=>Array.isArray(t))))throw new Error(`Expected ${t} and each of it's elements to be an array`);return t.reduce(((t,e)=>t.length>e.length?t:e),[])}function h(t){return!isNaN(parseInt(t))}t.r(e),t.d(e,{FLAT_CLASS_NOTES:()=>p,INTERVALS:()=>S,Measure:()=>ht,NOTE_DURATIONS:()=>M,NOTE_DURATIONS_AS_SIZE_IN_MEASURE:()=>N,NUMBER_OF_PITCH_CLASSES:()=>l,OCTAVES_ON_PIANO:()=>_,PITCH_CLASSES:()=>b,PITCH_CLASS_LETTERS:()=>g,PITCH_STANDARD:()=>d,SEMITONE:()=>f,SHARP_CLASS_NOTES:()=>m,Score:()=>lt,ScoreHandler:()=>ft,TIME_SIG_NUMERATORS:()=>C,centsOffFromFreq:()=>st,enharmonicPitchClass:()=>G,extractOctave:()=>P,extractPitchClass:()=>D,firstToUpper:()=>n,fitArrayToSize:()=>c,freqFromMidi:()=>et,freqFromPitch:()=>Y,freqToFloatMidi:()=>nt,freqToMidi:()=>rt,freqToPitch:()=>tt,getChordFromScale:()=>Ct,getClassSet:()=>E,getInterval:()=>A,getNotesInterval:()=>x,getPatternFromNotes:()=>X,getPatternFromPitchClasses:()=>W,getPitchClassIndex:()=>L,getPitchClassSet:()=>y,getPitchClassesInterval:()=>$,highestNote:()=>k,highestNoteFromArray:()=>Q,intervalsToNotes:()=>St,invertChord:()=>_t,isDuration:()=>R,isNote:()=>q,isNumberAsString:()=>h,isPitchClass:()=>j,isRest:()=>B,longestArray:()=>u,lowestNote:()=>Z,lowestNoteFromArray:()=>K,lowestPitch:()=>J,mapString:()=>o,maxInterval:()=>H,midiToFreq:()=>it,midiToPitch:()=>ot,normalizeNote:()=>I,normalizePitchClass:()=>F,noteFromFrequency:()=>bt,noteToObject:()=>w,notesInRange:()=>z,notesToPitchClasses:()=>T,occurrencesInString:()=>a,pitchClassesToNotes:()=>O,pitchClassesToNumbers:()=>v,pitchClassesToPianoChordNotes:()=>ct,pitchToMidi:()=>at,rearrangeArray:()=>i,scaleDegree:()=>Mt,scaleToChords:()=>Nt,spellScale:()=>ut,switchMembers:()=>s,toFlat:()=>V,toSemitones:()=>U,transpose:()=>gt,transposeNote:()=>pt,transposePitchClass:()=>mt,twoDigitFormat:()=>r});const d={pitchClass:"A",octave:4,midi:69,frequency:440},l=12,f=Math.pow(2,1/12),m=["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"],p=["C","Db","D","Eb","E","F","Gb","G","Ab","A","Bb","B"],g=["C","D","E","F","G","A","B"],b=["C","C#","Db","D","D#","Eb","E","F","F#","Gb","G","G#","Ab","A","A#","Bb","B"],S={U:0,m2:1,M2:2,m3:3,M3:4,P4:5,aug4:6,dim5:6,P5:7,m6:8,M6:9,m7:10,M7:11,P8:12},_=8,M=[1,2,4,8,16,32,64],C=[1,2,3,4,5,6,7,8,9,10,11,12],N={"1n":64,"2n":32,"2n.":48,"2t":21,"4n":16,"8n":8,"16n":4,"32n":2,"64n":1};function v(t){return t.map((t=>L(t)))}function O(t,e){return t.map((t=>`${t}${e}`))}function T(t){return t.map((t=>w(t).pitchClass))}function $(t,e){const n=F(t),r=F(e),s=L(n),i=L(r);return s-i<=0?Math.abs(s-i):12-(s-i)}function x(t,e){const{pitchClass:n,octave:r}=w(t),{pitchClass:s,octave:i}=w(e),o=F(n),a=F(s),c=$(o,a),u=i-r,h=L(o);return 12*(u-(L(a)>=h?0:1))+c}function A(t,e){return q(t)?x(t,e):$(t,e)}function E(t){return t.includes("#")?"#":"b"}function y(t){return"#"===t?m:"b"===t?p:b}function P(t){return t[t.length-1]}function D(t){return t.slice(0,t.length-1)}function F(t){const e=n(t[0]),r=t[1];let s,i,o;switch(r){case"#":return["E","B"].includes(e)?m[(m.indexOf(e)+1)%12]:`${e}${r}`;case"x":return s=2*a(t,"x"),"#"===t[t.length-1]&&++s,m[(m.indexOf(e)+s)%l];case"b":return["C","F"].includes(e)||2!==t.length?(s=a(t,"b"),i=p.indexOf(e)-s,o=i>=0?i:l+i,p[o%l]):`${e}${r}`;default:return e}}function I(t){const{pitchClass:e,octave:n}=w(t),r=F(e);let s=0;const i="B"===e[0]&&e.includes("#"),o="A#x"===e,a="C"===e[0]&&e.includes("b");return i||o?s=1:(a||"Dbbb"===e)&&(s=-1),`${r}${n+s}`}function w(t){return{pitchClass:n(t.slice(0,t.length-1)),octave:parseInt(t[t.length-1])}}function j(t){const e=t[0];return!!g.includes(e)&&t.slice(1).split("").every((t=>["b","#","x"].includes(t)))}function q(t){return t.length>1&&h(t[t.length-1])}function B(t){return["r","R"].includes(t)}function R(t){return Object.keys(N).includes(t)}function z(t,e){let{pitchClass:n,octave:r}=w(t);const s={};let i;for(let t=0;t<=e;++t){const e=(p.indexOf(n)+t)%12;i=p[e];const o={pitchClass:i,octave:r};s[`${i}${r}`]=o,"B"===i&&r++}return s}function L(t){return y(t.includes("#")?"#":"b").indexOf(t)}function G(t,e){const n=$(t,e),r=n>=7?"#":"b",s=n>=7?12-n:n;let i="";for(let t=0;t<s;++t)i=i.concat(r);return"#"===r&&(i=o(i,"##","x")),`${e}${i}`}function V(t){if(t.includes("#")){const{pitchClass:e,octave:n}=w(t);return isNaN(n)?p[m.indexOf(t)]:`${p[m.indexOf(e)]}${n}`}return t}function U(t){let e;return e="number"==typeof t?t:h(t)?S[t]:parseInt(t),e}function H(t){let e=-1/0;return t.forEach((t=>{const n=U(t);e=n>e?n:e})),e}function k(t,e){return Z(t,e)===t?e:t}function Z(t,e){const n=w(t),r=w(e);return n.octave<r.octave?t:n.octave>r.octave?e:`${J(n.pitchClass,r.pitchClass)}${n.octave}`}function J(t,e){const n=F(t),r=F(e);return b.indexOf(n)<=b.indexOf(r)?t:e}function K(t){return t.reduce(((t,e)=>Z(t,e)),t[0])}function Q(t){return t.reduce(((t,e)=>k(t,e)),t[0])}function W(t){const e=t[0];let n=0;return t.map(((t,r)=>(e===t&&r>0&&++n,$(e,t)+12*n)))}function X(t){const e=t[0];return t.map((t=>x(e,t)))}function Y(t){const{pitchClass:e,octave:n}=w(t),r=n-d.octave,s=L(e);return Math.pow(f,s-p.indexOf(d.pitchClass)+r*l)*d.frequency}function tt(t){const e=rt(t),n=e%l,r=Math.floor(e/l)-1;return`${p[n]}${r}`}function et(t){return d.frequency*Math.pow(2,(t-d.midi)/l)}function nt(t){return d.midi+l*Math.log2(t/d.frequency)}function rt(t){return Math.round(nt(t))}function st(t,e){return Math.floor(1200*Math.log(t/et(e))/Math.log(2))}function it(t){return d.frequency*Math.pow(2,(t-d.midi)/l)}function ot(t){return tt(et(t))}function at(t){return rt(Y(t))}function ct(t,e,n=0){n&&(t=i(t,n));let r=e,s=!1;return t.map(((e,n)=>{if(0!==n){const i=L(F(e)),o=L(F(t[n-1])),a="C"===F(e)&&"B"!==e[0],c=i<o&&"B"!==e[0],u=["C","D"].includes(e[0])&&["A","B"].includes(t[n-1][0]);(c||a||u)&&!s&&r++,s&&(s=!1),u&&(s=!0)}return`${e}${r}`}))}function ut(t){const e=i([...g],g.indexOf(t[0][0])),n=[t[0]];return[...t].slice(1).forEach(((t,r)=>{n.push(G(t,e[(r+1)%e.length]))})),n}class ht{constructor(t=64){this._maxDuration=t,this._duration="4n",this._data=[{notes:new Set,duration:"4n"}]}get maxDuration(){return this._maxDuration}get data(){return this._data}get length(){return this.data.reduce(((t,{duration:e})=>t+N[e]),0)/4}static measureDataToString(t){const e=Array.from(t.notes).join("-");if(e){const n=t.duration,r=t.name;return`${e}_${n}${r?`_${r}`:""}`}return""}static parseMeasureNoteMemberString(t){const[e,n,r]=t.split("_");return{notes:e.split("-"),duration:n,name:r}}static stringToMeasure({str:t,maxDuration:e}){const n=new ht(e);return t.split("__").map((t=>ht.parseMeasureNoteMemberString(t))).forEach(((t,e)=>n.addChord(t,e))),n}clone(){return this.transpose(0)}durationLeft(t=this.data.length){return this.maxDuration-this.data.slice(0,t).reduce(((t,e)=>e.notes.size?t+N[e.duration]:t),0)}addNote({note:t,duration:e},r){return!!this.canInsertToMeasure(r+1,e)&&(this.data[r].notes.add(n(t)),this.data[r].duration=e,this.initNext(r+1,e),!0)}addNotes({notes:t,duration:e},n){return t.every((t=>this.addNote({note:t,duration:e},n)))}addChord({notes:t,name:e,duration:n},r){return!!this.canInsertToMeasure(r+1,n)&&(e&&(this.data[r].name=e),this.addNotes({notes:t,duration:n},r))}deleteNote(t,e){return this.data[e].notes.delete(n(t))}deleteNotes(t,e){return t.every((t=>this.deleteNote(t,e)))}deleteMember(t){return!!this.data[t]&&(this.data.splice(t,1),0!==this.data[this.data.length-1].notes.size&&this.initNext(this.data.length),!0)}isFull(t){return!(N[t]<=this.durationLeft())}transpose(t){const e=new ht(this.maxDuration);return this.data.forEach(((n,r)=>{const{name:s,duration:i,notes:o}=n,a={notes:[...o].map((e=>B(e)?e:gt(e,t))),name:s,duration:i};e.addChord(a,r)})),e}clear(){return this.data.length=0,this.initNext(0),!0}toString(){return this.data.map((t=>ht.measureDataToString(t))).filter((t=>""!==t)).join("__")}initNext(t,e="4n"){this.durationLeft(this.data.length)>0&&(this.data[t]={notes:new Set,duration:e})}canInsertToMeasure(t,e){const n=t>this.data.length,r=N[e],s=r>this.durationLeft(t)+r;return!(n||s)}}class dt extends Error{constructor(t){super(`Invalid input: ${t}.`)}}class lt{constructor({bpm:t,timeSignature:e,name:n,voiceNames:r=[]}={}){this._name=n||"my_score",this._bpm=t||100;const s={};for(const t of r)s[t]=[];this._voices=s,this.timeSignature=e||[4,4],this.measureSize=lt.getMeasureSize(this.timeSignature)}get name(){return this._name}set name(t){this._name=t}get bpm(){return this._bpm}set bpm(t){this._bpm=t}get voices(){return this._voices}get length(){const t=Object.values(this.voices);if(t.length){const e=u(t),n=e.length-1;return`${n}:0:${e[n].length}`}return"0:0:0"}static getMeasureSize(t){if(!Array.isArray(t))throw new Error("time signature must be an array, e.g [4, 4]");return t[0]/t[1]*4*N[`${t[1]}n`]*t[1]/4}static stringToScore(t){const[e,n,r,s]=t.split("___"),i=r.split(",").map((t=>parseInt(t))),o=new lt({name:e,timeSignature:i,bpm:parseFloat(n)}),a=s.split("_v_").map((t=>t.split("_d_"))),c=lt.getMeasureSize(i);return a.forEach((([t,e])=>{o.addVoice(t),e.split("_m_").forEach(((e,n)=>{const r=ht.stringToMeasure({str:e,maxDuration:c});o.addMeasure(t,{index:n,measure:r})}))})),o}setTimeSignature(t){this.measureSize=lt.getMeasureSize(t),this.timeSignature=t}getVoice(t){const e=this.voices[t];if(!e)throw new dt(`the voice name ${t} does not exist!`);return e}addVoice(t,e=[]){this.voices[t]=e}deleteVoice(t){return delete this.voices[t]}getMeasure(t,e){const n=this.getVoice(t);if(n)return n[e]}addMeasure(t,{measure:e,index:n}={}){const r=this.getVoice(t);r&&(n=n||r.length,e=e instanceof ht?e:new ht(this.measureSize),r.splice(n,0,e))}addNote(t,e,n,{note:r,duration:s}){return this.addOperation("addNote",t,e,n,{note:r,duration:s})}addNotes(t,e,n,{notes:r,duration:s}){return this.addOperation("addNotes",t,e,n,{notes:r,duration:s})}addChord(t,e,n,{notes:r,name:s,duration:i}){return this.addOperation("addChord",t,e,n,{notes:r,name:s,duration:i})}addOperation(t,e,n,r,s){const i=this.getMeasure(e,n);return!!i&&!!i&&i[t](s,r)}deleteNote(t,e,n,r){return this.deleteOperation("deleteNote",t,e,n,r)}deleteNotes(t,e,n,r){return this.deleteOperation("deleteNotes",t,e,n,r)}deleteOperation(t,e,n,r,s){const i=this.getMeasure(e,n);return!!i&&i[t](s,r)}deleteMember(t,e,n){const r=this.getMeasure(t,e);return!!r&&r.deleteMember(n)}clearMeasure(t,e){const n=this.getMeasure(t,e);return!!n&&n.clear()}deleteMeasure(t,e){return!!this.getMeasure(t,e)&&this.voices[t].splice(e,1)}cloneMeasure(t,e){const n=this.getMeasure(t,e);if(n){const r=n.clone();return this.voices[t].splice(e,0,r),!0}return!1}transposeMeasure(t,e,n){const r=this.getMeasure(t,e);if(r){const s=r.transpose(n);return this.voices[t].splice(e,1,s),!0}return!1}transpose(t,e){this.voices[t]=this.voices[t].map((t=>t.transpose(e)))}toString(){const t=Object.entries(this.voices).map((([t,e])=>`${t}_d_${e.map((t=>t.toString())).join("_m_")}`)).filter((t=>""!==t)).join("_v_");return`${this.name}___${this.timeSignature}___${this.bpm}___${t}`}}class ft{static measureToObject(t){const e=t.data.map((t=>this.notesToObject(t)));return{maxDuration:t.maxDuration,data:e}}static notesToObject(t){const e={notes:Array.from(t.notes),duration:t.duration};return t.name&&(e.name=t.name),e}static objectToMeasure(t){const e=new ht(t.maxDuration);return t.data.forEach(((t,n)=>{t.name?e.addChord(Object.assign({},t),n):e.addNotes(Object.assign({},t),n)})),e}static scoreToObject(t){const e={};return Object.entries(t.voices).map((([t,n])=>e[t]=n.map((t=>this.measureToObject(t))))),{name:t.name,timeSignature:t.timeSignature,bpm:t.bpm,voices:e}}static objectToScore(t){const e=new lt({timeSignature:t.timeSignature,bpm:t.bpm,name:t.name});return Object.entries(t.voices).forEach((([t,n])=>{e.addVoice(t),n.forEach(((n,r)=>{e.addMeasure(t,{index:r,measure:this.objectToMeasure(n)})}))})),e}static cloneScore(t){return this.objectToScore(this.scoreToObject(t))}}function mt(t,e){const n=e%l,r=F(t),s=E(t),i=y(s).indexOf(r);if(b.includes(t)){const t=Math.abs((i+l+n)%l);return y(s)[t]}{const e=y(s).indexOf(t[0]),r=Math.abs((e+l+n)%l);let[i,o]=y(s)[r];const a=t.slice(1);if("b"===o&&!a.includes("b")){const t=G(`${i}${o}`,p[(p.indexOf(`${i}${o}`)-1)%l]);i=t[0],o=t[1]}return"#"===a[a.length-1]&&o?`${i}${a.slice(0,a.length-1)}x`:`${i}${a}${o||""}`}}function pt(t,e){const{pitchClass:n,octave:r}=w(t),s=F(n),i=y(E(n)).indexOf(s),o=mt(n,e);let a=Math.floor((i+e)/12);return e<0&&(a=i+e<0?a:0),`${o}${r+a}`}function gt(t,e){return q(t)?pt(t,e):mt(t,e)}function bt(t){const e=rt(t);return`${m[e%l]}${Math.floor(e/l-1)}`}function St(t,e){return e.map((e=>gt(t,e)))}function _t(t,e){if("number"==typeof e&&e<=t.length)return i(t,e);throw new Error("inversion cant be bigger then the number of pitch classes in the chord")}function Mt(t,e){return t[e-1]}function Ct(t,e,n=3){const r=Mt(t,e),s=e-1,i=[0];for(let e=1;e<n;++e){const n=s+2*e,o=t[n%t.length],{pitchClass:a,octave:c}=w(o),u=`${a}${c+Math.floor(n/t.length)}`;i.push(A(r,u))}return St(r,i)}function Nt(t,e=3){return t.map(((n,r)=>Ct(t,r+1,e)))}module.exports=e})();