home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************************
- * Copyright (C) 1994 Charles P. Peterson *
- * 4007 Enchanted Sun, San Antonio, Texas 78244-1254 *
- * Email: Charles_P_Peterson@fcircus.sat.tx.us *
- * *
- * This is free software with NO WARRANTY. *
- * See gfft.c, or run program itself, for details. *
- * Support is available for a fee. *
- ***************************************************************************
- *
- * Program: gfft--General FFT analysis
- * File: okread.c
- * Purpose: OK! Read the data
- * Author: Charles Peterson (CPP)
- * History: 24-July-1993 CPP; Created.
- * Comment:
- */
-
- #include <stdio.h>
- #include <string.h>
-
- #ifdef _AMIGA
- #ifdef _M68881
- #include <m68881.h>
- #endif
- #endif
-
- #include "gfft.h"
- #include "settings.h"
-
- static unsigned long save_data_entered (void);
-
- static unsigned long save_data_index = 0;
-
- static unsigned long started = FALSE;
-
- static unsigned long current_frame_count = 0;
-
- unsigned long ok_read (float *indata, unsigned long number_samples)
- {
- static unsigned long save_data_length = 0;
- unsigned long i;
- int j;
-
- union {
- unsigned short u;
- signed short s;
- } word;
-
- union {
- unsigned char u;
- signed char s;
- } byte;
- /*
- * If indata pointer is NULL, we're just scanning to find file length
- * Rewind file and return when done
- */
- if (!indata)
- {
- if (OkFrames != ULONG_MAX) /* Already known or specified */
- {
- return OkFrames;
- }
- if (ReadPtr != stdin)
- {
- error_message (SCANNING_FILE); /* This might take some time */
- for (i = 0; i < OkOffset; i++)
- {
- if (!fread (&byte, sizeof byte, 1, ReadPtr))
- {
- return 0L;
- }
- }
- if (InputFormat.bits <= 8)
- {
- for (i = 0; fread (&byte, sizeof byte, 1, ReadPtr); i++);
- }
- else
- {
- for (i = 0; fread (&word, sizeof word, 1, ReadPtr); i++);
- }
- rewind (ReadPtr);
- i /= OkChannels;
- FileFrames = i; /* This value is now known */
- if (i >= StartFrame)
- {
- i -= StartFrame;
- }
- else
- {
- i = 0L;
- }
- return i;
- }
- else
- {
- if (!Save_Data)
- {
- save_data_length = save_data_entered ();
- }
- return save_data_length;
- }
- }
-
- /*
- * An actual file pointer has been provided; read from file
- */
- if (ReadPtr != stdin)
- {
- if (!started)
- {
- /*
- * Skip over start bytes
- */
- for (i = 0L; i < OkOffset; i++)
- {
- if (!fread (&byte, sizeof byte, 1, ReadPtr))
- {
- return 0L;
- }
- }
- /*
- * Skip over start frames
- */
- if (InputFormat.bits <= 8)
- {
- ULONG start_samples = OkChannels * OkStartFrame;
- for (i = 0L; i < start_samples; i++)
- {
- if (!fread (&byte, sizeof byte, 1, ReadPtr))
- {
- return 0L;
- }
- }
- }
- else /* InputFormat.bits > 8 */
- {
- ULONG start_samples = OkChannels * OkStartFrame;
- for (i = 0L; i < start_samples; i++)
- {
- if (!fread (&word, sizeof word, 1, ReadPtr))
- {
- return 0L;
- }
- }
- }
- started = TRUE;
- }
- /*
- * Here is the main read loop
- */
- for (i = 0L; i < number_samples; i++)
- {
- if (++current_frame_count > OkFrames)
- {
- break;
- }
- if (Channel > 1)
- {
- for (j = 1; j < Channel; j++)
- {
- if (InputFormat.bits <= 8)
- {
- if (!fread (&byte, sizeof byte, 1, ReadPtr))
- {
- return i;
- }
- }
- else
- {
- if (!fread (&word, sizeof word, 1, ReadPtr))
- {
- return i;
- }
- }
- } /* end for (j...) */
- } /* end if Channel > 1 */
- if (InputFormat.bits <= 8)
- {
- if (!fread (&byte, sizeof byte, 1, ReadPtr))
- {
- break;
- }
- if (!InputFormat.zero)
- {
- for (j = InputFormat.bits; j < 8; j++)
- {
- byte.s /= 2;
- }
- *indata++ = byte.s;
- }
- else
- {
- for (j = InputFormat.bits; j < 8; j++)
- {
- byte.u /= 2;
- }
- if (byte.u >= InputFormat.zero)
- {
- *indata++ = byte.u - InputFormat.zero;
- }
- else
- {
- *indata = InputFormat.zero - byte.u;
- *indata = (- (*indata));
- indata++;
- }
- }
- }
- else /* Sizes greater than 16 bits not currently supported */
- {
- if (!fread (&word, sizeof word, 1, ReadPtr))
- {
- break;
- }
- if (!InputFormat.zero)
- {
- for (j = InputFormat.bits; j < 16; j++)
- {
- word.s /= 2;
- }
- *indata++ = word.s;
- }
- else
- {
- for (j = InputFormat.bits; j < 16; j++)
- {
- word.u /= 2;
- }
- if (word.u >= InputFormat.zero)
- {
- *indata++ = word.u - InputFormat.zero;
- }
- else
- {
- *indata = InputFormat.zero - word.u;
- *indata = (- (*indata));
- indata++;
- }
- }
- }
- if (Parseval)
- {
- Sample_Sum_Of_Squares += indata[-1] * indata[-1];
- }
- Sample_Frame_Count++;
- if (Channel < OkChannels)
- {
- for (j = Channel; j < OkChannels; j++)
- {
- if (InputFormat.bits <= 8)
- {
- if (!fread (&byte, sizeof byte, 1, ReadPtr))
- {
- return i;
- }
- }
- else
- {
- if (!fread (&word, sizeof word, 1, ReadPtr))
- {
- return i;
- }
- }
- } /* end for j */
- } /* end if Channel... */
- } /* end for current_frame_count */
- } /* end if ReadPtr != stdin */
- else
- /*
- * Interactive input
- * Read data from terminal into buffer
- * then output as demanded
- */
- {
- if (!Save_Data)
- {
- save_data_length = save_data_entered ();
- }
- for (i = 0L; i < number_samples; i++)
- {
- if (i + save_data_index >= save_data_length)
- {
- break;
- }
- *indata++ = Save_Data[i + save_data_index];
- if (Parseval)
- {
- Sample_Sum_Of_Squares += indata[-1] * indata[-1];
- }
- Sample_Frame_Count++;
- }
- save_data_index += i;
- }
- return i;
- }
-
- static unsigned long save_data_entered (void)
- {
- unsigned long allocated_length = 1;
- unsigned long i;
- char inputline[1024];
- float inputf;
-
- Save_Data = gmalloc (allocated_length * sizeof (float),
- NOTHING_SPECIAL);
- for (i = 0L; TRUE; i++)
- {
- printf ("Enter <value> (float OK) or <newline> to end input [%i]: ",i);
- gets (inputline);
- if (strlen (inputline) == 0)
- {
- break;
- }
- if (1 != sscanf (inputline,"%g",&inputf))
- {
- error_message (INVALID_NUMBER);
- i--;
- continue;
- }
- if (i >= allocated_length)
- {
- Save_Data = grealloc (Save_Data, (i+1) *
- sizeof (float), NOTHING_SPECIAL);
- allocated_length = i+1;
- }
- Save_Data[i] = inputf;
- }
- return i;
- }
-
-
- void ok_rewind (void)
- {
- started = FALSE;
- current_frame_count = 0;
- if (ReadPtr != stdin)
- {
- rewind (ReadPtr);
- }
- else
- {
- save_data_index = 0;
- }
- }
-