{
byte[] soundData = readFully(strm);
if (soundData == null)
return null;
DefineSoundTag tag = new DefineSoundTag();
tag.setSoundData(soundData);
tag.setSoundFormat(2); // MP3
tag.setSoundSize(1); // always 16-bit for compressed formats
/**
* 0 - version 2.5
* 1 - reserved
* 2 - version 2
* 3 - version 1
*/
int version = soundData[3] >> 3 & 0x3;
/**
* 0 - reserved
* 1 - layer III => 1152 samples
* 2 - layer II => 1152 samples
* 3 - layer I => 384 samples
*/
int layer = soundData[3] >> 1 & 0x3;
int samplingRate = soundData[4] >> 2 & 0x3;
/**
* 0 - stereo
* 1 - joint stereo
* 2 - dual channel
* 3 - single channel
*/
int channelMode = soundData[5] >> 6 & 0x3;
int frequency = mp3frequencies[samplingRate][version];
/**
* 1 - 11kHz
* 2 - 22kHz
* 3 - 44kHz
*/
int rate;
switch (frequency)
{
case 11025:
rate = 1;
break;
case 22050:
rate = 2;
break;
case 44100:
rate = 3;
break;
default:
problems.add(new EmbedUnsupportedSamplingRateProblem(data, frequency));
return null;
}
tag.setSoundRate(rate);
/**
* 0 - mono
* 1 - stereo
*/
tag.setSoundType(channelMode == 3 ? 0 : 1);
/**
* assume that the whole thing plays in one SWF frame
*
* sample count = number of MP3 frames * number of samples per MP3
*/
long sampleCount = countFrames(soundData) * (layer == 3 ? 384 : 1152);
tag.setSoundSampleCount(sampleCount);
if (sampleCount < 0)
{
// frame count == -1, error!
problems.add(new EmbedCouldNotDetermineSampleFrameCountProblem(data));