comparison examples/ZeroCrossing.cpp @ 0:6479539d1b32

* Importing first cut of Sonic Visualiser's Vamp plugin format SDK
author cannam
date Fri, 31 Mar 2006 14:21:51 +0000
parents
children 8f10d35a4090
comparison
equal deleted inserted replaced
-1:000000000000 0:6479539d1b32
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Vamp
5
6 An API for audio analysis and feature extraction plugins.
7
8 Centre for Digital Music, Queen Mary, University of London.
9 Copyright 2006 Chris Cannam.
10
11 Permission is hereby granted, free of charge, to any person
12 obtaining a copy of this software and associated documentation
13 files (the "Software"), to deal in the Software without
14 restriction, including without limitation the rights to use, copy,
15 modify, merge, publish, distribute, sublicense, and/or sell copies
16 of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 Except as contained in this notice, the names of the Centre for
31 Digital Music; Queen Mary, University of London; and Chris Cannam
32 shall not be used in advertising or otherwise to promote the sale,
33 use or other dealings in this Software without prior written
34 authorization.
35 */
36
37 #include "ZeroCrossing.h"
38
39 using std::string;
40 using std::vector;
41 using std::cerr;
42 using std::endl;
43
44
45 ZeroCrossing::ZeroCrossing(float inputSampleRate) :
46 Plugin(inputSampleRate),
47 m_stepSize(0),
48 m_previousSample(0.0f)
49 {
50 }
51
52 ZeroCrossing::~ZeroCrossing()
53 {
54 }
55
56 string
57 ZeroCrossing::getName() const
58 {
59 return "zerocrossing";
60 }
61
62 string
63 ZeroCrossing::getDescription() const
64 {
65 return "Zero Crossings";
66 }
67
68 string
69 ZeroCrossing::getMaker() const
70 {
71 return "QMUL";
72 }
73
74 int
75 ZeroCrossing::getPluginVersion() const
76 {
77 return 2;
78 }
79
80 string
81 ZeroCrossing::getCopyright() const
82 {
83 return "GPL";
84 }
85
86 bool
87 ZeroCrossing::initialise(size_t channels, size_t stepSize, size_t blockSize)
88 {
89 if (channels < getMinChannelCount() ||
90 channels > getMaxChannelCount()) return false;
91
92 m_stepSize = std::min(stepSize, blockSize);
93
94 return true;
95 }
96
97 void
98 ZeroCrossing::reset()
99 {
100 m_previousSample = 0.0f;
101 }
102
103 size_t
104 ZeroCrossing::getPreferredStepSize() const
105 {
106 return 4096; // or whatever
107 }
108
109 size_t
110 ZeroCrossing::getPreferredBlockSize() const
111 {
112 return getPreferredStepSize();
113 }
114
115 ZeroCrossing::OutputList
116 ZeroCrossing::getOutputDescriptors() const
117 {
118 OutputList list;
119
120 OutputDescriptor zc;
121 zc.name = "counts";
122 zc.unit = "crossings";
123 zc.description = "Zero Crossing Counts";
124 zc.hasFixedValueCount = true;
125 zc.valueCount = 1;
126 zc.hasKnownExtents = false;
127 zc.isQuantized = true;
128 zc.quantizeStep = 1.0;
129 zc.sampleType = OutputDescriptor::OneSamplePerStep;
130 list.push_back(zc);
131
132 zc.name = "zerocrossings";
133 zc.unit = "";
134 zc.description = "Zero Crossings";
135 zc.hasFixedValueCount = true;
136 zc.valueCount = 0;
137 zc.sampleType = OutputDescriptor::VariableSampleRate;
138 zc.sampleRate = m_inputSampleRate;
139 list.push_back(zc);
140
141 return list;
142 }
143
144 ZeroCrossing::FeatureSet
145 ZeroCrossing::process(float **inputBuffers, Vamp::RealTime timestamp)
146 {
147 if (m_stepSize == 0) {
148 cerr << "ERROR: ZeroCrossing::process: "
149 << "ZeroCrossing has not been initialised"
150 << endl;
151 return FeatureSet();
152 }
153
154 float prev = m_previousSample;
155 size_t count = 0;
156
157 FeatureSet returnFeatures;
158
159 for (size_t i = 0; i < m_stepSize; ++i) {
160
161 float sample = inputBuffers[0][i];
162 bool crossing = false;
163
164 if (sample <= 0.0) {
165 if (prev > 0.0) crossing = true;
166 } else if (sample > 0.0) {
167 if (prev <= 0.0) crossing = true;
168 }
169
170 if (crossing) {
171 ++count;
172 Feature feature;
173 feature.hasTimestamp = true;
174 feature.timestamp = timestamp +
175 Vamp::RealTime::frame2RealTime(i, (size_t)m_inputSampleRate);
176 returnFeatures[1].push_back(feature);
177 }
178
179 prev = sample;
180 }
181
182 m_previousSample = prev;
183
184 Feature feature;
185 feature.hasTimestamp = false;
186 feature.values.push_back(count);
187
188 returnFeatures[0].push_back(feature);
189 return returnFeatures;
190 }
191
192 ZeroCrossing::FeatureSet
193 ZeroCrossing::getRemainingFeatures()
194 {
195 return FeatureSet();
196 }
197