I have Dev-C++ as an IDE and MinGW as the compiler, and I am running C programs on the IDE. I have this project where I have to write raw PCM data (unsigned 8-bit i.e bytes of PCM) as an array. I have figured out the reading part but when I view the output on the screen the starting bytes are missing and the remaining integers just display fine. Where did the bytes at the beginning go? Why cant I see the entire data?? I am using the fread() function to read the data and it returns the number of values read by it. The return value is also fine, nothing absurd and upto my expectations. Any reason why this is happening?

The code below is very specific and is tailored to my wav file. I have used MATLAB to read the header. Is there any problem in the code??

#include<stdio.h>
#include<stdlib.h>
int main()
{FILE *fp;
fp=fopen("AE.wav", "rb");
unsigned char samples[4576];
fseek ( fp , 46 , SEEK_SET );
int nr = fread(samples, sizeof(char), 4576, fp);
int i;
for(i=0;i<4576;i++)
{
printf("%d",(int)samples[i]);
printf("\n");
}
printf("No. of samples read= %d", nr);
fclose(fp);
return 0;   
}

Recommended Answers

All 5 Replies

Are you sure you're fseeking to the right place to start reading the data? If you're missing the data at the start, perhaps you're going too far with the fseek.

I have used the wavread() function in MATLAB and thus I have seen the PCM samples, also using MATLAB I know what the header contains, hence it is very specific to my needs. Any ideas why the text file and the output on the screen don't match?

Any ideas why the text file and the output on the screen don't match?

One or both is reading them wrong, or reading the wrong place. Open the file with a hex editor and manually confirm the correct values.

the problem is with the o/p of my program. When I use printf() to print the samples, I don't get all the samples on the screen. The starting samples, which otherwise appear on the .txt file when I write them using fprintf, don't appear on the screen. What could be the reason??

Ignore this - didn't spot that you were casting each char to an int.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.