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@297: std::cerr << "AggregateWaveModel::AggregateWaveModel: WARNING: Component models do not all have the same sample rate" << std::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@297: size_t Chris@297: AggregateWaveModel::getFrameCount() const Chris@297: { Chris@297: size_t count = 0; Chris@297: Chris@297: for (ChannelSpecList::const_iterator i = m_components.begin(); Chris@297: i != m_components.end(); ++i) { Chris@297: size_t 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@297: size_t Chris@297: AggregateWaveModel::getChannelCount() const Chris@297: { Chris@297: return m_components.size(); Chris@297: } Chris@297: Chris@297: size_t 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@297: size_t Chris@300: AggregateWaveModel::getData(int channel, size_t start, size_t 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@300: for (size_t i = 0; i < count; ++i) { Chris@297: buffer[i] = 0.f; Chris@297: } Chris@297: } Chris@297: Chris@300: size_t sz = count; Chris@300: Chris@297: for (int c = ch0; c <= ch1; ++c) { Chris@297: size_t 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@300: for (size_t 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@297: size_t Chris@300: AggregateWaveModel::getData(int channel, size_t start, size_t 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@300: for (size_t i = 0; i < count; ++i) { Chris@300: buffer[i] = 0.0; Chris@297: } Chris@297: } Chris@297: Chris@300: size_t sz = count; Chris@297: Chris@297: for (int c = ch0; c <= ch1; ++c) { Chris@297: size_t 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@300: for (size_t 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@363: size_t Chris@363: AggregateWaveModel::getData(size_t fromchannel, size_t tochannel, Chris@363: size_t start, size_t count, Chris@363: float **buffer) const Chris@363: { Chris@363: size_t min = count; Chris@363: Chris@363: for (size_t c = fromchannel; c <= tochannel; ++c) { Chris@363: size_t 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@297: Chris@297: void Chris@300: AggregateWaveModel::getSummaries(size_t channel, size_t start, size_t count, Chris@300: RangeBlock &ranges, size_t &blockSize) const Chris@297: { Chris@297: //!!! complete Chris@297: } Chris@297: Chris@297: AggregateWaveModel::Range Chris@300: AggregateWaveModel::getSummary(size_t channel, size_t start, size_t count) const Chris@297: { Chris@297: //!!! complete Chris@297: return Range(); Chris@297: } Chris@297: Chris@297: size_t Chris@297: AggregateWaveModel::getComponentCount() const Chris@297: { Chris@297: return m_components.size(); Chris@297: } Chris@297: Chris@297: AggregateWaveModel::ModelChannelSpec Chris@297: AggregateWaveModel::getComponent(size_t 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@297: AggregateWaveModel::componentModelChanged(size_t start, size_t end) Chris@297: { Chris@297: emit modelChanged(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@297: AggregateWaveModel::toXml(QTextStream &out, Chris@297: QString indent, Chris@297: QString extraAttributes) const Chris@297: { Chris@297: //!!! complete Chris@297: } Chris@297: