Blender V2.61 - r43446

AUD_FileWriter.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * Copyright 2009-2011 Jörg Hermann Müller
00005  *
00006  * This file is part of AudaSpace.
00007  *
00008  * Audaspace is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * AudaSpace is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Audaspace; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00029 #ifdef WITH_FFMPEG
00030 // needed for INT64_C
00031 #ifndef __STDC_CONSTANT_MACROS
00032 #define __STDC_CONSTANT_MACROS
00033 #endif
00034 #include "AUD_FFMPEGWriter.h"
00035 #endif
00036 
00037 #ifdef WITH_SNDFILE
00038 #include "AUD_SndFileWriter.h"
00039 #endif
00040 
00041 #include "AUD_FileWriter.h"
00042 #include "AUD_Buffer.h"
00043 
00044 static const char* write_error = "AUD_FileWriter: File couldn't be written.";
00045 
00046 AUD_Reference<AUD_IWriter> AUD_FileWriter::createWriter(std::string filename,AUD_DeviceSpecs specs,
00047                                                         AUD_Container format, AUD_Codec codec, unsigned int bitrate)
00048 {
00049 #ifdef WITH_SNDFILE
00050     try
00051     {
00052         return new AUD_SndFileWriter(filename, specs, format, codec, bitrate);
00053     }
00054     catch(AUD_Exception&) {}
00055 #endif
00056 
00057 #ifdef WITH_FFMPEG
00058     try
00059     {
00060         return new AUD_FFMPEGWriter(filename, specs, format, codec, bitrate);
00061     }
00062     catch(AUD_Exception&) {}
00063 #endif
00064 
00065     AUD_THROW(AUD_ERROR_SPECS, write_error);
00066 }
00067 
00068 void AUD_FileWriter::writeReader(AUD_Reference<AUD_IReader> reader, AUD_Reference<AUD_IWriter> writer, unsigned int length, unsigned int buffersize)
00069 {
00070     AUD_Buffer buffer(buffersize * AUD_SAMPLE_SIZE(writer->getSpecs()));
00071     sample_t* buf = buffer.getBuffer();
00072 
00073     int len;
00074     bool eos = false;
00075     int channels = writer->getSpecs().channels;
00076 
00077     for(unsigned int pos = 0; ((pos < length) || (length <= 0)) && !eos; pos += len)
00078     {
00079         len = buffersize;
00080         if((len > length - pos) && (length > 0))
00081             len = length - pos;
00082         reader->read(len, eos, buf);
00083 
00084         for(int i = 0; i < len * channels; i++)
00085         {
00086             // clamping!
00087             if(buf[i] > 1)
00088                 buf[i] = 1;
00089             else if(buf[i] < -1)
00090                 buf[i] = -1;
00091         }
00092 
00093         writer->write(len, buf);
00094     }
00095 }