home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / misc / sci / gfft / source / okread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-25  |  6.8 KB  |  346 lines

  1. /***************************************************************************
  2.  *          Copyright (C) 1994  Charles P. Peterson                  *
  3.  *         4007 Enchanted Sun, San Antonio, Texas 78244-1254             *
  4.  *              Email: Charles_P_Peterson@fcircus.sat.tx.us                *
  5.  *                                                                         *
  6.  *          This is free software with NO WARRANTY.                  *
  7.  *          See gfft.c, or run program itself, for details.              *
  8.  *              Support is available for a fee.                      *
  9.  ***************************************************************************
  10.  *
  11.  * Program:     gfft--General FFT analysis
  12.  * File:        okread.c
  13.  * Purpose:     OK! Read the data
  14.  * Author:      Charles Peterson (CPP)
  15.  * History:     24-July-1993 CPP; Created.
  16.  * Comment:
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. #ifdef _AMIGA
  23. #ifdef _M68881
  24. #include <m68881.h>
  25. #endif
  26. #endif
  27.  
  28. #include "gfft.h"
  29. #include "settings.h"
  30.  
  31. static unsigned long save_data_entered (void);
  32.  
  33. static unsigned long save_data_index = 0;
  34.  
  35. static unsigned long started = FALSE;
  36.  
  37. static unsigned long current_frame_count = 0;
  38.  
  39. unsigned long ok_read (float *indata, unsigned long number_samples)
  40. {
  41.     static unsigned long save_data_length = 0;
  42.     unsigned long i;
  43.     int j;
  44.  
  45.     union {
  46.     unsigned short u;
  47.     signed short s;
  48.     } word;
  49.  
  50.     union {
  51.     unsigned char u;
  52.     signed char s;
  53.     } byte;
  54. /*
  55.  * If indata pointer is NULL, we're just scanning to find file length
  56.  * Rewind file and return when done
  57.  */
  58.     if (!indata)
  59.     {
  60.     if (OkFrames != ULONG_MAX)  /* Already known or specified */
  61.     {
  62.         return OkFrames;
  63.     }
  64.     if (ReadPtr != stdin)
  65.     {
  66.         error_message (SCANNING_FILE);  /* This might take some time */
  67.         for (i = 0; i < OkOffset; i++)
  68.         {
  69.         if (!fread (&byte, sizeof byte, 1, ReadPtr))
  70.         {
  71.             return 0L;
  72.         }
  73.         }
  74.         if (InputFormat.bits <= 8)
  75.         {
  76.         for (i = 0; fread (&byte, sizeof byte, 1, ReadPtr); i++);
  77.         }
  78.         else
  79.         {
  80.         for (i = 0; fread (&word, sizeof word, 1, ReadPtr); i++);
  81.         }
  82.         rewind (ReadPtr);
  83.         i /= OkChannels;
  84.         FileFrames = i;  /* This value is now known */
  85.         if (i >= StartFrame)
  86.         {
  87.         i -= StartFrame;
  88.         }
  89.         else
  90.         {
  91.         i = 0L;
  92.         }
  93.         return i;
  94.     }
  95.     else
  96.     {
  97.         if (!Save_Data)
  98.         {
  99.         save_data_length = save_data_entered ();
  100.         }
  101.         return save_data_length;
  102.     }
  103.     }
  104.  
  105. /*
  106.  * An actual file pointer has been provided; read from file
  107.  */
  108.     if (ReadPtr != stdin)
  109.     {
  110.     if (!started)
  111.     {
  112.     /*
  113.      * Skip over start bytes
  114.      */
  115.         for (i = 0L; i < OkOffset; i++)
  116.         {
  117.         if (!fread (&byte, sizeof byte, 1, ReadPtr))
  118.         {
  119.             return 0L;
  120.         }
  121.         }
  122.         /*
  123.          * Skip over start frames
  124.      */
  125.         if (InputFormat.bits <= 8)
  126.         {
  127.         ULONG start_samples = OkChannels * OkStartFrame;
  128.         for (i = 0L; i < start_samples; i++)
  129.         {
  130.             if (!fread (&byte, sizeof byte, 1, ReadPtr))
  131.             {
  132.             return 0L;
  133.             }
  134.         }
  135.         }
  136.         else  /* InputFormat.bits > 8 */
  137.         {
  138.         ULONG start_samples = OkChannels * OkStartFrame;
  139.         for (i = 0L; i < start_samples; i++)
  140.         {
  141.             if (!fread (&word, sizeof word, 1, ReadPtr))
  142.             {
  143.             return 0L;
  144.             }
  145.         }
  146.         }
  147.         started = TRUE;
  148.     }
  149.     /*
  150.      * Here is the main read loop
  151.      */
  152.     for (i = 0L; i < number_samples; i++)
  153.     {
  154.         if (++current_frame_count > OkFrames)
  155.         {
  156.         break;
  157.         }
  158.         if (Channel > 1)
  159.         {
  160.         for (j = 1; j < Channel; j++)
  161.         {
  162.             if (InputFormat.bits <= 8)
  163.             {
  164.             if (!fread (&byte, sizeof byte, 1, ReadPtr))
  165.             {
  166.                 return i;
  167.             }
  168.             }
  169.             else
  170.             {
  171.             if (!fread (&word, sizeof word, 1, ReadPtr))
  172.             {
  173.                 return i;
  174.             }
  175.             }
  176.         }                          /* end for (j...) */
  177.         }                              /* end if Channel > 1 */
  178.         if (InputFormat.bits <= 8)
  179.         {
  180.         if (!fread (&byte, sizeof byte, 1, ReadPtr))
  181.         {
  182.             break;
  183.         }
  184.         if (!InputFormat.zero)
  185.         {
  186.             for (j = InputFormat.bits; j < 8; j++)
  187.             {
  188.             byte.s /= 2;
  189.             }
  190.             *indata++ = byte.s;
  191.         }
  192.         else
  193.         {
  194.             for (j = InputFormat.bits; j < 8; j++)
  195.             {
  196.             byte.u /= 2;
  197.             }
  198.             if (byte.u >= InputFormat.zero)
  199.             {
  200.             *indata++ = byte.u - InputFormat.zero;
  201.             }
  202.             else
  203.             {
  204.             *indata = InputFormat.zero - byte.u;
  205.             *indata = (- (*indata));
  206.             indata++;
  207.             }
  208.         }
  209.         }
  210.         else /* Sizes greater than 16 bits not currently supported */
  211.         {
  212.         if (!fread (&word, sizeof word, 1, ReadPtr))
  213.         {
  214.             break;
  215.         }
  216.         if (!InputFormat.zero)
  217.         {
  218.             for (j = InputFormat.bits; j < 16; j++)
  219.             {
  220.             word.s /= 2;
  221.             }
  222.             *indata++ = word.s;
  223.         }
  224.         else
  225.         {
  226.             for (j = InputFormat.bits; j < 16; j++)
  227.             {
  228.             word.u /= 2;
  229.             }
  230.             if (word.u >= InputFormat.zero)
  231.             {
  232.             *indata++ = word.u - InputFormat.zero;
  233.             }
  234.             else
  235.             {
  236.             *indata = InputFormat.zero - word.u;
  237.             *indata = (- (*indata));
  238.             indata++;
  239.             }
  240.         }
  241.         }
  242.         if (Parseval)
  243.         {
  244.         Sample_Sum_Of_Squares += indata[-1] * indata[-1];
  245.         }
  246.         Sample_Frame_Count++;
  247.         if (Channel < OkChannels)
  248.         {
  249.         for (j = Channel; j < OkChannels; j++)
  250.         {
  251.             if (InputFormat.bits <= 8)
  252.             {
  253.             if (!fread (&byte, sizeof byte, 1, ReadPtr))
  254.             {
  255.                 return i;
  256.             }
  257.             }
  258.             else
  259.             {
  260.             if (!fread (&word, sizeof word, 1, ReadPtr))
  261.             {
  262.                 return i;
  263.             }
  264.             }
  265.         }                          /* end for j */
  266.         }                              /* end if Channel... */
  267.     }                                  /* end for current_frame_count */
  268.     }                                      /* end if ReadPtr != stdin */
  269.     else
  270. /*
  271.  * Interactive input
  272.  * Read data from terminal into buffer
  273.  * then output as demanded
  274.  */
  275.     {
  276.     if (!Save_Data)
  277.     {
  278.         save_data_length = save_data_entered ();
  279.     }
  280.     for (i = 0L; i < number_samples; i++)
  281.     {
  282.         if (i + save_data_index >= save_data_length) 
  283.         {
  284.         break;
  285.         }
  286.         *indata++ = Save_Data[i + save_data_index];
  287.         if (Parseval)
  288.         {
  289.         Sample_Sum_Of_Squares += indata[-1] * indata[-1];
  290.         }
  291.         Sample_Frame_Count++;
  292.     }
  293.     save_data_index += i;
  294.     }
  295.     return i;
  296. }
  297.     
  298. static unsigned long save_data_entered (void)
  299. {
  300.     unsigned long allocated_length = 1;
  301.     unsigned long i;
  302.     char inputline[1024];
  303.     float inputf;
  304.  
  305.     Save_Data = gmalloc (allocated_length * sizeof (float), 
  306.              NOTHING_SPECIAL);
  307.     for (i = 0L; TRUE; i++)
  308.     {
  309.     printf ("Enter <value> (float OK) or <newline> to end input [%i]: ",i);
  310.     gets (inputline);
  311.     if (strlen (inputline) == 0) 
  312.     {
  313.         break;
  314.     }
  315.     if (1 != sscanf (inputline,"%g",&inputf))
  316.     {
  317.         error_message (INVALID_NUMBER);
  318.         i--;
  319.         continue;
  320.     }
  321.     if (i >= allocated_length)
  322.     {
  323.         Save_Data = grealloc (Save_Data, (i+1) * 
  324.                   sizeof (float), NOTHING_SPECIAL);
  325.         allocated_length = i+1;
  326.     }
  327.     Save_Data[i] = inputf;
  328.     }
  329.     return i;
  330. }
  331.  
  332.  
  333. void ok_rewind (void)
  334. {
  335.     started = FALSE;
  336.     current_frame_count = 0;
  337.     if (ReadPtr != stdin)
  338.     {
  339.     rewind (ReadPtr);
  340.     }
  341.     else
  342.     {
  343.     save_data_index = 0;
  344.     }
  345. }
  346.