Blender  V2.59
AUD_StreamBufferFactory.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: AUD_StreamBufferFactory.cpp 35141 2011-02-25 10:21:56Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * Copyright 2009-2011 Jörg Hermann Müller
00007  *
00008  * This file is part of AudaSpace.
00009  *
00010  * Audaspace is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * AudaSpace is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with Audaspace; if not, write to the Free Software Foundation,
00022  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00023  *
00024  * ***** END GPL LICENSE BLOCK *****
00025  */
00026 
00032 #include "AUD_StreamBufferFactory.h"
00033 #include "AUD_BufferReader.h"
00034 #include "AUD_Buffer.h"
00035 
00036 #include <cstring>
00037 
00038 AUD_StreamBufferFactory::AUD_StreamBufferFactory(AUD_IFactory* factory) :
00039         m_buffer(new AUD_Buffer())
00040 {
00041         AUD_IReader* reader = factory->createReader();
00042 
00043         m_specs = reader->getSpecs();
00044 
00045         int sample_size = AUD_SAMPLE_SIZE(m_specs);
00046         int length;
00047         int index = 0;
00048         sample_t* buffer;
00049 
00050         // get an approximated size if possible
00051         int size = reader->getLength();
00052 
00053         if(size <= 0)
00054                 size = AUD_BUFFER_RESIZE_BYTES / sample_size;
00055         else
00056                 size += m_specs.rate;
00057 
00058         // as long as we fill our buffer to the end
00059         while(index == m_buffer.get()->getSize() / sample_size)
00060         {
00061                 // increase
00062                 m_buffer.get()->resize(size*sample_size, true);
00063 
00064                 // read more
00065                 length = size-index;
00066                 reader->read(length, buffer);
00067                 memcpy(m_buffer.get()->getBuffer() + index * m_specs.channels,
00068                            buffer,
00069                            length * sample_size);
00070                 size += AUD_BUFFER_RESIZE_BYTES / sample_size;
00071                 index += length;
00072         }
00073 
00074         m_buffer.get()->resize(index * sample_size, true);
00075         delete reader;
00076 }
00077 
00078 AUD_IReader* AUD_StreamBufferFactory::createReader() const
00079 {
00080         return new AUD_BufferReader(m_buffer, m_specs);
00081 }