import{_,d as a,i as u,j as m,M as R,Y as b,a9 as w,R as A,m as C,c as d,g as $,af as k,ag as f,f as l,t as c,n as y,q as B,F as D,z as M,A as V,C as L}from"./entry.4d7b4fe3.js";const E={props:{tag:{type:String,default:"div"}},emits:["start","stop","dataavailable"],data(){return{isAllowed:!0,isSupported:!1,isRecording:!1,audioStream:null,mediaRecorder:null,audioBlobs:[]}},computed:{slotBindings(){return{state:{isAllowed:this.isAllowed,isSupported:this.isSupported,isRecording:this.isRecording},methods:{startRecording:this.startRecording,stopRecording:this.stopRecording},context:{audioStream:this.audioStream,audioBlobs:this.audioBlobs}}}},mounted(){var t;this.isSupported=(t=navigator==null?void 0:navigator.mediaDevices)==null?void 0:t.getUserMedia},methods:{async startRecording(){var t;try{this.audioStream=await((t=navigator.mediaDevices)==null?void 0:t.getUserMedia({audio:!0})),this.mediaRecorder=new MediaRecorder(this.audioStream),this.mediaRecorder.addEventListener("stop",this.onStop),this.mediaRecorder.addEventListener("dataavailable",this.onDataAvailable),this.isRecording=!0,this.mediaRecorder.start(),this.$emit("start",this.slotBindings)}catch(e){console.error(e),this.isAllowed=!1}},stopRecording(){this.mediaRecorder.stop(),this.audioStream.getTracks().forEach(t=>t.stop())},onDataAvailable({data:t}){this.$emit("dataavailable",{...this.slotBindings,data:t}),this.audioBlobs.push(t)},onStop(){const{audioBlobs:t}=this,e=new Blob(t,{type:"audio/mp3"});this.$emit("stop",{...this.slotBindings,data:e}),this.isRecording=!1,this.mediaRecorder=null,this.audioStream=null}}};function F(t,e,o,i,n,s){return t.$slots.default?(a(),u(A(o.tag),{key:0,class:"c-input-audio"},{default:m(()=>[R(t.$slots,"default",b(w(s.slotBindings)))]),_:3})):C("",!0)}const U=_(E,[["render",F]]),q={viewBox:"0 0 16 16",fill:"none",xmlns:"http://www.w3.org/2000/svg","aria-hidden":"true",focusable:"false"},z=$('<rect x="5.5" y="1.5" width="5" height="8" rx="2.5" stroke="currentColor"></rect><mask id="icon-microphone_svg__mask0_318_2277" style="mask-type:alpha;" maskUnits="userSpaceOnUse" x="0" y="7" width="16" height="6"><rect y="7" width="16" height="6" fill="#D9D9D9"></rect></mask><g mask="url(#icon-microphone_svg__mask0_318_2277)"><circle cx="8" cy="7" r="5.5" stroke="currentColor"></circle></g><line x1="8" y1="12" x2="8" y2="15" stroke="currentColor"></line>',4),N=[z];function T(t,e){return a(),d("svg",q,N)}const j={render:T},I={components:{SvgMicrophone:j,SvgCross:k},props:{modelValue:{validator(t){return t==null||typeof t=="object"}}},emits:["update:modelValue"],data(){return{isRunning:!1,data:null,start:0,duration:0,audioContext:null,audioAnalyser:null,audioSource:null,bufferLength:null,dataArray:null,digestableDataArray:[]}},computed:{durationString(){const t=Math.round(this.duration/1e3%60);return`${Math.floor(this.duration/1e3/60)}:${`00${t}`.slice(-2)}`}},methods:{onStart({context:t}){this.initialise(t==null?void 0:t.audioStream),this.start=new Date().getTime()},onStop({data:t}){this.$emit("update:modelValue",t),this.data=t,this.isRunning=!1,this.duration=new Date().getTime()-this.start},toggleRecording(){var t,e,o,i,n;(t=this.$refs.input)!=null&&t.isRecording?(o=(e=this.$refs.input)==null?void 0:e.stopRecording)==null||o.call(e):(n=(i=this.$refs.input)==null?void 0:i.startRecording)==null||n.call(i)},initialise(t){const e=this.audioContext=new AudioContext;this.audioAnalyser=e.createAnalyser(),this.audioSource=e.createMediaStreamSource(t),this.audioAnalyser.smoothingTimeConstant=.85,this.audioSource.connect(this.audioAnalyser),this.bufferLength=this.audioAnalyser.frequencyBinCount,this.dataArray=new Uint8Array(Math.min(this.bufferLength,256)),this.isRunning=!0,this.update()},update(){this.isRunning&&(window.requestAnimationFrame(this.update),this.audioAnalyser.getByteFrequencyData(this.dataArray),this.digestableDataArray=this.gaussianBlur([...this.dataArray]))},gaussianBlur(t){const e=[2,22,97,159,97,22,2],o=e.reduce((n,s)=>n+s,0),i=[];return t.forEach((n,s)=>{i[s]=0;for(let r=0;r<e.length;r++){const h=r-(e.length-1)/2;i[s]+=t[s+h]*e[r]}i[s]/=o}),i}}},P=["textContent"],O={class:"font-gt-america-extended text-underline text-center uppercase opacity-60"},Y=["textContent"],G=["textContent"],H=["textContent"],J={class:y(["w-full h-full opacity-5","flex items-center","absolute top-full left-1/2 scale-105","transform -translate-x-1/2 -translate-y-1/2"])};function K(t,e,o,i,n,s){const r=f("SvgMicrophone"),h=f("SvgCross"),S=L,v=U;return a(),u(v,{ref:"input",class:"c-audio-visualiser flex flex-col gap-y-sm",onStart:s.onStart,onStop:s.onStop},{default:m(({state:p})=>[l("div",{class:"text-button",textContent:c(`${t.$t("record-your-message")} *`)},null,8,P),l("button",{class:y(["relative overflow-hidden","flex flex-col justify-center items-center gap-y-2xs","w-full aspect-video rounded-sm border border-strong border-opacity-20","focus:border-opacity-100","py-xl px-lg outline-none"]),onClick:e[0]||(e[0]=(...g)=>s.toggleRecording&&s.toggleRecording(...g))},[B(S,{tag:"div"},{icon:m(()=>[p.isRecording?(a(),u(h,{key:1})):(a(),u(r,{key:0}))]),_:2},1024),l("div",O,[n.data&&!p.isRecording?(a(),d("span",{key:0,textContent:c(s.durationString)},null,8,Y)):p.isRecording?(a(),d("span",{key:2,textContent:c(t.$t("stop-recording"))},null,8,H)):(a(),d("span",{key:1,textContent:c(t.$t("tell-your-story"))},null,8,G))]),l("div",J,[(a(!0),d(D,null,M(n.digestableDataArray,(g,x)=>(a(),d("div",{key:`bar-${x}`,class:"flex-1 rounded-full bg-current",style:V(`height: calc(${g/256*100}% + 8px)`)},null,4))),128))])])]),_:1},8,["onStart","onStop"])}const W=_(I,[["render",K]]);async function X(t,e){const o=new FormData;o.append("name",t),o.append("contents",e),$fetch("/api/audio",{method:"post",body:o})}export{W as _,X as s};