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@152: #include Chris@152: 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@182: model->setColumn(i, m_data[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@152: void Chris@182: EditableDenseThreeDimensionalModel::getColumn(size_t index, Chris@182: Column &result) const Chris@152: { Chris@152: QMutexLocker locker(&m_mutex); Chris@152: Chris@182: if (index < m_data.size()) { Chris@152: result = m_data[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@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@182: const Column &s = m_data[index]; Chris@256: // std::cerr << "index " << index << ", n " << n << ", res " << m_resolution << ", size " << s.size() Chris@256: // << std::endl; Chris@152: if (n < s.size()) return s[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@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@152: void Chris@152: EditableDenseThreeDimensionalModel::setCompletion(int completion) 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@152: if (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@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@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@152: for (size_t j = 0; j < m_data[i].size(); ++j) { Chris@152: if (j > 0) out << " "; Chris@152: out << m_data[i][j]; Chris@152: } Chris@152: out << QString("\n"); Chris@152: } Chris@152: Chris@152: out << indent + "\n"; Chris@152: } Chris@152: Chris@152: