Essentia  2.1-beta6-dev
metadatautils.h
Go to the documentation of this file.
1 
2 /*
3  * Copyright (C) 2006-2021 Music Technology Group - Universitat Pompeu Fabra
4  *
5  * This file is part of Essentia
6  *
7  * Essentia is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License as published by the Free
9  * Software Foundation (FSF), either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the Affero GNU General Public License
18  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
19  */
20 
21 #ifndef ESSENTIA_METADATAUTILS_H
22 #define ESSENTIA_METADATAUTILS_H
23 
24 #include <memory>
25 #include "algorithmfactory.h"
26 #include "vectoroutput.h"
27 #include "source.h"
28 
29 
30 namespace essentia {
31 
32 void pcmMetadata(const std::string& filename, int& sr, int& ch, int& bitrate) {
33 
34  size_t pos = filename.rfind('.');
35  if (pos != std::string::npos) {
36  std::string ext = filename.substr(pos, std::string::npos);
37  if (ext != ".wav" && ext != ".aiff" && ext != ".aif") {
38  throw EssentiaException("metadatautils: pcmMetadata cannot read files which are neither \"wav\" nor \"aiff\"");
39  }
40  } else {
41  throw EssentiaException("metadatautils: pcmMetadata cannot guess the filetype by extension");
42  }
43 
44  // (trick) create an audioloader to know the original samplerate
45  std::unique_ptr<streaming::Algorithm> audioloader(streaming::AlgorithmFactory::create("AudioLoader",
46  "filename", filename));
47 
48  sr = (int)streaming::lastTokenProduced<Real>(audioloader->output("sampleRate"));
49  ch = streaming::lastTokenProduced<int>(audioloader->output("numberChannels"));
50  bitrate = int(16.0/1000.0*sr*ch);
51 }
52 
53 } // namespace essentia
54 
55 #endif
Definition: types.h:77
static BaseAlgorithm * create(const std::string &id)
Definition: algorithmfactory.h:86
Definition: algorithm.h:28
void pcmMetadata(const std::string &filename, int &sr, int &ch, int &bitrate)
Definition: metadatautils.h:32