2 Copyright (C) 2012 Simon A. Eugster (Granjow) <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 3 of the License, or
8 (at your option) any later version.
11 #include "audioCorrelation.h"
12 #include "fftCorrelation.h"
18 AudioCorrelation::AudioCorrelation(AudioEnvelope *mainTrackEnvelope) :
19 m_mainTrackEnvelope(mainTrackEnvelope)
21 m_mainTrackEnvelope->normalizeEnvelope();
24 AudioCorrelation::~AudioCorrelation()
26 delete m_mainTrackEnvelope;
27 foreach (AudioEnvelope *envelope, m_children) {
30 std::cout << "Envelope deleted." << std::endl;
33 int AudioCorrelation::addChild(AudioEnvelope *envelope, bool useFFT)
35 envelope->normalizeEnvelope();
37 const int sizeMain = m_mainTrackEnvelope->envelopeSize();
38 const int sizeSub = envelope->envelopeSize();
41 AudioCorrelationInfo *info = new AudioCorrelationInfo(sizeMain, sizeSub);
42 int64_t *correlation = info->correlationVector();
44 const int64_t *envMain = m_mainTrackEnvelope->envelope();
45 const int64_t *envSub = envelope->envelope();
49 FFTCorrelation::correlate(envMain, sizeMain,
53 correlate(envMain, sizeMain,
61 m_children.append(envelope);
62 m_correlations.append(info);
64 Q_ASSERT(m_correlations.size() == m_children.size());
66 return m_children.indexOf(envelope);
69 int AudioCorrelation::getShift(int childIndex) const
71 Q_ASSERT(childIndex >= 0);
72 Q_ASSERT(childIndex < m_correlations.size());
74 int indexOffset = m_correlations.at(childIndex)->maxIndex();
75 indexOffset -= m_children.at(childIndex)->envelopeSize();
80 AudioCorrelationInfo const* AudioCorrelation::info(int childIndex) const
82 Q_ASSERT(childIndex >= 0);
83 Q_ASSERT(childIndex < m_correlations.size());
85 return m_correlations.at(childIndex);
89 void AudioCorrelation::correlate(const int64_t *envMain, int sizeMain,
90 const int64_t *envSub, int sizeSub,
94 Q_ASSERT(correlation != NULL);
109 [ sub ]----sM--->[ sub ]
112 ^ correlation vector index = SHIFT + sS
114 main is fixed, sub is shifted along main.
121 for (int shift = -sizeSub; shift <= sizeMain; shift++) {
126 size = std::min(sizeSub+shift, sizeMain);
129 right = envMain+shift;
130 size = std::min(sizeSub, sizeMain-shift);
134 for (int i = 0; i < size; i++) {
135 sum += (*left) * (*right);
139 correlation[sizeSub+shift] = std::abs(sum);
146 std::cout << "Correlation calculated. Time taken: " << t.elapsed() << " ms." << std::endl;
148 if (out_max != NULL) {