annotate data/model/EditableDenseThreeDimensionalModel.cpp @ 187:89b05b679dc3

* Fixes to update logic in writable wave file model
author Chris Cannam
date Thu, 12 Oct 2006 16:10:49 +0000
parents f75f8a1cd7b1
children 91fdc752e540
rev   line source
Chris@152 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@152 2
Chris@152 3 /*
Chris@152 4 Sonic Visualiser
Chris@152 5 An audio file viewer and annotation editor.
Chris@152 6 Centre for Digital Music, Queen Mary, University of London.
Chris@152 7 This file copyright 2006 Chris Cannam.
Chris@152 8
Chris@152 9 This program is free software; you can redistribute it and/or
Chris@152 10 modify it under the terms of the GNU General Public License as
Chris@152 11 published by the Free Software Foundation; either version 2 of the
Chris@152 12 License, or (at your option) any later version. See the file
Chris@152 13 COPYING included with this distribution for more information.
Chris@152 14 */
Chris@152 15
Chris@152 16 #include "EditableDenseThreeDimensionalModel.h"
Chris@152 17
Chris@152 18 #include <QTextStream>
Chris@152 19
Chris@181 20 #include <iostream>
Chris@181 21
Chris@152 22 EditableDenseThreeDimensionalModel::EditableDenseThreeDimensionalModel(size_t sampleRate,
Chris@152 23 size_t resolution,
Chris@152 24 size_t yBinCount,
Chris@152 25 bool notifyOnAdd) :
Chris@152 26 m_sampleRate(sampleRate),
Chris@152 27 m_resolution(resolution),
Chris@152 28 m_yBinCount(yBinCount),
Chris@152 29 m_minimum(0.0),
Chris@152 30 m_maximum(0.0),
Chris@152 31 m_notifyOnAdd(notifyOnAdd),
Chris@152 32 m_sinceLastNotifyMin(-1),
Chris@152 33 m_sinceLastNotifyMax(-1),
Chris@152 34 m_completion(100)
Chris@152 35 {
Chris@152 36 }
Chris@152 37
Chris@152 38 bool
Chris@152 39 EditableDenseThreeDimensionalModel::isOK() const
Chris@152 40 {
Chris@152 41 return true;
Chris@152 42 }
Chris@152 43
Chris@152 44 size_t
Chris@152 45 EditableDenseThreeDimensionalModel::getSampleRate() const
Chris@152 46 {
Chris@152 47 return m_sampleRate;
Chris@152 48 }
Chris@152 49
Chris@152 50 size_t
Chris@152 51 EditableDenseThreeDimensionalModel::getStartFrame() const
Chris@152 52 {
Chris@152 53 return 0;
Chris@152 54 }
Chris@152 55
Chris@152 56 size_t
Chris@152 57 EditableDenseThreeDimensionalModel::getEndFrame() const
Chris@152 58 {
Chris@152 59 return m_resolution * m_data.size() + (m_resolution - 1);
Chris@152 60 }
Chris@152 61
Chris@152 62 Model *
Chris@152 63 EditableDenseThreeDimensionalModel::clone() const
Chris@152 64 {
Chris@152 65 EditableDenseThreeDimensionalModel *model =
Chris@152 66 new EditableDenseThreeDimensionalModel
Chris@152 67 (m_sampleRate, m_resolution, m_yBinCount);
Chris@152 68
Chris@152 69 model->m_minimum = m_minimum;
Chris@152 70 model->m_maximum = m_maximum;
Chris@152 71
Chris@152 72 for (size_t i = 0; i < m_data.size(); ++i) {
Chris@182 73 model->setColumn(i, m_data[i]);
Chris@152 74 }
Chris@152 75
Chris@152 76 return model;
Chris@152 77 }
Chris@152 78
Chris@152 79 size_t
Chris@152 80 EditableDenseThreeDimensionalModel::getResolution() const
Chris@152 81 {
Chris@152 82 return m_resolution;
Chris@152 83 }
Chris@152 84
Chris@152 85 void
Chris@152 86 EditableDenseThreeDimensionalModel::setResolution(size_t sz)
Chris@152 87 {
Chris@152 88 m_resolution = sz;
Chris@152 89 }
Chris@152 90
Chris@152 91 size_t
Chris@182 92 EditableDenseThreeDimensionalModel::getWidth() const
Chris@182 93 {
Chris@182 94 return m_data.size();
Chris@182 95 }
Chris@182 96
Chris@182 97 size_t
Chris@182 98 EditableDenseThreeDimensionalModel::getHeight() const
Chris@152 99 {
Chris@152 100 return m_yBinCount;
Chris@152 101 }
Chris@152 102
Chris@152 103 void
Chris@182 104 EditableDenseThreeDimensionalModel::setHeight(size_t sz)
Chris@152 105 {
Chris@152 106 m_yBinCount = sz;
Chris@152 107 }
Chris@152 108
Chris@152 109 float
Chris@152 110 EditableDenseThreeDimensionalModel::getMinimumLevel() const
Chris@152 111 {
Chris@152 112 return m_minimum;
Chris@152 113 }
Chris@152 114
Chris@152 115 void
Chris@152 116 EditableDenseThreeDimensionalModel::setMinimumLevel(float level)
Chris@152 117 {
Chris@152 118 m_minimum = level;
Chris@152 119 }
Chris@152 120
Chris@152 121 float
Chris@152 122 EditableDenseThreeDimensionalModel::getMaximumLevel() const
Chris@152 123 {
Chris@152 124 return m_maximum;
Chris@152 125 }
Chris@152 126
Chris@152 127 void
Chris@152 128 EditableDenseThreeDimensionalModel::setMaximumLevel(float level)
Chris@152 129 {
Chris@152 130 m_maximum = level;
Chris@152 131 }
Chris@152 132
Chris@152 133 void
Chris@182 134 EditableDenseThreeDimensionalModel::getColumn(size_t index,
Chris@182 135 Column &result) const
Chris@152 136 {
Chris@152 137 QMutexLocker locker(&m_mutex);
Chris@152 138
Chris@182 139 if (index < m_data.size()) {
Chris@152 140 result = m_data[index];
Chris@152 141 } else {
Chris@152 142 result.clear();
Chris@152 143 }
Chris@152 144
Chris@152 145 while (result.size() < m_yBinCount) result.push_back(m_minimum);
Chris@152 146 }
Chris@152 147
Chris@152 148 float
Chris@182 149 EditableDenseThreeDimensionalModel::getValueAt(size_t index, size_t n) const
Chris@152 150 {
Chris@152 151 QMutexLocker locker(&m_mutex);
Chris@152 152
Chris@182 153 if (index < m_data.size()) {
Chris@182 154 const Column &s = m_data[index];
Chris@181 155 std::cerr << "index " << index << ", n " << n << ", res " << m_resolution << ", size " << s.size()
Chris@181 156 << std::endl;
Chris@152 157 if (n < s.size()) return s[n];
Chris@152 158 }
Chris@152 159
Chris@152 160 return m_minimum;
Chris@152 161 }
Chris@152 162
Chris@152 163 void
Chris@182 164 EditableDenseThreeDimensionalModel::setColumn(size_t index,
Chris@182 165 const Column &values)
Chris@152 166 {
Chris@152 167 QMutexLocker locker(&m_mutex);
Chris@152 168
Chris@182 169 while (index >= m_data.size()) {
Chris@182 170 m_data.push_back(Column());
Chris@152 171 }
Chris@152 172
Chris@152 173 bool newExtents = (m_data.empty() && (m_minimum == m_maximum));
Chris@152 174 bool allChange = false;
Chris@152 175
Chris@152 176 for (size_t i = 0; i < values.size(); ++i) {
Chris@152 177 if (newExtents || values[i] < m_minimum) {
Chris@152 178 m_minimum = values[i];
Chris@152 179 allChange = true;
Chris@152 180 }
Chris@152 181 if (newExtents || values[i] > m_maximum) {
Chris@152 182 m_maximum = values[i];
Chris@152 183 allChange = true;
Chris@152 184 }
Chris@152 185 }
Chris@152 186
Chris@152 187 m_data[index] = values;
Chris@152 188
Chris@182 189 long windowStart = index;
Chris@182 190 windowStart *= m_resolution;
Chris@182 191
Chris@152 192 if (m_notifyOnAdd) {
Chris@152 193 if (allChange) {
Chris@152 194 emit modelChanged();
Chris@152 195 } else {
Chris@152 196 emit modelChanged(windowStart, windowStart + m_resolution);
Chris@152 197 }
Chris@152 198 } else {
Chris@152 199 if (allChange) {
Chris@152 200 m_sinceLastNotifyMin = -1;
Chris@152 201 m_sinceLastNotifyMax = -1;
Chris@152 202 emit modelChanged();
Chris@152 203 } else {
Chris@152 204 if (m_sinceLastNotifyMin == -1 ||
Chris@152 205 windowStart < m_sinceLastNotifyMin) {
Chris@152 206 m_sinceLastNotifyMin = windowStart;
Chris@152 207 }
Chris@152 208 if (m_sinceLastNotifyMax == -1 ||
Chris@152 209 windowStart > m_sinceLastNotifyMax) {
Chris@152 210 m_sinceLastNotifyMax = windowStart;
Chris@152 211 }
Chris@152 212 }
Chris@152 213 }
Chris@152 214 }
Chris@152 215
Chris@152 216 QString
Chris@152 217 EditableDenseThreeDimensionalModel::getBinName(size_t n) const
Chris@152 218 {
Chris@152 219 if (m_binNames.size() > n) return m_binNames[n];
Chris@152 220 else return "";
Chris@152 221 }
Chris@152 222
Chris@152 223 void
Chris@152 224 EditableDenseThreeDimensionalModel::setBinName(size_t n, QString name)
Chris@152 225 {
Chris@152 226 while (m_binNames.size() <= n) m_binNames.push_back("");
Chris@152 227 m_binNames[n] = name;
Chris@152 228 emit modelChanged();
Chris@152 229 }
Chris@152 230
Chris@152 231 void
Chris@152 232 EditableDenseThreeDimensionalModel::setBinNames(std::vector<QString> names)
Chris@152 233 {
Chris@152 234 m_binNames = names;
Chris@152 235 emit modelChanged();
Chris@152 236 }
Chris@152 237
Chris@152 238 void
Chris@152 239 EditableDenseThreeDimensionalModel::setCompletion(int completion)
Chris@152 240 {
Chris@152 241 if (m_completion != completion) {
Chris@152 242 m_completion = completion;
Chris@152 243
Chris@152 244 if (completion == 100) {
Chris@152 245
Chris@152 246 m_notifyOnAdd = true; // henceforth
Chris@152 247 emit modelChanged();
Chris@152 248
Chris@152 249 } else if (!m_notifyOnAdd) {
Chris@152 250
Chris@152 251 if (m_sinceLastNotifyMin >= 0 &&
Chris@152 252 m_sinceLastNotifyMax >= 0) {
Chris@152 253 emit modelChanged(m_sinceLastNotifyMin,
Chris@152 254 m_sinceLastNotifyMax + m_resolution);
Chris@152 255 m_sinceLastNotifyMin = m_sinceLastNotifyMax = -1;
Chris@152 256 } else {
Chris@152 257 emit completionChanged();
Chris@152 258 }
Chris@152 259 } else {
Chris@152 260 emit completionChanged();
Chris@152 261 }
Chris@152 262 }
Chris@152 263 }
Chris@152 264
Chris@152 265 void
Chris@152 266 EditableDenseThreeDimensionalModel::toXml(QTextStream &out,
Chris@152 267 QString indent,
Chris@152 268 QString extraAttributes) const
Chris@152 269 {
Chris@152 270 // For historical reasons we read and write "resolution" as "windowSize"
Chris@152 271
Chris@152 272 out << Model::toXmlString
Chris@152 273 (indent, QString("type=\"dense\" dimensions=\"3\" windowSize=\"%1\" yBinCount=\"%2\" minimum=\"%3\" maximum=\"%4\" dataset=\"%5\" %6")
Chris@152 274 .arg(m_resolution)
Chris@152 275 .arg(m_yBinCount)
Chris@152 276 .arg(m_minimum)
Chris@152 277 .arg(m_maximum)
Chris@152 278 .arg(getObjectExportId(&m_data))
Chris@152 279 .arg(extraAttributes));
Chris@152 280
Chris@152 281 out << indent;
Chris@152 282 out << QString("<dataset id=\"%1\" dimensions=\"3\" separator=\" \">\n")
Chris@152 283 .arg(getObjectExportId(&m_data));
Chris@152 284
Chris@152 285 for (size_t i = 0; i < m_binNames.size(); ++i) {
Chris@152 286 if (m_binNames[i] != "") {
Chris@152 287 out << indent + " ";
Chris@152 288 out << QString("<bin number=\"%1\" name=\"%2\"/>\n")
Chris@152 289 .arg(i).arg(m_binNames[i]);
Chris@152 290 }
Chris@152 291 }
Chris@152 292
Chris@152 293 for (size_t i = 0; i < m_data.size(); ++i) {
Chris@152 294 out << indent + " ";
Chris@152 295 out << QString("<row n=\"%1\">").arg(i);
Chris@152 296 for (size_t j = 0; j < m_data[i].size(); ++j) {
Chris@152 297 if (j > 0) out << " ";
Chris@152 298 out << m_data[i][j];
Chris@152 299 }
Chris@152 300 out << QString("</row>\n");
Chris@152 301 }
Chris@152 302
Chris@152 303 out << indent + "</dataset>\n";
Chris@152 304 }
Chris@152 305
Chris@152 306 QString
Chris@152 307 EditableDenseThreeDimensionalModel::toXmlString(QString indent,
Chris@152 308 QString extraAttributes) const
Chris@152 309 {
Chris@152 310 QString s;
Chris@152 311
Chris@152 312 {
Chris@152 313 QTextStream out(&s);
Chris@152 314 toXml(out, indent, extraAttributes);
Chris@152 315 }
Chris@152 316
Chris@152 317 return s;
Chris@152 318 }
Chris@152 319
Chris@152 320 #ifdef INCLUDE_MOCFILES
Chris@152 321 #include "EditableDenseThreeDimensionalModel.moc.cpp"
Chris@152 322 #endif
Chris@152 323