Forums

Is 8-bit Audio Unsigned?

Started by Chris Barrett January 19, 2007
Chris Barrett wrote:
> I've found that I've been forced to use an if statement to read 24-bit > audio. I'm doing this with the following code: > > > //------------------------------------------------- > long data24; > > fread(&data24,sampleSize,1,file_In); > if(data24 > 8388607) > array1[i] = (data24)/8388608.0 - 2.0; > if(data24 <= 8388607) > array1[i] = (data24)/8388608.0; > //----------------------------------------------- > > > The strange thing is that I've been able to write it without using an if > statement. I've done this using the following code: > > > //------------------------------------------------ > long signed data24; > > data24 = (long)floor((array2[i])*8388608.0+0.5); > fwrite(&data24,sampleSize,1,file_out); > //----------------------------------------------- > > > This makes no sense to me. How can it require an if statement to read, > but not one to write? Is there a way around this?
You are using a long data type, which is 32-bits on many systems. Your reader will work whether or not the upper 8 bits are all ones. Your writer will always fill the upper 8 bits with ones (given full range signals), which may not be symmetric and might fool some readers which assume the upper 8 bits have some other meaning. If you only write out the bottom 24 bits, then whether or not the upper 8 bits are correct or not may be irrelevant. IMHO. YMMV. -- rhn A.T nicholson d.0.t C-o-M