{"ast":null,"code":"import fxauto from \"./fxauto.js\";\nexport async function getPitch(config, onPitchDetected) {\n  try {\n    const stream = await navigator.mediaDevices.getUserMedia({\n      audio: true\n    });\n    const audioContext = new window.AudioContext();\n    const source = audioContext.createMediaStreamSource(stream);\n    const fxCalc = new fxauto(config.fxmin, config.fxlow, config.fxhigh, config.fxmax, config.SRATE);\n    const captureNode = audioContext.createScriptProcessor(2048, 1, 1);\n    let pitchValues = [];\n    let startTime = Date.now();\n    captureNode.onaudioprocess = e => {\n      const buf = e.inputBuffer.getChannelData(0);\n      const fxest = fxCalc.CalculateFx(buf, buf.length);\n      if (fxest.en > 0) {\n        if (fxest.fx < 1) {\n          const randomtem = Math.random();\n          onPitchDetected(randomtem);\n        } else {\n          onPitchDetected(fxest.fx);\n        }\n      }\n    };\n    source.connect(captureNode);\n    captureNode.connect(audioContext.destination);\n\n    // Return cleaning function\n    return () => {\n      captureNode.disconnect();\n      source.disconnect();\n      audioContext.close();\n    };\n  } catch (err) {\n    onPitchDetected(null);\n    return () => {};\n  }\n}","map":{"version":3,"names":["fxauto","getPitch","config","onPitchDetected","stream","navigator","mediaDevices","getUserMedia","audio","audioContext","window","AudioContext","source","createMediaStreamSource","fxCalc","fxmin","fxlow","fxhigh","fxmax","SRATE","captureNode","createScriptProcessor","pitchValues","startTime","Date","now","onaudioprocess","e","buf","inputBuffer","getChannelData","fxest","CalculateFx","length","en","fx","randomtem","Math","random","connect","destination","disconnect","close","err"],"sources":["/var/www/gavt/react-demo/src/function/getPitch.ts"],"sourcesContent":["import fxauto from \"./fxauto.js\";\nimport React, { useContext, useEffect, useRef, useState } from \"react\";\nexport interface IPitchDetectionConfig {\n  fxmin: number;\n  fxlow: number;\n  fxhigh: number;\n  fxmax: number;\n  SRATE: number;\n}\n\nexport async function getPitch(\n  config: IPitchDetectionConfig,\n  onPitchDetected: (pitch: number | null) => void\n): Promise<() => void> {\n  try {\n    const stream = await navigator.mediaDevices.getUserMedia({ audio: true });\n    const audioContext = new window.AudioContext();\n    const source = audioContext.createMediaStreamSource(stream);\n    const fxCalc = new fxauto(\n      config.fxmin,\n      config.fxlow,\n      config.fxhigh,\n      config.fxmax,\n      config.SRATE\n    );\n    const captureNode = audioContext.createScriptProcessor(2048, 1, 1);\n\n    let pitchValues: number[] = [];\n    let startTime = Date.now();\n\n    captureNode.onaudioprocess = (e: AudioProcessingEvent) => {\n      const buf = e.inputBuffer.getChannelData(0);\n      const fxest = fxCalc.CalculateFx(buf, buf.length);\n\n      if (fxest.en > 0) {\n        if (fxest.fx < 1) {\n          const randomtem = Math.random();\n          onPitchDetected(randomtem);\n        } else {\n          onPitchDetected(fxest.fx);\n        }\n      }\n    };\n\n    source.connect(captureNode);\n    captureNode.connect(audioContext.destination);\n\n    // Return cleaning function\n    return () => {\n      captureNode.disconnect();\n      source.disconnect();\n      audioContext.close();\n    };\n  } catch (err) {\n    onPitchDetected(null);\n    return () => {};\n  }\n}\n"],"mappings":"AAAA,OAAOA,MAAM,MAAM,aAAa;AAUhC,OAAO,eAAeC,QAAQA,CAC5BC,MAA6B,EAC7BC,eAA+C,EAC1B;EACrB,IAAI;IACF,MAAMC,MAAM,GAAG,MAAMC,SAAS,CAACC,YAAY,CAACC,YAAY,CAAC;MAAEC,KAAK,EAAE;IAAK,CAAC,CAAC;IACzE,MAAMC,YAAY,GAAG,IAAIC,MAAM,CAACC,YAAY,CAAC,CAAC;IAC9C,MAAMC,MAAM,GAAGH,YAAY,CAACI,uBAAuB,CAACT,MAAM,CAAC;IAC3D,MAAMU,MAAM,GAAG,IAAId,MAAM,CACvBE,MAAM,CAACa,KAAK,EACZb,MAAM,CAACc,KAAK,EACZd,MAAM,CAACe,MAAM,EACbf,MAAM,CAACgB,KAAK,EACZhB,MAAM,CAACiB,KACT,CAAC;IACD,MAAMC,WAAW,GAAGX,YAAY,CAACY,qBAAqB,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;IAElE,IAAIC,WAAqB,GAAG,EAAE;IAC9B,IAAIC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