CLAM-Development  1.4.0
MonoAudioFileWriter.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "MonoAudioFileWriter.hxx"
23 #include "AudioCodecs_Stream.hxx"
24 #include "FileSystem.hxx"
25 #include "ProcessingFactory.hxx"
26 
27 
28 namespace CLAM
29 {
30 
31 namespace Hidden
32 {
33  static const char * metadata[] = {
34  "key", "MonoAudioFileWriter",
35  "category", "Audio File I/O",
36  "description", "MonoAudioFileWriter",
37  0
38  };
39  static FactoryRegistrator<ProcessingFactory, MonoAudioFileWriter> reg = metadata;
40 }
41 
42 
44  : mInput( "Samples Write", this ),
45  mOutStream( NULL )
46  {
48  }
49 
51  : mInput( "Samples Write", this ),
52  mOutStream( NULL )
53  {
54  Configure( cfg );
55  }
56 
58  {
59  if ( mOutStream )
60  delete mOutStream;
61  if ( mConfig.HasTargetFile() )
62  FileSystem::GetInstance().UnlockFile( mConfig.GetTargetFile() );
63  }
64 
66  {
67  return "MonoAudioFileWriter";
68  }
69 
71  {
72  return mConfig;
73  }
74 
76  {
77  bool result = Do( mInput.GetAudio() );
78  mInput.Consume();
79  return result;
80  }
81 
82  bool MonoAudioFileWriter::Do( const Audio & data )
83  {
84  mOutStream->WriteData( 0, data.GetBuffer().GetPtr(), data.GetSize() );
85  return true;
86  }
87 
89  {
90  if ( mConfig.HasTargetFile() )
91  FileSystem::GetInstance().UnlockFile( mConfig.GetTargetFile() );
92 
94 
95  const std::string & location = mConfig.GetTargetFile();
96  if ( location == "")
97  return AddConfigErrorMessage("No file selected");
98 
99  AudioFileHeader header;
100  header.SetValues( mConfig.GetSampleRate(), 1, EAudioFileFormat::FormatFromFilename(location));
101  mFile.CreateNew(location, header);
102  if ( ! mFile.GetHeader().HasChannels())
103  {
104  return AddConfigErrorMessage("The file is not writeable");
105  }
106  if ( mFile.GetHeader().GetChannels() != 1 ) // this is the 'mono' file writer...
107  {
108  return AddConfigErrorMessage("Too many channels!");
109  }
110 
111  if ( !mFile.IsWritable() )
112  {
113  return AddConfigErrorMessage("The format does not support such number of channels, endiannes or sampling rate.");
114  }
115 
116  if ( FileSystem::GetInstance().IsFileLocked( mConfig.GetTargetFile() ) )
117  {
118  AddConfigErrorMessage("File: ");
119  AddConfigErrorMessage(mConfig.GetTargetFile() );
120  AddConfigErrorMessage(" has been locked by another Processing");
121 
122  return false;
123  }
124 
125  FileSystem::GetInstance().LockFile( mConfig.GetTargetFile() );
126 
128 
129  if ( !mOutStream )
130  {
131  return AddConfigErrorMessage("Could not get a valid audio file stream!");
132  }
133 
134  return true;
135  }
136 
138  {
140 
141  return true;
142  }
143 
145  {
146  mOutStream->Dispose();
147  delete mOutStream;
148  mOutStream = NULL;
149 
150  return true;
151  }
152 }
153