big loop with big array

Started by
9 comments, last by lomateron 11 years, 10 months ago
I have this big array and this big {for} loop to fill this array
the loop reads a soundfile, and writes it to the array, the sound data is simple, 3minutes of 10hz constant
so the sound data should be something that starts 0,1,2,3,4,...65534 then goes down and up and down again and again

but debuggin i found that when it gets aproximately to the 200 loop the data that reads will stay the same so it will fill the array with the same value from that point until it finishes loop. the soundfile can be changed to another music that has same format and the same happens, why?

[source lang="cpp"] D3DXVECTOR4 dd[7935489]; //the big array, it is located up in the head of the program.


//Data of sound
BYTE id[40];
unsigned int ivb;
unsigned short p1[2]; //right and left 1
unsigned short p2[2]; //right and left 2

FILE * fp;
fp = fopen("12.wav","r");

fread(id,sizeof(BYTE),40,fp);//read unessesary info of sound
fread(&ivb,sizeof(unsigned int),1,fp);// read the sized of the data
unsigned int ss= ivb/4;// calculate num of samples
unsigned int i;
for(i=0;i < (ss/2);i++)//fill part of the array with all sound data
{
fread(p1,sizeof(unsigned short),2,fp);
fread(p2,sizeof(unsigned short),2,fp);
dd=D3DXVECTOR4(float(p1[0]),float(p1[1]),float(p2[0]),float(p2[1]));
}[/source]
Advertisement
What have you tried?
does it looks complex?,its simple man,if you know about how to make soundfiles, but i want to know if someone had that same problem.
with just that code and a soundfile,16 bits 2channels 44100samples/second, you can try it yourself and see.

and yes i just wasted like a day to get to this. I created code that was based on that array so i had to recheck it, it took me time...like a day
I have no clue about the file format. I only can suggest to check if your variables are initialized correctly. A verification whether or not you are reading past the end might reveal a problem as well.

I would also suggest to open the file as binary ("rb").

Crafter 2D: the open source 2D game framework

?Github: https://github.com/crafter2d/crafter2d
Twitter: [twitter]crafter_2d[/twitter]


I would also suggest to open the file as binary ("rb").

This is most likely it. Opening a binary file as text can cause interesting things to happen, such as data read being interpreted differently.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

well the variables are initialized the same way you can see them in the code

[background=rgb(250, 251, 252)]reading past the end--nop because the loop is around 2 million times and the data starts to get repeated at the 220 loop[/background]




the problem isn't in the soundfile, i had opened it(http://ie.microsoft....L5/TypedArrays/),i have played it too ,and i have opened with that code other soundfiles and at some point near the start the array starts to be filled with the same data until loop finishes.
This was wrong
cant find the problem
My first guess would be that the value of ivb is being read wrong?

Put a breakpoint on the for loop and make a note of the variables.

On other words, you need to make sure (ss/2) is the expected size for your data.
I'm going to ask you a series of questions, and I want you to give specific and detailed answers:

  • Did you do what jeroenb suggested and open the file as a binary file (as "rb") (you should be doing this anyway, even if your problem persists)?
  • Are you checking the status of the file with ferror and feof?
  • Are you sure fread is reading the requested number of bytes (it can read fewer than requested)?
  • Are you 100% sure this is a 16-bit, 2 channel PCM WAV file?
  • Have you properly read the "unnecessary info of sound" and verified that you're getting what you expect in the header?
  • Is ivb == size of the file in bytes - 44?


Also, 16 bit PCM WAV files use signed numbers (you should be using [font=courier new,courier,monospace]short[/font]), not unsigned numbers (you shouldn't be using [font=courier new,courier,monospace]unsigned short[/font]) (you'll get the wrong values). And [font=courier new,courier,monospace]sizeof(BYTE)[/font] isn't intuitive, as a [font=courier new,courier,monospace]BYTE[/font] should always have a size of 1 (and if it's more than that for some broken reason, you'll read too much and go past the header).
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement