PluginHostAdapter.cpp

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Vamp
00005 
00006     An API for audio analysis and feature extraction plugins.
00007 
00008     Centre for Digital Music, Queen Mary, University of London.
00009     Copyright 2006 Chris Cannam.
00010   
00011     Permission is hereby granted, free of charge, to any person
00012     obtaining a copy of this software and associated documentation
00013     files (the "Software"), to deal in the Software without
00014     restriction, including without limitation the rights to use, copy,
00015     modify, merge, publish, distribute, sublicense, and/or sell copies
00016     of the Software, and to permit persons to whom the Software is
00017     furnished to do so, subject to the following conditions:
00018 
00019     The above copyright notice and this permission notice shall be
00020     included in all copies or substantial portions of the Software.
00021 
00022     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00023     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00024     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00025     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
00026     ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00027     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00028     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00029 
00030     Except as contained in this notice, the names of the Centre for
00031     Digital Music; Queen Mary, University of London; and Chris Cannam
00032     shall not be used in advertising or otherwise to promote the sale,
00033     use or other dealings in this Software without prior written
00034     authorization.
00035 */
00036 
00037 #include "PluginHostAdapter.h"
00038 #include <cstdlib>
00039 
00040 namespace Vamp
00041 {
00042 
00043 PluginHostAdapter::PluginHostAdapter(const VampPluginDescriptor *descriptor,
00044                                      float inputSampleRate) :
00045     Plugin(inputSampleRate),
00046     m_descriptor(descriptor)
00047 {
00048 //    std::cerr << "PluginHostAdapter::PluginHostAdapter (plugin = " << descriptor->name << ")" << std::endl;
00049     m_handle = m_descriptor->instantiate(m_descriptor, inputSampleRate);
00050     if (!m_handle) {
00051 //        std::cerr << "WARNING: PluginHostAdapter: Plugin instantiation failed for plugin " << m_descriptor->name << std::endl;
00052     }
00053 }
00054 
00055 PluginHostAdapter::~PluginHostAdapter()
00056 {
00057 //    std::cerr << "PluginHostAdapter::~PluginHostAdapter (plugin = " << m_descriptor->name << ")" << std::endl;
00058     if (m_handle) m_descriptor->cleanup(m_handle);
00059 }
00060 
00061 std::vector<std::string>
00062 PluginHostAdapter::getPluginPath()
00063 {
00064     std::vector<std::string> path;
00065     std::string envPath;
00066 
00067     char *cpath = getenv("VAMP_PATH");
00068     if (cpath) envPath = cpath;
00069 
00070 #ifdef _WIN32
00071 #define PATH_SEPARATOR ';'
00072 #define DEFAULT_VAMP_PATH "%ProgramFiles%\\Vamp Plugins"
00073 #else
00074 #define PATH_SEPARATOR ':'
00075 #ifdef __APPLE__
00076 #define DEFAULT_VAMP_PATH "$HOME/Library/Audio/Plug-Ins/Vamp:/Library/Audio/Plug-Ins/Vamp"
00077 #else
00078 #define DEFAULT_VAMP_PATH "$HOME/vamp:$HOME/.vamp:/usr/local/lib/vamp:/usr/lib/vamp"
00079 #endif
00080 #endif
00081 
00082     if (envPath == "") {
00083         envPath = DEFAULT_VAMP_PATH;
00084         char *chome = getenv("HOME");
00085         if (chome) {
00086             std::string home(chome);
00087             std::string::size_type f;
00088             while ((f = envPath.find("$HOME")) != std::string::npos &&
00089                     f < envPath.length()) {
00090                 envPath.replace(f, 5, home);
00091             }
00092         }
00093 #ifdef _WIN32
00094         char *cpfiles = getenv("ProgramFiles");
00095         if (!cpfiles) cpfiles = "C:\\Program Files";
00096         std::string pfiles(cpfiles);
00097         std::string::size_type f;
00098         while ((f = envPath.find("%ProgramFiles%")) != std::string::npos &&
00099                f < envPath.length()) {
00100             envPath.replace(f, 14, pfiles);
00101         }
00102 #endif
00103     }
00104 
00105     std::string::size_type index = 0, newindex = 0;
00106 
00107     while ((newindex = envPath.find(PATH_SEPARATOR, index)) < envPath.size()) {
00108         path.push_back(envPath.substr(index, newindex - index));
00109         index = newindex + 1;
00110     }
00111     
00112     path.push_back(envPath.substr(index));
00113 
00114     return path;
00115 }
00116 
00117 bool
00118 PluginHostAdapter::initialise(size_t channels,
00119                               size_t stepSize,
00120                               size_t blockSize)
00121 {
00122     if (!m_handle) return false;
00123     return m_descriptor->initialise(m_handle, channels, stepSize, blockSize) ?
00124         true : false;
00125 }
00126 
00127 void
00128 PluginHostAdapter::reset()
00129 {
00130     if (!m_handle) return;
00131     m_descriptor->reset(m_handle);
00132 }
00133 
00134 PluginHostAdapter::InputDomain
00135 PluginHostAdapter::getInputDomain() const
00136 {
00137     if (m_descriptor->inputDomain == vampFrequencyDomain) {
00138         return FrequencyDomain;
00139     } else {
00140         return TimeDomain;
00141     }
00142 }
00143 
00144 unsigned int
00145 PluginHostAdapter::getVampApiVersion() const
00146 {
00147     return m_descriptor->vampApiVersion;
00148 }
00149 
00150 std::string
00151 PluginHostAdapter::getIdentifier() const
00152 {
00153     return m_descriptor->identifier;
00154 }
00155 
00156 std::string
00157 PluginHostAdapter::getName() const
00158 {
00159     return m_descriptor->name;
00160 }
00161 
00162 std::string
00163 PluginHostAdapter::getDescription() const
00164 {
00165     return m_descriptor->description;
00166 }
00167 
00168 std::string
00169 PluginHostAdapter::getMaker() const
00170 {
00171     return m_descriptor->maker;
00172 }
00173 
00174 int
00175 PluginHostAdapter::getPluginVersion() const
00176 {
00177     return m_descriptor->pluginVersion;
00178 }
00179 
00180 std::string
00181 PluginHostAdapter::getCopyright() const
00182 {
00183     return m_descriptor->copyright;
00184 }
00185 
00186 PluginHostAdapter::ParameterList
00187 PluginHostAdapter::getParameterDescriptors() const
00188 {
00189     ParameterList list;
00190     for (unsigned int i = 0; i < m_descriptor->parameterCount; ++i) {
00191         const VampParameterDescriptor *spd = m_descriptor->parameters[i];
00192         ParameterDescriptor pd;
00193         pd.identifier = spd->identifier;
00194         pd.name = spd->name;
00195         pd.description = spd->description;
00196         pd.unit = spd->unit;
00197         pd.minValue = spd->minValue;
00198         pd.maxValue = spd->maxValue;
00199         pd.defaultValue = spd->defaultValue;
00200         pd.isQuantized = spd->isQuantized;
00201         pd.quantizeStep = spd->quantizeStep;
00202         if (pd.isQuantized && spd->valueNames) {
00203             for (unsigned int j = 0; spd->valueNames[j]; ++j) {
00204                 pd.valueNames.push_back(spd->valueNames[j]);
00205             }
00206         }
00207         list.push_back(pd);
00208     }
00209     return list;
00210 }
00211 
00212 float
00213 PluginHostAdapter::getParameter(std::string param) const
00214 {
00215     if (!m_handle) return 0.0;
00216 
00217     for (unsigned int i = 0; i < m_descriptor->parameterCount; ++i) {
00218         if (param == m_descriptor->parameters[i]->identifier) {
00219             return m_descriptor->getParameter(m_handle, i);
00220         }
00221     }
00222 
00223     return 0.0;
00224 }
00225 
00226 void
00227 PluginHostAdapter::setParameter(std::string param, 
00228                                 float value)
00229 {
00230     if (!m_handle) return;
00231 
00232     for (unsigned int i = 0; i < m_descriptor->parameterCount; ++i) {
00233         if (param == m_descriptor->parameters[i]->identifier) {
00234             m_descriptor->setParameter(m_handle, i, value);
00235             return;
00236         }
00237     }
00238 }
00239 
00240 PluginHostAdapter::ProgramList
00241 PluginHostAdapter::getPrograms() const
00242 {
00243     ProgramList list;
00244     
00245     for (unsigned int i = 0; i < m_descriptor->programCount; ++i) {
00246         list.push_back(m_descriptor->programs[i]);
00247     }
00248     
00249     return list;
00250 }
00251 
00252 std::string
00253 PluginHostAdapter::getCurrentProgram() const
00254 {
00255     if (!m_handle) return "";
00256 
00257     int pn = m_descriptor->getCurrentProgram(m_handle);
00258     return m_descriptor->programs[pn];
00259 }
00260 
00261 void
00262 PluginHostAdapter::selectProgram(std::string program)
00263 {
00264     if (!m_handle) return;
00265 
00266     for (unsigned int i = 0; i < m_descriptor->programCount; ++i) {
00267         if (program == m_descriptor->programs[i]) {
00268             m_descriptor->selectProgram(m_handle, i);
00269             return;
00270         }
00271     }
00272 }
00273 
00274 size_t
00275 PluginHostAdapter::getPreferredStepSize() const
00276 {
00277     if (!m_handle) return 0;
00278     return m_descriptor->getPreferredStepSize(m_handle);
00279 }
00280 
00281 size_t
00282 PluginHostAdapter::getPreferredBlockSize() const
00283 {
00284     if (!m_handle) return 0;
00285     return m_descriptor->getPreferredBlockSize(m_handle);
00286 }
00287 
00288 size_t
00289 PluginHostAdapter::getMinChannelCount() const
00290 {
00291     if (!m_handle) return 0;
00292     return m_descriptor->getMinChannelCount(m_handle);
00293 }
00294 
00295 size_t
00296 PluginHostAdapter::getMaxChannelCount() const
00297 {
00298     if (!m_handle) return 0;
00299     return m_descriptor->getMaxChannelCount(m_handle);
00300 }
00301 
00302 PluginHostAdapter::OutputList
00303 PluginHostAdapter::getOutputDescriptors() const
00304 {
00305     OutputList list;
00306     if (!m_handle) {
00307 //        std::cerr << "PluginHostAdapter::getOutputDescriptors: no handle " << std::endl;
00308         return list;
00309     }
00310 
00311     unsigned int count = m_descriptor->getOutputCount(m_handle);
00312 
00313     for (unsigned int i = 0; i < count; ++i) {
00314         VampOutputDescriptor *sd = m_descriptor->getOutputDescriptor(m_handle, i);
00315         OutputDescriptor d;
00316         d.identifier = sd->identifier;
00317         d.name = sd->name;
00318         d.description = sd->description;
00319         d.unit = sd->unit;
00320         d.hasFixedBinCount = sd->hasFixedBinCount;
00321         d.binCount = sd->binCount;
00322         if (d.hasFixedBinCount) {
00323             for (unsigned int j = 0; j < sd->binCount; ++j) {
00324                 d.binNames.push_back(sd->binNames[j] ? sd->binNames[j] : "");
00325             }
00326         }
00327         d.hasKnownExtents = sd->hasKnownExtents;
00328         d.minValue = sd->minValue;
00329         d.maxValue = sd->maxValue;
00330         d.isQuantized = sd->isQuantized;
00331         d.quantizeStep = sd->quantizeStep;
00332 
00333         switch (sd->sampleType) {
00334         case vampOneSamplePerStep:
00335             d.sampleType = OutputDescriptor::OneSamplePerStep; break;
00336         case vampFixedSampleRate:
00337             d.sampleType = OutputDescriptor::FixedSampleRate; break;
00338         case vampVariableSampleRate:
00339             d.sampleType = OutputDescriptor::VariableSampleRate; break;
00340         }
00341 
00342         d.sampleRate = sd->sampleRate;
00343 
00344         list.push_back(d);
00345 
00346         m_descriptor->releaseOutputDescriptor(sd);
00347     }
00348 
00349     return list;
00350 }
00351 
00352 PluginHostAdapter::FeatureSet
00353 PluginHostAdapter::process(const float *const *inputBuffers,
00354                            RealTime timestamp)
00355 {
00356     FeatureSet fs;
00357     if (!m_handle) return fs;
00358 
00359     int sec = timestamp.sec;
00360     int nsec = timestamp.nsec;
00361     
00362     VampFeatureList *features = m_descriptor->process(m_handle,
00363                                                       inputBuffers,
00364                                                       sec, nsec);
00365     
00366     convertFeatures(features, fs);
00367     m_descriptor->releaseFeatureSet(features);
00368     return fs;
00369 }
00370 
00371 PluginHostAdapter::FeatureSet
00372 PluginHostAdapter::getRemainingFeatures()
00373 {
00374     FeatureSet fs;
00375     if (!m_handle) return fs;
00376     
00377     VampFeatureList *features = m_descriptor->getRemainingFeatures(m_handle); 
00378 
00379     convertFeatures(features, fs);
00380     m_descriptor->releaseFeatureSet(features);
00381     return fs;
00382 }
00383 
00384 void
00385 PluginHostAdapter::convertFeatures(VampFeatureList *features,
00386                                    FeatureSet &fs)
00387 {
00388     if (!features) return;
00389 
00390     unsigned int outputs = m_descriptor->getOutputCount(m_handle);
00391 
00392     for (unsigned int i = 0; i < outputs; ++i) {
00393         
00394         VampFeatureList &list = features[i];
00395 
00396         if (list.featureCount > 0) {
00397 
00398             Feature feature;
00399             feature.values.reserve(list.features[0].valueCount);
00400 
00401             for (unsigned int j = 0; j < list.featureCount; ++j) {
00402 
00403                 feature.hasTimestamp = list.features[j].hasTimestamp;
00404                 feature.timestamp = RealTime(list.features[j].sec,
00405                                              list.features[j].nsec);
00406 
00407                 for (unsigned int k = 0; k < list.features[j].valueCount; ++k) {
00408                     feature.values.push_back(list.features[j].values[k]);
00409                 }
00410 
00411                 if (list.features[j].label) {
00412                     feature.label = list.features[j].label;
00413                 }
00414 
00415                 fs[i].push_back(feature);
00416 
00417                 if (list.features[j].valueCount > 0) {
00418                     feature.values.clear();
00419                 }
00420 
00421                 if (list.features[j].label) {
00422                     feature.label = "";
00423                 }
00424             }
00425         }
00426     }
00427 }
00428 
00429 }

Generated on Thu Jun 19 13:35:56 2008 for VampPluginSDK by  doxygen 1.5.5