Transform.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006-2007 Chris Cannam and QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "Transform.h"
17 
19 
22 
23 #include <QXmlAttributes>
24 
25 #include <QDomDocument>
26 #include <QDomElement>
27 #include <QDomNamedNodeMap>
28 #include <QDomAttr>
29 
30 #include <QTextStream>
31 
32 #include <iostream>
33 
35  m_summaryType(NoSummary),
36  m_stepSize(0),
37  m_blockSize(0),
38  m_windowType(HanningWindow),
39  m_sampleRate(0)
40 {
41 }
42 
43 Transform::Transform(QString xml) :
45  m_stepSize(0),
46  m_blockSize(0),
48  m_sampleRate(0)
49 {
50  QDomDocument doc;
51 
52  QString error;
53  int errorLine;
54  int errorColumn;
55 
56  if (!doc.setContent(xml, false, &error, &errorLine, &errorColumn)) {
57  m_errorString = QString("%1 at line %2, column %3")
58  .arg(error).arg(errorLine).arg(errorColumn);
59  return;
60  }
61 
62  QDomElement transformElt = doc.firstChildElement("transform");
63  QDomNamedNodeMap attrNodes = transformElt.attributes();
64  QXmlAttributes attrs;
65 
66  for (int i = 0; i < attrNodes.length(); ++i) {
67  QDomAttr attr = attrNodes.item(i).toAttr();
68  if (!attr.isNull()) attrs.append(attr.name(), "", "", attr.value());
69  }
70 
71  setFromXmlAttributes(attrs);
72 
73  for (QDomElement paramElt = transformElt.firstChildElement("parameter");
74  !paramElt.isNull();
75  paramElt = paramElt.nextSiblingElement("parameter")) {
76 
77  QDomNamedNodeMap paramAttrs = paramElt.attributes();
78 
79  QDomAttr nameAttr = paramAttrs.namedItem("name").toAttr();
80  if (nameAttr.isNull() || nameAttr.value() == "") continue;
81 
82  QDomAttr valueAttr = paramAttrs.namedItem("value").toAttr();
83  if (valueAttr.isNull() || valueAttr.value() == "") continue;
84 
85  setParameter(nameAttr.value(), valueAttr.value().toFloat());
86  }
87 
88  for (QDomElement configElt = transformElt.firstChildElement("configuration");
89  !configElt.isNull();
90  configElt = configElt.nextSiblingElement("configuration")) {
91 
92  QDomNamedNodeMap configAttrs = configElt.attributes();
93 
94  QDomAttr nameAttr = configAttrs.namedItem("name").toAttr();
95  if (nameAttr.isNull() || nameAttr.value() == "") continue;
96 
97  QDomAttr valueAttr = configAttrs.namedItem("value").toAttr();
98  if (valueAttr.isNull() || valueAttr.value() == "") continue;
99 
100  setConfigurationValue(nameAttr.value(), valueAttr.value());
101  }
102 }
103 
105 {
106 }
107 
108 bool
110 {
111  bool identical =
112  m_id == t.m_id &&
113  m_parameters == t.m_parameters &&
115  m_program == t.m_program &&
117  m_stepSize == t.m_stepSize &&
118  m_blockSize == t.m_blockSize &&
119  m_windowType == t.m_windowType &&
120  m_startTime == t.m_startTime &&
121  m_duration == t.m_duration &&
123 /*
124  SVDEBUG << "Transform::operator==: identical = " << identical << endl;
125  cerr << "A = " << endl;
126  cerr << toXmlString() << endl;
127  cerr << "B = " << endl;
128  cerr << t.toXmlString() << endl;
129 */
130  return identical;
131 }
132 
133 bool
135 {
136  if (m_id != t.m_id) {
137  return m_id < t.m_id;
138  }
139  if (m_parameters != t.m_parameters) {
140  return mapLessThan<QString, float>(m_parameters, t.m_parameters);
141  }
142  if (m_configuration != t.m_configuration) {
143  return mapLessThan<QString, QString>(m_configuration, t.m_configuration);
144  }
145  if (m_program != t.m_program) {
146  return m_program < t.m_program;
147  }
148  if (m_summaryType != t.m_summaryType) {
149  return int(m_summaryType) < int(t.m_summaryType);
150  }
151  if (m_stepSize != t.m_stepSize) {
152  return m_stepSize < t.m_stepSize;
153  }
154  if (m_blockSize != t.m_blockSize) {
155  return m_blockSize < t.m_blockSize;
156  }
157  if (m_windowType != t.m_windowType) {
158  return m_windowType < t.m_windowType;
159  }
160  if (m_startTime != t.m_startTime) {
161  return m_startTime < t.m_startTime;
162  }
163  if (m_duration != t.m_duration) {
164  return m_duration < t.m_duration;
165  }
166  if (m_sampleRate != t.m_sampleRate) {
167  return m_sampleRate < t.m_sampleRate;
168  }
169  return false;
170 }
171 
172 void
174 {
175  m_id = id;
176 }
177 
180 {
181  return m_id;
182 }
183 
184 QString
185 Transform::createIdentifier(QString type, QString soName, QString label,
186  QString output)
187 {
188  QString pluginId = PluginIdentifier::createIdentifier(type, soName, label);
189  return pluginId + ":" + output;
190 }
191 
192 void
193 Transform::parseIdentifier(QString identifier,
194  QString &type, QString &soName,
195  QString &label, QString &output)
196 {
197  output = identifier.section(':', 3);
198  PluginIdentifier::parseIdentifier(identifier.section(':', 0, 2),
199  type, soName, label);
200 }
201 
204 {
206  return RealTimeEffect;
207  } else {
208  return FeatureExtraction;
209  }
210 }
211 
212 QString
214 {
215  return m_id.section(':', 0, 2);
216 }
217 
218 QString
220 {
221  return m_id.section(':', 3);
222 }
223 
224 void
225 Transform::setPluginIdentifier(QString pluginIdentifier)
226 {
227  m_id = pluginIdentifier + ':' + getOutput();
228 }
229 
230 void
231 Transform::setOutput(QString output)
232 {
233  m_id = getPluginIdentifier() + ':' + output;
234 }
235 
238  QString output)
239 {
240  return pluginIdentifier + ':' + output;
241 }
242 
245 {
246  return m_parameters;
247 }
248 
249 void
251 {
252  m_parameters = pm;
253 }
254 
255 void
256 Transform::setParameter(QString name, float value)
257 {
258 // SVDEBUG << "Transform::setParameter(" << name// << ") -> " << value << endl;
259  m_parameters[name] = value;
260 }
261 
264 {
265  return m_configuration;
266 }
267 
268 void
270 {
271  m_configuration = cm;
272 }
273 
274 void
275 Transform::setConfigurationValue(QString name, QString value)
276 {
277  SVDEBUG << "Transform::setConfigurationValue(" << name << ") -> " << value << endl;
278  m_configuration[name] = value;
279 }
280 
281 QString
283 {
284  return m_pluginVersion;
285 }
286 
287 void
289 {
290  m_pluginVersion = version;
291 }
292 
293 QString
295 {
296  return m_program;
297 }
298 
299 void
300 Transform::setProgram(QString program)
301 {
302  m_program = program;
303 }
304 
307 {
308  return m_summaryType;
309 }
310 
311 void
313 {
314  m_summaryType = type;
315 }
316 
317 int
319 {
320  return m_stepSize;
321 }
322 
323 void
325 {
326  m_stepSize = s;
327 }
328 
329 int
331 {
332  return m_blockSize;
333 }
334 
335 void
337 {
338  m_blockSize = s;
339 }
340 
343 {
344  return m_windowType;
345 }
346 
347 void
349 {
350  m_windowType = type;
351 }
352 
353 RealTime
355 {
356  return m_startTime;
357 }
358 
359 void
361 {
362  m_startTime = t;
363 }
364 
365 RealTime
367 {
368  return m_duration;
369 }
370 
371 void
373 {
374  m_duration = d;
375 }
376 
379 {
380  return m_sampleRate;
381 }
382 
383 void
385 {
386  m_sampleRate = rate;
387 }
388 
389 void
390 Transform::toXml(QTextStream &out, QString indent, QString extraAttributes) const
391 {
392  out << indent;
393 
394  bool haveContent = true;
395  if (m_parameters.empty() && m_configuration.empty()) haveContent = false;
396 
397  out << QString("<transform\n id=\"%1\"\n pluginVersion=\"%2\"\n program=\"%3\"\n stepSize=\"%4\"\n blockSize=\"%5\"\n windowType=\"%6\"\n startTime=\"%7\"\n duration=\"%8\"\n sampleRate=\"%9\"")
398  .arg(encodeEntities(m_id))
401  .arg(m_stepSize)
402  .arg(m_blockSize)
404  .arg(encodeEntities(m_startTime.toString().c_str()))
405  .arg(encodeEntities(m_duration.toString().c_str()))
406  .arg(m_sampleRate);
407 
408  if (m_summaryType != NoSummary) {
409  out << QString("\n summaryType=\"%1\"").arg(summaryTypeToString(m_summaryType));
410  }
411 
412  if (extraAttributes != "") {
413  out << " " << extraAttributes;
414  }
415 
416  if (haveContent) {
417 
418  out << ">\n";
419 
420  for (ParameterMap::const_iterator i = m_parameters.begin();
421  i != m_parameters.end(); ++i) {
422  out << indent << " "
423  << QString("<parameter name=\"%1\" value=\"%2\"/>\n")
424  .arg(encodeEntities(i->first))
425  .arg(i->second);
426  }
427 
428  for (ConfigurationMap::const_iterator i = m_configuration.begin();
429  i != m_configuration.end(); ++i) {
430  out << indent << " "
431  << QString("<configuration name=\"%1\" value=\"%2\"/>\n")
432  .arg(encodeEntities(i->first))
433  .arg(encodeEntities(i->second));
434  }
435 
436  out << indent << "</transform>\n";
437 
438  } else {
439 
440  out << "/>\n";
441  }
442 }
443 
446 {
447  str = str.toLower();
448  if (str == "minimum" || str == "min") return Minimum;
449  if (str == "maximum" || str == "max") return Maximum;
450  if (str == "mean") return Mean;
451  if (str == "median") return Median;
452  if (str == "mode") return Mode;
453  if (str == "sum") return Sum;
454  if (str == "variance") return Variance;
455  if (str == "standard-deviation" || str == "standardDeviation" ||
456  str == "standard deviation" || str == "sd") return StandardDeviation;
457  if (str == "count") return Count;
458  if (str == "") return NoSummary;
459  SVDEBUG << "Transform::stringToSummaryType: unknown summary type \""
460  << str << "\"" << endl;
461  return NoSummary;
462 }
463 
464 QString
466 {
467  switch (type) {
468  case Minimum: return "min";
469  case Maximum: return "max";
470  case Mean: return "mean";
471  case Median: return "median";
472  case Mode: return "mode";
473  case Sum: return "sum";
474  case Variance: return "variance";
475  case StandardDeviation: return "sd";
476  case Count: return "count";
477  case NoSummary: return "";
478  default:
479  SVDEBUG << "Transform::summaryTypeToString: unexpected summary type "
480  << int(type) << endl;
481  return "";
482  }
483 }
484 
485 void
486 Transform::setFromXmlAttributes(const QXmlAttributes &attrs)
487 {
488  if (attrs.value("id") != "") {
489  setIdentifier(attrs.value("id"));
490  }
491 
492  if (attrs.value("pluginVersion") != "") {
493  setPluginVersion(attrs.value("pluginVersion"));
494  }
495 
496  if (attrs.value("program") != "") {
497  setProgram(attrs.value("program"));
498  }
499 
500  if (attrs.value("stepSize") != "") {
501  setStepSize(attrs.value("stepSize").toInt());
502  }
503 
504  if (attrs.value("blockSize") != "") {
505  setBlockSize(attrs.value("blockSize").toInt());
506  }
507 
508  if (attrs.value("windowType") != "") {
510  (attrs.value("windowType").toStdString()));
511  }
512 
513  if (attrs.value("startTime") != "") {
514  setStartTime(RealTime::fromString(attrs.value("startTime").toStdString()));
515  }
516 
517  if (attrs.value("duration") != "") {
518  setDuration(RealTime::fromString(attrs.value("duration").toStdString()));
519  }
520 
521  if (attrs.value("sampleRate") != "") {
522  setSampleRate(attrs.value("sampleRate").toFloat());
523  }
524 
525  if (attrs.value("summaryType") != "") {
526  setSummaryType(stringToSummaryType(attrs.value("summaryType")));
527  }
528 }
529 
double sv_samplerate_t
Sample rate.
Definition: BaseTypes.h:51
sv_samplerate_t getSampleRate() const
Definition: Transform.cpp:378
sv_samplerate_t m_sampleRate
Definition: Transform.h:200
QString getOutput() const
Definition: Transform.cpp:219
void setFromXmlAttributes(const QXmlAttributes &)
Set the main transform data from the given XML attributes.
Definition: Transform.cpp:486
static RealTime fromString(std::string)
Convert a string as obtained from toString back to a RealTime object.
Definition: RealTimeSV.cpp:230
int getStepSize() const
Definition: Transform.cpp:318
Transform()
Construct a new Transform with default data and no identifier.
Definition: Transform.cpp:34
void setDuration(RealTime d)
Definition: Transform.cpp:372
RealTime getDuration() const
Definition: Transform.cpp:366
void setSampleRate(sv_samplerate_t rate)
Definition: Transform.cpp:384
void setWindowType(WindowType type)
Definition: Transform.cpp:348
Definition: Window.h:43
const ParameterMap & getParameters() const
Definition: Transform.cpp:244
Type getType() const
Definition: Transform.cpp:203
static RealTimePluginFactory * instanceFor(QString identifier)
void setPluginVersion(QString version)
Definition: Transform.cpp:288
int m_blockSize
Definition: Transform.h:196
std::map< QString, QString > ConfigurationMap
Definition: Transform.h:92
static void parseIdentifier(QString identifier, QString &type, QString &soName, QString &label)
bool operator==(const Transform &) const
Compare two Transforms.
Definition: Transform.cpp:109
void setStartTime(RealTime t)
Definition: Transform.cpp:360
std::string toString(bool align=false) const
Return a human-readable debug-type string to full precision (probably not a format to show to a user ...
Definition: RealTimeSV.cpp:213
static QString createIdentifier(QString type, QString soName, QString label)
int m_stepSize
Definition: Transform.h:195
QString getProgram() const
Definition: Transform.cpp:294
WindowType
Definition: Window.h:30
const ConfigurationMap & getConfiguration() const
Definition: Transform.cpp:263
static TransformId getIdentifierForPluginOutput(QString pluginIdentifier, QString output="")
Definition: Transform.cpp:237
ConfigurationMap m_configuration
Definition: Transform.h:191
WindowType m_windowType
Definition: Transform.h:197
RealTime m_duration
Definition: Transform.h:199
WindowType getWindowType() const
Definition: Transform.cpp:342
virtual ~Transform()
Definition: Transform.cpp:104
static QString encodeEntities(QString)
static void parseIdentifier(QString identifier, QString &type, QString &soName, QString &label, QString &output)
Definition: Transform.cpp:193
void setOutput(QString output)
Definition: Transform.cpp:231
void setIdentifier(TransformId id)
Definition: Transform.cpp:173
void setPluginIdentifier(QString pluginIdentifier)
Definition: Transform.cpp:225
bool operator<(const Transform &) const
Order two Transforms, so that they can be used as keys in containers.
Definition: Transform.cpp:134
QString m_program
Definition: Transform.h:194
SummaryType m_summaryType
Definition: Transform.h:192
void setConfigurationValue(QString name, QString value)
Definition: Transform.cpp:275
void setConfiguration(const ConfigurationMap &cm)
Definition: Transform.cpp:269
void setBlockSize(int s)
Definition: Transform.cpp:336
std::map< QString, float > ParameterMap
Definition: Transform.h:86
#define SVDEBUG
Definition: Debug.h:106
QString m_pluginVersion
Definition: Transform.h:193
QString m_errorString
Definition: Transform.h:201
QString getPluginVersion() const
Definition: Transform.cpp:282
TransformId getIdentifier() const
Definition: Transform.cpp:179
static SummaryType stringToSummaryType(QString)
Definition: Transform.cpp:445
void setSummaryType(SummaryType type)
Definition: Transform.cpp:312
static QString createIdentifier(QString type, QString soName, QString label, QString output)
Definition: Transform.cpp:185
ParameterMap m_parameters
Definition: Transform.h:190
SummaryType getSummaryType() const
Definition: Transform.cpp:306
void setParameter(QString name, float value)
Definition: Transform.cpp:256
QString getPluginIdentifier() const
Definition: Transform.cpp:213
void setProgram(QString program)
Definition: Transform.cpp:300
RealTime m_startTime
Definition: Transform.h:198
RealTime getStartTime() const
Definition: Transform.cpp:354
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const override
Stream this exportable object out to XML on a text stream.
Definition: Transform.cpp:390
QString TransformId
Definition: Transform.h:30
static QString summaryTypeToString(SummaryType)
Definition: Transform.cpp:465
void setStepSize(int s)
Definition: Transform.cpp:324
int getBlockSize() const
Definition: Transform.cpp:330
void setParameters(const ParameterMap &pm)
Definition: Transform.cpp:250
TransformId m_id
Definition: Transform.h:166
RealTime represents time values to nanosecond precision with accurate arithmetic and frame-rate conve...
Definition: RealTime.h:42