fix crash during first-run configuration of the application, caused by using an incom...
[ardour.git] / libs / vamp-plugins / SimilarityPlugin.h
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
3 /*
4  * SimilarityPlugin.h
5  *
6  * Copyright 2008 Centre for Digital Music, Queen Mary, University of London.
7
8     This program is free software; you can redistribute it and/or
9     modify it under the terms of the GNU General Public License as
10     published by the Free Software Foundation; either version 2 of the
11     License, or (at your option) any later version.  See the file
12     COPYING included with this distribution for more information.
13  */
14
15 #ifndef _SIMILARITY_PLUGIN_H_
16 #define _SIMILARITY_PLUGIN_H_
17
18 #include <vamp-sdk/Plugin.h>
19 #include <vamp-sdk/RealTime.h>
20
21 #include <vector>
22 #include <deque>
23
24 class MFCC;
25 class Chromagram;
26 class Decimator;
27
28 class SimilarityPlugin : public Vamp::Plugin
29 {
30 public:
31     SimilarityPlugin(float inputSampleRate);
32     virtual ~SimilarityPlugin();
33         
34     bool initialise(size_t channels, size_t stepSize, size_t blockSize);
35     void reset();
36         
37     std::string getIdentifier() const;
38     std::string getName() const;
39     std::string getDescription() const;
40     std::string getMaker() const;
41     int getPluginVersion() const;
42     std::string getCopyright() const;
43         
44     size_t getPreferredStepSize() const;
45     size_t getPreferredBlockSize() const;
46     InputDomain getInputDomain() const { return TimeDomain; }
47     
48     size_t getMinChannelCount() const;
49     size_t getMaxChannelCount() const;
50
51     SimilarityPlugin::ParameterList getParameterDescriptors() const;
52     float getParameter(std::string param) const;
53     void setParameter(std::string param, float value);
54     
55     OutputList getOutputDescriptors() const;
56     
57     FeatureSet process(const float *const *inputBuffers, Vamp::RealTime timestamp);
58     
59     FeatureSet getRemainingFeatures();
60         
61 protected:
62     int getDecimationFactor() const;
63     
64     enum Type {
65         TypeMFCC,
66         TypeChroma
67     };
68
69     void calculateBlockSize() const;
70     bool needRhythm() const { return m_rhythmWeighting > m_noRhythm; }
71     bool needTimbre() const { return m_rhythmWeighting < m_allRhythm; }
72
73     Type m_type;
74     MFCC *m_mfcc;
75     MFCC *m_rhythmfcc;
76     Chromagram *m_chromagram;
77     Decimator *m_decimator;
78     int m_featureColumnSize;
79     float m_rhythmWeighting;
80     float m_rhythmClipDuration;
81     float m_rhythmClipOrigin;
82     int m_rhythmClipFrameSize;
83     int m_rhythmClipFrames;
84     int m_rhythmColumnSize;
85     mutable int m_blockSize; // before decimation
86     int m_fftSize; // after decimation
87     int m_channels;
88     int m_processRate;
89     int m_frameNo;
90     bool m_done;
91
92     static const float m_noRhythm;
93     static const float m_allRhythm;
94
95     std::vector<int> m_lastNonEmptyFrame; // per channel
96     std::vector<int> m_emptyFrameCount; // per channel
97
98     mutable int m_distanceMatrixOutput;
99     mutable int m_distanceVectorOutput;
100     mutable int m_sortedVectorOutput;
101     mutable int m_meansOutput;
102     mutable int m_variancesOutput;
103     mutable int m_beatSpectraOutput;
104
105     typedef std::vector<double> FeatureColumn;
106     typedef std::vector<FeatureColumn> FeatureMatrix;
107     typedef std::vector<FeatureMatrix> FeatureMatrixSet;
108
109     typedef std::deque<FeatureColumn> FeatureColumnQueue;
110     typedef std::vector<FeatureColumnQueue> FeatureQueueSet;
111
112     FeatureMatrixSet m_values;
113     FeatureQueueSet m_rhythmValues;
114
115     FeatureMatrix calculateTimbral(FeatureSet &returnFeatures);
116     FeatureMatrix calculateRhythmic(FeatureSet &returnFeatures);
117     double getDistance(const FeatureMatrix &timbral,
118                        const FeatureMatrix &rhythmic,
119                        int i, int j);
120 };
121
122 #endif
123