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"
20 AudioCorrelation::AudioCorrelation(AudioEnvelope *mainTrackEnvelope) :
21 m_mainTrackEnvelope(mainTrackEnvelope)
23 m_mainTrackEnvelope->normalizeEnvelope();
26 AudioCorrelation::~AudioCorrelation()
28 delete m_mainTrackEnvelope;
29 foreach (AudioEnvelope *envelope, m_children) {
32 foreach (AudioCorrelationInfo *info, m_correlations) {
36 qDebug() << "Envelope deleted.";
39 int AudioCorrelation::addChild(AudioEnvelope *envelope, bool useFFT)
41 envelope->normalizeEnvelope();
43 const int sizeMain = m_mainTrackEnvelope->envelopeSize();
44 const int sizeSub = envelope->envelopeSize();
46 AudioCorrelationInfo *info = new AudioCorrelationInfo(sizeMain, sizeSub);
47 int64_t *correlation = info->correlationVector();
49 const int64_t *envMain = m_mainTrackEnvelope->envelope();
50 const int64_t *envSub = envelope->envelope();
54 FFTCorrelation::correlate(envMain, sizeMain,
58 correlate(envMain, sizeMain,
66 m_children.append(envelope);
67 m_correlations.append(info);
69 Q_ASSERT(m_correlations.size() == m_children.size());
71 return m_children.indexOf(envelope);
74 int AudioCorrelation::getShift(int childIndex) const
76 Q_ASSERT(childIndex >= 0);
77 Q_ASSERT(childIndex < m_correlations.size());
79 int indexOffset = m_correlations.at(childIndex)->maxIndex();
80 indexOffset -= m_children.at(childIndex)->envelopeSize();
85 AudioCorrelationInfo const* AudioCorrelation::info(int childIndex) const
87 Q_ASSERT(childIndex >= 0);
88 Q_ASSERT(childIndex < m_correlations.size());
90 return m_correlations.at(childIndex);
94 void AudioCorrelation::correlate(const int64_t *envMain, int sizeMain,
95 const int64_t *envSub, int sizeSub,
99 Q_ASSERT(correlation != NULL);
102 int64_t const* right;
114 [ sub ]----sM--->[ sub ]
117 ^ correlation vector index = SHIFT + sS
119 main is fixed, sub is shifted along main.
126 for (int shift = -sizeSub; shift <= sizeMain; shift++) {
131 size = std::min(sizeSub+shift, sizeMain);
134 right = envMain+shift;
135 size = std::min(sizeSub, sizeMain-shift);
139 for (int i = 0; i < size; ++i) {
140 sum += (*left) * (*right);
144 correlation[sizeSub+shift] = qAbs(sum);
151 qDebug() << "Correlation calculated. Time taken: " << t.elapsed() << " ms.";
153 if (out_max != NULL) {