Chris@152: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@152: Chris@152: /* Chris@152: Sonic Visualiser Chris@152: An audio file viewer and annotation editor. Chris@152: Centre for Digital Music, Queen Mary, University of London. Chris@202: This file copyright 2006 Chris Cannam and QMUL. Chris@152: Chris@152: This program is free software; you can redistribute it and/or Chris@152: modify it under the terms of the GNU General Public License as Chris@152: published by the Free Software Foundation; either version 2 of the Chris@152: License, or (at your option) any later version. See the file Chris@152: COPYING included with this distribution for more information. Chris@152: */ Chris@152: Chris@152: #include "EditableDenseThreeDimensionalModel.h" Chris@152: Chris@478: #include "base/LogRange.h" Chris@478: Chris@152: #include Chris@387: #include Chris@387: Chris@181: #include Chris@181: Chris@256: #include Chris@256: Chris@152: EditableDenseThreeDimensionalModel::EditableDenseThreeDimensionalModel(size_t sampleRate, Chris@152: size_t resolution, Chris@152: size_t yBinCount, Chris@152: bool notifyOnAdd) : Chris@152: m_sampleRate(sampleRate), Chris@152: m_resolution(resolution), Chris@152: m_yBinCount(yBinCount), Chris@152: m_minimum(0.0), Chris@152: m_maximum(0.0), Chris@256: m_haveExtents(false), Chris@152: m_notifyOnAdd(notifyOnAdd), Chris@152: m_sinceLastNotifyMin(-1), Chris@152: m_sinceLastNotifyMax(-1), Chris@152: m_completion(100) Chris@152: { Chris@152: } Chris@152: Chris@152: bool Chris@152: EditableDenseThreeDimensionalModel::isOK() const Chris@152: { Chris@152: return true; Chris@152: } Chris@152: Chris@152: size_t Chris@152: EditableDenseThreeDimensionalModel::getSampleRate() const Chris@152: { Chris@152: return m_sampleRate; Chris@152: } Chris@152: Chris@152: size_t Chris@152: EditableDenseThreeDimensionalModel::getStartFrame() const Chris@152: { Chris@152: return 0; Chris@152: } Chris@152: Chris@152: size_t Chris@152: EditableDenseThreeDimensionalModel::getEndFrame() const Chris@152: { Chris@152: return m_resolution * m_data.size() + (m_resolution - 1); Chris@152: } Chris@152: Chris@152: Model * Chris@152: EditableDenseThreeDimensionalModel::clone() const Chris@152: { Chris@152: EditableDenseThreeDimensionalModel *model = Chris@152: new EditableDenseThreeDimensionalModel Chris@152: (m_sampleRate, m_resolution, m_yBinCount); Chris@152: Chris@152: model->m_minimum = m_minimum; Chris@152: model->m_maximum = m_maximum; Chris@256: model->m_haveExtents = m_haveExtents; Chris@152: Chris@152: for (size_t i = 0; i < m_data.size(); ++i) { Chris@533: model->setColumn(i, m_data.at(i)); Chris@152: } Chris@152: Chris@152: return model; Chris@152: } Chris@152: Chris@152: size_t Chris@152: EditableDenseThreeDimensionalModel::getResolution() const Chris@152: { Chris@152: return m_resolution; Chris@152: } Chris@152: Chris@152: void Chris@152: EditableDenseThreeDimensionalModel::setResolution(size_t sz) Chris@152: { Chris@152: m_resolution = sz; Chris@152: } Chris@152: Chris@152: size_t Chris@182: EditableDenseThreeDimensionalModel::getWidth() const Chris@182: { Chris@182: return m_data.size(); Chris@182: } Chris@182: Chris@182: size_t Chris@182: EditableDenseThreeDimensionalModel::getHeight() const Chris@152: { Chris@152: return m_yBinCount; Chris@152: } Chris@152: Chris@152: void Chris@182: EditableDenseThreeDimensionalModel::setHeight(size_t sz) Chris@152: { Chris@152: m_yBinCount = sz; Chris@152: } Chris@152: Chris@152: float Chris@152: EditableDenseThreeDimensionalModel::getMinimumLevel() const Chris@152: { Chris@152: return m_minimum; Chris@152: } Chris@152: Chris@152: void Chris@152: EditableDenseThreeDimensionalModel::setMinimumLevel(float level) Chris@152: { Chris@152: m_minimum = level; Chris@152: } Chris@152: Chris@152: float Chris@152: EditableDenseThreeDimensionalModel::getMaximumLevel() const Chris@152: { Chris@152: return m_maximum; Chris@152: } Chris@152: Chris@152: void Chris@152: EditableDenseThreeDimensionalModel::setMaximumLevel(float level) Chris@152: { Chris@152: m_maximum = level; Chris@152: } Chris@152: Chris@533: EditableDenseThreeDimensionalModel::Column Chris@533: EditableDenseThreeDimensionalModel::getColumn(size_t index) const Chris@152: { Chris@152: QMutexLocker locker(&m_mutex); Chris@152: Chris@533: Column result; Chris@533: Chris@182: if (index < m_data.size()) { Chris@533: result = m_data.at(index); Chris@152: } else { Chris@152: result.clear(); Chris@152: } Chris@152: Chris@152: while (result.size() < m_yBinCount) result.push_back(m_minimum); Chris@533: return result; Chris@152: } Chris@152: Chris@152: float Chris@182: EditableDenseThreeDimensionalModel::getValueAt(size_t index, size_t n) const Chris@152: { Chris@152: QMutexLocker locker(&m_mutex); Chris@152: Chris@182: if (index < m_data.size()) { Chris@533: const Column &s = m_data.at(index); Chris@256: // std::cerr << "index " << index << ", n " << n << ", res " << m_resolution << ", size " << s.size() Chris@256: // << std::endl; Chris@533: if (n < s.size()) return s.at(n); Chris@152: } Chris@152: Chris@152: return m_minimum; Chris@152: } Chris@152: Chris@152: void Chris@182: EditableDenseThreeDimensionalModel::setColumn(size_t index, Chris@182: const Column &values) Chris@152: { Chris@152: QMutexLocker locker(&m_mutex); Chris@152: Chris@182: while (index >= m_data.size()) { Chris@182: m_data.push_back(Column()); Chris@152: } Chris@152: Chris@152: bool allChange = false; Chris@152: Chris@439: if (values.size() > m_yBinCount) m_yBinCount = values.size(); Chris@439: Chris@152: for (size_t i = 0; i < values.size(); ++i) { Chris@256: float value = values[i]; Chris@257: if (std::isnan(value) || std::isinf(value)) { Chris@256: continue; Chris@256: } Chris@256: if (!m_haveExtents || value < m_minimum) { Chris@256: m_minimum = value; Chris@152: allChange = true; Chris@152: } Chris@256: if (!m_haveExtents || value > m_maximum) { Chris@256: m_maximum = value; Chris@152: allChange = true; Chris@152: } Chris@256: m_haveExtents = true; Chris@152: } Chris@152: Chris@152: m_data[index] = values; Chris@152: Chris@182: long windowStart = index; Chris@182: windowStart *= m_resolution; Chris@182: Chris@152: if (m_notifyOnAdd) { Chris@152: if (allChange) { Chris@152: emit modelChanged(); Chris@152: } else { Chris@152: emit modelChanged(windowStart, windowStart + m_resolution); Chris@152: } Chris@152: } else { Chris@152: if (allChange) { Chris@152: m_sinceLastNotifyMin = -1; Chris@152: m_sinceLastNotifyMax = -1; Chris@152: emit modelChanged(); Chris@152: } else { Chris@152: if (m_sinceLastNotifyMin == -1 || Chris@152: windowStart < m_sinceLastNotifyMin) { Chris@152: m_sinceLastNotifyMin = windowStart; Chris@152: } Chris@152: if (m_sinceLastNotifyMax == -1 || Chris@152: windowStart > m_sinceLastNotifyMax) { Chris@152: m_sinceLastNotifyMax = windowStart; Chris@152: } Chris@152: } Chris@152: } Chris@152: } Chris@152: Chris@152: QString Chris@152: EditableDenseThreeDimensionalModel::getBinName(size_t n) const Chris@152: { Chris@152: if (m_binNames.size() > n) return m_binNames[n]; Chris@152: else return ""; Chris@152: } Chris@152: Chris@152: void Chris@152: EditableDenseThreeDimensionalModel::setBinName(size_t n, QString name) Chris@152: { Chris@152: while (m_binNames.size() <= n) m_binNames.push_back(""); Chris@152: m_binNames[n] = name; Chris@152: emit modelChanged(); Chris@152: } Chris@152: Chris@152: void Chris@152: EditableDenseThreeDimensionalModel::setBinNames(std::vector names) Chris@152: { Chris@152: m_binNames = names; Chris@152: emit modelChanged(); Chris@152: } Chris@152: Chris@478: bool Chris@478: EditableDenseThreeDimensionalModel::shouldUseLogValueScale() const Chris@478: { Chris@533: QVector sample; Chris@533: QVector n; Chris@478: Chris@478: for (int i = 0; i < 10; ++i) { Chris@478: size_t index = i * 10; Chris@478: if (index < m_data.size()) { Chris@533: const Column &c = m_data.at(index); Chris@478: while (c.size() > sample.size()) { Chris@478: sample.push_back(0.f); Chris@478: n.push_back(0); Chris@478: } Chris@478: for (int j = 0; j < c.size(); ++j) { Chris@533: sample[j] += c.at(j); Chris@478: ++n[j]; Chris@478: } Chris@478: } Chris@478: } Chris@478: Chris@478: if (sample.empty()) return false; Chris@478: for (int j = 0; j < sample.size(); ++j) { Chris@478: if (n[j]) sample[j] /= n[j]; Chris@478: } Chris@478: Chris@533: return LogRange::useLogScale(sample.toStdVector()); Chris@478: } Chris@478: Chris@152: void Chris@333: EditableDenseThreeDimensionalModel::setCompletion(int completion, bool update) Chris@152: { Chris@152: if (m_completion != completion) { Chris@152: m_completion = completion; Chris@152: Chris@152: if (completion == 100) { Chris@152: Chris@152: m_notifyOnAdd = true; // henceforth Chris@152: emit modelChanged(); Chris@152: Chris@152: } else if (!m_notifyOnAdd) { Chris@152: Chris@333: if (update && Chris@333: m_sinceLastNotifyMin >= 0 && Chris@152: m_sinceLastNotifyMax >= 0) { Chris@152: emit modelChanged(m_sinceLastNotifyMin, Chris@152: m_sinceLastNotifyMax + m_resolution); Chris@152: m_sinceLastNotifyMin = m_sinceLastNotifyMax = -1; Chris@152: } else { Chris@152: emit completionChanged(); Chris@152: } Chris@152: } else { Chris@152: emit completionChanged(); Chris@152: } Chris@152: } Chris@152: } Chris@152: Chris@318: QString Chris@318: EditableDenseThreeDimensionalModel::toDelimitedDataString(QString delimiter) const Chris@318: { Chris@318: QString s; Chris@318: for (size_t i = 0; i < m_data.size(); ++i) { Chris@318: QStringList list; Chris@533: for (size_t j = 0; j < m_data.at(i).size(); ++j) { Chris@533: list << QString("%1").arg(m_data.at(i).at(j)); Chris@318: } Chris@318: s += list.join(delimiter) + "\n"; Chris@318: } Chris@318: return s; Chris@318: } Chris@318: Chris@152: void Chris@152: EditableDenseThreeDimensionalModel::toXml(QTextStream &out, Chris@314: QString indent, Chris@314: QString extraAttributes) const Chris@152: { Chris@152: // For historical reasons we read and write "resolution" as "windowSize" Chris@152: Chris@318: std::cerr << "EditableDenseThreeDimensionalModel::toXml" << std::endl; Chris@318: Chris@314: Model::toXml Chris@314: (out, indent, Chris@314: QString("type=\"dense\" dimensions=\"3\" windowSize=\"%1\" yBinCount=\"%2\" minimum=\"%3\" maximum=\"%4\" dataset=\"%5\" %6") Chris@152: .arg(m_resolution) Chris@152: .arg(m_yBinCount) Chris@152: .arg(m_minimum) Chris@152: .arg(m_maximum) Chris@152: .arg(getObjectExportId(&m_data)) Chris@152: .arg(extraAttributes)); Chris@152: Chris@152: out << indent; Chris@152: out << QString("\n") Chris@152: .arg(getObjectExportId(&m_data)); Chris@152: Chris@152: for (size_t i = 0; i < m_binNames.size(); ++i) { Chris@152: if (m_binNames[i] != "") { Chris@152: out << indent + " "; Chris@152: out << QString("\n") Chris@152: .arg(i).arg(m_binNames[i]); Chris@152: } Chris@152: } Chris@152: Chris@152: for (size_t i = 0; i < m_data.size(); ++i) { Chris@152: out << indent + " "; Chris@152: out << QString("").arg(i); Chris@533: for (size_t j = 0; j < m_data.at(i).size(); ++j) { Chris@152: if (j > 0) out << " "; Chris@533: out << m_data.at(i).at(j); Chris@152: } Chris@152: out << QString("\n"); Chris@318: out.flush(); Chris@152: } Chris@152: Chris@152: out << indent + "\n"; Chris@152: } Chris@152: Chris@152: