let canvas,soundFile,loadSoundFile=!1,pad,video,videoPath,videoScreen,videoRecorder,recordStartTime,recording=!1,videoDoneLoading=!1,thumbnail;function preload(){tokenID=tokenId,console.log("preload"),console.log("tokenID: "+tokenID),video=createVideo("https://xsvdzh47vjed2fk5cswadlljgz3taktplxxkmk5ou3r74bhuj4la.arweave.net/vKo8n5-qSD0VXRSsAa1pNncwKm9d7qYrrqbj_gT0TxY/"+tokenID+".mp4",videoLoaded);var e=tokenID%40;soundFile=loadSound("https://ixx47ownixpg26ptrzvtxwrucwzcw6pm3efp2xgqydbbpb2g5dmq.arweave.net/Re_Pus1F3m15845rO9o0FbIreezZCv1c0MDCF4dG6Nk/"+e+".wav"),loadSoundFile=!0,thumbnail=loadImage("https://7lo4gwrtaks7u2b4vfm5dtfzinl4joogohmpbd4keyosbah7ttba.arweave.net/-t3DWjMCpfpoPKlZ0cy5Q1fEucZx2PCPiiYdIID_nMI/"+tokenID+".jpg")}function setup(){console.log("setup"),console.log("visit www.myfi.studio for code + media licenses"),(canvas=createCanvas(windowWidth,windowHeight)).drop(gotFile),pad=new padInterface,(videoRecorder=new p5.VideoRecorder).onFileReady=saveVideo,image(thumbnail,0,0,windowWidth,windowHeight)}function draw(){mouseIsPressed&&videoDoneLoading?(pad.touch(),videoScreen.touch()):pad.stop(),recording&&millis()>recordStartTime+1e4&&(videoRecorder.stop(),recording=!1,console.log("recording stopped"))}class padInterface{constructor(){this.nextLoopStart=0}touch(){this.x=mouseX/windowWidth,1<this.x&&(this.x=1),this.y=mouseY/windowHeight,loadSoundFile?null!=soundFile?soundFile.isLoaded()&&(console.log("sample finished loading ::: duration = "+soundFile.duration()+" frames:"+soundFile.frames()),loadSoundFile=!1,this.soundFileLoaded=!0,this.sliceTime=.4,this.play()):console.log("no files dropped"):this.play()}play(){var o=millis()/1e3;if(!soundFile.isPlaying()||o>=this.nextLoopStart){soundFile.stop();var t=map(this.y,0,1,1e-4,2)*this.sliceTime;let e=this.x*soundFile.duration()-.5*t;e<0?e=0:e>soundFile.duration()&&(e=soundFile.duration()-this.sliceTime),soundFile.loop(0,1,1,e,this.sliceTime),this.nextLoopStart=o+t}}stop(){1==pad.soundFileLoaded&&soundFile.isPlaying()&&soundFile.stop()}}function gotFile(e){"audio"===e.type?(soundFile=loadSound(e),loadSoundFile=!0,console.log("loading a new audio file from drag + dropper")):console.log("not an audio file 404")}function videoLoaded(){console.log("video loaded"),console.log("video duration: "+video.duration()),videoScreen=new videoScreenInterface,videoDoneLoading=!0}class videoScreenInterface{constructor(){this.duration=video.duration(),console.log("videoScreen is constructed")}touch(){this.x=mouseX/windowWidth;let e=this.x*this.duration+random(-.1,.1);e<0?e=random(0,.1):e>=this.duration&&(e=random(this.duration-.1,this.duration)),video.time(e),video.hide(),image(video,0,0,windowWidth,windowHeight)}}function keyTyped(){68!==keyCode||recording||(videoRecorder.start(),recordStartTime=millis(),recording=!0,console.log("recording started"))}function saveVideo(){videoRecorder.url;videoRecorder.save("video_art_")}function mouseWheel(){return!1}p5.VideoRecorder=class{#blob;#chunks;#input;#mimeType;#recorder;#stream;#url;#onFileReady;constructor(e,o="webm"){this.format=o,this.input=e}get blob(){return this.#blob}get stream(){return this.#stream}get input(){return this.#input}set input(e){if(void 0===this.#stream||this.#stream!==e){if(this.recording)return console.error("VideoRecorder input was assigned a new value while recording. Call stop() before changing the input");var o;void 0===e?(void 0===drawingContext?.canvas?console.error("VideoRecorder couldn't find canvas to record"):this.input=drawingContext.canvas,"undefined"!=typeof soundOut&&void 0!==soundOut.output&&this.addInput(soundOut.output)):(o=Array.isArray(e)?this.#inputArrayToStream(e):this.#inputToStream(e),this.#input=e,this.#stream=o,this.#createRecorder())}}get format(){return this.#recorder.mimeType}set format(e){return"."===e.charAt(0)&&(e=e.slice(1)),0==p5.VideoRecorder.isSupported(e)?console.error(`Video format ${e} is not supported on this browser`):this.recording?console.error("Can't set format while video recorder is recording"):(this.#mimeType=1<e.split("/").length?e:"video/"+e,void(void 0!==this.#stream&&this.#createRecorder()))}get onFileReady(){return this.#onFileReady}set onFileReady(e){if("function"!=typeof e)return console.error("VideoRecorder onFileReady must be of type function but was assigned to "+typeof e);this.#onFileReady=e}get recording(){return void 0!==this.#recorder&&"recording"===this.#recorder.state}get url(){return this.#url}addInput(e){void 0===this.#stream?this.input=e:(this.#inputToStream(e).getTracks().forEach(e=>this.#stream.addTrack(e)),this.#createRecorder())}#audioNodeToStream(e){var o=e["context"],o=o.createMediaStreamDestination();return e.connect(o),o.stream}erase(){if(this.recording)return console.error("erase() was called while the video recorder was recording. Call stop() before erasing.");this.#chunks=[]}canRecord(e){return p5.VideoRecorder.canRecord(e)}static canRecord(e){return e instanceof MediaStream||e instanceof AudioNode||"function"==typeof e.captureStream||"function"==typeof e.elt?.captureStream}#createBlob(){this.#blob=new Blob(this.#chunks,{type:this.#recorder.mimeType}),this.#url=URL.createObjectURL(this.#blob),"function"==typeof this.#onFileReady&&this.#onFileReady()}#createRecorder(){this.#recorder=new MediaRecorder(this.#stream,{mimeType:this.#mimeType}),this.#recorder.onstop=()=>this.#createBlob(),this.#recorder.ondataavailable=e=>this.#chunks.push(e.data)}#inputArrayToStream(e){return e.forEach(e=>this.addInput(e)),this.#stream}#inputToStream(e){return 0==this.canRecord(e)?console.error(`Selected VideoRecorder input of type ${typeof e} cannot be recorded in this browser`):e instanceof MediaStream?e:e instanceof AudioNode?this.#audioNodeToStream(e):"function"==typeof e.captureStream?this.#mediaElementToStream(e):e instanceof p5.Element?this.#mediaElementToStream(e.elt):void 0}isSupported(e){return p5.VideoRecorder.isSupported(e)}static isSupported(e){return"."===e.charAt(0)&&(e=e.slice(1)),MediaRecorder.isTypeSupported(1<e.split("/").length?e:"video/"+e)}#mediaElementToStream(e){return"function"!=typeof e.captureStream?console.error("Can't capture stream from input "+e):e.captureStream()}pause(){this.#recorder.pause()}resume(){this.#recorder.resume()}save(e){if(void 0===this.#blob)return console.error("save() was called before a video file was created. Use onFileReady event to call a function when the video file is ready.");var o=this.#mimeType.match(/\/([^;]*)/)?.[1];[e,o]=p5.prototype._checkFileExtension(e,o),p5.prototype.downloadFile(this.#blob,e,o)}start(){this.erase(),this.#recorder.start()}stop(){console.assert(this.recording,"stop() was called while the video recorder was not recording. Call start() before stopping."),this.#recorder.stop()}};