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 //#define DEBUG_COMPLETION 1
24 
26 {
27  SVDEBUG << "Model::~Model: " << this << " with id " << getId() << endl;
28 }
29 
30 void
32 {
33  QMutexLocker locker(&m_mutex);
34 
35  m_sourceModel = modelId;
36 
37  auto model = ModelById::get(m_sourceModel);
38  if (model) {
39  connect(model.get(), SIGNAL(alignmentCompletionChanged(ModelId)),
40  this, SIGNAL(alignmentCompletionChanged(ModelId)));
41  }
42 }
43 
44 void
46 {
47  QMutexLocker locker(&m_mutex);
48 
49  SVDEBUG << "Model(" << this << "): accepting alignment model "
50  << alignmentModel << endl;
51 
52  if (auto model = ModelById::get(m_alignmentModel)) {
53  disconnect(model.get(), SIGNAL(completionChanged(ModelId)),
55  }
56 
57  m_alignmentModel = alignmentModel;
58 
59  if (auto model = ModelById::get(m_alignmentModel)) {
60  connect(model.get(), SIGNAL(completionChanged(ModelId)),
62  }
63 }
64 
65 void
67 {
69 }
70 
71 const ModelId
73 {
74  QMutexLocker locker(&m_mutex);
75  return m_alignmentModel;
76 }
77 
78 const ModelId
80 {
81  QMutexLocker locker(&m_mutex);
82  auto model = ModelById::getAs<AlignmentModel>(m_alignmentModel);
83  if (model) return model->getReferenceModel();
84  else return {};
85 }
86 
89 {
90  ModelId alignmentModelId, sourceModelId;
91  {
92  QMutexLocker locker(&m_mutex);
93  alignmentModelId = m_alignmentModel;
94  sourceModelId = m_sourceModel;
95  }
96 
97  auto alignmentModel = ModelById::getAs<AlignmentModel>(alignmentModelId);
98 
99  if (!alignmentModel) {
100  auto sourceModel = ModelById::get(sourceModelId);
101  if (sourceModel) {
102  return sourceModel->alignToReference(frame);
103  }
104  return frame;
105  }
106 
107  sv_frame_t refFrame = alignmentModel->toReference(frame);
108  auto refModel = ModelById::get(alignmentModel->getReferenceModel());
109  if (refModel && refFrame > refModel->getEndFrame()) {
110  refFrame = refModel->getEndFrame();
111  }
112  return refFrame;
113 }
114 
117 {
118  ModelId alignmentModelId, sourceModelId;
119  {
120  QMutexLocker locker(&m_mutex);
121  alignmentModelId = m_alignmentModel;
122  sourceModelId = m_sourceModel;
123  }
124 
125  auto alignmentModel = ModelById::getAs<AlignmentModel>(alignmentModelId);
126 
127  if (!alignmentModel) {
128  auto sourceModel = ModelById::get(sourceModelId);
129  if (sourceModel) {
130  return sourceModel->alignFromReference(refFrame);
131  }
132  return refFrame;
133  }
134 
135  sv_frame_t frame = alignmentModel->fromReference(refFrame);
136  if (frame > getEndFrame()) frame = getEndFrame();
137  return frame;
138 }
139 
140 int
142 {
143  ModelId alignmentModelId, sourceModelId;
144  {
145  QMutexLocker locker(&m_mutex);
146  alignmentModelId = m_alignmentModel;
147  sourceModelId = m_sourceModel;
148  }
149 
150 #ifdef DEBUG_COMPLETION
151  SVCERR << "Model(" << this
152  << ")::getAlignmentCompletion: m_alignmentModel = "
153  << m_alignmentModel << endl;
154 #endif
155 
156  auto alignmentModel = ModelById::getAs<AlignmentModel>(alignmentModelId);
157 
158  if (!alignmentModel) {
159  auto sourceModel = ModelById::get(sourceModelId);
160  if (sourceModel) {
161  return sourceModel->getAlignmentCompletion();
162  }
163  return 100;
164  }
165 
166  int completion = 0;
167  (void)alignmentModel->isReady(&completion);
168 #ifdef DEBUG_COMPLETION
169  SVCERR << "Model(" << this
170  << ")::getAlignmentCompletion: completion = " << completion
171  << endl;
172 #endif
173  return completion;
174 }
175 
176 QString
178 {
179  QMutexLocker locker(&m_mutex);
180  auto source = ModelById::get(m_sourceModel);
181  if (source) return source->getTitle();
182  else return "";
183 }
184 
185 QString
187 {
188  QMutexLocker locker(&m_mutex);
189  auto source = ModelById::get(m_sourceModel);
190  if (source) return source->getMaker();
191  else return "";
192 }
193 
194 QString
196 {
197  QMutexLocker locker(&m_mutex);
198  auto source = ModelById::get(m_sourceModel);
199  if (source) return source->getLocation();
200  else return "";
201 }
202 
203 void
204 Model::toXml(QTextStream &stream, QString indent,
205  QString extraAttributes) const
206 {
207  stream << indent;
208  stream << QString("<model id=\"%1\" name=\"%2\" sampleRate=\"%3\" start=\"%4\" end=\"%5\" %6/>\n")
209  .arg(getExportId())
210  .arg(encodeEntities(objectName()))
211  .arg(getSampleRate())
212  .arg(getStartFrame())
213  .arg(getEndFrame())
214  .arg(extraAttributes);
215 }
216 
217 
void alignmentCompletionChanged(ModelId myId)
Emitted when the completion percentage changes for the calculation of this model&#39;s alignment model...
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const override
Stream this exportable object out to XML on a text stream.
Definition: Model.cpp:204
virtual sv_frame_t alignFromReference(sv_frame_t referenceFrame) const
Return the frame number in this model that corresponds to the given frame number of the reference mod...
Definition: Model.cpp:116
virtual void setAlignment(ModelId alignmentModel)
Specify an alignment between this model&#39;s timeline and that of a reference model. ...
Definition: Model.cpp:45
int64_t sv_frame_t
Frame index, the unit of our time axis.
Definition: BaseTypes.h:31
virtual ~Model()
Definition: Model.cpp:25
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:141
void alignmentModelCompletionChanged(ModelId)
Definition: Model.cpp:66
virtual QString getLocation() const
Return the location of the data in this model (e.g.
Definition: Model.cpp:195
virtual QString getMaker() const
Return the "artist" or "maker" of the model, if known.
Definition: Model.cpp:186
Id getId() const
Return an id for this object.
Definition: ById.h:193
sv_frame_t getEndFrame() const
Return the audio frame at the end of the model, i.e.
Definition: Model.h:87
static QString encodeEntities(QString)
virtual const ModelId getAlignmentReference() const
Return the reference model for the current alignment timeline, if any.
Definition: Model.cpp:79
ModelId m_sourceModel
Definition: Model.h:338
virtual sv_samplerate_t getSampleRate() const =0
Return the frame rate in frames per second.
ModelId m_alignmentModel
Definition: Model.h:339
void completionChanged(ModelId myId)
Emitted when some internal processing has advanced a stage, but the model has not changed externally...
QMutex m_mutex
Definition: Model.h:337
ExportId getExportId() const
Return the numerical export identifier for this object.
virtual void setSourceModel(ModelId model)
Set the source model for this model.
Definition: Model.cpp:31
virtual sv_frame_t getStartFrame() const =0
Return the first audio frame spanned by the model.
#define SVDEBUG
Definition: Debug.h:106
virtual QString getTitle() const
Return the "work title" of the model, if known.
Definition: Model.cpp:177
#define SVCERR
Definition: Debug.h:109
virtual const ModelId getAlignment() const
Retrieve the alignment model for this model.
Definition: Model.cpp:72
virtual sv_frame_t alignToReference(sv_frame_t frame) const
Return the frame number of the reference model that corresponds to the given frame number in this mod...
Definition: Model.cpp:88
Definition: ById.h:115
static std::shared_ptr< Item > get(Id id)
Definition: ById.h:251