Essentia  2.1-beta5-dev
fileoutput.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2016 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef ESSENTIA_FILEOUTPUT_H
21 #define ESSENTIA_FILEOUTPUT_H
22 
23 #include <fstream>
24 #include "../streamingalgorithm.h"
25 #include "../../streamutil.h"
26 
27 namespace essentia {
28 namespace streaming {
29 
30 
31 template <typename TokenType> inline void write_binary(std::ostream* _stream,
32  const TokenType& value) {
33  _stream->write((const char*) &value, sizeof(TokenType));
34 }
35 
36 template <> void inline write_binary<std::vector<Real> >(std::ostream* _stream,
37  const std::vector<Real>& value) {
38  _stream->write((const char*) &value[0], value.size() * sizeof(Real));
39 }
40 
41 
42 template <typename TokenType, typename StorageType = TokenType>
43 class FileOutput : public Algorithm {
44  protected:
46  std::ostream* _stream;
47  std::string _filename;
48  bool _binary;
49 
50  public:
51  FileOutput() : Algorithm(), _stream(NULL) {
52  setName("FileOutput");
53  declareInput(_data, 1, "data", "the incoming data to be stored in the output file");
54 
56  }
57 
59  if (_stream != &std::cout) delete _stream;
60  }
61 
63  declareParameter("filename", "the name of the output file (use '-' for stdout)", "", "out.txt");
64  declareParameter("mode", "output mode", "{text,binary}", "text");
65  }
66 
67  void configure() {
68  if (!parameter("filename").isConfigured()) {
69  throw EssentiaException("FileOutput: please provide the 'filename' parameter");
70  }
71 
72  _filename = parameter("filename").toString();
73 
74  if (_filename == "") {
75  throw EssentiaException("FileOutput: empty filenames are not allowed.");
76  }
77 
78  _binary = (parameter("mode").toString() == "binary");
79  }
80 
82  if (_filename == "-") {
83  _stream = &std::cout;
84  }
85  else {
86  _stream = _binary ? new std::ofstream(_filename.c_str(), std::ofstream::binary)
87  : new std::ofstream(_filename.c_str());
88 
89  if (_stream->fail()) {
90  throw EssentiaException("FileOutput: Could not open file for writing: ", _filename);
91  }
92  }
93  }
94 
96  if (!_stream) {
98  }
99 
100  EXEC_DEBUG("process()");
101 
102  if (!_data.acquire(1)) return NO_INPUT;
103 
104  write(_data.firstToken());
105 
106  _data.release(1);
107 
108  return OK;
109  }
110 
111  void write(const TokenType& value) {
112  if (!_stream) throw EssentiaException("FileOutput: not configured properly");
113  if (_binary) {
114  write_binary(_stream, value);
115  }
116  else {
117  *_stream << value << "\n";
118  }
119  }
120 };
121 
122 } // namespace streaming
123 } // namespace essentia
124 
125 #endif // ESSENTIA_FILEOUTPUT_H
FileOutput()
Definition: fileoutput.h:51
void declareInput(SinkBase &sink, const std::string &name, const std::string &desc)
const TokenType & firstToken() const
Definition: sink.h:61
std::string toString(int precision=12) const
void declareParameter(const std::string &name, const std::string &desc, const std::string &range, const Parameter &defaultValue)
void configure()
Definition: fileoutput.h:67
void release()
Definition: sink.h:79
void setName(const std::string &name)
Definition: configurable.h:53
void write_binary(std::ostream *_stream, const TokenType &value)
Definition: fileoutput.h:31
const Parameter & parameter(const std::string &key) const
Definition: configurable.h:105
void createOutputStream()
Definition: fileoutput.h:81
#define NULL
Definition: tnt_i_refvec.h:33
AlgorithmStatus process()
Definition: fileoutput.h:95
bool _binary
Definition: fileoutput.h:48
float Real
Definition: types.h:68
Definition: streamingalgorithm.h:111
void declareParameters()
Definition: fileoutput.h:62
std::string _filename
Definition: fileoutput.h:47
Definition: streamingalgorithm.h:107
AlgorithmStatus
Definition: streamingalgorithm.h:106
~FileOutput()
Definition: fileoutput.h:58
Definition: algorithm.h:28
Definition: types.h:76
std::ostream * _stream
Definition: fileoutput.h:46
Definition: fileoutput.h:43
Definition: streamingalgorithm.h:140
void acquire()
Definition: sink.h:67
void write(const TokenType &value)
Definition: fileoutput.h:111
Definition: sink.h:35
#define EXEC_DEBUG(msg)
Definition: debugging.h:161
Sink< TokenType > _data
Definition: fileoutput.h:45