MonoAudioFileWriter.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #include "MonoAudioFileWriter.hxx"
00023 #include "AudioCodecs_Stream.hxx"
00024 #include "FileSystem.hxx"
00025 #include "ProcessingFactory.hxx"
00026 
00027 
00028 namespace CLAM
00029 {
00030 
00031 namespace Hidden
00032 {
00033         static const char * metadata[] = {
00034                 "key", "MonoAudioFileWriter",
00035                 "category", "Audio File I/O",
00036                 "description", "MonoAudioFileWriter",
00037                 0
00038         };
00039         static FactoryRegistrator<ProcessingFactory, MonoAudioFileWriter> reg = metadata;
00040 }
00041         
00042         
00043         MonoAudioFileWriter::MonoAudioFileWriter()
00044                 : mInput( "Samples Write", this ),
00045                   mOutStream( NULL )
00046         {
00047                 Configure( MonoAudioFileWriterConfig() );
00048         }
00049 
00050         MonoAudioFileWriter::MonoAudioFileWriter( const ProcessingConfig& cfg )
00051                 : mInput( "Samples Write", this ),
00052                   mOutStream( NULL )
00053         {
00054                 Configure( cfg );
00055         }
00056 
00057         MonoAudioFileWriter::~MonoAudioFileWriter()
00058         {
00059                 if ( mOutStream )
00060                         delete mOutStream;
00061                 if ( mConfig.HasTargetFile() )
00062                         FileSystem::GetInstance().UnlockFile( mConfig.GetTargetFile() );
00063         }
00064 
00065         const char* MonoAudioFileWriter::GetClassName() const
00066         {
00067                 return "MonoAudioFileWriter";
00068         }
00069         
00070         const ProcessingConfig& MonoAudioFileWriter::GetConfig() const
00071         {
00072                 return mConfig;
00073         }
00074 
00075         bool MonoAudioFileWriter::Do()
00076         {
00077                 bool result = Do( mInput.GetAudio() );
00078                 mInput.Consume();
00079                 return result;          
00080         }
00081         
00082         bool MonoAudioFileWriter::Do( const Audio & data )
00083         {
00084                 mOutStream->WriteData( 0, data.GetBuffer().GetPtr(), data.GetSize() );
00085                 return true;
00086         }
00087 
00088         bool MonoAudioFileWriter::ConcreteConfigure( const ProcessingConfig& cfg )
00089         {
00090                 if ( mConfig.HasTargetFile() )
00091                         FileSystem::GetInstance().UnlockFile( mConfig.GetTargetFile() );
00092 
00093                 CopyAsConcreteConfig( mConfig, cfg );
00094 
00095                 const std::string & location = mConfig.GetTargetFile();
00096                 if ( location == "")
00097                         return AddConfigErrorMessage("No file selected");
00098 
00099                 AudioFileHeader header;
00100                 header.SetValues( mConfig.GetSampleRate(), 1, EAudioFileFormat::FormatFromFilename(location));
00101                 mFile.CreateNew(location, header);
00102                 if ( ! mFile.GetHeader().HasChannels())
00103                 {
00104                         return AddConfigErrorMessage("The file is not writeable");
00105                 }
00106                 if ( mFile.GetHeader().GetChannels() != 1 ) // this is the 'mono' file writer...
00107                 {
00108                         return AddConfigErrorMessage("Too many channels!");
00109                 }
00110 
00111                 if ( !mFile.IsWritable() )
00112                 {
00113                         return AddConfigErrorMessage("The format does not support such number of channels, endiannes or sampling rate.");
00114                 }
00115 
00116                 if ( FileSystem::GetInstance().IsFileLocked( mConfig.GetTargetFile() ) )
00117                 {
00118                         AddConfigErrorMessage("File: ");
00119                         AddConfigErrorMessage(mConfig.GetTargetFile() );
00120                         AddConfigErrorMessage(" has been locked by another Processing");
00121 
00122                         return false;
00123                 }
00124 
00125                 FileSystem::GetInstance().LockFile( mConfig.GetTargetFile() );
00126 
00127                 mOutStream = mFile.GetStream();
00128 
00129                 if ( !mOutStream )
00130                 {
00131                         return AddConfigErrorMessage("Could not get a valid audio file stream!");
00132                 }
00133 
00134                 return true;
00135         }
00136 
00137         bool MonoAudioFileWriter::ConcreteStart()
00138         {
00139                 mOutStream->PrepareWriting();
00140                 
00141                 return true;
00142         }
00143 
00144         bool MonoAudioFileWriter::ConcreteStop()
00145         {
00146                 mOutStream->Dispose();
00147                 delete mOutStream;
00148                 mOutStream = NULL;
00149 
00150                 return true;
00151         }
00152 }
00153 
Generated by  doxygen 1.6.3