annotate data/model/AggregateWaveModel.cpp @ 300:5877d68815c7

* Change WaveFileModel API from getValues(start,end) to getData(start,count). It's much less error-prone to pass in frame counts instead of start/end locations. Should have done this ages ago. This closes #1794563. * Add option to apply a transform to only the selection region, instead of the whole audio. * (to make the above work properly) Add start frame offset to wave models
author Chris Cannam
date Mon, 01 Oct 2007 13:48:38 +0000
parents c022976d18e8
children 70a232b1f12a
rev   line source
Chris@297 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@297 2
Chris@297 3 /*
Chris@297 4 Sonic Visualiser
Chris@297 5 An audio file viewer and annotation editor.
Chris@297 6 Centre for Digital Music, Queen Mary, University of London.
Chris@297 7 This file copyright 2007 QMUL.
Chris@297 8
Chris@297 9 This program is free software; you can redistribute it and/or
Chris@297 10 modify it under the terms of the GNU General Public License as
Chris@297 11 published by the Free Software Foundation; either version 2 of the
Chris@297 12 License, or (at your option) any later version. See the file
Chris@297 13 COPYING included with this distribution for more information.
Chris@297 14 */
Chris@297 15
Chris@297 16 #include "AggregateWaveModel.h"
Chris@297 17
Chris@297 18 #include <iostream>
Chris@297 19
Chris@297 20 PowerOfSqrtTwoZoomConstraint
Chris@297 21 AggregateWaveModel::m_zoomConstraint;
Chris@297 22
Chris@297 23 AggregateWaveModel::AggregateWaveModel(ChannelSpecList channelSpecs) :
Chris@297 24 m_components(channelSpecs)
Chris@297 25 {
Chris@297 26 for (ChannelSpecList::const_iterator i = channelSpecs.begin();
Chris@297 27 i != channelSpecs.end(); ++i) {
Chris@297 28 if (i->model->getSampleRate() !=
Chris@297 29 channelSpecs.begin()->model->getSampleRate()) {
Chris@297 30 std::cerr << "AggregateWaveModel::AggregateWaveModel: WARNING: Component models do not all have the same sample rate" << std::endl;
Chris@297 31 break;
Chris@297 32 }
Chris@297 33 }
Chris@297 34 }
Chris@297 35
Chris@297 36 AggregateWaveModel::~AggregateWaveModel()
Chris@297 37 {
Chris@297 38 }
Chris@297 39
Chris@297 40 bool
Chris@297 41 AggregateWaveModel::isOK() const
Chris@297 42 {
Chris@297 43 for (ChannelSpecList::const_iterator i = m_components.begin();
Chris@297 44 i != m_components.end(); ++i) {
Chris@297 45 if (!i->model->isOK()) return false;
Chris@297 46 }
Chris@297 47 return true;
Chris@297 48 }
Chris@297 49
Chris@297 50 bool
Chris@297 51 AggregateWaveModel::isReady(int *completion) const
Chris@297 52 {
Chris@297 53 if (completion) *completion = 100;
Chris@297 54 bool ready = true;
Chris@297 55 for (ChannelSpecList::const_iterator i = m_components.begin();
Chris@297 56 i != m_components.end(); ++i) {
Chris@297 57 int completionHere = 100;
Chris@297 58 if (!i->model->isReady(&completionHere)) ready = false;
Chris@297 59 if (completion && completionHere < *completion) {
Chris@297 60 *completion = completionHere;
Chris@297 61 }
Chris@297 62 }
Chris@297 63 return ready;
Chris@297 64 }
Chris@297 65
Chris@297 66 size_t
Chris@297 67 AggregateWaveModel::getFrameCount() const
Chris@297 68 {
Chris@297 69 size_t count = 0;
Chris@297 70
Chris@297 71 for (ChannelSpecList::const_iterator i = m_components.begin();
Chris@297 72 i != m_components.end(); ++i) {
Chris@297 73 size_t thisCount = i->model->getEndFrame() - i->model->getStartFrame();
Chris@297 74 if (thisCount > count) count = thisCount;
Chris@297 75 }
Chris@297 76
Chris@297 77 return count;
Chris@297 78 }
Chris@297 79
Chris@297 80 size_t
Chris@297 81 AggregateWaveModel::getChannelCount() const
Chris@297 82 {
Chris@297 83 return m_components.size();
Chris@297 84 }
Chris@297 85
Chris@297 86 size_t
Chris@297 87 AggregateWaveModel::getSampleRate() const
Chris@297 88 {
Chris@297 89 if (m_components.empty()) return 0;
Chris@297 90 return m_components.begin()->model->getSampleRate();
Chris@297 91 }
Chris@297 92
Chris@297 93 Model *
Chris@297 94 AggregateWaveModel::clone() const
Chris@297 95 {
Chris@297 96 return new AggregateWaveModel(m_components);
Chris@297 97 }
Chris@297 98
Chris@297 99 size_t
Chris@300 100 AggregateWaveModel::getData(int channel, size_t start, size_t count,
Chris@300 101 float *buffer) const
Chris@297 102 {
Chris@297 103 int ch0 = channel, ch1 = channel;
Chris@297 104 bool mixing = false;
Chris@297 105 if (channel == -1) {
Chris@297 106 ch0 = 0;
Chris@297 107 ch1 = getChannelCount()-1;
Chris@297 108 mixing = true;
Chris@297 109 }
Chris@297 110
Chris@297 111 float *readbuf = buffer;
Chris@297 112 if (mixing) {
Chris@300 113 readbuf = new float[count];
Chris@300 114 for (size_t i = 0; i < count; ++i) {
Chris@297 115 buffer[i] = 0.f;
Chris@297 116 }
Chris@297 117 }
Chris@297 118
Chris@300 119 size_t sz = count;
Chris@300 120
Chris@297 121 for (int c = ch0; c <= ch1; ++c) {
Chris@297 122 size_t szHere =
Chris@300 123 m_components[c].model->getData(m_components[c].channel,
Chris@300 124 start, count,
Chris@300 125 readbuf);
Chris@297 126 if (szHere < sz) sz = szHere;
Chris@297 127 if (mixing) {
Chris@300 128 for (size_t i = 0; i < count; ++i) {
Chris@297 129 buffer[i] += readbuf[i];
Chris@297 130 }
Chris@297 131 }
Chris@297 132 }
Chris@297 133
Chris@297 134 if (mixing) delete[] readbuf;
Chris@297 135 return sz;
Chris@297 136 }
Chris@297 137
Chris@297 138 size_t
Chris@300 139 AggregateWaveModel::getData(int channel, size_t start, size_t count,
Chris@300 140 double *buffer) const
Chris@297 141 {
Chris@297 142 int ch0 = channel, ch1 = channel;
Chris@297 143 bool mixing = false;
Chris@297 144 if (channel == -1) {
Chris@297 145 ch0 = 0;
Chris@297 146 ch1 = getChannelCount()-1;
Chris@297 147 mixing = true;
Chris@297 148 }
Chris@297 149
Chris@297 150 double *readbuf = buffer;
Chris@297 151 if (mixing) {
Chris@300 152 readbuf = new double[count];
Chris@300 153 for (size_t i = 0; i < count; ++i) {
Chris@300 154 buffer[i] = 0.0;
Chris@297 155 }
Chris@297 156 }
Chris@297 157
Chris@300 158 size_t sz = count;
Chris@297 159
Chris@297 160 for (int c = ch0; c <= ch1; ++c) {
Chris@297 161 size_t szHere =
Chris@300 162 m_components[c].model->getData(m_components[c].channel,
Chris@300 163 start, count,
Chris@300 164 readbuf);
Chris@297 165 if (szHere < sz) sz = szHere;
Chris@297 166 if (mixing) {
Chris@300 167 for (size_t i = 0; i < count; ++i) {
Chris@297 168 buffer[i] += readbuf[i];
Chris@297 169 }
Chris@297 170 }
Chris@297 171 }
Chris@297 172
Chris@297 173 if (mixing) delete[] readbuf;
Chris@297 174 return sz;
Chris@297 175 }
Chris@297 176
Chris@297 177 void
Chris@300 178 AggregateWaveModel::getSummaries(size_t channel, size_t start, size_t count,
Chris@300 179 RangeBlock &ranges, size_t &blockSize) const
Chris@297 180 {
Chris@297 181 //!!! complete
Chris@297 182 }
Chris@297 183
Chris@297 184 AggregateWaveModel::Range
Chris@300 185 AggregateWaveModel::getSummary(size_t channel, size_t start, size_t count) const
Chris@297 186 {
Chris@297 187 //!!! complete
Chris@297 188 return Range();
Chris@297 189 }
Chris@297 190
Chris@297 191 size_t
Chris@297 192 AggregateWaveModel::getComponentCount() const
Chris@297 193 {
Chris@297 194 return m_components.size();
Chris@297 195 }
Chris@297 196
Chris@297 197 AggregateWaveModel::ModelChannelSpec
Chris@297 198 AggregateWaveModel::getComponent(size_t c) const
Chris@297 199 {
Chris@297 200 return m_components[c];
Chris@297 201 }
Chris@297 202
Chris@297 203 void
Chris@297 204 AggregateWaveModel::componentModelChanged()
Chris@297 205 {
Chris@297 206 emit modelChanged();
Chris@297 207 }
Chris@297 208
Chris@297 209 void
Chris@297 210 AggregateWaveModel::componentModelChanged(size_t start, size_t end)
Chris@297 211 {
Chris@297 212 emit modelChanged(start, end);
Chris@297 213 }
Chris@297 214
Chris@297 215 void
Chris@297 216 AggregateWaveModel::componentModelCompletionChanged()
Chris@297 217 {
Chris@297 218 emit completionChanged();
Chris@297 219 }
Chris@297 220
Chris@297 221 void
Chris@297 222 AggregateWaveModel::toXml(QTextStream &out,
Chris@297 223 QString indent,
Chris@297 224 QString extraAttributes) const
Chris@297 225 {
Chris@297 226 //!!! complete
Chris@297 227 }
Chris@297 228