00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00097 if ( location == "")
00098 {
00099 AddConfigErrorMessage("No file selected");
00100 return false;
00101 }
00102 AudioFileHeader header;
00103 header.SetValues( mConfig.GetSampleRate(), 1, EAudioFileFormat::FormatFromFilename(location));
00104 mFile.CreateNew(location, header);
00105 if ( ! mFile.GetHeader().HasChannels())
00106 {
00107 AddConfigErrorMessage("The file is not writeable");
00108 return false;
00109 }
00110 if ( mFile.GetHeader().GetChannels() != 1 )
00111 {
00112 AddConfigErrorMessage("Too many channels!");
00113 return false;
00114 }
00115
00116 if ( !mFile.IsWritable() )
00117 {
00118 AddConfigErrorMessage("The format does not support such number of channels, endiannes or sampling rate.");
00119 return false;
00120 }
00121
00122 if ( FileSystem::GetInstance().IsFileLocked( mConfig.GetTargetFile() ) )
00123 {
00124 AddConfigErrorMessage("File: ");
00125 AddConfigErrorMessage(mConfig.GetTargetFile() );
00126 AddConfigErrorMessage(" has been locked by another Processing");
00127
00128 return false;
00129 }
00130
00131 FileSystem::GetInstance().LockFile( mConfig.GetTargetFile() );
00132
00133 mOutStream = mFile.GetStream();
00134
00135 if ( !mOutStream )
00136 {
00137 AddConfigErrorMessage("Could not get a valid audio file stream!");
00138 return false;
00139 }
00140
00141 return true;
00142 }
00143
00144 bool MonoAudioFileWriter::ConcreteStart()
00145 {
00146 mOutStream->PrepareWriting();
00147
00148 return true;
00149 }
00150
00151 bool MonoAudioFileWriter::ConcreteStop()
00152 {
00153 mOutStream->Dispose();
00154 delete mOutStream;
00155 mOutStream = NULL;
00156
00157 return true;
00158 }
00159 }
00160