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) {
29 std::cout << "Envelope deleted." << std::endl;
32 int AudioCorrelation::addChild(AudioEnvelope *envelope)
34 envelope->normalizeEnvelope();
36 const int sizeMain = m_mainTrackEnvelope->envelopeSize();
37 const int sizeSub = envelope->envelopeSize();
40 AudioCorrelationInfo *info = new AudioCorrelationInfo(sizeMain, sizeSub);
41 int64_t *correlation = info->correlationVector();
43 const int64_t *envMain = m_mainTrackEnvelope->envelope();
44 const int64_t *envSub = envelope->envelope();
58 [ sub ]----sM--->[ sub ]
61 ^ correlation vector index = SHIFT + sS
63 main is fixed, sub is shifted along main.
70 for (int shift = -sizeSub; shift <= sizeMain; shift++) {
75 size = std::min(sizeSub+shift, sizeMain);
78 right = envMain+shift;
79 size = std::min(sizeSub, sizeMain-shift);
83 for (int i = 0; i < size; i++) {
84 sum += (*left) * (*right);
88 correlation[sizeSub+shift] = std::abs(sum);
96 std::cout << "Correlation calculated. Time taken: " << t.elapsed() << " ms." << std::endl;
99 m_children.append(envelope);
100 m_correlations.append(info);
102 Q_ASSERT(m_correlations.size() == m_children.size());
104 return m_children.indexOf(envelope);
107 int AudioCorrelation::getShift(int childIndex) const
109 Q_ASSERT(childIndex >= 0);
110 Q_ASSERT(childIndex < m_correlations.size());
112 int indexOffset = m_correlations.at(childIndex)->maxIndex();
113 indexOffset -= m_children.at(childIndex)->envelopeSize();
118 AudioCorrelationInfo const* AudioCorrelation::info(int childIndex) const
120 Q_ASSERT(childIndex >= 0);
121 Q_ASSERT(childIndex < m_correlations.size());
123 return m_correlations.at(childIndex);