GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Nov-2007, 02:52
mahendra mahendra is offline
New Member
 
Join Date: Nov 2007
Posts: 14
mahendra is an unknown quantity at this point

Error: Broken Pipe


hai,
i got one problem during fread on wave file.. i found the error message as follows "Broken Pipe". If any one know the reason for that error and how to recover from that error.. then please answer me
  #2  
Old 06-Nov-2007, 07:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: hai any one help me...


Quote:
Originally Posted by mahendra
...problem during fread on wave...

Show us some code. Tell us about the .wav file.

Did you print the values you read from the headers in the .wav file? Are the values consistent with the properties of the file you are reading?

Regards,

Dave
  #3  
Old 06-Nov-2007, 08:51
davis
 
Posts: n/a

Re: hai any one help me...


Quote:
Originally Posted by mahendra
hai,
i got one problem during fread on wave file.. i found the error message as follows "Broken Pipe". If any one know the reason for that error and how to recover from that error.. then please answer me

My experience suggests that you've tried to read past the end of the file.


:davis:
  #4  
Old 06-Nov-2007, 22:32
mahendra mahendra is offline
New Member
 
Join Date: Nov 2007
Posts: 14
mahendra is an unknown quantity at this point

Re: hai any one help me...


Quote:
Originally Posted by davekw7x
Show us some code. Tell us about the .wav file.

Did you print the values you read from the headers in the .wav file? Are the values consistent with the properties of the file you are reading?

Regards,

Dave


hai..
i am attaching TEXT file that has code and comments and i mentioned where the problem i got and what the error message i found.
Actually i used following code in conversion of PCM format data TRUESPEECH.
But i got error while reading PCM format file..
Please go through the code and please help me.. i am awaiting for your reply.

Regards
Mahendra
Attached Files
File Type: txt Code.txt (1.7 KB, 14 views)
  #5  
Old 06-Nov-2007, 23:45
mahendra mahendra is offline
New Member
 
Join Date: Nov 2007
Posts: 14
mahendra is an unknown quantity at this point

Re: hai any one help me...


hai..
I just read the wave file header (this is just to place file pointer after header 58/90 bytes).. i can replace fread with fseek.. then also no problem.
I read the header data in to msg variable(see it in code).. i didnot used any where that header.

after reading from 58/90 bytes that is data containing in file it is almost binary data.. here i am getting the error..... some cases it working fine
some other cases it's not done proper reading.. i think my logic is good but it is the error thrown by the OS... what are the possible reasons?
is my code help u while tracing the problem?

CPP / C++ / C Code:
// i am using this code for compressing wave data from PCM to TRUESPEECH
//it reads 240 bytes of data excluding header and it compresses 240 bytes to 32 bytes and writes it into output file
//here i am reading the header
int inbytes = 0;
if (m_nConversionType)//If WAV file is PCM format header size 58 bytes
  inbytes = fread(msg, 1, 58, i_file);

else///If WAV file is TRUESPEECH format header size 90 bytes
  inbytes = fread(msg, 1, 90, i_file);
//I just read the header to place file pointer after header i didn't use this any where
//srcsize=240
while (1)
{
  memset(header.pbSrc, 0, srcsize);
  memset(header.pbDst, 0, destsize);
  inbytes = fread(header.pbSrc, 1, srcsize, i_file);

    if (inbytes != srcsize)
    {
      if (feof(i_file))
      {
       	AfxMessageBox("EOF");
        break;
      }
      AfxMessageBox(strerror(ferror(i_file)));///here it showes broken pipe error
//It reads 240*16+198 bytes it failed to read remaining data ERROR was "Broken pipe"
      if(ferror(i_file))
      {
	clearerr(i_file);//no use by this statement (error cleared here), but again it failed to read data
      }
      free(header.pbSrc);
      free(header.pbDst);
      free(ts_wf);
      fclose(i_file);
      fclose(o_file))
      return -1;
   }

//compression methods used here to compress data of 240(PCM) to 32(TRUESPEECH)
   if (fwrite(header.pbDst, 1, header.cbDstLengthUsed, o_file) !=
        header.cbDstLengthUsed)
    {
       AfxMessageBox("error writing file\n");
       free(header.pbSrc);
       free(header.pbDst);
       free(ts_wf);
       fclose(i_file);
       fclose(o_file))
	
      return 0;
    }      
  }

// this problem get solved by closing the file and again called same on that file
//But i need it to work on first time.. will you help me
Last edited by admin : 07-Nov-2007 at 05:02. Reason: Please insert your C/C++ example codes between [CPP] and [/CPP] tags
  #6  
Old 07-Nov-2007, 08:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: hai any one help me...


Quote:
Originally Posted by mahendra
I just read the wave file header (this is just to place file pointer after header 58/90 bytes)
Huh? There are a total of 44 bytes before the first sample data byte in a PCM .wav file
Quote:
Originally Posted by mahendra
i didnot used any where that header.
Then how can your program know the file parameters (mono/stereo mode, bits-per-sample, compression algorithm, etc.)?
Quote:
Originally Posted by mahendra
...here i am getting the error..... some cases it working fine some other cases it's not done proper reading..
Did you open the file in binary mode?

Regards,

Dave
  #7  
Old 07-Nov-2007, 21:19
mahendra mahendra is offline
New Member
 
Join Date: Nov 2007
Posts: 14
mahendra is an unknown quantity at this point

Re: hai any one help me...


hai..
yaa i am openeing the file in binary mode and my application uses two versions of files PCM (MONO, 8khz, 8bit) this takes 58 bytes header size...
and another one is TRUESPEECh (MONO, 8khz, 1bit) this takes 90 bytes header... so i provide these two file formats to my application then it will manage
  #8  
Old 08-Nov-2007, 07:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: hai any one help me...


Quote:
Originally Posted by mahendra
hai..
yaa i am openeing the file in binary mode and my application uses two versions of files PCM (MONO, 8khz, 8bit) this takes 58 bytes header size...
and another one is TRUESPEECh (MONO, 8khz, 1bit) this takes 90 bytes header... so i provide these two file formats to my application then it will manage
Here is a test program that I use for evaluating PCM .wav files

Program file:
CPP / C++ / C Code:
/* 
 * readwave.c
 * Use file formats from wavefmt.h to access PCM .wav file parameters
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "wavefmt.h"

void getinput(FILE *);

int main(int argc, char **argv)
{
    FILE *infile;

    uint32_t number_of_samples;
    int i;
    int fileno;

    ChunkDescriptorHdr   c_hdr;
    FormatSubChunkHdr    f_hdr;
    DataSubChunkHdr      d_hdr;
    
    printf("Size of Chunk  Descriptor Header = %2d\n", sizeof(c_hdr));
    printf("Size of Format SubChunk   Header = %2d\n", sizeof(f_hdr));
    printf("Size of Data   SubChunk   Header = %2d\n", sizeof(d_hdr));
    if ((sizeof(c_hdr) + sizeof(f_hdr) + sizeof(d_hdr)) != 44) {
        printf("Header sizes must add up to 44. Otherwise it can't work.\n");
        exit(EXIT_FAILURE);
    }
    printf("Number of bytes before samples   = %2d\n\n", 
            sizeof(c_hdr)+sizeof(f_hdr)+sizeof(d_hdr));
    if (argc < 2) {
        printf("Usage: %s <wavefilename>\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    for (fileno = 1; fileno < argc; fileno++) {
        if ((infile = fopen(argv[fileno], "rb")) == NULL) {
            printf("Can't open file %s for reading\n", argv[fileno]);
            exit(EXIT_FAILURE);
        }
        printf("Opened %s for reading\n", argv[fileno]);

        /*  Read and analyze Chunk Descriptor Header */
        if (fread(&c_hdr, sizeof(c_hdr), 1, infile) != 1) {
            printf("  Error reading Chunk Descriptor Header\n");
            exit(EXIT_FAILURE);
        }
        printf("  ID   from Chunk Descriptor Header:  <");
        for (i = 0; i < 4; i++) {
            if (isprint(c_hdr.ID[i])) {
                printf("%c", c_hdr.ID[i]);
            }
            else {
                printf(" 0x%02x ", c_hdr.ID[i]);
            }

        }
        printf(">\n");

        if (strncmp(c_hdr.ID, "RIFF", 4) != 0) {
            printf("  ID is not <RIFF>\n");
            exit(EXIT_FAILURE);
        }
        printf("  Size from Chunk Descriptor Header = %d\n\n", c_hdr.Size);

        if (strncmp(c_hdr.Format, "WAVE", 4) != 0) {
            printf("  Format is not <WAVE>\n");
            exit(EXIT_FAILURE);
        }

        /* Read and analyze FormatSubChunkHdr */
        fread(&f_hdr, sizeof(f_hdr), 1, infile);
        printf("  ID from Format SubChunk Headr:      <");
        for (i = 0; i < 4; i++) {

            if (isprint(f_hdr.ID[i])) {
                printf("%c", f_hdr.ID[i]);
            }
            else {
                printf(" 0x%02x ", f_hdr.ID[i]);
            }
        }
        printf(">\n");
        if (strncmp(f_hdr.ID, "fmt ", 4) != 0) {
            printf("  ID is not <fmt >\n");
            exit(EXIT_FAILURE);
        }

        printf("  Size = %d\n", f_hdr.Size);
        if (f_hdr.Size != 16) {
            printf("  Size is not 16\n");
            exit(EXIT_FAILURE);
        }
        printf("  Audio Format        = %u\n", f_hdr.AudioFormat);
        if (f_hdr.AudioFormat != 1) {
            printf("  AudioFormat must be 1\n");
            exit(EXIT_FAILURE);
        }

        printf("  Number of channels  = %u\n", f_hdr.NumChannels);
        printf("  Sample rate         = %u\n", f_hdr.SampleRate);
        printf("  Byte rate           = %u\n", f_hdr.ByteRate);
        printf("  Block Align         = %u\n", f_hdr.BlockAlign);
        printf("  Bits per sample     = %u\n", f_hdr.BitsPerSample);
        printf("\n");

    /* check channel number and block size are good */
        if (f_hdr.NumChannels > 4 || f_hdr.BlockAlign > 8) {
            if (f_hdr.NumChannels > 4) {
                printf("  Number of channels must be less than or equal to 4\n");
            }
            if (f_hdr.BlockAlign > 8) {
                printf("  Block align must be less than or equal to 8\n");
            }
            exit(EXIT_FAILURE);
        }

        fread(&d_hdr, sizeof(d_hdr), 1, infile);
        printf("  ID from Data Subchunk Header:       <");
        for (i = 0; i < 4; i++) {
            if (isprint(d_hdr.ID[i])) {
                printf("%c", d_hdr.ID[i]);
            }
            else {
                printf(" 0x%02x ", d_hdr.ID[i]);
            }
        }
        printf(">\n");
        printf("  Data Size           = %d\n", d_hdr.Size);

        number_of_samples = d_hdr.Size / f_hdr.BlockAlign;
        printf("  Samples per Channel = %u\n\n", number_of_samples);
        printf("  Now the file pointer is ready to read the data.\n");
        /* Here's where you process the samples */
        fclose(infile);
        printf("  Closed %s\n", argv[fileno]);
        printf("\n\n");
    }
    printf("\n");
    printf("Number of files processed = %d\n", fileno-1);

    return 0;
}

Here's the header
CPP / C++ / C Code:
#ifndef WAVEFMT_H__
#define WAVEFMT_H__
/* typedefs for PCM .wav file headers */
/* 
 * Ref: [url]http://msdn2.microsoft.com/en-us/library/aa446573.aspx[/url]
 * Ref: [url]http://ccrma.stanford.edu/courses/422/projects/WaveFormat/[/url]
 *
 * All PCM .wav files have the following three headers
 *
 * There may be additional Data Subchunks.
 * If so, then each has its own DataSubChunkHdr.
 *
 * For RIFF .wav files, multi-byte integer data are stored in
 * little-endian format. (So if your architecture is little-endian
 * everything falls into place by reading the headers a block
 * at a time.)
 *
 * Data samples are always supposed to fall on even-byte boundaries.
 *
 */

#ifdef __GNUC__
#include <stdint.h>
#else
typedef unsigned int uint32_t;
typedef int int32_t;
typedef short int16_t;
typedef unsigned short uint16_t;
#endif

/* Waveform Chunk descriptor size is 12 */
typedef struct {
    char     ID[4];         /* 'R', 'I', 'F', 'F'                         */
    int32_t  Size;          /* file size - 8                              */
    char     Format[4];     /* 'W', A', V', E'                            */
} ChunkDescriptorHdr;

/* PCM Format SubChunk header size is 24 */
typedef struct {
    char     ID[4];         /* 'f', 'm', 't', ' '                         */
    int32_t Size;           /* 16 for PCM                                 */
    uint16_t AudioFormat;   /* 1 for PCM                                  */
    uint16_t NumChannels;   /* mono = 1; stereo = 2                       */
    uint32_t SampleRate;    /* 8000, 41000, etc.                          */
    uint32_t ByteRate;      /* SampleRate * NumChannels *BitsPerSample/8  */
    uint16_t BlockAlign;    /* NumChannels * BitsPerSample/8              */
    uint16_t BitsPerSample; /* 8, 16, etc.                                */
} FormatSubChunkHdr;

/* Data SubChunk header size is 8 (not counting the actual data bytes)    */
typedef struct {
    char     ID[4];         /* 'd', 'a', 't', 'a'    */
    int32_t Size;           /* NumSamples * NumChannels * BitsPerSample/8 */
    /* The data samples follow */
} DataSubChunkHdr;


/* flags for AudioFormat field of FormatSubChunkHdr */
#define WAVE_FORMAT_PCM     1

#endif
I have attached a small .wav file (8000 8-bit samples per second, monaural)

Output
Code:
Size of Chunk Descriptor Header = 12 Size of Format SubChunk Header = 24 Size of Data SubChunk Header = 8 Number of bytes before samples = 44 Opened song.wav for reading ID from Chunk Descriptor Header: <RIFF> Size from Chunk Descriptor Header = 145473 ID from Format SubChunk Headr: <fmt > Size = 16 Audio Format = 1 Number of channels = 1 Sample rate = 8000 Byte rate = 8000 Block Align = 1 Bits per sample = 8 ID from Data Subchunk Header: <data> Data Size = 145437 Samples per Channel = 145437 Now the file pointer is ready to read the data. Closed song.wav Number of files processed = 1

You should be able to compile my program with any recent Microsoft or Borland or GNU C/C++ compiler. Try it.

What does my program tell you about my .wav file?
What does my program tell you about your .wav file?
Does your program work with my .wav file?

Regards,

Dave
Attached Files
File Type: zip song_wav.zip (56.5 KB, 2 views)
Last edited by davekw7x : 08-Nov-2007 at 08:33.
  #9  
Old 19-Nov-2007, 01:47
mahendra mahendra is offline
New Member
 
Join Date: Nov 2007
Posts: 14
mahendra is an unknown quantity at this point

Re: Error: Broken Pipe


Hai..
sorry for the delay.
following output obtained when i used ur code for my wave file
is there any thing wrong in that.

OutPUT::
Size of Chunk Descriptor Header = 12
Size of Format SubChunk Header = 24
Size of Data SubChunk Header = 8
Number of bytes before samples = 44

Opened 100257.wav for reading
ID from Chunk Descriptor Header: <RIFF>
Size from Chunk Descriptor Header = 50

ID from Format SubChunk Headr: <fmt >
Size = 18
Audio Format = 1
Number of channels = 1
Sample rate = 8000
Byte rate = 8000
Block Align = 1
Bits per sample = 8

ID from Data Subchunk Header: < 0x00 0x00 fa>
Data Size = 291939
Samples per Channel = 291939

Now the file pointer is ready to read the data.
Closed 100257.wav



Number of files processed = 1
Attached Files
File Type: zip empty.zip (264 Bytes, 4 views)
  #10  
Old 19-Nov-2007, 09:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Error: Broken Pipe


Quote:
Originally Posted by mahendra
...when i used ur code

The idea was not for me to analyze your problem but to get you to do it.

However...
The following just make sure that the sizes of the variables and the packing of the structs are consistent with the rest if the program, and have nothing to do with the .wav file itself.
Quote:
Originally Posted by mahendra
Code:
Size of Chunk Descriptor Header = 12 Size of Format SubChunk Header = 24 Size of Data SubChunk Header = 8

The following is calculated for a "classical" .wav file, and does not represent what is actually in the file.
Quote:
Originally Posted by mahendra
Code:
Number of bytes before samples = 44

The values are correct, so the program proceeds:
Quote:
Originally Posted by mahendra
Code:
Opened 100257.wav for reading ID from Chunk Descriptor Header: <RIFF> Size from Chunk Descriptor Header = 50
Already this tells me that this isn't a "classical" .wav file. See footnote.
The "classical" .wav file has puts the file size minus eight bytes here. This also tells me where you got your 58-byte header size that I disagreed with. That doesn't mean that one of us is right and the other is wrong. It just means that we were talking about two different kinds of <RIFF> files. One is what I have been calling a "classical" .wav file and is described in the references that I gave you previously.

According to the .wav format references that I use, for a "classical" .wav file the value of the size in the chunk descriptor header should be the total file size minus the eight bytes of the header. For example, the size of my "song.wav" file is 145481 bytes, and the value printed for that file is 145473.

Quote:
Originally Posted by mahendra
Code:
ID from Format SubChunk Headr: <fmt > Size = 18 Audio Format = 1 Number of channels = 1 Sample rate = 8000 Byte rate = 8000 Block Align = 1 Bits per sample = 8
According to my references, for a "classical" .wav file, the size here should be 16. Other RIFF format files may have additional stuff in the header, but from this point onward, my program doesn't really give valid output for your file. Everything else (other than the size) is OK for a file with 8000 8-bit samples per second monaural file, but the fact that the size is shown to be 18, leads us to the next clue. See footnote.
Quote:
Originally Posted by mahendra
Code:
ID from Data Subchunk Header: < 0x00 0x00 fa>
For a "classical" .wav file, it would be the four chars 'd','a','t','a'
SInce your previous header was two bytes longer than mine, the two 0x00 bytes are actually part of the previous header and the first two bytes of this subchunk header are 'f', 'a'. Deducing the next two bytes from your output, it seems to me that the subchunk header bytes are 'f', 'a', 'c', 't'

Here is a reference that tells you about the fact chunk http://www.sonicspot.com/guide/wavefiles.html#fact

Now, the "fact" chunk is not required for uncompressed pcm, and my program doesn't take it into account, and I won't go any farther with it. It is perfectly "legal" to have a fmt chunk even in an uncompressed pcm file, but it's not part of the "classical" .wav file format.

Bottom line: It is certainly possible for your program to process the file, regardless of whether it's a "classical" .wav file or one with a "fact" chunk. In either case you have to do some little work to get the correct file size so that you can get the correct buffer size to hold the data.

From the reference above, about a "fact" chunk:

"There is currently only one field defined for the format dependant data. It is a single 4-byte value that specifies the number of samples in the waveform data chunk."


In your case, since it's a monaural 8-bit data file, the number of samples is equal to the number of bytes in the .wav part itself, so the 32-bit value following 'f','a','c','t' gives the number of data bytes in the file. (In general you have to multiply by the number of channels and the number of bytes per sample.)

Since you have performed a read operation into a char array, I have to ask you how big is the array?

In a previous post you indicated that you didn't use any information from the header.

Regards,

Dave

Footnote: my simple program was used to get some information from a "classic" .wav file, and, as written, can't handle anything like a "fact" chunk. A more generic program could take such things into account. If you want to handle a specific type of file, you could specialize your program. In my opinion, you really must check the header information to make sure your program knows what it is dealing with.
Last edited by davekw7x : 19-Nov-2007 at 09:37.
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.