svcore  1.9
Model.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "Model.h"
17 #include "AlignmentModel.h"
18 
19 #include <QTextStream>
20 
21 #include <iostream>
22 
23 const int Model::COMPLETION_UNKNOWN = -1;
24 
26 {
27 // SVDEBUG << "Model::~Model(" << this << ")" << endl;
28 
29  if (!m_aboutToDelete) {
30  SVDEBUG << "NOTE: Model::~Model(" << this << ", \""
31  << objectName() << "\"): Model deleted "
32  << "with no aboutToDelete notification" << endl;
33  }
34 
35  if (m_alignment) {
37  delete m_alignment;
38  }
39 }
40 
41 void
43 {
44  if (m_sourceModel) {
45  disconnect(m_sourceModel, SIGNAL(aboutToBeDeleted()),
46  this, SLOT(sourceModelAboutToBeDeleted()));
47  }
48 
49  m_sourceModel = model;
50 
51  if (m_sourceModel) {
52  connect(m_sourceModel, SIGNAL(alignmentCompletionChanged()),
53  this, SIGNAL(alignmentCompletionChanged()));
54  connect(m_sourceModel, SIGNAL(aboutToBeDeleted()),
55  this, SLOT(sourceModelAboutToBeDeleted()));
56  }
57 }
58 
59 void
61 {
62 // cerr << "Model(" << this << ")::aboutToDelete()" << endl;
63 
64  if (m_aboutToDelete) {
65  cerr << "WARNING: Model(" << this << ", \""
66  << objectName() << "\")::aboutToDelete: "
67  << "aboutToDelete called more than once for the same model"
68  << endl;
69  }
70 
71  emit aboutToBeDeleted();
72  m_aboutToDelete = true;
73 }
74 
75 void
77 {
78  m_sourceModel = 0;
79 }
80 
81 void
83 {
84  if (m_alignment) {
86  delete m_alignment;
87  }
88  m_alignment = alignment;
89  connect(m_alignment, SIGNAL(completionChanged()),
90  this, SIGNAL(alignmentCompletionChanged()));
91 }
92 
93 const AlignmentModel *
95 {
96  return m_alignment;
97 }
98 
99 const Model *
101 {
102  if (!m_alignment) {
104  return 0;
105  }
106  return m_alignment->getReferenceModel();
107 }
108 
109 int
110 Model::alignToReference(int frame) const
111 {
112 // cerr << "Model(" << this << ")::alignToReference(" << frame << ")" << endl;
113  if (!m_alignment) {
114  if (m_sourceModel) return m_sourceModel->alignToReference(frame);
115  else return frame;
116  }
117  int refFrame = m_alignment->toReference(frame);
118  const Model *m = m_alignment->getReferenceModel();
119  if (m && refFrame > m->getEndFrame()) refFrame = m->getEndFrame();
120 // cerr << "have alignment, aligned is " << refFrame << endl;
121  return refFrame;
122 }
123 
124 int
125 Model::alignFromReference(int refFrame) const
126 {
127 // cerr << "Model(" << this << ")::alignFromReference(" << refFrame << ")" << endl;
128  if (!m_alignment) {
129  if (m_sourceModel) return m_sourceModel->alignFromReference(refFrame);
130  else return refFrame;
131  }
132  int frame = m_alignment->fromReference(refFrame);
133  if (frame > getEndFrame()) frame = getEndFrame();
134 // cerr << "have alignment, aligned is " << frame << endl;
135  return frame;
136 }
137 
138 int
140 {
141 // SVDEBUG << "Model::getAlignmentCompletion" << endl;
142  if (!m_alignment) {
144  else return 100;
145  }
146  int completion = 0;
147  (void)m_alignment->isReady(&completion);
148 // cerr << " -> " << completion << endl;
149  return completion;
150 }
151 
152 QString
154 {
155  if (m_sourceModel) return m_sourceModel->getTitle();
156  else return "";
157 }
158 
159 QString
161 {
162  if (m_sourceModel) return m_sourceModel->getMaker();
163  else return "";
164 }
165 
166 QString
168 {
169  if (m_sourceModel) return m_sourceModel->getLocation();
170  else return "";
171 }
172 
173 void
174 Model::toXml(QTextStream &stream, QString indent,
175  QString extraAttributes) const
176 {
177  stream << indent;
178  stream << QString("<model id=\"%1\" name=\"%2\" sampleRate=\"%3\" start=\"%4\" end=\"%5\" %6/>\n")
179  .arg(getObjectExportId(this))
180  .arg(encodeEntities(objectName()))
181  .arg(getSampleRate())
182  .arg(getStartFrame())
183  .arg(getEndFrame())
184  .arg(extraAttributes);
185 }
186 
187 
void aboutToBeDeleted()
Emitted when something notifies this model (through calling aboutToDelete() that it is about to delet...
virtual ~Model()
Definition: Model.cpp:25
virtual QString getMaker() const
Return the "artist" or "maker" of the model, if known.
Definition: Model.cpp:160
int fromReference(int frame) const
static const int COMPLETION_UNKNOWN
Definition: Model.h:147
bool m_aboutToDelete
Definition: Model.h:305
virtual int getStartFrame() const =0
Return the first audio frame spanned by the model.
AlignmentModel * m_alignment
Definition: Model.h:302
void completionChanged()
Emitted when some internal processing has advanced a stage, but the model has not changed externally.
static int getObjectExportId(const void *)
int toReference(int frame) const
virtual QString getLocation() const
Return the location of the data in this model (e.g.
Definition: Model.cpp:167
void alignmentCompletionChanged()
Emitted when the completion percentage changes for the calculation of this model's alignment model.
virtual void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Stream this exportable object out to XML on a text stream.
Definition: Model.cpp:174
static QString encodeEntities(QString)
virtual const AlignmentModel * getAlignment() const
Retrieve the alignment model for this model.
Definition: Model.cpp:94
virtual void setSourceModel(Model *model)
Set the source model for this model.
Definition: Model.cpp:42
virtual int alignFromReference(int referenceFrame) const
Return the frame number in this model that corresponds to the given frame number of the reference mod...
Definition: Model.cpp:125
virtual void setAlignment(AlignmentModel *alignment)
Specify an aligment between this model's timeline and that of a reference model.
Definition: Model.cpp:82
virtual int getSampleRate() const =0
Return the frame rate in frames per second.
Model is the base class for all data models that represent any sort of data on a time scale based on ...
Definition: Model.h:35
virtual bool isReady(int *completion=0) const
Return true if the model has finished loading or calculating all its data, for a model that is capabl...
virtual int alignToReference(int frame) const
Return the frame number of the reference model that corresponds to the given frame number in this mod...
Definition: Model.cpp:110
#define SVDEBUG
Definition: Debug.h:42
virtual int getEndFrame() const =0
Return the last audio frame spanned by the model.
void aboutToDelete()
Definition: Model.cpp:60
virtual QString getTitle() const
Return the "work title" of the model, if known.
Definition: Model.cpp:153
Model * m_sourceModel
Definition: Model.h:301
void sourceModelAboutToBeDeleted()
Definition: Model.cpp:76
virtual const Model * getAlignmentReference() const
Return the reference model for the current alignment timeline, if any.
Definition: Model.cpp:100
const Model * getReferenceModel() const
virtual int getAlignmentCompletion() const
Return the completion percentage for the alignment model: 100 if there is no alignment model or it ha...
Definition: Model.cpp:139