1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2007 QMUL.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "AlignmentModel.h"
#include "SparseTimeValueModel.h"
//#define DEBUG_ALIGNMENT_MODEL 1
AlignmentModel::AlignmentModel(Model *reference,
Model *aligned,
Model *inputModel,
SparseTimeValueModel *path) :
m_reference(reference),
m_aligned(aligned),
m_inputModel(inputModel),
m_rawPath(path),
m_path(0),
m_reversePath(0),
m_pathBegun(false),
m_pathComplete(false)
{
if (m_rawPath) {
connect(m_rawPath, SIGNAL(modelChanged()),
this, SLOT(pathChanged()));
connect(m_rawPath, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
this, SLOT(pathChangedWithin(sv_frame_t, sv_frame_t)));
connect(m_rawPath, SIGNAL(completionChanged()),
this, SLOT(pathCompletionChanged()));
constructPath();
constructReversePath();
}
if (m_rawPath && m_rawPath->isReady()) {
pathCompletionChanged();
}
}
AlignmentModel::~AlignmentModel()
{
if (m_inputModel) m_inputModel->aboutToDelete();
delete m_inputModel;
if (m_rawPath) m_rawPath->aboutToDelete();
delete m_rawPath;
if (m_path) m_path->aboutToDelete();
delete m_path;
if (m_reversePath) m_reversePath->aboutToDelete();
delete m_reversePath;
}
bool
AlignmentModel::isOK() const
{
if (m_rawPath) return m_rawPath->isOK();
else return true;
}
sv_frame_t
AlignmentModel::getStartFrame() const
{
sv_frame_t a = m_reference->getStartFrame();
sv_frame_t b = m_aligned->getStartFrame();
return std::min(a, b);
}
sv_frame_t
AlignmentModel::getEndFrame() const
{
sv_frame_t a = m_reference->getEndFrame();
sv_frame_t b = m_aligned->getEndFrame();
return std::max(a, b);
}
sv_samplerate_t
AlignmentModel::getSampleRate() const
{
return m_reference->getSampleRate();
}
bool
AlignmentModel::isReady(int *completion) const
{
if (!m_pathBegun && m_rawPath) {
if (completion) *completion = 0;
return false;
}
if (m_pathComplete) {
if (completion) *completion = 100;
return true;
}
if (!m_rawPath) {
// lack of raw path could mean path is complete (in which case
// m_pathComplete true above) or else no alignment has been
// set at all yet (this case)
if (completion) *completion = 0;
return false;
}
return m_rawPath->isReady(completion);
}
const ZoomConstraint *
AlignmentModel::getZoomConstraint() const
{
return 0;
}
const Model *
AlignmentModel::getReferenceModel() const
{
return m_reference;
}
const Model *
AlignmentModel::getAlignedModel() const
{
return m_aligned;
}
sv_frame_t
AlignmentModel::toReference(sv_frame_t frame) const
{
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "AlignmentModel::toReference(" << frame << ")" << endl;
#endif
if (!m_path) {
if (!m_rawPath) return frame;
constructPath();
}
return align(m_path, frame);
}
sv_frame_t
AlignmentModel::fromReference(sv_frame_t frame) const
{
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "AlignmentModel::fromReference(" << frame << ")" << endl;
#endif
if (!m_reversePath) {
if (!m_rawPath) return frame;
constructReversePath();
}
return align(m_reversePath, frame);
}
void
AlignmentModel::pathChanged()
{
if (m_pathComplete) {
cerr << "AlignmentModel: deleting raw path model" << endl;
if (m_rawPath) m_rawPath->aboutToDelete();
delete m_rawPath;
m_rawPath = 0;
}
}
void
AlignmentModel::pathChangedWithin(sv_frame_t, sv_frame_t)
{
if (!m_pathComplete) return;
constructPath();
constructReversePath();
}
void
AlignmentModel::pathCompletionChanged()
{
if (!m_rawPath) return;
m_pathBegun = true;
if (!m_pathComplete) {
int completion = 0;
m_rawPath->isReady(&completion);
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "AlignmentModel::pathCompletionChanged: completion = "
<< completion << endl;
#endif
m_pathComplete = (completion == 100);
if (m_pathComplete) {
constructPath();
constructReversePath();
if (m_inputModel) m_inputModel->aboutToDelete();
delete m_inputModel;
m_inputModel = 0;
}
}
emit completionChanged();
}
void
AlignmentModel::constructPath() const
{
if (!m_path) {
if (!m_rawPath) {
cerr << "ERROR: AlignmentModel::constructPath: "
<< "No raw path available" << endl;
return;
}
m_path = new PathModel
(m_rawPath->getSampleRate(), m_rawPath->getResolution(), false);
} else {
if (!m_rawPath) return;
}
m_path->clear();
SparseTimeValueModel::PointList points = m_rawPath->getPoints();
for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
i != points.end(); ++i) {
sv_frame_t frame = i->frame;
double value = i->value;
sv_frame_t rframe = lrint(value * m_aligned->getSampleRate());
m_path->addPoint(PathPoint(frame, rframe));
}
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "AlignmentModel::constructPath: " << m_path->getPointCount() << " points, at least " << (2 * m_path->getPointCount() * (3 * sizeof(void *) + sizeof(int) + sizeof(PathPoint))) << " bytes" << endl;
#endif
}
void
AlignmentModel::constructReversePath() const
{
if (!m_reversePath) {
if (!m_path) {
cerr << "ERROR: AlignmentModel::constructReversePath: "
<< "No forward path available" << endl;
return;
}
m_reversePath = new PathModel
(m_path->getSampleRate(), m_path->getResolution(), false);
} else {
if (!m_path) return;
}
m_reversePath->clear();
PathModel::PointList points = m_path->getPoints();
for (PathModel::PointList::const_iterator i = points.begin();
i != points.end(); ++i) {
sv_frame_t frame = i->frame;
sv_frame_t rframe = i->mapframe;
m_reversePath->addPoint(PathPoint(rframe, frame));
}
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "AlignmentModel::constructReversePath: " << m_reversePath->getPointCount() << " points, at least " << (2 * m_reversePath->getPointCount() * (3 * sizeof(void *) + sizeof(int) + sizeof(PathPoint))) << " bytes" << endl;
#endif
}
sv_frame_t
AlignmentModel::align(PathModel *path, sv_frame_t frame) const
{
if (!path) return frame;
// The path consists of a series of points, each with frame equal
// to the frame on the source model and mapframe equal to the
// frame on the target model. Both should be monotonically
// increasing.
const PathModel::PointList &points = path->getPoints();
if (points.empty()) {
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "AlignmentModel::align: No points" << endl;
#endif
return frame;
}
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "AlignmentModel::align: frame " << frame << " requested" << endl;
#endif
PathModel::Point point(frame);
PathModel::PointList::const_iterator i = points.lower_bound(point);
if (i == points.end()) {
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "Note: i == points.end()" << endl;
#endif
--i;
}
while (i != points.begin() && i->frame > frame) --i;
sv_frame_t foundFrame = i->frame;
sv_frame_t foundMapFrame = i->mapframe;
sv_frame_t followingFrame = foundFrame;
sv_frame_t followingMapFrame = foundMapFrame;
if (++i != points.end()) {
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "another point available" << endl;
#endif
followingFrame = i->frame;
followingMapFrame = i->mapframe;
} else {
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "no other point available" << endl;
#endif
}
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "foundFrame = " << foundFrame << ", foundMapFrame = " << foundMapFrame
<< ", followingFrame = " << followingFrame << ", followingMapFrame = "
<< followingMapFrame << endl;
#endif
if (foundMapFrame < 0) return 0;
sv_frame_t resultFrame = foundMapFrame;
if (followingFrame != foundFrame && frame > foundFrame) {
double interp =
double(frame - foundFrame) /
double(followingFrame - foundFrame);
resultFrame += lrint(double(followingMapFrame - foundMapFrame) * interp);
}
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "AlignmentModel::align: resultFrame = " << resultFrame << endl;
#endif
return resultFrame;
}
void
AlignmentModel::setPathFrom(SparseTimeValueModel *rawpath)
{
if (m_rawPath) m_rawPath->aboutToDelete();
delete m_rawPath;
m_rawPath = rawpath;
connect(m_rawPath, SIGNAL(modelChanged()),
this, SLOT(pathChanged()));
connect(m_rawPath, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
this, SLOT(pathChangedWithin(sv_frame_t, sv_frame_t)));
connect(m_rawPath, SIGNAL(completionChanged()),
this, SLOT(pathCompletionChanged()));
constructPath();
constructReversePath();
if (m_rawPath->isReady()) {
pathCompletionChanged();
}
}
void
AlignmentModel::setPath(PathModel *path)
{
if (m_path) m_path->aboutToDelete();
delete m_path;
m_path = path;
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "AlignmentModel::setPath: path = " << m_path << endl;
#endif
constructReversePath();
#ifdef DEBUG_ALIGNMENT_MODEL
cerr << "AlignmentModel::setPath: after construction path = "
<< m_path << ", rpath = " << m_reversePath << endl;
#endif
}
void
AlignmentModel::toXml(QTextStream &stream,
QString indent,
QString extraAttributes) const
{
if (!m_path) {
SVDEBUG << "AlignmentModel::toXml: no path" << endl;
return;
}
m_path->toXml(stream, indent, "");
Model::toXml(stream, indent,
QString("type=\"alignment\" reference=\"%1\" aligned=\"%2\" path=\"%3\" %4")
.arg(getObjectExportId(m_reference))
.arg(getObjectExportId(m_aligned))
.arg(getObjectExportId(m_path))
.arg(extraAttributes));
}
|