104 lines
14 KiB
C
104 lines
14 KiB
C
#include <exec/types.h>
|
|
#include <intuition/intuition.h>
|
|
#include <graphics/gfxbase.h>
|
|
#include <rexx/storage.h>
|
|
#include <dos/dostags.h>
|
|
#include <proto/exec.h>
|
|
#include <proto/dos.h>
|
|
#include <proto/intuition.h>
|
|
#include <proto/graphics.h>
|
|
#include <proto/rexxsyslib.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include "fft.h"
|
|
#define W 760
|
|
#define H 560
|
|
#define MAXSONGS 6000
|
|
struct IntuitionBase*IntuitionBase;struct GfxBase*GfxBase;struct RxsLib*RexxSysBase;
|
|
struct PTChannel{BYTE*start;UWORD length;BYTE*loop;UWORD replen,volume,period,priv;};
|
|
struct HipPort{struct MsgPort port;ULONG priv1;APTR kpl;UWORD reserved;UBYTE quit,opens,volume,play,type,res2;struct PTChannel*ch[4];BYTE*left,*right;ULONG offs,maxoffs;UBYTE trigger[4];APTR priv2;ULONG seconds,colordiv;UWORD rate;};
|
|
struct Song{char*title,*path,*key;};static struct Song songs[MAXSONGS];static int matches[MAXSONGS],total=0,found=0,selected=0,top=0;
|
|
static UBYTE *frame,*scaled,*font,*panel;static LONG pen[12];static UBYTE colors[][3]={{17,18,15},{238,222,190},{255,180,62},{93,71,37},{33,32,24},{185,147,82},{116,211,138},{37,75,49},{236,105,56},{255,220,142},{53,43,27},{12,14,12}};
|
|
static struct HipPort*hip=0;static struct MsgPort*reply=0;static int pending=0,playing=0,volume=48,editing=1,rows=17;static char query[64]="",now[96]="Scegli un brano dalla libreria",currentPath[320]="";
|
|
static short pcm[256],re[256],im[256];static int bars[24],peak[24],target[24],vu[4],available=0;static ULONG seconds=0;
|
|
static void pixel(int x,int y,int c){if(x>=0&&x<W&&y>=0&&y<H)frame[y*W+x]=(UBYTE)pen[c];}
|
|
static void rect(int x,int y,int w,int h,int c){int j;for(j=0;j<h;j++)if(y+j>=0&&y+j<H){int l=x<0?0:x,r=x+w>W?W:x+w;if(r>l)memset(frame+(y+j)*W+l,(UBYTE)pen[c],r-l);}}
|
|
static void box(int x,int y,int w,int h,int c){rect(x,y,w,h,3);rect(x+1,y+1,w-2,h-2,c);}
|
|
static void text(int x,int y,char*s,int c,int scale,int max){int a,b,k,ch;for(k=0;s[k]&&k<max;k++){ch=(unsigned char)s[k]-32;if(ch<0||ch>=96)ch='?'-32;for(b=0;b<12;b++)for(a=0;a<8;a++)if(font[b*768+ch*8+a])rect(x+(k*8+a)*scale,y+b*scale,scale,scale,c);}}
|
|
static void center(int x,int y,int w,char*s,int c){text(x+(w-strlen(s)*8)/2,y,s,c,1,99);}
|
|
static void button(int x,int y,int w,char*s,int active){box(x,y,w,28,active?10:4);center(x,y+7,w,s,active?2:1);}
|
|
static char*dup(char*s){char*p=malloc(strlen(s)+1);if(p)strcpy(p,s);return p;}
|
|
static void lower(char*s){for(;*s;s++)*s=tolower((unsigned char)*s);}
|
|
static void filter(void){int i;char q[64];strcpy(q,query);lower(q);found=0;for(i=0;i<total;i++)if(!q[0]||strstr(songs[i].key,q))matches[found++]=i;selected=top=0;}
|
|
static void catalog(void){FILE*f=fopen("SYS:Utilities/AmberMod/catalog.txt","r");char line[700],key[700],*p,*e;if(!f)return;while(total<MAXSONGS&&fgets(line,sizeof(line),f)){p=strchr(line,'\t');if(!p)continue;*p++=0;e=strpbrk(p,"\r\n");if(e)*e=0;songs[total].title=dup(line);songs[total].path=dup(p);snprintf(key,sizeof(key),"%s %s",line,p);lower(key);songs[total].key=dup(key);if(!songs[total].title||!songs[total].path||!songs[total].key)break;total++;}fclose(f);filter();}
|
|
static void replies(void){struct RexxMsg*m;while((m=(struct RexxMsg*)GetMsg(reply))){if(m->rm_Args[0])DeleteArgstring(m->rm_Args[0]);if(m->rm_Result1==0&&m->rm_Result2)DeleteArgstring((UBYTE*)m->rm_Result2);DeleteRexxMsg(m);pending--;}}
|
|
static int sendcommand(char*s){struct RexxMsg*m;struct MsgPort*dest;m=CreateRexxMsg(reply,0,"HIPPOPLAYER");if(!m)return 0;m->rm_Action=RXCOMM;m->rm_Args[0]=CreateArgstring(s,strlen(s));if(!m->rm_Args[0]){DeleteRexxMsg(m);return 0;}Forbid();dest=FindPort("HIPPOPLAYER");if(dest){PutMsg(dest,(struct Message*)m);pending++;}Permit();if(!dest){DeleteArgstring(m->rm_Args[0]);DeleteRexxMsg(m);return 0;}return 1;}
|
|
static char commands[16][450];static int qhead=0,qtail=0,qcount=0;
|
|
static int command(char*s){if(qcount>=16)return 0;strncpy(commands[qtail],s,449);commands[qtail][449]=0;qtail=(qtail+1)%16;qcount++;return 1;}
|
|
static void dispatch(void){if(!pending&&qcount){sendcommand(commands[qhead]);qhead=(qhead+1)%16;qcount--;}}
|
|
static void attach(void){if(hip&&hip->quit){Forbid();hip->opens--;hip=0;Permit();}if(!hip){Forbid();hip=(struct HipPort*)FindPort("HiP-Port");if(hip&&!hip->quit)hip->opens++;else hip=0;Permit();}}
|
|
static void play(void){char cmd[450];int n;if(!found)return;n=matches[selected];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;}}
|
|
static void move(int delta,int start){if(!found)return;selected=(selected+delta+found)%found;if(selected<top)top=selected;if(selected>=top+rows)top=selected-rows+1;if(start)play();}
|
|
static void scrollto(int y){int max=found-rows;if(max<0)max=0;top=(y-140)*max/340;if(top<0)top=0;if(top>max)top=max;selected=top;}
|
|
static void audio(void){int i,c;memset(pcm,0,sizeof(pcm));memset(vu,0,sizeof(vu));available=0;seconds=0;attach();if(!hip)return;
|
|
Forbid();if(!hip->quit){seconds=hip->seconds;if(hip->play&&hip->type!=49){for(c=0;c<4;c++){struct PTChannel*p=hip->ch[c];BYTE*s,*loop;ULONG len,looplen,step,off;int vol,period;if(!p)continue;s=p->start;loop=p->loop;len=(ULONG)p->length*2;looplen=(ULONG)p->replen*2;vol=p->volume;period=p->period;if(!s||len<2||period<40||period>4000||vol>64)continue;available=1;step=(3546895UL*256)/(period*11025UL);vu[c]=vol;for(i=0;i<256;i++){off=(i*step)>>8;if(off<len)pcm[i]+=s[off]*vol/256;else if(loop&&looplen>2)pcm[i]+=loop[(off-len)%looplen]*vol/256;}}}
|
|
else if(hip->play&&hip->type==49&&hip->left&&hip->right&&hip->maxoffs<1048576&&hip->maxoffs>255){available=1;for(i=0;i<256;i++){ULONG off=(hip->offs+(ULONG)i*(hip->rate?hip->rate:22050)/11025)&hip->maxoffs;pcm[i]=(hip->left[off]+hip->right[off])/2;}vu[0]=vu[1]=hip->volume;}}
|
|
Permit();
|
|
}
|
|
static void spectrum(void){int i,j,k,len,half,step,idx,a,b,tr,ti,ur,ui,mag,t;audio();for(i=0;i<256;i++){re[i]=pcm[i]*hann[i]/256;im[i]=0;}j=0;for(i=1;i<256;i++){k=128;while(j&k){j^=k;k>>=1;}j^=k;if(i<j){t=re[i];re[i]=re[j];re[j]=t;}}
|
|
for(len=2;len<=256;len*=2){half=len/2;step=256/len;for(i=0;i<256;i+=len)for(j=0;j<half;j++){idx=j*step;a=i+j;b=a+half;tr=(twcos[idx]*re[b]-twsin[idx]*im[b])/16384;ti=(twcos[idx]*im[b]+twsin[idx]*re[b])/16384;ur=re[a];ui=im[a];re[a]=(ur+tr)/2;im[a]=(ui+ti)/2;re[b]=(ur-tr)/2;im[b]=(ui-ti)/2;}}
|
|
for(i=0;i<24;i++){mag=0;for(j=bandedge[i];j<=bandedge[i+1]&&j<128;j++){t=abs(re[j])+abs(im[j]);if(t>mag)mag=t;}mag/=5;if(mag>255)mag=255;t=available?logheight[mag]:0;if(t>144)t=144;target[i]=t;}
|
|
}
|
|
|
|
static void drawAudio(void){int i,h,b;for(i=0;i<24;i++){
|
|
int d=target[i]-bars[i];if(d){int delta=d/(d>0?2:4);if(!delta)delta=d>0?1:-1;bars[i]+=delta;}
|
|
if(bars[i]>peak[i])peak[i]=bars[i];else if(peak[i]>1)peak[i]-=2;else peak[i]=0;
|
|
h=bars[i];b=h<70?h:70;if(b)rect(398+i*13,303-b,9,b,6);
|
|
b=h>112?42:h-70;if(b>0)rect(398+i*13,233-b,9,b,2);
|
|
if(h>112)rect(398+i*13,303-h,9,h-112,8);
|
|
if(h)rect(398+i*13,303-h,9,1,9);
|
|
if(peak[i])rect(398+i*13,302-peak[i],9,1,9);
|
|
}
|
|
for(i=0;i<4;i++){rect(382+i*90,504,80,5,4);rect(382+i*90,504,vu[i]*70/64,5,5);}
|
|
}
|
|
|
|
int main(void){struct Screen*sc=0;struct Window*w=0;struct IntuiMessage*m;struct MsgPort*single=0;int got=0,done=0,i,x,y,iw,ih,rw,rh,ox,oy,frames=0,active=0,dragging=0,dirty=1,lastState=-1;ULONG lastSecond=0xffffffff,cls,lasts=0,lastm=0,ms,mm;UWORD code;char buf[160],cmd[40];FILE*f;
|
|
IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",39);GfxBase=(struct GfxBase*)OpenLibrary("graphics.library",40);RexxSysBase=(struct RxsLib*)OpenLibrary("rexxsyslib.library",0);if(!IntuitionBase||!GfxBase||!RexxSysBase)goto end;
|
|
single=CreateMsgPort();if(!single)goto end;single->mp_Node.ln_Name="AMBER.MOD.PLAYER";Forbid();if(FindPort("AMBER.MOD.PLAYER")){Permit();DeleteMsgPort(single);single=0;goto end;}AddPort(single);Permit();reply=CreateMsgPort();if(!reply)goto end;
|
|
sc=LockPubScreen(0);if(!sc)goto end;frame=malloc(W*H);panel=malloc(W*H);font=malloc(768*12);scaled=malloc((ULONG)sc->Width*sc->Height);if(!frame||!panel||!font||!scaled)goto end;f=fopen("SYS:Utilities/AmberMod/font.bin","rb");if(!f)goto end;i=fread(font,1,768*12,f);fclose(f);if(i!=768*12)goto end;
|
|
for(i=0;i<12;i++){pen[i]=ObtainBestPen(sc->ViewPort.ColorMap,colors[i][0]*0x01010101UL,colors[i][1]*0x01010101UL,colors[i][2]*0x01010101UL,TAG_DONE);if(pen[i]<0)goto end;got++;}catalog();
|
|
command("HIDE 0");snprintf(cmd,sizeof(cmd),"VOLUME %d",volume);command(cmd);
|
|
w=OpenWindowTags(0,WA_PubScreen,(ULONG)sc,WA_Left,230,WA_Top,150,WA_InnerWidth,W,WA_InnerHeight,H,WA_Title,(ULONG)"MOD Player",WA_Activate,TRUE,WA_CloseGadget,TRUE,WA_DragBar,TRUE,WA_DepthGadget,TRUE,WA_SizeGadget,TRUE,WA_SizeBBottom,TRUE,WA_MinWidth,650,WA_MinHeight,495,WA_MaxWidth,sc->Width,WA_MaxHeight,sc->Height,WA_ReportMouse,TRUE,WA_SimpleRefresh,TRUE,WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY|IDCMP_RAWKEY|IDCMP_MOUSEBUTTONS|IDCMP_MOUSEMOVE|IDCMP_REFRESHWINDOW,TAG_DONE);if(!w)goto end;
|
|
while(!done){replies();dispatch();while((m=(struct IntuiMessage*)GetMsg(w->UserPort))){cls=m->Class;code=m->Code;ms=m->Seconds;mm=m->Micros;x=(m->MouseX-w->BorderLeft)*W/(w->Width-w->BorderLeft-w->BorderRight);y=(m->MouseY-w->BorderTop)*H/(w->Height-w->BorderTop-w->BorderBottom);ReplyMsg((struct Message*)m);if(cls!=IDCMP_MOUSEMOVE||dragging)dirty=1;if(cls==IDCMP_CLOSEWINDOW)done=1;if(cls==IDCMP_REFRESHWINDOW){BeginRefresh(w);EndRefresh(w,TRUE);}
|
|
if(cls==IDCMP_MOUSEBUTTONS&&code==SELECTUP)dragging=0;if(cls==IDCMP_MOUSEMOVE&&dragging)scrollto(y);if(cls==IDCMP_MOUSEBUTTONS&&code==SELECTDOWN&&x>=348&&x<366&&y>=138&&y<=480){dragging=1;scrollto(y);}if(cls==IDCMP_MOUSEBUTTONS&&code==SELECTDOWN){editing=y>=48&&y<=84;if(y>=140&&y<480&&x>=18&&x<348){i=(y-140)/20+top;if(i<found){selected=i;if(DoubleClick(lasts,lastm,ms,mm))play();lasts=ms;lastm=mm;}}
|
|
if(y>=422&&y<450){if(x>=382&&x<442)move(-1,1);if(x>=450&&x<520)play();if(x>=528&&x<608){if(playing==2){command("CONT");playing=1;}else{command("STOP");playing=2;}}if(x>=616&&x<690){command("STOP");playing=0;}}
|
|
if(y>=462&&y<490){if(x>=382&&x<442)move(1,1);if(x>=450&&x<548&&found){selected=rand()%found;top=selected>rows?selected-rows/2:0;play();}if(x>=558&&x<596&&volume>=4)volume-=4;if(x>=650&&x<690&&volume<=60)volume+=4;snprintf(cmd,sizeof(cmd),"VOLUME %d",volume);command(cmd);}}
|
|
if(cls==IDCMP_VANILLAKEY){if(code==27){if(editing){editing=0;query[0]=0;filter();}else done=1;}else if(code==13)play();else if(editing){i=strlen(query);if(code==8&&i){query[i-1]=0;filter();}else if(code>=32&&code<127&&i<62){query[i]=code;query[i+1]=0;filter();}}else if(code==' '){if(playing==2){command("CONT");playing=1;}else{command("STOP");playing=2;}}else if(code=='/'){editing=1;}}
|
|
if(cls==IDCMP_RAWKEY){if(code==0x4c)move(-1,0);if(code==0x4d)move(1,0);if(code==0x7a)move(-3,0);if(code==0x7b)move(3,0);}}
|
|
frames++;if(frames%2==0)spectrum();
|
|
if(dirty||seconds!=lastSecond||(hip?hip->play:0)!=lastState){
|
|
memset(frame,(UBYTE)pen[0],W*H);text(20,12,"MOD / PLAYER",1,2,30);text(470,21,"AMIGA MODULE LIBRARY",5,1,30);
|
|
box(18,48,724,36,editing?10:4);text(30,60,"CERCA >",5,1,10);text(106,60,query,1,1,64);if(editing&&frames%20<12)rect(106+strlen(query)*8,60,6,12,2);if(!query[0]&&!editing)text(106,60,"Titolo, nome file o cartella...",3,1,64);
|
|
box(18,104,350,408,11);sprintf(buf,"LIBRERIA %d / %d",found,total);text(30,116,buf,5,1,40);
|
|
for(i=0;i<rows;i++){int n=i+top;if(n>=found)break;if(n==selected)rect(24,138+i*20,338,19,10);text(30,141+i*20,songs[matches[n]].title,n==selected?2:1,1,38);}
|
|
rect(350,138,11,340,4);{int size=found>rows?rows*340/found:340;int pos;if(size<22)size=22;pos=found>rows?top*(340-size)/(found-rows):0;rect(351,138+pos,9,size,5);rect(354,143+pos,3,size>10?size-10:1,2);}
|
|
box(382,104,360,238,11);text(396,116,"SPECTRUM / FFT",5,1,35);
|
|
for(y=151;y<=302;y+=30)rect(396,y,330,1,4);
|
|
|
|
text(398,317,"44Hz 1kHz 5.5kHz",3,1,40);if(hip&&hip->play&&!available)center(396,220,330,"ANALISI NON DISPONIBILE",5);
|
|
box(382,354,360,56,4);text(394,362,now,1,1,42);if(strlen(now)>42)text(394,376,now+42,1,1,42);sprintf(buf,"%s %02lu:%02lu %s",hip&&hip->play?"PLAY":playing==2?"PAUSE":"STOP",seconds/60,seconds%60,hip&&hip->type==33?"4CH / PAULA":"HIPPO ENGINE");text(394,393,buf,5,1,42);
|
|
button(382,422,60,"|<",0);button(450,422,70,"PLAY",playing==1);button(528,422,80,"PAUSA",playing==2);button(616,422,74,"STOP",playing==0);
|
|
button(382,462,60,">|",0);button(450,462,98,"RANDOM",0);button(558,462,38,"-",0);sprintf(buf,"%02d",volume*100/64);center(598,470,50,buf,2);button(650,462,40,"+",0);
|
|
|
|
text(20,532,"DOPPIO CLIC / INVIO: PLAY /: CERCA SPAZIO: PAUSA",5,1,80);
|
|
memcpy(panel,frame,W*H);dirty=0;lastSecond=seconds;lastState=hip?hip->play:0;}
|
|
memcpy(frame,panel,W*H);drawAudio();
|
|
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==W&&ih==H)WriteChunkyPixels(w->RPort,w->BorderLeft,w->BorderTop,w->BorderLeft+W-1,w->BorderTop+H-1,frame,W);else{rw=iw;rh=iw*H/W;if(rh>ih){rh=ih;rw=ih*W/H;}ox=(iw-rw)/2;oy=(ih-rh)/2;memset(scaled,(UBYTE)pen[0],(ULONG)iw*ih);for(y=0;y<rh;y++){int sy=(ULONG)y*H/rh;for(x=0;x<rw;x++)scaled[(ULONG)(oy+y)*iw+ox+x]=frame[sy*W+(ULONG)x*W/rw];}WriteChunkyPixels(w->RPort,w->BorderLeft,w->BorderTop,w->BorderLeft+iw-1,w->BorderTop+ih-1,scaled,iw);}}
|
|
Delay(1);
|
|
}
|
|
end:
|
|
if(hip){Forbid();hip->opens--;hip=0;Permit();}if(reply&&RexxSysBase){command("STOP");while(pending||qcount){dispatch();if(pending)WaitPort(reply);replies();}}if(w)CloseWindow(w);if(reply)DeleteMsgPort(reply);if(single){RemPort(single);DeleteMsgPort(single);}if(sc){for(i=0;i<got;i++)ReleasePen(sc->ViewPort.ColorMap,pen[i]);UnlockPubScreen(0,sc);}for(i=0;i<total;i++){free(songs[i].title);free(songs[i].path);free(songs[i].key);}free(frame);free(panel);free(font);free(scaled);if(RexxSysBase)CloseLibrary((struct Library*)RexxSysBase);if(GfxBase)CloseLibrary((struct Library*)GfxBase);if(IntuitionBase)CloseLibrary((struct Library*)IntuitionBase);return 0;
|
|
}
|