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 <iostream>
Chris@297: 
Chris@314: #include <QTextStream>
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@1038: sv_frame_t
Chris@297: AggregateWaveModel::getFrameCount() const
Chris@297: {
Chris@1038:     sv_frame_t count = 0;
Chris@297: 
Chris@297:     for (ChannelSpecList::const_iterator i = m_components.begin();
Chris@297:          i != m_components.end(); ++i) {
Chris@1038:         sv_frame_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@929: int
Chris@297: AggregateWaveModel::getChannelCount() const
Chris@297: {
Chris@1038:     return int(m_components.size());
Chris@297: }
Chris@297: 
Chris@1040: sv_samplerate_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@1038: sv_frame_t
Chris@1038: AggregateWaveModel::getData(int channel, sv_frame_t start, sv_frame_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@1038:         for (sv_frame_t i = 0; i < count; ++i) {
Chris@297:             buffer[i] = 0.f;
Chris@297:         }
Chris@297:     }
Chris@297: 
Chris@1038:     sv_frame_t longest = 0;
Chris@1008:     
Chris@297:     for (int c = ch0; c <= ch1; ++c) {
Chris@1038:         sv_frame_t here = 
Chris@300:             m_components[c].model->getData(m_components[c].channel,
Chris@300:                                            start, count,
Chris@300:                                            readbuf);
Chris@1008:         if (here > longest) {
Chris@1008:             longest = here;
Chris@1008:         }
Chris@1008:         if (here < count) {
Chris@1038:             for (sv_frame_t i = here; i < count; ++i) {
Chris@1008:                 readbuf[i] = 0.f;
Chris@1008:             }
Chris@1008:         }
Chris@297:         if (mixing) {
Chris@1038:             for (sv_frame_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@1008:     return longest;
Chris@297: }
Chris@363: 
Chris@1038: sv_frame_t
Chris@1086: AggregateWaveModel::getMultiChannelData(int fromchannel, int tochannel,
Chris@1086:                                         sv_frame_t start, sv_frame_t count,
Chris@1086:                                         float **buffer) const
Chris@363: {
Chris@1038:     sv_frame_t min = count;
Chris@363: 
Chris@929:     for (int c = fromchannel; c <= tochannel; ++c) {
Chris@1038:         sv_frame_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@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@1038: AggregateWaveModel::getSummaries(int, sv_frame_t, sv_frame_t,
Chris@929:                                  RangeBlock &, int &) const
Chris@297: {
Chris@297:     //!!! complete
Chris@297: }
Chris@297: 
Chris@297: AggregateWaveModel::Range
Chris@1038: AggregateWaveModel::getSummary(int, sv_frame_t, sv_frame_t) 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@1038:     return int(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@1038: AggregateWaveModel::componentModelChangedWithin(sv_frame_t start, sv_frame_t 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: