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()
27 int AudioCorrelation::addChild(AudioEnvelope *envelope)
29 envelope->normalizeEnvelope();
31 const int sizeMain = m_mainTrackEnvelope->envelopeSize();
32 const int sizeSub = envelope->envelopeSize();
35 AudioCorrelationInfo *info = new AudioCorrelationInfo(sizeMain, sizeSub);
36 int64_t *correlation = info->correlationVector();
38 const int64_t *envMain = m_mainTrackEnvelope->envelope();
39 const int64_t *envSub = envelope->envelope();
53 [ sub ]----sM--->[ sub ]
56 ^ correlation vector index = SHIFT + sS
58 main is fixed, sub is shifted along main.
65 for (int shift = -sizeSub; shift <= sizeMain; shift++) {
70 size = std::min(sizeSub+shift, sizeMain);
73 right = envMain+shift;
74 size = std::min(sizeSub, sizeMain-shift);
78 for (int i = 0; i < size; i++) {
79 sum += (*left) * (*right);
83 correlation[sizeSub+shift] = std::abs(sum);
91 std::cout << "Correlation calculated. Time taken: " << t.elapsed() << " ms." << std::endl;
94 m_children.append(envelope);
95 m_correlations.append(info);
97 Q_ASSERT(m_correlations.size() == m_children.size());
99 return m_children.indexOf(envelope);
102 int AudioCorrelation::getShift(int childIndex) const
104 Q_ASSERT(childIndex >= 0);
105 Q_ASSERT(childIndex < m_correlations.size());
107 int indexOffset = m_correlations.at(childIndex)->maxIndex();
108 indexOffset -= m_children.at(childIndex)->envelopeSize();
113 AudioCorrelationInfo const* AudioCorrelation::info(int childIndex) const
115 Q_ASSERT(childIndex >= 0);
116 Q_ASSERT(childIndex < m_correlations.size());
118 return m_correlations.at(childIndex);