home *** CD-ROM | disk | FTP | other *** search
/ Audio 4.94 - Over 11,000 Files / audio-11000.iso / msdos / sndbords / proaudio / freq3 / procinp.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  8KB  |  422 lines

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <ctype.h>
  4. #include <graphics.h>
  5. #include <math.h>
  6. #include "freq.h"
  7.  
  8. double amp;
  9. double db;
  10. double phase;
  11.  
  12. extern int *BitReversed;
  13. extern int x[];
  14. extern int lasty[];
  15. extern int *fftdata;
  16.  
  17. extern int logamp;
  18. extern int logfreq;
  19. extern int fftlen;
  20. extern long SampleRate;
  21. extern int windfunc;
  22. extern int logs;
  23. extern int logb;
  24. extern int ys;
  25.  
  26. extern int gain3db;
  27. extern int deriv;
  28. extern int gain;
  29. extern long ref_freq;
  30.  
  31. void cleanup_vga(void)
  32. {
  33.    /*
  34.     *  Clean up VGA registers so Borland libraries work again.
  35.     */
  36.    outportb(0x3c4,2);
  37.    outportb(0x3c5,0x0f);   // Use all bit planes
  38. }
  39.  
  40. void setup_vga(void)
  41. {
  42.    /*
  43.     *  Set up VGA registers for graphics display
  44.     */
  45.    outportb(0x3ce,1);
  46.    outportb(0x3cf,0);   // Disable set/reset for all planes
  47.    outportb(0x3ce,3);
  48.    outportb(0x3cf,0);   // No rotate, just write data
  49.    outportb(0x3ce,4);
  50.    outportb(0x3cf,0);   // Read from bit plane 0 (blue)
  51.    outportb(0x3ce,5);
  52.    outportb(0x3cf,inportb(0x3cf)&0x70); // Read and write direct to memory
  53.    outportb(0x3ce,8);
  54.    outportb(0x3cf,0xff);  // Allow modification of all bits
  55.    outportb(0x3c4,2);
  56.    outportb(0x3c5,1);   // Use bit plane 0
  57. }
  58.  
  59. void update_display(int bin)
  60. {
  61.    char ach[100];
  62.  
  63.    settextjustify(LEFT_TEXT,TOP_TEXT);
  64.    setcolor(TEXT_COLOR);
  65.    setfillstyle(SOLID_FILL,0);
  66.    bar(0,22,160,70);
  67.    sprintf(ach," Freq: %7.5lg Hz ",(double)bin*SampleRate/fftlen);
  68.    outtextxy(0,22,ach);
  69.    sprintf(ach,"  Amp: %7.5lg   ",amp);
  70.    outtextxy(0,34,ach);
  71.    sprintf(ach,"       %7.5lg db ",db);
  72.    outtextxy(0,46,ach);
  73.    sprintf(ach,"Phase: %7.4lg deg. ",phase);
  74.    outtextxy(0,58,ach);
  75. }
  76.  
  77.  
  78. void highlight(int bin)
  79. {
  80.    int i,j;
  81.    int bri=BitReversed[bin];
  82.    double re=fftdata[bri];
  83.    double im=fftdata[bri+1];
  84.  
  85.    amp=sqrt(re*re+im*im)/32768.0;
  86.  
  87.    if(gain3db)
  88.       amp*=sqrt((double)bin*(double)SampleRate/fftlen/(double)ref_freq);
  89.  
  90.    if(deriv==1)
  91.       amp*=(double)SampleRate/(2*M_PI*(double)ref_freq);
  92.  
  93.    if(deriv==2)
  94.       amp*= (double)SampleRate/(2*M_PI*(double)ref_freq)
  95.            *(double)SampleRate/(2*M_PI*(double)ref_freq);
  96.  
  97.    if(amp!=0)
  98.    {
  99.       phase=atan2(im,re)*2*M_PI;
  100.       db=20*log10(amp);
  101.    }
  102.    else
  103.    {
  104.       phase=0;
  105.       db=-100;
  106.    }
  107.  
  108.    for(i=0;(x[i]<bin) && i<(WINDOW_RIGHT-WINDOW_LEFT);i++);
  109.    for(j=i+1;(x[j]==-1) && j<(WINDOW_RIGHT-WINDOW_LEFT);j++);
  110.  
  111.    if(i==0) j=0;
  112.    i=(i+j)/2;
  113.  
  114.    setcolor(DARK_HIGHLIGHT);
  115.    moveto(WINDOW_LEFT+i,WINDOW_TOP);
  116.    lineto(WINDOW_LEFT+i,lasty[i]);
  117.    setcolor(LIGHT_HIGHLIGHT);
  118.    lineto(WINDOW_LEFT+i,WINDOW_BOTTOM);
  119. }
  120.  
  121.  
  122. void dehighlight(int bin)
  123. {
  124.    int i,j;
  125.  
  126.    for(i=0;(x[i]<bin) && i<(WINDOW_RIGHT-WINDOW_LEFT);i++);
  127.    for(j=i+1;(x[j]==-1) && j<(WINDOW_RIGHT-WINDOW_LEFT);j++);
  128.  
  129.    if(i==0) j=0;
  130.    i=(i+j)/2;
  131.  
  132.    setcolor(BLACK);
  133.    moveto(WINDOW_LEFT+i,WINDOW_TOP);
  134.    lineto(WINDOW_LEFT+i,lasty[i]);
  135.    setcolor(GRAPH_COLOR);
  136.    lineto(WINDOW_LEFT+i,WINDOW_BOTTOM);
  137. }
  138.  
  139.  
  140. int process_input(void)
  141. {
  142.    int c;
  143.    int bin;
  144.  
  145.    cleanup_vga();
  146.  
  147.    c=toupper(getch());
  148.  
  149.    if(c==0) c=0x100+getch();
  150.  
  151.    if(c==' ')
  152.    {
  153.       bin=logfreq;
  154.  
  155.       highlight(bin);
  156.  
  157.       setcolor(TEXT_COLOR);
  158.       settextjustify(CENTER_TEXT,TOP_TEXT);
  159.       settextstyle(0,0,0);
  160.       outtextxy(320,86,"Use arrows to move, Space to continue.");
  161.  
  162.       c=0;
  163.       while(c!=' ' && c!='Q' && c!='X' && c!='E' && c!=0x1B)
  164.       {
  165.          update_display(bin);
  166.  
  167.          c=toupper(getch());
  168.          if(c==0)
  169.             c=getch()+256;
  170.  
  171.          if(c==LEFT)
  172.          {
  173.             if(bin>logfreq)
  174.             {
  175.                dehighlight(bin);
  176.                bin--;
  177.                highlight(bin);
  178.             }
  179.          }
  180.          else if(c==RIGHT)
  181.          {
  182.             if(bin<fftlen/2-1)
  183.             {
  184.                dehighlight(bin);
  185.                bin++;
  186.                highlight(bin);
  187.             }
  188.          }
  189.          else if(c==CTL_LEFT)
  190.          {
  191.             dehighlight(bin);
  192.             bin-=10;
  193.             if(bin<logfreq)
  194.                bin=logfreq;
  195.             highlight(bin);
  196.          }
  197.          else if(c==CTL_RIGHT)
  198.          {
  199.             dehighlight(bin);
  200.             bin+=10;
  201.             if(bin>=fftlen/2)
  202.                bin=fftlen/2-1;
  203.             highlight(bin);
  204.          }
  205.       }
  206.       dehighlight(bin);
  207.  
  208.       setfillstyle(SOLID_FILL,0);
  209.       bar(0,22,160,70);
  210.       bar(160,86,480,96);
  211.    }
  212.    else if(c==UP)
  213.    {
  214.       if(logamp)
  215.       {
  216.          logs++;
  217.          logb++;
  218.          if(logb>10)
  219.          {
  220.             logb--;
  221.             logs--;
  222.          }
  223.          setup_logscales();
  224.       }
  225.       else
  226.       {
  227.          ys--;
  228.          if(ys<1) ys=1;
  229.          setup_linscales();
  230.       }
  231.  
  232.       amplitude_scale();
  233.    }
  234.    else if(c==DOWN)
  235.    {
  236.       if(logamp)
  237.       {
  238.          logs--;
  239.          logb--;
  240.          if(logs<0)
  241.          {
  242.             logb++;
  243.             logs++;
  244.          }
  245.          setup_logscales();
  246.       }
  247.       else
  248.       {
  249.          ys++;
  250.          if(ys>20) ys=20;
  251.          setup_linscales();
  252.       }
  253.  
  254.       amplitude_scale();
  255.    }
  256.    else if(c==LEFT)
  257.    {
  258.       if(logamp)
  259.       {
  260.          logb++;
  261.          if(logb>10)
  262.          {
  263.             logb--;
  264.             logs--;
  265.             if(logs<0) logs++;
  266.          }
  267.          setup_logscales();
  268.       }
  269.       else
  270.       {
  271.          ys++;
  272.          if(ys>20) ys=20;
  273.          setup_linscales();
  274.       }
  275.  
  276.       amplitude_scale();
  277.    }
  278.    else if(c==RIGHT)
  279.    {
  280.       if(logamp)
  281.       {
  282.          logb--;
  283.          if(logb<logs+3) logb++;
  284.          setup_logscales();
  285.       }
  286.       else
  287.       {
  288.          ys--;
  289.          if(ys<1) ys=1;
  290.          setup_linscales();
  291.       }
  292.  
  293.       amplitude_scale();
  294.    }
  295.    else if(c=='X')
  296.    {
  297.       /*
  298.        *  Toggle between linear and logarithmic frequency scale
  299.        */
  300.       logfreq=!logfreq;
  301.  
  302.       setup_xscale();
  303.       if(logamp)
  304.          setup_logscales();
  305.       else
  306.          setup_linscales();
  307.  
  308.       frequency_scale();
  309.    }
  310.    else if(c=='Y')
  311.    {
  312.       /*
  313.        *  Toggle between linear and logarithmic amplitude scale
  314.        */
  315.       logamp=!logamp;
  316.  
  317.       if(logamp)
  318.          setup_logscales();
  319.       else
  320.          setup_linscales();
  321.  
  322.       amplitude_scale();
  323.    }
  324.    else if(c=='W')
  325.    {
  326.       /*
  327.        *  Change the windowing function
  328.        */
  329.       windfunc++;
  330.       if(windfunc>6) windfunc=0;
  331.       compute_window_function();
  332.  
  333.       update_header();
  334.    }
  335.    else if(c=='0')
  336.    {
  337.       /*
  338.        *  0 dB/octave gain
  339.        */
  340.       gain=0;
  341.       gain3db=0;
  342.       deriv=0;
  343.  
  344.       if(logamp)
  345.          setup_logscales();
  346.       else
  347.          setup_linscales();
  348.  
  349.       update_header();
  350.    }
  351.    else if(c=='3')
  352.    {
  353.       /*
  354.        *  3 dB/octave gain
  355.        */
  356.       gain=3;
  357.       gain3db=1;
  358.       deriv=0;
  359.  
  360.       if(logamp)
  361.          setup_logscales();
  362.       else
  363.          setup_linscales();
  364.  
  365.       update_header();
  366.    }
  367.    else if(c=='6')
  368.    {
  369.       /*
  370.        *  6 dB/octave gain
  371.        */
  372.       gain=6;
  373.       gain3db=0;
  374.       deriv=1;
  375.  
  376.       if(logamp)
  377.          setup_logscales();
  378.       else
  379.          setup_linscales();
  380.  
  381.       update_header();
  382.    }
  383.    else if(c=='9')
  384.    {
  385.       /*
  386.        *  9 dB/octave gain
  387.        */
  388.       gain=9;
  389.       gain3db=1;
  390.       deriv=1;
  391.  
  392.       if(logamp)
  393.          setup_logscales();
  394.       else
  395.          setup_linscales();
  396.  
  397.       update_header();
  398.    }
  399.    else if(c=='1')
  400.    {
  401.       /*
  402.        *  12 dB/octave gain
  403.        */
  404.       gain=12;
  405.       gain3db=0;
  406.       deriv=2;
  407.  
  408.       if(logamp)
  409.          setup_logscales();
  410.       else
  411.          setup_linscales();
  412.  
  413.       update_header();
  414.    }
  415.  
  416.    setup_vga();
  417.  
  418.    if(c=='Q' || c=='E' || c==0x1B)
  419.       return(0);
  420.    else
  421.       return(1);
  422. }