converting aiff sound data into AudioBuffer.mData format
converting aiff sound data into AudioBuffer.mData format
- Subject: converting aiff sound data into AudioBuffer.mData format
- From: email@hidden
- Date: Mon, 3 Sep 2001 01:48:40 -0500 (CDT)
Hello,
While being offlist I wrote a message with some similar concerns; I
don't know if it will post (moderator can ignore it, I suppose) but this
isn't completely redundant. Anyway...
For practice I wrote an AIFF/AIFC parser and am at the point of trying
to figure out how to properly format the data before putting it into the
mData field in the AudioBuffer. I know the bit-depth of the sound data but
I don't know how to configure it to play correctly in the audiobuffer. I
don't want to embarrass myself too much by going into the details of my
current attempt, so here are a few things I think may work:
-divide the DefaultAudioDevice's sampleRate by the file's sample rate and
use that as the marker for repition of the file's samples in comparison
with the device's. I realize that even if this works, it only works when
the device's rate is a multiple of the file's rate.
- I'm assuming that if the aiff has multiple channels, you use the first
set of bytes for the first channel, the second set for the 2nd channel and
so on. As far as the above mentioned repetition of samples for meeting the
device's sample rate, I'm not certain how to do this.
From everything I've read, the bit depth of the file is how the sound
data in the file should be treated, with a rounding up according to byte
divisions (e.g. 12 bit formatted as 16 bit). One thing I'm confused about
is how to properly segment the data on the fly according to bit depth,
e.g.
for (i=startingSamplePos; i<(startingSamplePos+(numSamples/2));
++curSamplesFrameCount)
{
if (i >= (sampleLength-1)) // > tempbd
{
return 0;
}
//an attempt at handling 16 bit sound data when stored in a 32 bit
type, aka float
*mData++ = sampleHolder->samples[i];//first 16 bit value
*mData++ = 65536*sampleHolder->samples[i] ; //second 16 bit value?
if (curSamplesFrameCount != 0 &&
(curSamplesFrameCount%numFramesPerSoundSample == 0))
{//repeated this set of samples enough to compensate for file's
//slower sampling rate, move to next set of samples
i += 1;
curSamplePos = i;
}
}
where samples is a float pointer. What I really need to know is how to do
something similar to the above code, but correct for different bit
depths/sample rates and number of channels. I've seen old Aiff
specification sheets that refer to the sound data as a char array but
I don't know how to combine multiple members in the char array (e.g. 2
chars into 1 16 bit depth item) properly bit-wise. I'm also confused by
the fact that I've only seen mData treated as a float array but yet my
current, incorrect code which gives NaN and -Inf oftentimes when printing
as a float still loosely resembles the actual file's timbre. Any help
appreciated.
Thanks,
Ben D.