svcore  1.9
AggregateWaveModel.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2007 QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "AggregateWaveModel.h"
00017 
00018 #include <iostream>
00019 
00020 #include <QTextStream>
00021 
00022 PowerOfSqrtTwoZoomConstraint
00023 AggregateWaveModel::m_zoomConstraint;
00024 
00025 AggregateWaveModel::AggregateWaveModel(ChannelSpecList channelSpecs) :
00026     m_components(channelSpecs)
00027 {
00028     for (ChannelSpecList::const_iterator i = channelSpecs.begin();
00029          i != channelSpecs.end(); ++i) {
00030         if (i->model->getSampleRate() !=
00031             channelSpecs.begin()->model->getSampleRate()) {
00032             SVDEBUG << "AggregateWaveModel::AggregateWaveModel: WARNING: Component models do not all have the same sample rate" << endl;
00033             break;
00034         }
00035     }
00036 }
00037 
00038 AggregateWaveModel::~AggregateWaveModel()
00039 {
00040 }
00041 
00042 bool
00043 AggregateWaveModel::isOK() const
00044 {
00045     for (ChannelSpecList::const_iterator i = m_components.begin();
00046          i != m_components.end(); ++i) {
00047         if (!i->model->isOK()) return false;
00048     }
00049     return true;
00050 }
00051 
00052 bool
00053 AggregateWaveModel::isReady(int *completion) const
00054 {
00055     if (completion) *completion = 100;
00056     bool ready = true;
00057     for (ChannelSpecList::const_iterator i = m_components.begin();
00058          i != m_components.end(); ++i) {
00059         int completionHere = 100;
00060         if (!i->model->isReady(&completionHere)) ready = false;
00061         if (completion && completionHere < *completion) {
00062             *completion = completionHere;
00063         }
00064     }
00065     return ready;
00066 }
00067 
00068 int
00069 AggregateWaveModel::getFrameCount() const
00070 {
00071     int count = 0;
00072 
00073     for (ChannelSpecList::const_iterator i = m_components.begin();
00074          i != m_components.end(); ++i) {
00075         int thisCount = i->model->getEndFrame() - i->model->getStartFrame();
00076         if (thisCount > count) count = thisCount;
00077     }
00078 
00079     return count;
00080 }
00081 
00082 int
00083 AggregateWaveModel::getChannelCount() const
00084 {
00085     return m_components.size();
00086 }
00087 
00088 int
00089 AggregateWaveModel::getSampleRate() const
00090 {
00091     if (m_components.empty()) return 0;
00092     return m_components.begin()->model->getSampleRate();
00093 }
00094 
00095 Model *
00096 AggregateWaveModel::clone() const
00097 {
00098     return new AggregateWaveModel(m_components);
00099 }
00100 
00101 int
00102 AggregateWaveModel::getData(int channel, int start, int count,
00103                             float *buffer) const
00104 {
00105     int ch0 = channel, ch1 = channel;
00106     bool mixing = false;
00107     if (channel == -1) {
00108         ch0 = 0;
00109         ch1 = getChannelCount()-1;
00110         mixing = true;
00111     }
00112 
00113     float *readbuf = buffer;
00114     if (mixing) {
00115         readbuf = new float[count];
00116         for (int i = 0; i < count; ++i) {
00117             buffer[i] = 0.f;
00118         }
00119     }
00120 
00121     int sz = count;
00122 
00123     for (int c = ch0; c <= ch1; ++c) {
00124         int szHere = 
00125             m_components[c].model->getData(m_components[c].channel,
00126                                            start, count,
00127                                            readbuf);
00128         if (szHere < sz) sz = szHere;
00129         if (mixing) {
00130             for (int i = 0; i < count; ++i) {
00131                 buffer[i] += readbuf[i];
00132             }
00133         }
00134     }
00135 
00136     if (mixing) delete[] readbuf;
00137     return sz;
00138 }
00139          
00140 int
00141 AggregateWaveModel::getData(int channel, int start, int count,
00142                             double *buffer) const
00143 {
00144     int ch0 = channel, ch1 = channel;
00145     bool mixing = false;
00146     if (channel == -1) {
00147         ch0 = 0;
00148         ch1 = getChannelCount()-1;
00149         mixing = true;
00150     }
00151 
00152     double *readbuf = buffer;
00153     if (mixing) {
00154         readbuf = new double[count];
00155         for (int i = 0; i < count; ++i) {
00156             buffer[i] = 0.0;
00157         }
00158     }
00159 
00160     int sz = count;
00161     
00162     for (int c = ch0; c <= ch1; ++c) {
00163         int szHere = 
00164             m_components[c].model->getData(m_components[c].channel,
00165                                            start, count,
00166                                            readbuf);
00167         if (szHere < sz) sz = szHere;
00168         if (mixing) {
00169             for (int i = 0; i < count; ++i) {
00170                 buffer[i] += readbuf[i];
00171             }
00172         }
00173     }
00174     
00175     if (mixing) delete[] readbuf;
00176     return sz;
00177 }
00178 
00179 int
00180 AggregateWaveModel::getData(int fromchannel, int tochannel,
00181                             int start, int count,
00182                             float **buffer) const
00183 {
00184     int min = count;
00185 
00186     for (int c = fromchannel; c <= tochannel; ++c) {
00187         int here = getData(c, start, count, buffer[c - fromchannel]);
00188         if (here < min) min = here;
00189     }
00190     
00191     return min;
00192 }
00193 
00194 int
00195 AggregateWaveModel::getSummaryBlockSize(int desired) const
00196 {
00198     return desired;
00199 }
00200         
00201 void
00202 AggregateWaveModel::getSummaries(int, int, int,
00203                                  RangeBlock &, int &) const
00204 {
00206 }
00207 
00208 AggregateWaveModel::Range
00209 AggregateWaveModel::getSummary(int, int, int) const
00210 {
00212     return Range();
00213 }
00214         
00215 int
00216 AggregateWaveModel::getComponentCount() const
00217 {
00218     return m_components.size();
00219 }
00220 
00221 AggregateWaveModel::ModelChannelSpec
00222 AggregateWaveModel::getComponent(int c) const
00223 {
00224     return m_components[c];
00225 }
00226 
00227 void
00228 AggregateWaveModel::componentModelChanged()
00229 {
00230     emit modelChanged();
00231 }
00232 
00233 void
00234 AggregateWaveModel::componentModelChangedWithin(int start, int end)
00235 {
00236     emit modelChangedWithin(start, end);
00237 }
00238 
00239 void
00240 AggregateWaveModel::componentModelCompletionChanged()
00241 {
00242     emit completionChanged();
00243 }
00244 
00245 void
00246 AggregateWaveModel::toXml(QTextStream &,
00247                           QString ,
00248                           QString ) const
00249 {
00251 }
00252