1 /***************************************************************************
2 * Copyright (C) 2012 by Simon Andreas Eugster (simon.eu@gmail.com) *
3 * This file is part of kdenlive. See www.kdenlive.org. *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
11 #include "audioCorrelation.h"
17 AudioCorrelation::AudioCorrelation(AudioEnvelope *mainTrackEnvelope) :
18 m_mainTrackEnvelope(mainTrackEnvelope)
20 m_mainTrackEnvelope->normalizeEnvelope();
23 AudioCorrelation::~AudioCorrelation()
25 delete m_mainTrackEnvelope;
26 foreach (AudioEnvelope *envelope, m_children) {
31 int AudioCorrelation::addChild(AudioEnvelope *envelope)
33 envelope->normalizeEnvelope();
35 const int sizeMain = m_mainTrackEnvelope->envelopeSize();
36 const int sizeSub = envelope->envelopeSize();
39 AudioCorrelationInfo *info = new AudioCorrelationInfo(sizeMain, sizeSub);
40 int64_t *correlation = info->correlationVector();
42 const int64_t *envMain = m_mainTrackEnvelope->envelope();
43 const int64_t *envSub = envelope->envelope();
57 [ sub ]----sM--->[ sub ]
60 ^ correlation vector index = SHIFT + sS
62 main is fixed, sub is shifted along main.
69 for (int shift = -sizeSub; shift <= sizeMain; shift++) {
74 size = std::min(sizeSub+shift, sizeMain);
77 right = envMain+shift;
78 size = std::min(sizeSub, sizeMain-shift);
82 for (int i = 0; i < size; i++) {
83 sum += (*left) * (*right);
87 correlation[sizeSub+shift] = std::abs(sum);
95 std::cout << "Correlation calculated. Time taken: " << t.elapsed() << " ms." << std::endl;
98 m_children.append(envelope);
99 m_correlations.append(info);
101 Q_ASSERT(m_correlations.size() == m_children.size());
103 return m_children.indexOf(envelope);
106 int AudioCorrelation::getShift(int childIndex) const
108 Q_ASSERT(childIndex >= 0);
109 Q_ASSERT(childIndex < m_correlations.size());
111 int indexOffset = m_correlations.at(childIndex)->maxIndex();
112 indexOffset -= m_children.at(childIndex)->envelopeSize();
117 AudioCorrelationInfo const* AudioCorrelation::info(int childIndex) const
119 Q_ASSERT(childIndex >= 0);
120 Q_ASSERT(childIndex < m_correlations.size());
122 return m_correlations.at(childIndex);