(function(){let reverbEnabled=!1;let convolver=null;let currentIR=null;let audioContext=null;function initReverb(ctx){audioContext=ctx;convolver=audioContext.createConvolver()}
function loadReverb(irUrl){if(!audioContext||!irUrl)return;if(currentIR===irUrl)return;fetch(irUrl).then(response=>response.arrayBuffer()).then(arrayBuffer=>audioContext.decodeAudioData(arrayBuffer)).then(audioBuffer=>{convolver.buffer=audioBuffer;currentIR=irUrl}).catch(err=>console.error("Error loading IR:",err))}
function setReverbEnabled(enabled){reverbEnabled=enabled}
function applyReverb(sourceNode,analyserNode){if(reverbEnabled&&convolver&&convolver.buffer){sourceNode.connect(convolver);convolver.connect(analyserNode)}else{sourceNode.connect(analyserNode)}}
document.addEventListener('DOMContentLoaded',()=>{const irCheckbox=document.getElementById('ptpian_reverb_enable');const irSelect=document.getElementById('ptpian_reverb_ir');if(irCheckbox){irCheckbox.addEventListener('change',()=>{setReverbEnabled(irCheckbox.checked);if(irCheckbox.checked&&irSelect&&irSelect.value){const irUrl=ptpianReverbData?.irs?.[irSelect.value];loadReverb(irUrl)}})}
if(irSelect){irSelect.addEventListener('change',()=>{if(irCheckbox.checked&&irSelect.value){const irUrl=ptpianReverbData?.irs?.[irSelect.value];loadReverb(irUrl)}})}});window.ptpReverb={initReverb,loadReverb,setReverbEnabled,applyReverb}})()