UNPKG

essentia.js

Version:

JavaScript library for music/audio analysis and processing, powered by Essentia WebAssembly

35 lines (32 loc) 5.27 kB
/*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ var t=function(i,e){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,i){t.__proto__=i}||function(t,i){for(var e in i)i.hasOwnProperty(e)&&(t[e]=i[e])})(i,e)};function i(i,e){function o(){this.constructor=i}t(i,e),i.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)} /** * @license * Copyright (C) 2006-2020 Music Technology Group - Universitat Pompeu Fabra * * This file is part of Essentia * * Essentia is free software: you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License as published by the Free * Software Foundation (FSF), either version 3 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the Affero GNU General Public License * version 3 along with this program. If not, see http://www.gnu.org/licenses/ */var e={isPlotting:!1,startTimeIndex:0},o={title:"Melody Contour",plot_bgcolor:"transparent",paper_bgcolor:"#FCF7F7",autosize:!1,width:670,height:300,xaxis:{type:"time",title:"Time"},yaxis:{autorange:!1,range:[30,3e3],type:"linear",title:"Frequency (Hz)"}},r={title:"",plot_bgcolor:"transparent",paper_bgcolor:"#FCF7F7",autosize:!1,width:670,height:300,xaxis:{autorange:!0,time:"Time",title:"Time"},yaxis:{title:"Pitch class",range:[0,11]}},a={title:"",plot_bgcolor:"transparent",paper_bgcolor:"#FCF7F7",autosize:!1,width:670,height:300,xaxis:{title:"Time",autorange:!0,time:"Time"},yaxis:{title:"Bands",range:null,type:"linear"}},n=function(){function t(t,i){void 0===i&&(i=e),this.Plotly=t,this.options=i,this.isPlotting=i.isPlotting,this.startTimeIndex=i.startTimeIndex}return t.prototype.makeLinearSpace=function(t,i,e){if(void 0===e&&(e=Math.max(Math.round(i-t)+1,1)),e<2)return 1===e?[t]:[];var o,r=Array(e);for(o=--e;o>=0;o--)r[o]=(o*i+(e-o)*t)/e;return r},t}(),s=function(t){function e(i,e,r){void 0===r&&(r=o);var a=t.call(this,i)||this;return a.Plotly=i,a.divId=e,a.plotLayout=r,a}return i(e,t),e.prototype.create=function(t,i,e,o){this.plotLayout.title=i;var r=this.makeLinearSpace(this.startTimeIndex,e/o,t.length);this.isPlotting?(r=this.makeLinearSpace(this.startTimeIndex,this.startTimeIndex+e/o,t.length),this.startTimeIndex=r[r.length-1],this.Plotly.extendTraces(this.divId,{x:[r],y:[t]},[0])):(this.Plotly.newPlot(this.divId,[{x:r,y:t,mode:"lines",line:{color:"#2B6FAC",width:2}}],this.plotLayout),this.isPlotting=!0,this.startTimeIndex=r[r.length-1])},e.prototype.destroy=function(){this.Plotly.deleteTraces(this.divId,0),this.isPlotting=!1,this.startTimeIndex=0},e}(n),l=function(t){function e(i,e,o,r){void 0===o&&(o="chroma"),void 0===r&&(r=a);var n=t.call(this,i)||this;if(n.Plotly=i,n.divId=e,n.plotType=o,n.plotLayout=r,"chroma"===o)n.yAxis=["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"];else{if("spectrogram"!==o)throw"Invalid value for argument 'plotType'. Should be either 'chroma' or 'spectrogram'";n.yAxis=null}return n}return i(e,t),e.prototype.create=function(t,i,e,o,r,a){if(void 0===a&&(a="Jet"),this.plotLayout.title=i,"spectrogram"===this.plotType){var n=t[0].length;this.plotLayout.yaxis.range=[0,n+1]}if(this.isPlotting){s=void 0,l=void 0;if(t[0].constructor!==Array&&t[0].constructor!==Float32Array)throw"Got 1D array as input, expect a 2D array...";1==t.length?(s=t,l=[this.startTimeIndex+r/o,this.startTimeIndex+e/o]):(s=t,l=this.makeLinearSpace(this.startTimeIndex,e/o,s.length)),this.startTimeIndex=l[l.length-1],this.Plotly.extendTraces(this.divId,{x:[l],z:[t]},[0])}else{var s=void 0,l=void 0;if(t[0].constructor!==Array&&t[0].constructor!==Float32Array)throw"Got 1D array as input, expect a 2D array...";1==t.length?(s=t,l=[this.startTimeIndex+r/o,this.startTimeIndex+e/o]):(s=t,l=this.makeLinearSpace(this.startTimeIndex,e/o,s.length));var h={x:l,y:this.yAxis,z:s,colorscale:a,type:"heatmap",transpose:!0};this.Plotly.newPlot(this.divId,[h],this.plotLayout),this.isPlotting=!0,this.startTimeIndex=l[l.length-1]}},e.prototype.destroy=function(){this.Plotly.deleteTraces(this.divId,0),this.isPlotting=!1,this.startTimeIndex=0},e}(n);export{n as EssentiaPlot,r as LayoutChromaPlot,o as LayoutMelodyContourPlot,a as LayoutSpectrogramPlot,e as PlotConfig,l as PlotHeatmap,s as PlotMelodyContour};