home *** CD-ROM | disk | FTP | other *** search
- #include<gemfast.h>
- #include<aesbind.h>
- #include<vdibind.h>
- #include<stdio.h>
- #include<math.h>
- #include<string.h>
- #include<unistd.h>
- #include<stdlib.h>
- #include<signal.h>
- #include<sound.h>
- #include<osbind.h>
-
- #define TRUE 1
- #define FALSE 0
-
- #define RL 20
- #define RO 10
- #define RR 10
- #define RU 10
- #define N 128
- #define LODBUFFSIZE 2048
- #define DMABUFFSIZE 4096
-
- void draw_fft(int flag, int x, int y, int w, int h);
-
- typedef struct
- {
- long playbufptr;
- long recbufptr;
- long res1;
- long res2;
- } bufptrtype;
-
- typedef struct
- {
- short li,re;
- } STEREO;
-
- typedef struct
- {
- int grid[46];
- int rahmen[10];
- char txth[5][5];
- int thx[5], thy[5];
- char txtv[5][5];
- int tvx[5], tvy[5];
- } FFTGRID;
-
- long _stksize=-50000L;
-
- int gl_wbox, gl_hbox, gl_wchar, gl_hchar;
- int g_handle, gl_xmax, gl_ymax;
- int w_handle=-1,w_open=FALSE,w_ontop=FALSE;
- int work_in[11], work_out[57];
- int ap_id;
- int max_polyline_xy;
- int abtastfrequenz,clk;
- GRECT win;
- FFTGRID fftgrid;
- int lispec1[256],lispec2[256],respec1[256],respec2[256];
- int *lispectrum,*lialtspectrum,*respectrum,*realtspectrum;
- STEREO dmadata[DMABUFFSIZE];
- int lodbuffer[LODBUFFSIZE];
- bufptrtype bufptr;
- int wordsize,xmem,ymem,ability,flag,max_dspval,min_dspval;
- int sigende=FALSE;
-
- void init_fft()
- {
- int i,rw,rh,f;
- double rast;
-
- rw=win.g_w-RL-RR; rh=win.g_h/2-RO-RU;
- fftgrid.rahmen[0]=RL; fftgrid.rahmen[1]=RO;
- fftgrid.rahmen[2]=RL+rw; fftgrid.rahmen[3]=RO;
- fftgrid.rahmen[4]=RL+rw; fftgrid.rahmen[5]=RO+rh;
- fftgrid.rahmen[6]=RL; fftgrid.rahmen[7]=RO+rh;
- fftgrid.rahmen[8]=RL; fftgrid.rahmen[9]=RO;
- rast=(double)((double)abtastfrequenz/1000)/((int)(abtastfrequenz/1000));
- rast=(double)rw/rast/8;
- for(i=0;i<8;i++)
- {
- fftgrid.grid[i*4]=RL+(int)(rast*(i+1));
- fftgrid.grid[i*4+2]=fftgrid.grid[i*4];
- if((i%2)==0)
- {
- fftgrid.grid[i*4+1]=RO;
- fftgrid.grid[i*4+3]=RO+rh;
- }
- else
- {
- fftgrid.grid[i*4+1]=RO+rh;
- fftgrid.grid[i*4+3]=RO;
- }
- }
- for(i=0;i<5;i++)
- {
- fftgrid.thy[i]=RO-2;
- f=abtastfrequenz/4000*i;
- if(f%2)
- sprintf(&fftgrid.txth[i][0],"%4.1f",(double)f/2);
- else
- sprintf(&fftgrid.txth[i][0],"%d",f/2);
- fftgrid.thx[i]=RL+(int)(rast*i*2)-strlen(&fftgrid.txth[i][0])*2;
- }
- rast=(double)rh/4.0;
- fftgrid.grid[32]=RL+rw; fftgrid.grid[33]=RO;
- for(i=0;i<3;i++)
- {
- if((i%2)==0)
- {
- fftgrid.grid[i*4+34]=RL+rw;
- fftgrid.grid[i*4+36]=RL;
- }
- else
- {
- fftgrid.grid[i*4+34]=RL;
- fftgrid.grid[i*4+36]=RL+rw;
- }
- fftgrid.grid[i*4+35]=RO+(int)(rast*(i+1));
- fftgrid.grid[i*4+37]=fftgrid.grid[i*4+35];
- }
- for(i=0;i<5;i++)
- {
- fftgrid.tvx[i]=2;
- fftgrid.tvy[i]=RO+2+(int)(rast*i);
- sprintf(&fftgrid.txtv[i][0],"%3d",i*(-20));
- }
- for(i=0;i<256;i+=2)
- {
- lispec1[i]=lispec2[i]=respec1[i]=respec2[i]=RL+i*rw/256+1;
- lispec1[i+1]=lispec2[i+1]=respec1[i+1]=respec2[i+1]=RO+rh;
- }
- lispectrum=lispec1;
- lialtspectrum=lispec2;
- respectrum=respec1;
- realtspectrum=respec2;
- }
-
- void clear_connections()
- {
- Devconnect(0,0,0,0,1);
- Devconnect(1,0,0,0,1);
- Devconnect(2,0,0,0,1);
- Devconnect(3,0,0,0,1);
- }
-
- int init_sound()
- {
- if (Locksnd()>0)
- {
- clear_connections();
- Soundcmd(LTATTEN,0x60);
- Soundcmd(RTATTEN,0x60);
- Soundcmd(LTGAIN,0xf0);
- Soundcmd(RTGAIN,0xf0);
- Soundcmd(ADDRIN,0x02);
- Soundcmd(ADCINPUT,0x00);
- Setsndmode(1);
- Setbuffer(1,dmadata,dmadata+DMABUFFSIZE);
- Setmontrack(0);
- Settrack(0,0);
- Dsptristate(1,1);
- Devconnect(ADC,DSPREC | DAC,0,clk,1);
- Devconnect(DSPXMIT,DMAREC,0,clk,1);
- return(TRUE);
- }
- else
- {
- Unlocksnd();
- return(FALSE);
- }
- }
-
- void restore_soundsystem()
- {
- Unlocksnd();
- Buffoper(0);
- flag=Dsp_Hf0(0);
- Dsptristate(0,0);
- clear_connections();
- Devconnect(3,0,0,CLK50K,1);
- Soundcmd(LTATTEN,0x80);
- Soundcmd(RTATTEN,0x70);
- Soundcmd(LTGAIN,0xb0);
- Soundcmd(RTGAIN,0x40);
- Soundcmd(ADDRIN,0x01);
- Soundcmd(ADCINPUT,0x03);
- Dsp_Unlock();
- }
-
- int dsp_init()
- {
- int state;
- char cwd_spec[256];
-
- if(!(state=Dsp_Lock()))
- {
- wordsize=Dsp_GetWordSize();
- max_dspval=((1<<(8*wordsize))>>1)-1;
- min_dspval=-max_dspval-1;
- Dsp_Available(&xmem,&ymem);
- if(!(state=Dsp_Reserve(xmem,ymem)))
- {
- ability=Dsp_RequestUniqueAbility();
- flag=Dsp_Hf0(0);
- Dgetpath(cwd_spec,0);
- strcat(cwd_spec,"\\spektrum.lod");
- state=Dsp_LoadProg(cwd_spec,ability,lodbuffer);
- if(state)
- {
- form_alert(1,"[1][Wo ist die Datei|SPEKTRUM.LOD][ Ende ]");
- Dsp_Unlock();
- return(FALSE);
- }
- return(TRUE);
- }
- else
- {
- Dsp_Unlock();
- return(FALSE);
- }
- }
- return(FALSE);
- }
-
- int start_soundsystem()
- {
- int state;
-
- if(state=init_sound())
- {
- if(state&=dsp_init())
- {
- flag=Dsp_Hf0(1);
- Buffoper(0x0c);
- }
- else
- Unlocksnd();
- }
- if(!state)
- {
- form_alert(1,"[3][Fehler beim Start des|Soundsystems.][ Ende ]");
- }
- return(state);
- }
-
- int open_fftwindow(int x, int y, int w, int h)
- {
- int dummy,fx,fy,fw,fh;
-
- wind_get(0,WF_FULLXYWH,&fx,&fy,&fw,&fh);
- w_handle=wind_create(NAME | CLOSER | MOVER,fx,fy,fw,fh);
- if(w_handle>0)
- {
- wind_set(w_handle,WF_NAME,"Spektrum",dummy,dummy,dummy);
- if(x<fx) x=fx;
- if(y<fy) y=fy;
- if(w>fw) w=fw;
- if(h>fh) h=fh;
- v_hide_c(g_handle);
- graf_growbox(x+w/2-w/40,y+h/2-h/40,w/20,h/20,x,y,w,h);
- wind_open(w_handle,x,y,w,h);
- wind_get(w_handle,WF_WORKXYWH,&win.g_x,&win.g_y,&win.g_w,&win.g_h);
- v_show_c(g_handle,1);
- w_open=TRUE; w_ontop=TRUE;
- init_fft();
- return(TRUE);
- }
- else
- {
- form_alert(1,"[1][Es sind zu viele Fenster offen.|Schließen Sie zuerst welche.][ Ende ]");
- w_open=FALSE; w_ontop=FALSE;
- return(FALSE);
- }
- }
-
- void fft_moved(int x, int y, int w, int h)
- {
- wind_set(w_handle,WF_CURRXYWH,x,y,w,h);
- wind_get(w_handle,WF_WORKXYWH,&win.g_x,&win.g_y,&win.g_w,&win.g_h);
- }
-
- void fft_topped()
- {
- int dummy;
- wind_set(w_handle,WF_TOP,dummy,dummy,dummy,dummy);
- }
-
- void fft_closed()
- {
- int x,y,w,h;
-
- wind_get(w_handle,WF_CURRXYWH,&x,&y,&w,&h);
- graf_shrinkbox(x+w/2-w/40,y+h/2-h/40,w/20,h/20,x,y,w,h);
- wind_close(w_handle);
- w_open=FALSE; w_ontop=FALSE; w_handle=-1;
- }
-
- void fft_redraw(int x, int y, int w, int h)
- {
- GRECT r1,r2;
- int pxy1[46],pxy2[46],pxy3[10],pxy4[10],pxyarray[4];
- int thx[5],thy1[5],thy2[5],tvx[5],tvy1[5],tvy2[5];
- int dummy,i,h2,cx,cy;
-
- r2.g_x=x; r2.g_y=y; r2.g_w=w; r2.g_h=h;
- wind_get(w_handle,WF_FIRSTXYWH,&r1.g_x,&r1.g_y,&r1.g_w,&r1.g_h);
- vswr_mode(g_handle,MD_REPLACE);
- vsl_width(g_handle,1);
- vsl_color(g_handle,BLACK);
- vst_alignment(g_handle,0,0,&dummy,&dummy);
- vst_height(g_handle,4,&dummy,&dummy,&dummy,&dummy);
- vst_color(g_handle,BLACK);
- vst_effects(g_handle,0);
- vsf_style(g_handle,3);
- vsf_perimeter(g_handle,TRUE);
- vsf_interior(g_handle,1);
- vsf_color(g_handle,WHITE);
- h2=win.g_h/2;
- for(i=0;i<46;i+=2)
- {
- pxy1[i]=fftgrid.grid[i]+win.g_x;
- pxy2[i]=pxy1[i];
- pxy1[i+1]=fftgrid.grid[i+1]+win.g_y;
- pxy2[i+1]=pxy1[i+1]+h2;
- }
- for(i=0;i<10;i+=2)
- {
- pxy3[i]=fftgrid.rahmen[i]+win.g_x;
- pxy4[i]=pxy3[i];
- pxy3[i+1]=fftgrid.rahmen[i+1]+win.g_y;
- pxy4[i+1]=pxy3[i+1]+h2;
- }
- for(i=0;i<5;i++)
- {
- thx[i]=fftgrid.thx[i]+win.g_x;
- thy1[i]=fftgrid.thy[i]+win.g_y;
- thy2[i]=thy1[i]+h2;
- }
- for(i=0;i<5;i++)
- {
- tvx[i]=fftgrid.tvx[i]+win.g_x;
- tvy1[i]=fftgrid.tvy[i]+win.g_y;
- tvy2[i]=tvy1[i]+h2;
- }
- cx=win.g_x+50; cy=win.g_y+win.g_h/2;
- v_hide_c(g_handle);
- wind_update(BEG_UPDATE);
- while (r1.g_w && r1.g_h) /* solange es was zu restaurieren gibt */
- {
- if (rc_intersect(&r2,&r1)) /* zu restaurierende Fläche */
- {
- grect_to_array(&r1,pxyarray); /* AES in VDI Rechteck umrechnen */
- vs_clip(g_handle,TRUE,pxyarray); /* clipping setzen */
- vr_recfl(g_handle,pxyarray); /* Hintergrund zeichnen */
- vsl_type(g_handle,3);
- v_pline(g_handle,23,pxy1);
- v_pline(g_handle,23,pxy2);
- vsl_type(g_handle,1);
- v_pline(g_handle,5,pxy3);
- v_pline(g_handle,5,pxy4);
- for(i=0;i<5;i++)
- {
- v_gtext(g_handle,thx[i],thy1[i],fftgrid.txth[i]);
- v_gtext(g_handle,thx[i],thy2[i],fftgrid.txth[i]);
- }
- for(i=0;i<5;i++)
- {
- v_gtext(g_handle,tvx[i],tvy1[i],fftgrid.txtv[i]);
- v_gtext(g_handle,tvx[i],tvy2[i],fftgrid.txtv[i]);
- }
- v_gtext(g_handle,cx,cy,"(c) 1993 Thomas Gerner");
- }
- wind_get(w_handle,WF_NEXTXYWH,&r1.g_x,&r1.g_y,&r1.g_w,&r1.g_h);
- }
- wind_update(END_UPDATE);
- v_show_c(g_handle,1);
- draw_fft(FALSE,x,y,w,h);
- }
-
- void draw_fft(int flag, int x, int y, int w, int h)
- {
- GRECT r1,r2;
- int lipxy1[256],lipxy2[256],repxy1[256],repxy2[256],pxyarray[4],*sa;
- int dummy,mx,my,m_aus,i;
-
- r2.g_x=x; r2.g_y=y; r2.g_w=w; r2.g_h=h;
- for(i=0;i<256;)
- {
- lipxy1[i]=repxy1[i]=lialtspectrum[i]+win.g_x;
- lipxy2[i]=repxy2[i]=lispectrum[i]+win.g_x; i++;
- lipxy1[i]=lialtspectrum[i]+win.g_y;
- repxy1[i]=realtspectrum[i]+win.g_y;
- lipxy2[i]=lispectrum[i]+win.g_y;
- repxy2[i]=respectrum[i]+win.g_y; i++;
- }
- if(flag)
- { sa=lispectrum; lispectrum=lialtspectrum; lialtspectrum=sa;
- sa=respectrum; respectrum=realtspectrum; realtspectrum=sa; }
- vsl_type(g_handle,1);
- vsl_width(g_handle,1);
- vsl_color(g_handle,BLACK);
- vswr_mode(g_handle,MD_XOR);
- graf_mkstate(&mx,&my,&dummy,&dummy);
- m_aus=(mx>(r2.g_x-8))&&(mx<(r2.g_x+r2.g_w))&&(my>(r2.g_y-16))&&(my<(r2.g_y+r2.g_h));
- wind_get(w_handle,WF_FIRSTXYWH,&r1.g_x,&r1.g_y,&r1.g_w,&r1.g_h);
- if(m_aus) v_hide_c(g_handle);
- if(wind_update(BEG_UPDATE))
- while (r1.g_w && r1.g_h)
- {
- if (rc_intersect(&r2,&r1))
- {
- grect_to_array(&r1,pxyarray);
- vs_clip(g_handle,TRUE,pxyarray);
- v_pline(g_handle,128,lipxy1);
- if(flag)
- v_pline(g_handle,128,lipxy2);
- v_pline(g_handle,128,repxy1);
- if(flag)
- v_pline(g_handle,128,repxy2);
- }
- wind_get(w_handle,WF_NEXTXYWH,&r1.g_x,&r1.g_y,&r1.g_w,&r1.g_h);
- }
- wind_update(END_UPDATE);
- if(m_aus) v_show_c(g_handle,1);
- }
-
- void get_fftdata(int startindex)
- {
- int i,si,rh,h2,a;
- STEREO *data;
-
- i=0; data=dmadata+startindex;
- while((i<DMABUFFSIZE/4)&&(data->li!=0xffff8000)) { i++; data++; }
- if(i<DMABUFFSIZE/4)
- {
- si=(startindex+i-129)%DMABUFFSIZE;
- if(si<0) si+=DMABUFFSIZE;
- data=dmadata;
- h2=win.g_h/2;
- rh=h2-RO-RU;
- a=((data[si].li)>0) ? (-rh)*(log10(data[si].li)-4.5154)/4 : rh-1;
- if(a>rh) a=rh-1;
- lispectrum[1]=a+RO;
- a=((data[si].re)>0) ? (-rh)*(log10(data[si].re)-4.5154)/4 : rh-1;
- if(a>rh) a=rh-1;
- respectrum[1]=a+RO+h2;
- si=(si+1)%DMABUFFSIZE;
- for(i=3;i<256;i+=2)
- {
- a=((-rh)*data[si].li)>>11;
- if(a>rh) a=rh-1;
- lispectrum[i]=a+RO;
- a=((-rh)*data[si].re)>>11;
- if(a>rh) a=rh-1;
- respectrum[i]=a+RO+h2;
- si=(si+1)%DMABUFFSIZE;
- }
- draw_fft(TRUE,win.g_x,win.g_y,win.g_w,win.g_h);
- /* printf("\033H\n\nStart %5d\n",si); */
- }
- }
-
- void get_next_fft()
- {
- Buffptr(&bufptr);
- if((STEREO *)bufptr.recbufptr>(dmadata+DMABUFFSIZE*3/4))
- get_fftdata(DMABUFFSIZE/2);
- else if((STEREO *)bufptr.recbufptr>(dmadata+DMABUFFSIZE/2))
- get_fftdata(DMABUFFSIZE/4);
- else if((STEREO *)bufptr.recbufptr>(dmadata+DMABUFFSIZE/4))
- get_fftdata(0);
- else
- get_fftdata(DMABUFFSIZE*3/4);
- }
-
- void fft_event()
- {
- int ende=FALSE;
- int msg[8];
- int event;
- int mausx,mausy,mausbutton,mausclicks;
- int tastenstatus,taste;
- do
- {
- event=evnt_multi(MU_MESAG | MU_TIMER,0,0,0,0,0,0,0,0,0,0,0,0,0, /* Rechteck 2 */
- msg,100,&mausx,&mausy,&mausbutton,
- &tastenstatus,&taste,&mausclicks);
- if(event & MU_MESAG)
- switch(msg[0])
- {
- case WM_REDRAW:
- fft_redraw(msg[4],msg[5],msg[6],msg[7]);
- break;
- case WM_TOPPED:
- fft_topped(); break;
- case WM_CLOSED:
- fft_closed(); ende=TRUE; break;
- case WM_MOVED:
- fft_moved(msg[4],msg[5],msg[6],msg[7]); break;
- case WM_NEWTOP:
- break;
- case WM_UNTOPPED:
- break;
- case WM_ONTOP:
- break;
- case AP_TERM:
- fft_closed(); ende=TRUE; break;
- default:
- }
- if(event & MU_TIMER)
- get_next_fft();
- if(sigende)
- {
- fft_closed(); ende=TRUE;
- }
- } while(!ende);
- }
-
- int gemstart(void)
- {
- int i;
-
- if(ap_id=appl_init()!=-1)
- {
- for (i=1;i<10;work_in[i++]=1); /* Workstation öffnen */
- work_in[10]=2;
- g_handle=graf_handle(&gl_wchar,&gl_hchar,&gl_wbox,&gl_hbox);
- work_in[0]=g_handle;
- v_opnvwk(work_in,&g_handle,work_out);
- gl_xmax=work_out[0]; gl_ymax=work_out[1];
- if (!g_handle) return(FALSE);
- vq_extnd(g_handle,1,work_out);
- max_polyline_xy=work_out[14];
- return(TRUE);
- }
- return(FALSE);
- }
-
- void get_frequenz(char *str)
- {
- switch(strtol(str,NULL,0))
- {
- case 50: abtastfrequenz=49170; clk=CLK50K; break;
- case 33: abtastfrequenz=33880; clk=CLK33K; break;
- case 25: abtastfrequenz=24585; clk=CLK25K; break;
- case 20: abtastfrequenz=20770; clk=CLK20K; break;
- case 16: abtastfrequenz=16490; clk=CLK16K; break;
- case 12: abtastfrequenz=12292; clk=CLK12K; break;
- case 10: abtastfrequenz=9834; clk=CLK10K; break;
- case 8: abtastfrequenz=8195; clk=CLK8K; break;
- default:
- }
- }
-
- void signalhandler(int sig)
- {
- sigende=TRUE;
- }
-
- int main(int argc, char **argv)
- {
- extern char *optarg;
- extern int optind;
- char c;
-
- abtastfrequenz=49170;
- clk=CLK50K;
- if(argc>1)
- while((c=getopt(argc,argv,"f:b:h:"))!=EOF)
- switch (c)
- {
- case 'f':
- get_frequenz(optarg);
- break;
- case 'b':
- ;
- break;
- case 'h':
- ;
- break;
- default:
- }
- if(gemstart())
- {
- graf_mouse(ARROW,NULL);
- shel_write(9,0,1,NULL,NULL);
- if(max_polyline_xy<N)
- form_alert(1,"[3][Es sind nicht genug Polyline-|elemente möglich. Benutzen|Sie ein anderes VDI.][ Ende ]");
- else
- if(start_soundsystem())
- {
- signal(SIGINT,&signalhandler);
- signal(SIGTERM,&signalhandler);
- if(open_fftwindow(gl_xmax/2-9*gl_wbox,gl_ymax/2-6*gl_hbox,18*gl_wbox,12*gl_hbox))
- fft_event();
- restore_soundsystem();
- }
- }
- v_clsvwk(g_handle);
- appl_exit();
- return(0);
- }