Files
amiga/AmberMod/resize-auto.py
T

42 lines
4.5 KiB
Python

from pathlib import Path
p=Path(__file__).parent/'player.c';s=p.read_text()
s=s.replace('#include <dos/dostags.h>','#include <dos/dostags.h>\n#include <dos/dos.h>')
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<total;i++)playlist[i]=i;plistPos=matches[selected];}else{plistCount=found;for(i=0;i<found;i++)playlist[i]=matches[i];plistPos=selected;}loadsong(matches[selected]);}
static void nextsong(void){int i;if(!plistCount)return;plistPos=(plistPos+1)%plistCount;loadsong(playlist[plistPos]);for(i=0;i<found;i++)if(matches[i]==currentSong){selected=i;if(selected<top)top=selected;if(selected>=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;n<animTicks;n++){')
s=s.replace('else peak[i]=0;\n h=bars[i];','else peak[i]=0;}\n h=bars[i];')
# Scale only the requested dirty region, with lookup tables instead of division per pixel.
at=s.index('int main(void)')
helper='''static void scaledRegion(struct Window*w,int iw,int rw,int rh,int ox,int oy,int x0,int y0,int x1,int y1){int x,y,dx0=ox+x0*rw/W,dx1=ox+(x1*rw+W-1)/W,dy0=oy+y0*rh/H,dy1=oy+(y1*rh+H-1)/H;for(y=dy0;y<dy1;y++){UBYTE*src=frame+ymap[y-oy]*W;UBYTE*dst=scaled+(ULONG)y*iw;for(x=dx0;x<dx1;x++)dst[x]=src[xmap[x-ox]];}WriteChunkyPixels(w->RPort,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+2<previousSeconds){nextsong();dirty=1;}else seenPlaying=1;}else if(seenPlaying){nextsong();dirty=1;}}
previousSeconds=seconds;
full=dirty||seconds!=lastSecond||(hip?hip->play: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;x<rw;x++)xmap[x]=(ULONG)x*W/rw;for(y=0;y<rh;y++)ymap[y]=(ULONG)y*H/rh;memset(scaled,(UBYTE)pen[0],(ULONG)iw*ih);}
if(iw==W&&ih==H){
if(full)WriteChunkyPixels(w->RPort,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)