from pathlib import Path p=Path(__file__).parent/'player.c';s=p.read_text() s=s.replace('#include ','#include \n#include ') s=s.replace('static UBYTE *frame,*scaled,*font,*panel;','static UBYTE *frame,*scaled,*font,*panel;static UWORD *xmap,*ymap;') mark='static struct HipPort*hip=0;' s=s.replace(mark,'static int playlist[MAXSONGS],plistCount=0,plistPos=0,currentSong=-1;static ULONG clockTicks=0,playTick=0,previousSeconds=0;static int seenPlaying=0,animTicks=1;\n'+mark) start=s.index('static void play(void)');end=s.index('static void move(',start) s=s[:start]+'''static void loadsong(int n){char cmd[450];snprintf(cmd,sizeof(cmd),"PLAY %s",songs[n].path);if(command(cmd)){strncpy(now,songs[n].title,95);now[95]=0;strncpy(currentPath,songs[n].path,319);currentPath[319]=0;playing=1;editing=0;currentSong=n;playTick=clockTicks;previousSeconds=0;seenPlaying=0;}} static void play(void){int i;if(!found)return;if(found==1){plistCount=total;for(i=0;i=top+rows)top=selected-rows+1;break;}} ''' +s[end:] # Animate in clock ticks, independent of render duration. s=s.replace('static void drawAudio(void){int i,h,b;for(i=0;i<24;i++){','static void drawAudio(void){int i,h,b,n;for(i=0;i<24;i++){for(n=0;nRPort,w->BorderLeft+dx0,w->BorderTop+dy0,w->BorderLeft+dx1-1,w->BorderTop+dy1-1,scaled+(ULONG)dy0*iw+dx0,iw);} ''' s=s[:at]+helper+s[at:] s=s.replace('dirty=1,lastState=-1;ULONG lastSecond','dirty=1,lastState=-1,full=1,lastw=0,lasth=0;struct DateStamp stamp;ULONG lastTick=0,lastAnalysis=0,lastSecond') s=s.replace('scaled=malloc((ULONG)sc->Width*sc->Height);if(!frame','scaled=malloc((ULONG)sc->Width*sc->Height);xmap=malloc(sc->Width*sizeof(UWORD));ymap=malloc(sc->Height*sizeof(UWORD));if(!xmap||!ymap||!frame') s=s.replace('while(!done){replies();dispatch();','while(!done){DateStamp(&stamp);clockTicks=(ULONG)stamp.ds_Minute*3000+stamp.ds_Tick;animTicks=lastTick?clockTicks-lastTick:1;if(animTicks<0||animTicks>10)animTicks=1;lastTick=clockTicks;replies();dispatch();') s=s.replace('frames++;if(frames%2==0)spectrum();\n if(dirty||seconds!=lastSecond||(hip?hip->play:0)!=lastState){','''frames++;if(clockTicks-lastAnalysis>=2){spectrum();lastAnalysis=clockTicks;} if(playing==1&&clockTicks-playTick>=150){if(hip&&hip->play){if(seenPlaying&&seconds+2play:0)!=lastState; if(full){''') start=s.index(' iw=w->Width-w->BorderLeft-w->BorderRight;');end=s.index(' Delay(1);',start) s=s[:start]+''' iw=w->Width-w->BorderLeft-w->BorderRight;ih=w->Height-w->BorderTop-w->BorderBottom; if(iw>0&&ih>0&&iw<=sc->Width&&ih<=sc->Height){ if(iw!=lastw||ih!=lasth){full=1;lastw=iw;lasth=ih;rw=iw;rh=iw*H/W;if(rh>ih){rh=ih;rw=ih*W/H;}ox=(iw-rw)/2;oy=(ih-rh)/2;for(x=0;xRPort,w->BorderLeft,w->BorderTop,w->BorderLeft+W-1,w->BorderTop+H-1,frame,W); else{WriteChunkyPixels(w->RPort,w->BorderLeft+382,w->BorderTop+104,w->BorderLeft+741,w->BorderTop+341,frame+104*W+382,W);WriteChunkyPixels(w->RPort,w->BorderLeft+382,w->BorderTop+504,w->BorderLeft+741,w->BorderTop+509,frame+504*W+382,W);} }else if(full){scaledRegion(w,iw,rw,rh,ox,oy,0,0,W,H);} else{scaledRegion(w,iw,rw,rh,ox,oy,382,104,742,342);scaledRegion(w,iw,rw,rh,ox,oy,382,504,742,510);} } ''' +s[end:] s=s.replace(' Delay(1);',' WaitTOF();') s=s.replace('free(frame);free(panel);','free(xmap);free(ymap);free(frame);free(panel);') p.write_text(s)