// wav playback var lastvol;; var forcedvol=0;; fun volfrombutton v=let 255-v -> v in 255-((v*v)>>8);; fun updatevol= let button3 -> v in if !forcedvol && v!=lastvol && (lastvol!=255 || v<250) then ( set lastvol=v; sndVol volfrombutton v );; fun forcevol v= sndVol volfrombutton (button3*v)>>8; set forcedvol=1; 0;; fun unforcevol= set forcedvol=0; set lastvol=nil; 0;; //-------------------------------------------------- const WAV_IDLE=0;; const WAV_RUN=1;; const WAV_EOF=2;; const WAV_BUFFER_STARTSIZE= 80000;; const WAV_BUFFER_MAXSIZE =400000;; const WAV_END_TIMEOUT=500;; const WAV_NET_TIMEOUT=15000;; var wav_end_timeout;; var wav_http_cb=nil;; var wav_state=0;; var wav_http;; var wav_fifo;; var wav_buffering=0;; var wav_buffering_since=0;; var wav_index;; var wav_lasttime;; var wav_lastnet;; var wav_zeros;; fun wavgetzeros= if wav_zeros==nil then ( set wav_zeros=strnew 2048; for i=0;i<2048 do strset wav_zeros i 0 ); wav_zeros;; fun wavstop = Secholn "###wavstop"; if wav_state!=WAV_IDLE then ( playStop; if wav_http!=nil then httpabort wav_http; set wav_http=nil; set wav_buffering = 0; set wav_state=WAV_IDLE );; fun wavrunning = if wav_state==WAV_IDLE then 0 else if wav_fifo==nil && wav_state==WAV_EOF && (time_ms-wav_lasttime > wav_end_timeout) then ( Secholn "###wav detected end"; wavstop; 0 ) else if wav_lasttime==nil then -1 else 1;; // The main wav playback callback // reads from wav_fifo and feeds playFeed fun _wavplaycb i = set wav_lasttime=time_ms; if wav_fifo==nil then ( // nothing in fifo if wav_state==WAV_EOF then playFeed nil nil nil else if !wav_buffering then ( Secho ">>>>buffering"; set wav_buffering=1; set wav_buffering_since = time_ms ) ) else ( // was buffering ? if wav_buffering && (wav_state==WAV_EOF || (slistlen wav_fifo)>=WAV_BUFFER_STARTSIZE) then ( Secholn "<<< sample in let strlen sample -> len in ( // push sample data // wav_index is the index within the current sample. if wav_index pushedbytes in ( // this is how many bytes we sent. set wav_index=wav_index+pushedbytes ) ); if wav_index>=len then ( // we sent the whole first sample. // (in fact, wav_index cannot be bigger than len). // reset the counter on the sample and transition to the next sample. set wav_index=0; set wav_fifo=tl wav_fifo; // Secho "[b-:" ; Iecho (slistlen wav_fifo); Secho "]"; if wav_http!=nil then if (slistlen wav_fifo)>8)::(ctoa i>>16)::(ctoa i>>24)::nil;; fun itobin2 i=strcatlist (ctoa i)::(ctoa i>>8)::nil;; fun mkwav freq channel bps= let strcatlist "WAVEfmt "::(itobin4 0x12):: (itobin2 1)::(itobin2 channel):: (itobin4 freq)::(itobin4 freq*channel*bps/8):: (itobin2 channel*bps/8)::(itobin4 bps):: "data"::(itobin4 0)::nil -> c in strcatlist "RIFF"::(itobin4 (strlen c))::c::nil;; */ fun _wavcbhttp httpreq req= set wav_lastnet = time_ms; if req == nil then ( Secholn ">>>>>>>>>>>>>>>>>>>>>>>>>>>>> n in ( if wav_index==nil && n>WAV_BUFFER_STARTSIZE then ( // had never started wav playback : start it set wav_fifo=tl wav_fifo; _wavstartnow ) else if n > WAV_BUFFER_MAXSIZE then ( Secho "##http wait##"; httpenable httpreq 0 ); nil ) ); updatevol; 0;; fun wavstarthttp url cb= wavstop; set wav_http_cb = cb; set wav_end_timeout = WAV_END_TIMEOUT; set wav_fifo = nil; set wav_state = WAV_RUN; set wav_index = nil; set wav_buffering = 1; set wav_buffering_since = time_ms; set wav_lasttime = nil; set wav_lastnet = time_ms; set wav_http = httprequest "GET" url nil #_wavcbhttp HTTP_STREAM;; fun wavtime = // Check timeout against wav_buffering_since if (wav_http!=nil && wav_state==WAV_RUN && wav_buffering && (time_ms - wav_buffering_since > WAV_NET_TIMEOUT)) then ( Secho "wavhttp timeout"; wavstop; if (wav_http_cb != nil) then call wav_http_cb [] ); 0;;