33 const TSize MpegAudioStream::mMaxDecodedBlockSize = 8192;
39 mEncodedSampleRate = (
int)file.
GetHeader().GetSampleRate();
46 if ( not mpHandle )
return;
52 mpHandle = fopen( mName.c_str(),
"rb");
56 std::string msgString =
"Could not open ";
58 msgString +=
" for reading!";
63 mBitstream.
Init( mpHandle );
72 CLAM_ASSERT(
false,
"CLAM does not encode Mpeg Audio!!!");
83 while( mDecodeBuffer[0].size() < nFrames
87 if (_mp3Frame>=_seekCache.size())
88 _seekCache.push_back(filePos);
102 "MP3 indexing not matching");
107 "MpegAudioStream: A frame had not the expected channels." );
109 "MpegAudioStream: Synthesis result had not the expected number of channels" );
115 mDecodeBuffer[i].insert( mDecodeBuffer[i].end(),
117 channelData + samplesDecodedThisTime );
119 mSamplesDecoded += samplesDecodedThisTime;
126 if (mDecodeBuffer[0].empty())
return;
128 for (
unsigned i = 0; i <
mChannels; i++ )
130 if ( mDecodeBuffer[i].size() < nFrames )
132 mDecodeBuffer[i].insert(
133 mDecodeBuffer[i].end(),
134 nFrames - mDecodeBuffer[i].size(),
138 ConsumeDecodedSamples();
141 void MpegAudioStream::ConsumeDecodedSamples()
144 for (
unsigned i = 0; i <
mChannels; i++ )
146 unsigned currOffset = 0;
147 for ( std::deque<mad_fixed_t>::iterator j = mDecodeBuffer[i].begin();
151 double sampleValue = mad_f_todouble(*j);
156 if ( sampleValue > 1.0 )
158 else if ( sampleValue < -1.0 )
163 mDecodeBuffer[i].erase( mDecodeBuffer[i].begin(),
164 mDecodeBuffer[i].begin() + nFrames );
171 CLAM_ASSERT(
false,
"CLAM does not encode Mpeg Audio!!!");
178 unsigned mp3FrameSize = 32*MAD_NSBSAMPLES(&mBitstream.
CurrentFrame().header);
179 unsigned targetMp3Frame = framePosition/mp3FrameSize;
180 std::cout <<
"targetMp3Frame: " << targetMp3Frame << std::endl;
182 unsigned maxForwarJump = _seekCache.size()+100;
183 if (targetMp3Frame>maxForwarJump) targetMp3Frame=maxForwarJump;
184 if (targetMp3Frame>=_seekCache.size())
187 fseek(mpHandle, _seekCache[_seekCache.size()-2], SEEK_SET);
189 for (
unsigned mp3Frame=_seekCache.size();
190 mp3Frame<=targetMp3Frame; mp3Frame++)
200 if (filePos<=_seekCache.back())
continue;
201 _seekCache.push_back(filePos);
204 unsigned skip = targetMp3Frame>4?3:0;
205 unsigned jumpingFrame = targetMp3Frame-skip;
206 std::cout <<
"jumpingFrame:" << jumpingFrame << std::endl;
207 fseek(mpHandle, _seekCache[jumpingFrame], SEEK_SET);
209 _mp3Frame = targetMp3Frame;
212 if (targetMp3Frame == 0)
return;
213 unsigned long previousFrameFilePos = _seekCache[targetMp3Frame-1];
223 if (filePos >= previousFrameFilePos)
break;