Mercurial > hg > piper-cpp
comparison bits/PluginOutputIdMapper.h @ 49:f3f7561233d6
Begin plugin output id / index mapping for use in feature sets
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Fri, 16 Sep 2016 14:13:21 +0100 |
parents | |
children | 12e3b396544c |
comparison
equal
deleted
inserted
replaced
48:ce6cb3308bd7 | 49:f3f7561233d6 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Vampipe | |
5 | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 Copyright 2006-2016 Chris Cannam and QMUL. | |
8 | |
9 Permission is hereby granted, free of charge, to any person | |
10 obtaining a copy of this software and associated documentation | |
11 files (the "Software"), to deal in the Software without | |
12 restriction, including without limitation the rights to use, copy, | |
13 modify, merge, publish, distribute, sublicense, and/or sell copies | |
14 of the Software, and to permit persons to whom the Software is | |
15 furnished to do so, subject to the following conditions: | |
16 | |
17 The above copyright notice and this permission notice shall be | |
18 included in all copies or substantial portions of the Software. | |
19 | |
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR | |
24 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
25 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
27 | |
28 Except as contained in this notice, the names of the Centre for | |
29 Digital Music; Queen Mary, University of London; and Chris Cannam | |
30 shall not be used in advertising or otherwise to promote the sale, | |
31 use or other dealings in this Software without prior written | |
32 authorization. | |
33 */ | |
34 | |
35 #ifndef VAMPIPE_PLUGIN_ID_MAPPER_H | |
36 #define VAMPIPE_PLUGIN_ID_MAPPER_H | |
37 | |
38 #include <vamp-hostsdk/Plugin.h> | |
39 | |
40 #include <map> | |
41 #include <string> | |
42 | |
43 namespace vampipe { | |
44 | |
45 class PluginOutputIdMapper | |
46 { | |
47 // NB not threadsafe. Does this matter? | |
48 | |
49 //!!! simplify. A single vector is almost certainly faster. | |
50 | |
51 public: | |
52 PluginOutputIdMapper(Vamp::Plugin *p) : m_plugin(p) { } | |
53 | |
54 class NotFound : virtual public std::runtime_error { | |
55 public: | |
56 NotFound() : runtime_error("output id or index not found in mapper") { } | |
57 }; | |
58 | |
59 int idToIndex(std::string outputId) const { | |
60 if (m_idIndexMap.empty()) populate(); | |
61 auto i = m_idIndexMap.find(outputId); | |
62 if (i == m_idIndexMap.end()) throw NotFound(); | |
63 return i->second; | |
64 } | |
65 | |
66 std::string indexToId(int index) const { | |
67 if (m_ids.empty()) populate(); | |
68 if (index < 0 || size_t(index) >= m_ids.size()) throw NotFound(); | |
69 return m_ids[index]; | |
70 } | |
71 | |
72 private: | |
73 Vamp::Plugin *m_plugin; | |
74 mutable std::vector<std::string> m_ids; | |
75 mutable std::map<std::string, int> m_idIndexMap; | |
76 | |
77 void populate() const { | |
78 Vamp::Plugin::OutputList outputs = m_plugin->getOutputDescriptors(); | |
79 for (const auto &d: outputs) { | |
80 m_idIndexMap[d.identifier] = m_ids.size(); | |
81 m_ids.push_back(d.identifier); | |
82 } | |
83 } | |
84 }; | |
85 | |
86 } | |
87 | |
88 #endif |