<!-- RUBY-BACKUP · part 326 -->
catch(e){}
}
function startAmbient(){
  try{
    stopAmbient();
    var ctx = _musicCtx;
    // deep drone
    var droneOsc = ctx.createOscillator();
    droneOsc.type = 'sine';
    droneOsc.frequency.value = 55; // A1
    var droneGain = ctx.createGain();
    droneGain.gain.value = 0.08;
    droneOsc.connect(droneGain);
    droneGain.connect(ctx.destination);
    droneOsc.start();
    _musicNodes.push(droneOsc, droneGain);
    // fifth
    var fifthOsc = ctx.createOscillator();
    fifthOsc.type = 'sine';
    fifthOsc.frequency.value = 82.5; // E2
    var fifthGain = ctx.createGain();
    fifthGain.gain.value = 0.05;
    fifthOsc.connect(fifthGain);
    fifthGain.connect(ctx.destination);
    fifthOsc.start();
    _musicNodes.push(fifthOsc, fifthGain);
    // slow shimmer — pentatonic plucks
    var notes = [220, 246.94, 277.18, 329.63, 369.99, 440]; // A major pentatonic
    function pluck(){
      if(!_musicOn) return;
      var o = ctx.createOscillator();
      o.type = 'triangle';
      o.frequency.value = notes[Math.floor(Math.random()*notes.length)];
      var g = ctx.createGain();
      g.gain.setValueAtTime(0.12, ctx.currentTime);
      g.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + 3);
      o.connect(g);
      g.connect(ctx.destination);
      o.start();
      o.stop(ctx.currentTime + 3);
      setTimeout(pluck, 2000 + Math.random()*4000);
    }
    pluck();
    // gentle filter sweep on the drone
    var lfo = ctx.createOscillator();
    lfo.frequency.value = 0.05;
    var lfoGain = ctx.createGain();
    lfoGain.gain.value = 20;
    lfo.connect(lfoGain);
    lfoGain.connect(droneOsc.frequency);
    lfo.start();
    _musicNodes.push(lfo, lfoGain);
  }catch(e){}
}
function stopAmbient(){
  try{
    _musicNodes.forEach(function(n){
      try{ if(n.stop) n.stop(); }catch(e){}
      try{ n.disconnect(); }catch(e){}
    });
    _musicNodes = [];
  }catch(e){}
}
/* SONG UPLOAD — 2 songs, back-to-back */
var _songs = [], _songIdx = 0, _audioEl = null;
function initSongDB(){
  return new Promise(function(resolve, reject){
    var req = indexedDB.open('ruby-songs', 1);
    req.onupgradeneeded = function(e){ e.target.result.createObjectStore('songs'); };
    req.onsuccess = function(e){ resolve(e.target.result); };
    req.onerror = reject;
  });
}
function saveSong(idx, blob){
  initSongDB().then(function(db){
    var tx = db.transaction('songs', 'readwrite');
    tx.objectStore('songs').put(blob, 'song'+idx);
  });
}
function loadSongs(){
  initSongDB().then(function(db){
    var tx = db.transaction('songs', 'readonly');
    var store = tx.objectStore('songs');
    _songs = [];
    [0,1].forEach(function(i){
      var req = store.get('song'+i);
      req.onsuccess = function(){
        if(req.result){
          _songs[i] = URL.createObjectURL(req.result);
          updateSongBtn();
        }
      };
    });
  });
}
function updateSongBtn(){
  var btn = document.getElementById('songbtn');
  if(btn){
    var n = _songs.filter(Boolean).length;
    btn.innerHTML = n>0 ? '🎶'+n : '🎶';
    btn.title = n>0 ? n+' song'+(n>1?'s':'')+' loaded — tap to play' : 'Upload 2 songs';
  }
}
function playSongs(){
  if(_songs.filter(Boolean).length===0){
    // no songs — open file picker
    document.getElementById('songfile').click();
    return;
  }
  if(!_audioEl){
    _audioEl = new Audio();
    _audioEl.addEventListener('ended', function(){
      _songIdx = (_songIdx + 1) % _songs.filter(Boolean).length;
      playCurrentSong();
    });
  }
  if(_audioEl.paused){
    // stop ambient if playing
    if(typeof _musicOn!=='undefined' && _musicOn && typeof toggleMusic==='function') toggleMusic();
    playCurrentSong();
    if(typeof codeStatus==='function') codeStatus('songs playing');
  }else{
    _audioEl.pause();
    if(typeof codeStatus==='function') codeStatus('songs paused');
  }
}
function playCurrentSong(){
  var list = _songs.filter(Boolean);
  if(list.length===0 || !_audioEl) return;
  _audioEl.src = list[_songIdx % list.length];
  _audioEl.play().catch(function(){});
}
(function(){
  // hidden file input
  var fi = document.createElement('input');
  fi.type = 'file';
  fi.id = 'songfile';
  fi.accept = 'audio/*';
  fi.multiple = true;
  fi.style.display = 'none';
  fi.addEventListener('change', function(){
    var files = Array.prototype.slice.call(fi.files).slice(0, 2);
    files.forEach(function(f, i){
      saveSong(i, f);
      _songs[i] = URL.createObjectURL(f);
    });
    _songIdx = 0;
    updateSongBtn();
    if(typeof codeStatus==='function') codeStatus(files.length+' song'+(files.length>1?'s':'')+' loaded');
  });
  document.body.appendChild(fi);
  /* song button deleted — King's order */
  // load saved songs on start
  setTimeout(loadSongs, 2000);
})();
/* music button deleted — King's order */
/* TAP THE PLAYA 2.0 BOX → ENGAGE BLOCK BUILDING */
(function(){
  var raycaster = null, mouseVec = null;
  function onTap(e){
    try{
      if(!window._creat