Essentia  2.1-beta6-dev
diskwriter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 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_DISKWRITER_H
21 #define ESSENTIA_DISKWRITER_H
22 
23 #include <fstream>
24 #include "../streamingalgorithm.h"
25 
26 namespace essentia {
27 namespace streaming {
28 
29 template <class T>
30 class DiskWriter : public Algorithm {
31  protected:
33 
34  std::string _filename;
35  std::ostream *_out;
36 
37  public:
38  DiskWriter(const std::string& filename) : Algorithm() {
39 
40  declareInput(_data, 1, "data", "the data to write to disk");
41  _name = "DiskWriter";
42 
43  if (filename != "-")
44  _out = new std::ofstream(_filename.c_str());
45  else
46  _out = &std::cout;
47  }
48 
50  if (_out != &std::cout) {
51  delete _out;
52  }
53  }
54 
56 
58  EXEC_DEBUG("process()");
59 
60  AlgorithmStatus status = acquireData();
61  if (status != OK) return status;
62 
63  EXEC_DEBUG("data acquired");
64 
65  *_out << _data.firstToken() << '\n';
66 
67  EXEC_DEBUG("produced data");
68 
69  EXEC_DEBUG("releasing");
70  releaseData();
71  EXEC_DEBUG("released");
72 
73  return OK;
74  }
75 };
76 
77 } // namespace streaming
78 } // namespace essentia
79 
80 #endif // ESSENTIA_DISKWRITER_H
std::string _name
Definition: configurable.h:171
Definition: streamingalgorithm.h:140
void declareInput(SinkBase &sink, const std::string &name, const std::string &desc)
AlgorithmStatus acquireData()
Definition: diskwriter.h:30
DiskWriter(const std::string &filename)
Definition: diskwriter.h:38
AlgorithmStatus process()
Definition: diskwriter.h:57
std::string _filename
Definition: diskwriter.h:34
void declareParameters()
Definition: diskwriter.h:55
Sink< T > _data
Definition: diskwriter.h:32
std::ostream * _out
Definition: diskwriter.h:35
~DiskWriter()
Definition: diskwriter.h:49
const TokenType & firstToken() const
Definition: sink.h:61
#define EXEC_DEBUG(msg)
Definition: debugging.h:161
AlgorithmStatus
Definition: streamingalgorithm.h:106
@ OK
Definition: streamingalgorithm.h:107
Definition: algorithm.h:28