25 m_reference(reference),
27 m_pathSource(pathSource),
29 m_reversePath(nullptr),
31 m_pathComplete(false),
33 m_explicitlySetCompletion(-1)
47 #ifdef DEBUG_ALIGNMENT_MODEL 48 SVCERR <<
"AlignmentModel(" <<
this <<
")::~AlignmentModel()" << endl;
55 if (
m_error !=
"")
return false;
57 auto pathSourceModel =
59 if (pathSourceModel) {
60 return pathSourceModel->isOK();
71 if (reference && aligned) {
74 return std::min(a, b);
86 if (reference && aligned) {
89 return std::max(a, b);
100 return reference->getSampleRate();
114 if (completion) *completion = 0;
115 #ifdef DEBUG_ALIGNMENT_MODEL 116 SVCERR <<
"AlignmentModel::isReady: path not begun" << endl;
121 if (completion) *completion = 100;
122 #ifdef DEBUG_ALIGNMENT_MODEL 123 SVCERR <<
"AlignmentModel::isReady: path complete" << endl;
131 if (completion) *completion = 0;
132 #ifdef DEBUG_ALIGNMENT_MODEL 133 SVCERR <<
"AlignmentModel::isReady: no raw path" << endl;
137 auto pathSourceModel =
139 if (pathSourceModel) {
140 return pathSourceModel->isReady(completion);
174 #ifdef DEBUG_ALIGNMENT_MODEL 175 cerr <<
"AlignmentModel::toReference(" << frame <<
")" << endl;
193 #ifdef DEBUG_ALIGNMENT_MODEL 194 cerr <<
"AlignmentModel::fromReference(" << frame <<
")" << endl;
220 auto pathSourceModel =
222 if (!pathSourceModel)
return;
229 pathSourceModel->isReady(&completion);
231 #ifdef DEBUG_ALIGNMENT_MODEL 232 SVCERR <<
"AlignmentModel::pathCompletionChanged: completion = " 233 << completion << endl;
243 #ifdef DEBUG_ALIGNMENT_MODEL 244 SVCERR <<
"AlignmentModel: path complete" << endl;
256 if (!alignedModel)
return;
258 auto pathSourceModel =
261 if (!pathSourceModel) {
262 cerr <<
"ERROR: AlignmentModel::constructPath: " 268 (pathSourceModel->getSampleRate(),
269 pathSourceModel->getResolution()));
271 if (!pathSourceModel)
return;
276 EventVector points = pathSourceModel->getAllEvents();
278 for (
const auto &p: points) {
280 double value = p.getValue();
281 sv_frame_t rframe = lrint(value * alignedModel->getSampleRate());
285 #ifdef DEBUG_ALIGNMENT_MODEL 286 cerr <<
"AlignmentModel::constructPath: " <<
m_path->getPointCount() <<
" points, at least " << (2 *
m_path->getPointCount() * (3 *
sizeof(
void *) +
sizeof(
int) +
sizeof(
PathPoint))) <<
" bytes" << endl;
295 cerr <<
"ERROR: AlignmentModel::constructReversePath: " 296 <<
"No forward path available" << endl;
301 m_path->getResolution()));
310 for (
auto p: points) {
316 #ifdef DEBUG_ALIGNMENT_MODEL 317 cerr <<
"AlignmentModel::constructReversePath: " <<
m_reversePath->getPointCount() <<
" points, at least " << (2 *
m_reversePath->getPointCount() * (3 *
sizeof(
void *) +
sizeof(
int) +
sizeof(
PathPoint))) <<
" bytes" << endl;
331 if (points.empty()) {
332 #ifdef DEBUG_ALIGNMENT_MODEL 333 cerr <<
"AlignmentModel::align: No points" << endl;
338 #ifdef DEBUG_ALIGNMENT_MODEL 339 cerr <<
"AlignmentModel::align: frame " << frame <<
" requested" << endl;
343 Path::Points::const_iterator i = points.lower_bound(point);
344 if (i == points.end()) {
345 #ifdef DEBUG_ALIGNMENT_MODEL 346 cerr <<
"Note: i == points.end()" << endl;
350 while (i != points.begin() && i->frame > frame) {
360 if (++i != points.end()) {
361 #ifdef DEBUG_ALIGNMENT_MODEL 362 cerr <<
"another point available" << endl;
364 followingFrame = i->frame;
365 followingMapFrame = i->mapframe;
367 #ifdef DEBUG_ALIGNMENT_MODEL 368 cerr <<
"no other point available" << endl;
372 #ifdef DEBUG_ALIGNMENT_MODEL 373 cerr <<
"foundFrame = " << foundFrame <<
", foundMapFrame = " << foundMapFrame
374 <<
", followingFrame = " << followingFrame <<
", followingMapFrame = " 375 << followingMapFrame << endl;
378 if (foundMapFrame < 0) {
384 if (followingFrame != foundFrame && frame > foundFrame) {
386 double(frame - foundFrame) /
387 double(followingFrame - foundFrame);
388 resultFrame += lrint(
double(followingMapFrame - foundMapFrame) * interp);
391 #ifdef DEBUG_ALIGNMENT_MODEL 392 cerr <<
"AlignmentModel::align: resultFrame = " << resultFrame << endl;
403 auto pathSourceModel =
406 if (pathSourceModel) {
408 connect(pathSourceModel.get(),
418 if (pathSourceModel->isReady()) {
435 QString extraAttributes)
const 438 SVDEBUG <<
"AlignmentModel::toXml: no path" << endl;
442 m_path->toXml(stream, indent,
"");
445 QString(
"type=\"alignment\" reference=\"%1\" aligned=\"%2\" path=\"%3\" %4")
448 .arg(
m_path->getExportId())
449 .arg(extraAttributes));
double sv_samplerate_t
Sample rate.
bool isOK() const override
Return true if the model was constructed successfully.
std::set< PathPoint > Points
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const override
Stream this exportable object out to XML on a text stream.
void constructPath() const
bool isReady(int *completion=0) const override
Return true if the model has finished loading or calculating all its data, for a model that is capabl...
void setPathFrom(ModelId pathSource)
int64_t sv_frame_t
Frame index, the unit of our time axis.
void setPath(const Path &path)
sv_frame_t toReference(sv_frame_t frame) const
void pathSourceCompletionChanged(ModelId)
void setCompletion(int completion)
sv_frame_t fromReference(sv_frame_t frame) const
void pathSourceChangedWithin(ModelId, sv_frame_t startFrame, sv_frame_t endFrame)
AlignmentModel(ModelId reference, ModelId aligned, ModelId path)
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const override
Stream this exportable object out to XML on a text stream.
const Points & getPoints() const
Id getId() const
Return an id for this object.
sv_frame_t getTrueEndFrame() const override
Return the audio frame at the end of the model.
sv_samplerate_t getSampleRate() const override
Return the frame rate in frames per second.
std::unique_ptr< Path > m_reversePath
sv_frame_t performAlignment(const Path &path, sv_frame_t frame) const
const ZoomConstraint * getZoomConstraint() const override
If this model imposes a zoom constraint, i.e.
std::unique_ptr< Path > m_path
sv_frame_t getStartFrame() const override
Return the first audio frame spanned by the model.
void modelChangedWithin(ModelId myId, sv_frame_t startFrame, sv_frame_t endFrame)
Emitted when a model has been edited (or more data retrieved from cache, in the case of a cached mode...
ZoomConstraint is a simple interface that describes a limitation on the available zoom sizes for a vi...
ModelId getReferenceModel() const
static int getExportId(Id id)
If the Item type is an XmlExportable, return the export ID of the given item ID.
int m_explicitlySetCompletion
ModelId getAlignedModel() const
std::vector< Event > EventVector
void constructReversePath() const
void completionChanged(ModelId)
static std::shared_ptr< Item > get(Id id)