Example #1
0
/*!
 *
 *  \brief Read header from a WAVEfile. Host endianess is handled accordingly.
 *  \wav->fp filepointer of type FILE*.
 *  \wavinfo SWavInfo struct where the decoded header info is stored into.
 *  \return 0 on success and non-zero on failure.
 *
 */
INT WAV_InputOpen (HANDLE_WAV *pWav, const char *filename)
{
    HANDLE_WAV wav = (HANDLE_WAV)FDKcalloc(1, sizeof(struct WAV));
    INT offset;

    if (wav == NULL) {
      FDKprintfErr("WAV_InputOpen(): Unable to allocate WAV struct.\n");
      goto error;
    }

    wav->fp = FDKfopen(filename, "rb");
    if (wav->fp == NULL) {
      FDKprintfErr("WAV_InputOpen(): Unable to open wav file. %s\n", filename);
      goto error;
    }

    /* read RIFF-chunk */
    if (FDKfread(&(wav->header.riffType), 1, 4, wav->fp) != 4) {
      FDKprintfErr("WAV_InputOpen(): couldn't read RIFF_ID\n");
      goto error;  /* bad error "couldn't read RIFF_ID" */
    }
    if (FDKstrncmp("RIFF", wav->header.riffType, 4)) {
      FDKprintfErr("WAV_InputOpen(): RIFF descriptor not found.\n") ;
      goto error;
    }

    /* Read RIFF size. Ignored. */
    FDKfread_EL(&(wav->header.riffSize), 4, 1, wav->fp);

    /* read WAVE-chunk */
    if (FDKfread(&wav->header.waveType, 1, 4, wav->fp) !=4) {
      FDKprintfErr("WAV_InputOpen(): couldn't read format\n");
      goto error;  /* bad error "couldn't read format" */
    }
    if (FDKstrncmp("WAVE", wav->header.waveType, 4)) {
      FDKprintfErr("WAV_InputOpen(): WAVE chunk ID not found.\n") ;
      goto error;
    }

    /* read format-chunk */
    if (FDKfread(&(wav->header.formatType), 1, 4, wav->fp) != 4) {
      FDKprintfErr("WAV_InputOpen(): couldn't read format_ID\n");
      goto error;  /* bad error "couldn't read format_ID" */
    }
    if (FDKstrncmp("fmt", wav->header.formatType, 3)) {
      FDKprintfErr("WAV_InputOpen(): fmt chunk format not found.\n") ;
     goto error;
    }


    FDKfread_EL(&wav->header.formatSize, 4, 1, wav->fp);   /* should be 16 for PCM-format (uncompressed) */


    /* read  info */
    FDKfread_EL(&(wav->header.compressionCode), 2, 1, wav->fp);
    FDKfread_EL(&(wav->header.numChannels), 2, 1, wav->fp);
    FDKfread_EL(&(wav->header.sampleRate), 4, 1, wav->fp);
    FDKfread_EL(&(wav->header.bytesPerSecond), 4, 1, wav->fp);
    FDKfread_EL(&(wav->header.blockAlign), 2, 1, wav->fp);
    FDKfread_EL(&(wav->header.bitsPerSample), 2, 1, wav->fp);

    offset = wav->header.formatSize - 16;

    /* Wave format extensible */
    if (wav->header.compressionCode == 0xFFFE) {
      static const UCHAR guidPCM[16] = {
          0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71
      };
      USHORT extraFormatBytes, validBitsPerSample;
      UCHAR guid[16];
      INT i;

      /* read extra bytes */
      FDKfread_EL(&(extraFormatBytes), 2, 1, wav->fp);
      offset -= 2;

      if (extraFormatBytes >= 22) {
        FDKfread_EL(&(validBitsPerSample), 2, 1, wav->fp);
        FDKfread_EL(&(wav->channelMask), 4, 1, wav->fp);
        FDKfread_EL(&(guid), 16, 1, wav->fp);

        /* check for PCM GUID */
        for (i = 0; i < 16; i++) if (guid[i] != guidPCM[i]) break;
        if (i == 16) wav->header.compressionCode = 0x01;

        offset -= 22;
      }
    }

    /* Skip rest of fmt header if any. */
    for (;offset > 0; offset--) {
      FDKfread(&wav->header.formatSize, 1, 1, wav->fp);
    }

    do {
      /* Read data chunk ID */
      if (FDKfread(wav->header.dataType, 1, 4, wav->fp) != 4) {
        FDKprintfErr("WAV_InputOpen(): Unable to read data chunk ID.\n");
        FDKfree(wav);
        goto error;
      }

      /* Read chunk length. */
      FDKfread_EL(&offset, 4, 1, wav->fp);

      /* Check for data chunk signature. */
      if (FDKstrncmp("data", wav->header.dataType, 4) == 0) {
        wav->header.dataSize = offset;
        break;
      }
      /* Jump over non data chunk. */
      for (;offset > 0; offset--) {
        FDKfread(&(wav->header.dataSize), 1, 1, wav->fp);
      }
    } while (!FDKfeof(wav->fp));

    /* return success */
    *pWav = wav;
    return 0;

    /* Error path */
error:

    if (wav) {
      if (wav->fp) {
        FDKfclose(wav->fp);
        wav->fp = NULL;
      }
      FDKfree(wav);
    }

    *pWav = NULL;

    return -1;
}
int IIS_ProcessCmdlList(const char* param_filename, int (*pFunction)(int, TEXTCHAR**))
{
    /* static to reduce required stack size */

    FDKFILE *config_fp;
    int argc;
    char *line_ptr;

#ifdef CMDFILE_PREFIX
    FDKstrcpy(tmp, CMDFILE_PREFIX);
    FDKstrcpy(tmp+FDKstrlen(CMDFILE_PREFIX), param_filename);
    /* Open the file with command lines */
    config_fp = FDKfopen(tmp, "r");
#else
    /* Open the file with command lines */
    config_fp = FDKfopen(param_filename, "r");
#endif

    if(config_fp == NULL)
    {
#ifdef CMDFILE_PREFIX
        FDKprintf("\ncould not open config file %s", tmp);
#else
        FDKprintf("\ncould not open config file %s", param_filename);
#endif
        return 1;
    }

    /* Obtain a command line from config file */
    while (FDKfgets(line, CMDL_MAX_STRLEN*CMDL_MAX_ARGC, config_fp) != NULL)
    {
        argc = 1;

        /* Eat \n */
        line_ptr =  (char*)FDKstrchr(line, '\n');
        if (line_ptr != NULL)
            *line_ptr = ' ';

        line_ptr = line;

        /* Scan the line and put the command line params into argv */
        do {
            /* Skip consecutive blanks. */
            while (*line_ptr == ' ' && line_ptr < line+CMDL_MAX_STRLEN)
                line_ptr++;
            /* Assign argument. TODO: maybe handle quotes */
            argv_ptr[argc] = line_ptr;
            /* Get pointer to next blank. */
            line_ptr = (char*)FDKstrchr(line_ptr, ' ');
            /*  */
            if (line_ptr != NULL) {
                /* Null terminate */
                *line_ptr = 0;
                /* Skip former blank (now null character) */
                line_ptr++;
                /* Advance argument counter */
            }
            argc++;
        } while ( line_ptr != NULL && argc < CMDL_MAX_ARGC);

        /* call "would be main()" */
        if (argc > 2 && *argv_ptr[1] != '#' && FDKstrlen(argv_ptr[1])>1)
        {
            int retval;

            retval = (*pFunction)(argc, argv_ptr);

            FDKprintf("main returned %d\n", retval);
        }
    }

    FDKfclose(config_fp);
    return 0;
}