Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

mp3stream.cpp

Go to the documentation of this file.
00001 
00006 #include <cassert>
00007 #include "mp3stream.h"
00008 #include "except.h"
00009 
00010 #ifdef DEBUG
00011 #include <iostream>
00012 #endif
00013 
00014 MP3Stream::MP3Stream(Bitstream &bs, int type) : m_stream(bs)
00015 {
00016   m_type = type;
00017   m_ff = m_f = new MP3Frame(this); //First frame is also a dataframe
00018   m_cutbytes = 0;
00019 }
00020 
00021 MP3Stream::~MP3Stream()
00022 {
00023   while(m_ff) //Delete allocated frames
00024     m_ff = m_ff->Destroy();
00025 }
00026 
00027 MP3Frame *MP3Stream::First() const
00028 {
00029   return m_f;
00030 }
00031 
00032 void MP3Stream::SetFirst(MP3Frame *frame)
00033 {
00034   assert(frame);
00035   m_f = frame;
00036 }
00037 
00038 Bitstream &MP3Stream::GetBitstream() const
00039 {
00040   return m_stream;
00041 }
00042 
00043 void MP3Stream::WriteData(Bitstream &bs, int numbytes, int startpos)
00044 {
00045   int cutstart, size, writesize;
00046   
00047   while(numbytes > 0 && m_f)
00048   {
00049     if(!m_cutbytes) //New frame: calculate it's data offset
00050       m_f->SetDataBegin(startpos);
00051     
00052     size = m_f->DataSize();
00053     //Calculate beginning of rest of the frame
00054     cutstart = m_cutbytes ? (size - m_cutbytes) : 0;
00055     
00056     //If the rest of frame is bigger than numbytes, cut its end
00057     if(size - cutstart > numbytes )
00058       m_cutbytes = size - cutstart - numbytes;
00059     else
00060       m_cutbytes = 0;
00061     
00062     //Sanity checks
00063     writesize = size - cutstart - m_cutbytes;
00064     assert(writesize <= numbytes);
00065     assert(writesize >= 0);
00066     
00067     if(writesize > 0) //Just optimalization
00068       m_f->WriteData(bs, m_ff, cutstart, m_cutbytes);
00069     
00070     numbytes -= writesize;
00071     startpos += writesize;
00072     
00073     try {
00074       if(!m_cutbytes) //Frame was written completely, go to next
00075         m_f = m_f->Next();    
00076     } 
00077     catch(eEOF e)
00078     {
00079       m_f = 0;
00080     }
00081   }
00082   
00083   if(numbytes > 0)  
00084   {  
00085     #ifdef DEBUG
00086     std::cout << "append " << numbytes << " zeroes" << std::endl;
00087     #endif
00088     
00089     bs.WriteNullBytes(numbytes);
00090   }
00091 }
00092 
00093 void MP3Stream::SeekTo(MP3Frame *frame)
00094 {
00095   //Seek to given frame, but locked frames can't be deleted
00096   while(m_ff != frame && !m_ff->isLocked())
00097   {  
00098     if(m_f == m_ff) m_f = frame; //Firstrame must be moved too
00099     m_ff = m_ff->Destroy();
00100   }
00101 }
00102 
00103 int MP3Stream::WriteMode() const
00104 {
00105   return m_type;
00106 }

Generated on Wed Sep 6 00:17:57 2006 for Kraken by  doxygen 1.4.4