Spaces:
Running
Running
Upload 2 files
Browse files
script.js
CHANGED
|
@@ -20752,4 +20752,4 @@ return a / b;`,DIV_PACKED=`
|
|
| 20752 |
* See the License for the specific language governing permissions and
|
| 20753 |
* limitations under the License.
|
| 20754 |
* =============================================================================
|
| 20755 |
-
*/const kernelConfigs=[_fusedMatMulConfig,absConfig,acosConfig,acoshConfig,addConfig,addNConfig,allConfig,anyConfig,argMaxConfig,argMinConfig,asinConfig,asinhConfig,atanConfig,atan2Config,atanhConfig,avgPoolConfig,avgPool3DConfig,avgPool3DGradConfig,avgPoolGradConfig,batchMatMulConfig,batchNormConfig,batchToSpaceNDConfig,bincountConfig,bitwiseAndConfig,broadcastArgsConfig,castConfig,ceilConfig,clipByValueConfig,complexConfig,complexAbsConfig,concatConfig,conv2DConfig,conv2DBackpropFilterConfig,conv2DBackpropInputConfig,conv3DConfig,conv3DBackpropFilterV2Config,conv3DBackpropInputConfig,cosConfig,coshConfig,cropAndResizeConfig,cumprodConfig,cumsumConfig,denseBincountConfig,depthToSpaceConfig,depthwiseConv2dNativeConfig,depthwiseConv2dNativeBackpropFilterConfig,depthwiseConv2dNativeBackpropInputConfig,diagConfig,dilation2DConfig,einsumConfig,eluConfig,eluGradConfig,equalConfig,erfConfig,expConfig,expandDimsConfig,expm1Config,fftConfig,fillConfig,flipLeftRightConfig,floorConfig,floorDivConfig,fromPixelsConfig,fusedConv2DConfig,fusedDepthwiseConv2DConfig,gatherNdConfig,gatherV2Config,greaterConfig,greaterEqualConfig,identityConfig,ifftConfig,imagConfig,isFiniteConfig,isInfConfig,isNaNConfig,leakyReluConfig,lessConfig,lessEqualConfig,linSpaceConfig,logConfig,log1pConfig,logicalAndConfig,logicalNotConfig,logicalOrConfig,LRNConfig,LRNGradConfig,maxConfig,maximumConfig,maxPoolConfig,maxPool3DConfig,maxPool3DGradConfig,maxPoolGradConfig,maxPoolWithArgmaxConfig,meanConfig,minConfig,minimumConfig,mirrorPadConfig,modConfig,multinomialConfig,multiplyConfig,negConfig,nonMaxSuppressionV3Config,nonMaxSuppressionV4Config,nonMaxSuppressionV5Config,notEqualConfig,oneHotConfig,onesLikeConfig,packConfig,padV2Config,powConfig,preluConfig,prodConfig,raggedGatherConfig,raggedRangeConfig,raggedTensorToTensorConfig,rangeConfig,realConfig,realDivConfig,reciprocalConfig,reluConfig,relu6Config,reshapeConfig,resizeBilinearConfig,resizeBilinearGradConfig,resizeNearestNeighborConfig,resizeNearestNeighborGradConfig,reverseConfig,rotateWithOffsetConfig,roundConfig,rsqrtConfig,scatterNdConfig,searchSortedConfig,selectConfig,seluConfig,sigmoidConfig,signConfig,sinConfig,sinhConfig,sliceConfig,softmaxConfig,softplusConfig,spaceToBatchNDConfig,sparseFillEmptyRowsConfig,sparseReshapeConfig,sparseSegmentMeanConfig,sparseSegmentSumConfig,sparseToDenseConfig,splitVConfig,sqrtConfig,squareConfig,squaredDifferenceConfig,staticRegexReplaceConfig,stepConfig,stridedSliceConfig,stringNGramsConfig,stringSplitConfig,stringToHashBucketFastConfig,subConfig,sumConfig,tanConfig,tanhConfig,tensorScatterUpdateConfig,tileConfig,topKConfig,transformConfig,transposeConfig,uniqueConfig,unpackConfig,unsortedSegmentSumConfig,zerosLikeConfig];for(const e of kernelConfigs)registerKernel(e);const AUDIO_SAMPLE_RATE=22050,FFT_HOP=256,AUDIO_WINDOW_LENGTH=2,AUDIO_N_SAMPLES=AUDIO_SAMPLE_RATE*AUDIO_WINDOW_LENGTH-FFT_HOP,N_OVERLAPPING_FRAMES=30,OVERLAP_LEN=N_OVERLAPPING_FRAMES*FFT_HOP,HOP_SIZE=AUDIO_N_SAMPLES-OVERLAP_LEN,ANNOTATIONS_FPS=AUDIO_SAMPLE_RATE/FFT_HOP;function windowAudio(e){const t=[];for(let n=0;n<e.length;n+=HOP_SIZE){let r=e.slice(n,n+AUDIO_N_SAMPLES);if(r.length<AUDIO_N_SAMPLES){const o=new Float32Array(AUDIO_N_SAMPLES);o.set(r),r=o}t.push(r)}return t}function unwrapOutput(e,t,n){const r=Math.floor(.5*n);let o=e;r>0&&(o=e.map(u=>u.slice(r,u.length-r)));const i=[];for(const u of o)for(const c of u)i.push(c);const s=Math.floor(t*(ANNOTATIONS_FPS/AUDIO_SAMPLE_RATE));return i.slice(0,s)}async function predict(e,t,n){const r=new Float32Array(t.length+OVERLAP_LEN/2);r.set(t,OVERLAP_LEN/2);const o=windowAudio(r),i={note:[],onset:[],contour:[]},s=o.length;for(let c=0;c<s;c++){const f=o[c],g=tensor(f,[1,AUDIO_N_SAMPLES]),m=e.predict({"serving_default_input_2:0":g});i.note.push(...await m["StatefulPartitionedCall:1"].array()),i.onset.push(...await m["StatefulPartitionedCall:2"].array()),i.contour.push(...await m["StatefulPartitionedCall:0"].array()),g.dispose(),n(c/s*100),await new Promise(h=>setTimeout(h,0))}const u=t.length;return{frames:unwrapOutput(i.note,u,N_OVERLAPPING_FRAMES),onsets:unwrapOutput(i.onset,u,N_OVERLAPPING_FRAMES),contours:unwrapOutput(i.contour,u,N_OVERLAPPING_FRAMES)}}class Basic_Pitch_tflite{constructor(t="model.tflite"){ll(this,"model_path");ll(this,"model");this.model_path=t}async load(){this.model=await loadTFLiteModel(this.model_path)}predict(t,n){return predict(this.model,t,n)}}const AudioUploader=({onAudioReceived:e})=>{const t=reactExports.useRef(null),[n,r]=reactExports.useState(!1),o=reactExports.useRef([]),[i,s]=reactExports.useState(null),[u,c]=reactExports.useState(null),f=h=>{var I;const _=(I=h.target.files)==null?void 0:I[0];if(!_)return;const S=URL.createObjectURL(_);s(S),c(_.name),e(_.name,S)},g=async()=>{try{const h=await navigator.mediaDevices.getUserMedia({audio:!0}),_=new MediaRecorder(h);t.current=_,o.current=[],_.ondataavailable=S=>{S.data.size>0&&o.current.push(S.data)},_.onstop=()=>{const S=new Blob(o.current,{type:"audio/webm"}),I=URL.createObjectURL(S);s(I),c("recorded_audio.webm"),e("recorded_audio.webm",I),o.current=[],h.getTracks().forEach(x=>x.stop())},_.start(),r(!0)}catch(h){console.error("Microphone access denied or error occurred:",h)}},m=()=>{var h;(h=t.current)==null||h.stop(),r(!1)};return jsxRuntimeExports.jsxs("div",{children:[jsxRuntimeExports.jsx("h3",{children:"Upload or Record Audio"}),jsxRuntimeExports.jsx("input",{type:"file",accept:"audio/*",onChange:f}),jsxRuntimeExports.jsx("div",{style:{marginTop:"10px"},children:n?jsxRuntimeExports.jsx("button",{onClick:m,children:"🛑 Stop Recording"}):jsxRuntimeExports.jsx("button",{onClick:g,children:"🎙️ Start Recording"})}),i&&jsxRuntimeExports.jsxs("div",{style:{marginTop:"20px"},children:[jsxRuntimeExports.jsxs("h4",{children:["Audio Preview: ",u]}),jsxRuntimeExports.jsx("audio",{controls:!0,src:i})]})]})},ProgressBar=({progress:e})=>{const t=Math.floor(e);return jsxRuntimeExports.jsxs("div",{children:[jsxRuntimeExports.jsx("div",{className:"progress-bar-container",children:jsxRuntimeExports.jsx("div",{className:"progress-bar-fill",style:{width:`${t}%`}})}),e>0&&e<100&&jsxRuntimeExports.jsxs("p",{children:["Processing: ",e.toFixed(0),"%"]})]})},ConfigComponent=({initialConfig:e,onChange:t})=>{const[n,r]=reactExports.useState(e),o=(i,s)=>{const u=s===""?void 0:typeof n[i]=="number"?Number(s):s,c={...n,[i]:u};r(c),t&&t(c)};return jsxRuntimeExports.jsxs("div",{children:[jsxRuntimeExports.jsx("h3",{children:"Detection Config"}),Object.entries(n).map(([i,s])=>jsxRuntimeExports.jsx("div",{style:{marginBottom:"10px"},children:jsxRuntimeExports.jsxs("label",{children:[i,":"," ",typeof s=="boolean"?jsxRuntimeExports.jsx("input",{type:"checkbox",checked:s,onChange:u=>o(i,u.target.checked)}):jsxRuntimeExports.jsx("input",{type:"number",value:s??"",onChange:u=>o(i,u.target.value)})]})},i))]})},App=()=>{const[e,t]=reactExports.useState(0),[n,r]=reactExports.useState({frames:[],onsets:[],contours:[]}),[o,i]=reactExports.useState("output.midi"),[s,u]=reactExports.useState(""),[c,f]=reactExports.useState(null),[g,m]=reactExports.useState(!0),[h,_]=reactExports.useState(!0),[S,I]=reactExports.useState(!0),[x,T]=reactExports.useState(""),[N,C]=reactExports.useState(),v={onsetThresh:.5,frameThresh:.3,minNoteLen:127.7,inferOnsets:!0,maxFreq:null,minFreq:null,melodiaTrick:!
|
|
|
|
| 20752 |
* See the License for the specific language governing permissions and
|
| 20753 |
* limitations under the License.
|
| 20754 |
* =============================================================================
|
| 20755 |
+
*/const kernelConfigs=[_fusedMatMulConfig,absConfig,acosConfig,acoshConfig,addConfig,addNConfig,allConfig,anyConfig,argMaxConfig,argMinConfig,asinConfig,asinhConfig,atanConfig,atan2Config,atanhConfig,avgPoolConfig,avgPool3DConfig,avgPool3DGradConfig,avgPoolGradConfig,batchMatMulConfig,batchNormConfig,batchToSpaceNDConfig,bincountConfig,bitwiseAndConfig,broadcastArgsConfig,castConfig,ceilConfig,clipByValueConfig,complexConfig,complexAbsConfig,concatConfig,conv2DConfig,conv2DBackpropFilterConfig,conv2DBackpropInputConfig,conv3DConfig,conv3DBackpropFilterV2Config,conv3DBackpropInputConfig,cosConfig,coshConfig,cropAndResizeConfig,cumprodConfig,cumsumConfig,denseBincountConfig,depthToSpaceConfig,depthwiseConv2dNativeConfig,depthwiseConv2dNativeBackpropFilterConfig,depthwiseConv2dNativeBackpropInputConfig,diagConfig,dilation2DConfig,einsumConfig,eluConfig,eluGradConfig,equalConfig,erfConfig,expConfig,expandDimsConfig,expm1Config,fftConfig,fillConfig,flipLeftRightConfig,floorConfig,floorDivConfig,fromPixelsConfig,fusedConv2DConfig,fusedDepthwiseConv2DConfig,gatherNdConfig,gatherV2Config,greaterConfig,greaterEqualConfig,identityConfig,ifftConfig,imagConfig,isFiniteConfig,isInfConfig,isNaNConfig,leakyReluConfig,lessConfig,lessEqualConfig,linSpaceConfig,logConfig,log1pConfig,logicalAndConfig,logicalNotConfig,logicalOrConfig,LRNConfig,LRNGradConfig,maxConfig,maximumConfig,maxPoolConfig,maxPool3DConfig,maxPool3DGradConfig,maxPoolGradConfig,maxPoolWithArgmaxConfig,meanConfig,minConfig,minimumConfig,mirrorPadConfig,modConfig,multinomialConfig,multiplyConfig,negConfig,nonMaxSuppressionV3Config,nonMaxSuppressionV4Config,nonMaxSuppressionV5Config,notEqualConfig,oneHotConfig,onesLikeConfig,packConfig,padV2Config,powConfig,preluConfig,prodConfig,raggedGatherConfig,raggedRangeConfig,raggedTensorToTensorConfig,rangeConfig,realConfig,realDivConfig,reciprocalConfig,reluConfig,relu6Config,reshapeConfig,resizeBilinearConfig,resizeBilinearGradConfig,resizeNearestNeighborConfig,resizeNearestNeighborGradConfig,reverseConfig,rotateWithOffsetConfig,roundConfig,rsqrtConfig,scatterNdConfig,searchSortedConfig,selectConfig,seluConfig,sigmoidConfig,signConfig,sinConfig,sinhConfig,sliceConfig,softmaxConfig,softplusConfig,spaceToBatchNDConfig,sparseFillEmptyRowsConfig,sparseReshapeConfig,sparseSegmentMeanConfig,sparseSegmentSumConfig,sparseToDenseConfig,splitVConfig,sqrtConfig,squareConfig,squaredDifferenceConfig,staticRegexReplaceConfig,stepConfig,stridedSliceConfig,stringNGramsConfig,stringSplitConfig,stringToHashBucketFastConfig,subConfig,sumConfig,tanConfig,tanhConfig,tensorScatterUpdateConfig,tileConfig,topKConfig,transformConfig,transposeConfig,uniqueConfig,unpackConfig,unsortedSegmentSumConfig,zerosLikeConfig];for(const e of kernelConfigs)registerKernel(e);const AUDIO_SAMPLE_RATE=22050,FFT_HOP=256,AUDIO_WINDOW_LENGTH=2,AUDIO_N_SAMPLES=AUDIO_SAMPLE_RATE*AUDIO_WINDOW_LENGTH-FFT_HOP,N_OVERLAPPING_FRAMES=30,OVERLAP_LEN=N_OVERLAPPING_FRAMES*FFT_HOP,HOP_SIZE=AUDIO_N_SAMPLES-OVERLAP_LEN,ANNOTATIONS_FPS=AUDIO_SAMPLE_RATE/FFT_HOP;function windowAudio(e){const t=[];for(let n=0;n<e.length;n+=HOP_SIZE){let r=e.slice(n,n+AUDIO_N_SAMPLES);if(r.length<AUDIO_N_SAMPLES){const o=new Float32Array(AUDIO_N_SAMPLES);o.set(r),r=o}t.push(r)}return t}function unwrapOutput(e,t,n){const r=Math.floor(.5*n);let o=e;r>0&&(o=e.map(u=>u.slice(r,u.length-r)));const i=[];for(const u of o)for(const c of u)i.push(c);const s=Math.floor(t*(ANNOTATIONS_FPS/AUDIO_SAMPLE_RATE));return i.slice(0,s)}async function predict(e,t,n){const r=new Float32Array(t.length+OVERLAP_LEN/2);r.set(t,OVERLAP_LEN/2);const o=windowAudio(r),i={note:[],onset:[],contour:[]},s=o.length;for(let c=0;c<s;c++){const f=o[c],g=tensor(f,[1,AUDIO_N_SAMPLES]),m=e.predict({"serving_default_input_2:0":g});i.note.push(...await m["StatefulPartitionedCall:1"].array()),i.onset.push(...await m["StatefulPartitionedCall:2"].array()),i.contour.push(...await m["StatefulPartitionedCall:0"].array()),g.dispose(),n(c/s*100),await new Promise(h=>setTimeout(h,0))}const u=t.length;return{frames:unwrapOutput(i.note,u,N_OVERLAPPING_FRAMES),onsets:unwrapOutput(i.onset,u,N_OVERLAPPING_FRAMES),contours:unwrapOutput(i.contour,u,N_OVERLAPPING_FRAMES)}}class Basic_Pitch_tflite{constructor(t="model.tflite"){ll(this,"model_path");ll(this,"model");this.model_path=t}async load(){this.model=await loadTFLiteModel(this.model_path)}predict(t,n){return predict(this.model,t,n)}}const AudioUploader=({onAudioReceived:e})=>{const t=reactExports.useRef(null),[n,r]=reactExports.useState(!1),o=reactExports.useRef([]),[i,s]=reactExports.useState(null),[u,c]=reactExports.useState(null),f=h=>{var I;const _=(I=h.target.files)==null?void 0:I[0];if(!_)return;const S=URL.createObjectURL(_);s(S),c(_.name),e(_.name,S)},g=async()=>{try{const h=await navigator.mediaDevices.getUserMedia({audio:!0}),_=new MediaRecorder(h);t.current=_,o.current=[],_.ondataavailable=S=>{S.data.size>0&&o.current.push(S.data)},_.onstop=()=>{const S=new Blob(o.current,{type:"audio/webm"}),I=URL.createObjectURL(S);s(I),c("recorded_audio.webm"),e("recorded_audio.webm",I),o.current=[],h.getTracks().forEach(x=>x.stop())},_.start(),r(!0)}catch(h){console.error("Microphone access denied or error occurred:",h)}},m=()=>{var h;(h=t.current)==null||h.stop(),r(!1)};return jsxRuntimeExports.jsxs("div",{children:[jsxRuntimeExports.jsx("h3",{children:"Upload or Record Audio"}),jsxRuntimeExports.jsx("input",{type:"file",accept:"audio/*",onChange:f,id:"audio-uploader-id",name:"audio-uploader-name"}),jsxRuntimeExports.jsx("div",{style:{marginTop:"10px"},children:n?jsxRuntimeExports.jsx("button",{onClick:m,children:"🛑 Stop Recording"}):jsxRuntimeExports.jsx("button",{onClick:g,children:"🎙️ Start Recording"})}),i&&jsxRuntimeExports.jsxs("div",{style:{marginTop:"20px"},children:[jsxRuntimeExports.jsxs("h4",{children:["Audio Preview: ",u]}),jsxRuntimeExports.jsx("audio",{controls:!0,src:i})]})]})},ProgressBar=({progress:e})=>{const t=Math.floor(e);return jsxRuntimeExports.jsxs("div",{children:[jsxRuntimeExports.jsx("div",{className:"progress-bar-container",children:jsxRuntimeExports.jsx("div",{className:"progress-bar-fill",style:{width:`${t}%`}})}),e>0&&e<100&&jsxRuntimeExports.jsxs("p",{children:["Processing: ",e.toFixed(0),"%"]})]})},ConfigComponent=({initialConfig:e,onChange:t})=>{const[n,r]=reactExports.useState(e),o=(i,s)=>{const u=s===""?void 0:typeof n[i]=="number"?Number(s):s,c={...n,[i]:u};r(c),t&&t(c)};return jsxRuntimeExports.jsxs("div",{children:[jsxRuntimeExports.jsx("h3",{children:"Detection Config"}),Object.entries(n).map(([i,s])=>jsxRuntimeExports.jsx("div",{style:{marginBottom:"10px"},children:jsxRuntimeExports.jsxs("label",{children:[i,":"," ",typeof s=="boolean"?jsxRuntimeExports.jsx("input",{type:"checkbox",checked:s,onChange:u=>o(i,u.target.checked),id:`${i}-id`,name:`${i}-name`}):jsxRuntimeExports.jsx("input",{type:"number",value:s??"",onChange:u=>o(i,u.target.value),id:`${i}-id`,name:`${i}-name`})]})},i))]})},App=()=>{const[e,t]=reactExports.useState(0),[n,r]=reactExports.useState({frames:[],onsets:[],contours:[]}),[o,i]=reactExports.useState("output.midi"),[s,u]=reactExports.useState(""),[c,f]=reactExports.useState(null),[g,m]=reactExports.useState(!0),[h,_]=reactExports.useState(!0),[S,I]=reactExports.useState(!0),[x,T]=reactExports.useState(""),[N,C]=reactExports.useState(),v={onsetThresh:.5,frameThresh:.3,minNoteLen:127.7,inferOnsets:!0,maxFreq:null,minFreq:null,melodiaTrick:!1,energyTolerance:11};function F(){return parseInt(crypto.randomUUID().replace(/\D/g,"").slice(0,15))}return reactExports.useEffect(()=>{(async()=>{const w=new Basic_Pitch_tflite;await w.load(),f(w)})()},[]),reactExports.useEffect(()=>{c&&(async()=>(r(await c.predict(await prepare_audio(s),w=>t(w))),t(0)))()},[g]),reactExports.useEffect(()=>{n.frames.length!==0&&(async()=>{const w=new Blob([await pred_to_midi(n,o,D=>t(D),N)],{type:"audio/midi"});t(0),T(URL.createObjectURL(w))})()},[S]),reactExports.useEffect(()=>{var D;if(!x)return;(D=document.getElementById("midiviz").contentWindow)==null||D.postMessage({type:"load-midi",url:x,name:o+F()+".mid"},"*")},[x]),reactExports.useEffect(()=>{if(!x)return;const w=document.createElement("a");w.href=x,w.download=o+".mid",document.body.appendChild(w),w.click(),document.body.removeChild(w)},[h]),jsxRuntimeExports.jsxs("div",{children:[!c&&"Loading model...",jsxRuntimeExports.jsx(AudioUploader,{onAudioReceived:async(w,D)=>{i(w),u(D)}}),jsxRuntimeExports.jsx("br",{}),jsxRuntimeExports.jsx("button",{onClick:()=>{m(!g)},children:"process audio"}),jsxRuntimeExports.jsx("br",{}),jsxRuntimeExports.jsx(ProgressBar,{progress:e}),jsxRuntimeExports.jsx("br",{}),jsxRuntimeExports.jsx(ConfigComponent,{initialConfig:v,onChange:w=>{C(w)}}),jsxRuntimeExports.jsx("br",{}),jsxRuntimeExports.jsx("button",{onClick:()=>{I(!S)},children:"convert midi"}),jsxRuntimeExports.jsx("br",{}),jsxRuntimeExports.jsx("button",{onClick:()=>{_(!h)},children:"download midi"}),jsxRuntimeExports.jsx("br",{}),jsxRuntimeExports.jsx("iframe",{src:"vizulizer.html",width:"100%",height:"500px",id:"midiviz"})]})};clientExports.createRoot(document.getElementById("root")).render(jsxRuntimeExports.jsx(reactExports.StrictMode,{children:jsxRuntimeExports.jsx(App,{})}));
|