Blender V2.61 - r43446

AUD_SndFileWriter.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 
00030 #include "AUD_SndFileWriter.h"
00031 
00032 #include <cstring>
00033 
00034 static const char* fileopen_error = "AUD_SndFileWriter: File couldn't be written.";
00035 static const char* format_error = "AUD_SndFileWriter: Unsupported format.";
00036 
00037 AUD_SndFileWriter::AUD_SndFileWriter(std::string filename, AUD_DeviceSpecs specs,
00038                                      AUD_Container format, AUD_Codec codec, unsigned int bitrate) :
00039     m_specs(specs)
00040 {
00041     SF_INFO sfinfo;
00042 
00043     sfinfo.channels = specs.channels;
00044     sfinfo.samplerate = int(specs.rate);
00045 
00046     switch(format)
00047     {
00048     case AUD_CONTAINER_FLAC:
00049         sfinfo.format = SF_FORMAT_FLAC;
00050         switch(specs.format)
00051         {
00052         case AUD_FORMAT_S16:
00053             sfinfo.format |= SF_FORMAT_PCM_16;
00054             break;
00055         case AUD_FORMAT_S24:
00056             sfinfo.format |= SF_FORMAT_PCM_24;
00057             break;
00058         case AUD_FORMAT_S32:
00059             sfinfo.format |= SF_FORMAT_PCM_32;
00060             break;
00061         case AUD_FORMAT_FLOAT32:
00062             sfinfo.format |= SF_FORMAT_FLOAT;
00063             break;
00064         case AUD_FORMAT_FLOAT64:
00065             sfinfo.format |= SF_FORMAT_DOUBLE;
00066             break;
00067         default:
00068             sfinfo.format = 0;
00069             break;
00070         }
00071         break;
00072     case AUD_CONTAINER_OGG:
00073         if(codec == AUD_CODEC_VORBIS)
00074             sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS;
00075         else
00076             sfinfo.format = 0;
00077         break;
00078     case AUD_CONTAINER_WAV:
00079         sfinfo.format = SF_FORMAT_WAV;
00080         switch(specs.format)
00081         {
00082         case AUD_FORMAT_U8:
00083             sfinfo.format |= SF_FORMAT_PCM_U8;
00084             break;
00085         case AUD_FORMAT_S16:
00086             sfinfo.format |= SF_FORMAT_PCM_16;
00087             break;
00088         case AUD_FORMAT_S24:
00089             sfinfo.format |= SF_FORMAT_PCM_24;
00090             break;
00091         case AUD_FORMAT_S32:
00092             sfinfo.format |= SF_FORMAT_PCM_32;
00093             break;
00094         case AUD_FORMAT_FLOAT32:
00095             sfinfo.format |= SF_FORMAT_FLOAT;
00096             break;
00097         case AUD_FORMAT_FLOAT64:
00098             sfinfo.format |= SF_FORMAT_DOUBLE;
00099             break;
00100         default:
00101             sfinfo.format = 0;
00102             break;
00103         }
00104         break;
00105     default:
00106         sfinfo.format = 0;
00107         break;
00108     }
00109 
00110     if(sfinfo.format == 0)
00111         AUD_THROW(AUD_ERROR_SPECS, format_error);
00112 
00113     m_sndfile = sf_open(filename.c_str(), SFM_WRITE, &sfinfo);
00114 
00115     if(!m_sndfile)
00116         AUD_THROW(AUD_ERROR_FILE, fileopen_error);
00117 }
00118 
00119 AUD_SndFileWriter::~AUD_SndFileWriter()
00120 {
00121     sf_close(m_sndfile);
00122 }
00123 
00124 int AUD_SndFileWriter::getPosition() const
00125 {
00126     return m_position;
00127 }
00128 
00129 AUD_DeviceSpecs AUD_SndFileWriter::getSpecs() const
00130 {
00131     return m_specs;
00132 }
00133 
00134 void AUD_SndFileWriter::write(unsigned int length, sample_t* buffer)
00135 {
00136     length = sf_writef_float(m_sndfile, buffer, length);
00137 
00138     m_position += length;
00139 }