Chris@297: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@297: Chris@297: /* Chris@297: Sonic Visualiser Chris@297: An audio file viewer and annotation editor. Chris@297: Centre for Digital Music, Queen Mary, University of London. Chris@297: This file copyright 2007 QMUL. Chris@297: Chris@297: This program is free software; you can redistribute it and/or Chris@297: modify it under the terms of the GNU General Public License as Chris@297: published by the Free Software Foundation; either version 2 of the Chris@297: License, or (at your option) any later version. See the file Chris@297: COPYING included with this distribution for more information. Chris@297: */ Chris@297: Chris@297: #include "AggregateWaveModel.h" Chris@297: Chris@297: #include Chris@297: Chris@314: #include Chris@314: Chris@297: PowerOfSqrtTwoZoomConstraint Chris@297: AggregateWaveModel::m_zoomConstraint; Chris@297: Chris@297: AggregateWaveModel::AggregateWaveModel(ChannelSpecList channelSpecs) : Chris@297: m_components(channelSpecs) Chris@297: { Chris@297: for (ChannelSpecList::const_iterator i = channelSpecs.begin(); Chris@297: i != channelSpecs.end(); ++i) { Chris@297: if (i->model->getSampleRate() != Chris@297: channelSpecs.begin()->model->getSampleRate()) { Chris@690: SVDEBUG << "AggregateWaveModel::AggregateWaveModel: WARNING: Component models do not all have the same sample rate" << endl; Chris@297: break; Chris@297: } Chris@297: } Chris@297: } Chris@297: Chris@297: AggregateWaveModel::~AggregateWaveModel() Chris@297: { Chris@297: } Chris@297: Chris@297: bool Chris@297: AggregateWaveModel::isOK() const Chris@297: { Chris@297: for (ChannelSpecList::const_iterator i = m_components.begin(); Chris@297: i != m_components.end(); ++i) { Chris@297: if (!i->model->isOK()) return false; Chris@297: } Chris@297: return true; Chris@297: } Chris@297: Chris@297: bool Chris@297: AggregateWaveModel::isReady(int *completion) const Chris@297: { Chris@297: if (completion) *completion = 100; Chris@297: bool ready = true; Chris@297: for (ChannelSpecList::const_iterator i = m_components.begin(); Chris@297: i != m_components.end(); ++i) { Chris@297: int completionHere = 100; Chris@297: if (!i->model->isReady(&completionHere)) ready = false; Chris@297: if (completion && completionHere < *completion) { Chris@297: *completion = completionHere; Chris@297: } Chris@297: } Chris@297: return ready; Chris@297: } Chris@297: Chris@929: int Chris@297: AggregateWaveModel::getFrameCount() const Chris@297: { Chris@929: int count = 0; Chris@297: Chris@297: for (ChannelSpecList::const_iterator i = m_components.begin(); Chris@297: i != m_components.end(); ++i) { Chris@929: int thisCount = i->model->getEndFrame() - i->model->getStartFrame(); Chris@297: if (thisCount > count) count = thisCount; Chris@297: } Chris@297: Chris@297: return count; Chris@297: } Chris@297: Chris@929: int Chris@297: AggregateWaveModel::getChannelCount() const Chris@297: { Chris@297: return m_components.size(); Chris@297: } Chris@297: Chris@929: int Chris@297: AggregateWaveModel::getSampleRate() const Chris@297: { Chris@297: if (m_components.empty()) return 0; Chris@297: return m_components.begin()->model->getSampleRate(); Chris@297: } Chris@297: Chris@297: Model * Chris@297: AggregateWaveModel::clone() const Chris@297: { Chris@297: return new AggregateWaveModel(m_components); Chris@297: } Chris@297: Chris@929: int Chris@929: AggregateWaveModel::getData(int channel, int start, int count, Chris@300: float *buffer) const Chris@297: { Chris@297: int ch0 = channel, ch1 = channel; Chris@297: bool mixing = false; Chris@297: if (channel == -1) { Chris@297: ch0 = 0; Chris@297: ch1 = getChannelCount()-1; Chris@297: mixing = true; Chris@297: } Chris@297: Chris@297: float *readbuf = buffer; Chris@297: if (mixing) { Chris@300: readbuf = new float[count]; Chris@929: for (int i = 0; i < count; ++i) { Chris@297: buffer[i] = 0.f; Chris@297: } Chris@297: } Chris@297: Chris@929: int sz = count; Chris@300: Chris@297: for (int c = ch0; c <= ch1; ++c) { Chris@929: int szHere = Chris@300: m_components[c].model->getData(m_components[c].channel, Chris@300: start, count, Chris@300: readbuf); Chris@297: if (szHere < sz) sz = szHere; Chris@297: if (mixing) { Chris@929: for (int i = 0; i < count; ++i) { Chris@297: buffer[i] += readbuf[i]; Chris@297: } Chris@297: } Chris@297: } Chris@297: Chris@297: if (mixing) delete[] readbuf; Chris@297: return sz; Chris@297: } Chris@297: Chris@929: int Chris@929: AggregateWaveModel::getData(int channel, int start, int count, Chris@300: double *buffer) const Chris@297: { Chris@297: int ch0 = channel, ch1 = channel; Chris@297: bool mixing = false; Chris@297: if (channel == -1) { Chris@297: ch0 = 0; Chris@297: ch1 = getChannelCount()-1; Chris@297: mixing = true; Chris@297: } Chris@297: Chris@297: double *readbuf = buffer; Chris@297: if (mixing) { Chris@300: readbuf = new double[count]; Chris@929: for (int i = 0; i < count; ++i) { Chris@300: buffer[i] = 0.0; Chris@297: } Chris@297: } Chris@297: Chris@929: int sz = count; Chris@297: Chris@297: for (int c = ch0; c <= ch1; ++c) { Chris@929: int szHere = Chris@300: m_components[c].model->getData(m_components[c].channel, Chris@300: start, count, Chris@300: readbuf); Chris@297: if (szHere < sz) sz = szHere; Chris@297: if (mixing) { Chris@929: for (int i = 0; i < count; ++i) { Chris@297: buffer[i] += readbuf[i]; Chris@297: } Chris@297: } Chris@297: } Chris@297: Chris@297: if (mixing) delete[] readbuf; Chris@297: return sz; Chris@297: } Chris@363: Chris@929: int Chris@929: AggregateWaveModel::getData(int fromchannel, int tochannel, Chris@929: int start, int count, Chris@363: float **buffer) const Chris@363: { Chris@929: int min = count; Chris@363: Chris@929: for (int c = fromchannel; c <= tochannel; ++c) { Chris@929: int here = getData(c, start, count, buffer[c - fromchannel]); Chris@363: if (here < min) min = here; Chris@363: } Chris@363: Chris@363: return min; Chris@363: } Chris@377: Chris@929: int Chris@929: AggregateWaveModel::getSummaryBlockSize(int desired) const Chris@377: { Chris@377: //!!! complete Chris@377: return desired; Chris@377: } Chris@297: Chris@297: void Chris@929: AggregateWaveModel::getSummaries(int, int, int, Chris@929: RangeBlock &, int &) const Chris@297: { Chris@297: //!!! complete Chris@297: } Chris@297: Chris@297: AggregateWaveModel::Range Chris@929: AggregateWaveModel::getSummary(int, int, int) const Chris@297: { Chris@297: //!!! complete Chris@297: return Range(); Chris@297: } Chris@297: Chris@929: int Chris@297: AggregateWaveModel::getComponentCount() const Chris@297: { Chris@297: return m_components.size(); Chris@297: } Chris@297: Chris@297: AggregateWaveModel::ModelChannelSpec Chris@929: AggregateWaveModel::getComponent(int c) const Chris@297: { Chris@297: return m_components[c]; Chris@297: } Chris@297: Chris@297: void Chris@297: AggregateWaveModel::componentModelChanged() Chris@297: { Chris@297: emit modelChanged(); Chris@297: } Chris@297: Chris@297: void Chris@947: AggregateWaveModel::componentModelChangedWithin(int start, int end) Chris@297: { Chris@947: emit modelChangedWithin(start, end); Chris@297: } Chris@297: Chris@297: void Chris@297: AggregateWaveModel::componentModelCompletionChanged() Chris@297: { Chris@297: emit completionChanged(); Chris@297: } Chris@297: Chris@297: void Chris@929: AggregateWaveModel::toXml(QTextStream &, Chris@929: QString , Chris@929: QString ) const Chris@297: { Chris@297: //!!! complete Chris@297: } Chris@297: