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 foreach (AudioCorrelationInfo *info, m_correlations) {
34 std::cout << "Envelope deleted." << std::endl;
37 int AudioCorrelation::addChild(AudioEnvelope *envelope, bool useFFT)
39 envelope->normalizeEnvelope();
41 const int sizeMain = m_mainTrackEnvelope->envelopeSize();
42 const int sizeSub = envelope->envelopeSize();
45 AudioCorrelationInfo *info = new AudioCorrelationInfo(sizeMain, sizeSub);
46 int64_t *correlation = info->correlationVector();
48 const int64_t *envMain = m_mainTrackEnvelope->envelope();
49 const int64_t *envSub = envelope->envelope();
53 FFTCorrelation::correlate(envMain, sizeMain,
57 correlate(envMain, sizeMain,
65 m_children.append(envelope);
66 m_correlations.append(info);
68 Q_ASSERT(m_correlations.size() == m_children.size());
70 return m_children.indexOf(envelope);
73 int AudioCorrelation::getShift(int childIndex) const
75 Q_ASSERT(childIndex >= 0);
76 Q_ASSERT(childIndex < m_correlations.size());
78 int indexOffset = m_correlations.at(childIndex)->maxIndex();
79 indexOffset -= m_children.at(childIndex)->envelopeSize();
84 AudioCorrelationInfo const* AudioCorrelation::info(int childIndex) const
86 Q_ASSERT(childIndex >= 0);
87 Q_ASSERT(childIndex < m_correlations.size());
89 return m_correlations.at(childIndex);
93 void AudioCorrelation::correlate(const int64_t *envMain, int sizeMain,
94 const int64_t *envSub, int sizeSub,
98 Q_ASSERT(correlation != NULL);
101 int64_t const* right;
113 [ sub ]----sM--->[ sub ]
116 ^ correlation vector index = SHIFT + sS
118 main is fixed, sub is shifted along main.
125 for (int shift = -sizeSub; shift <= sizeMain; shift++) {
130 size = std::min(sizeSub+shift, sizeMain);
133 right = envMain+shift;
134 size = std::min(sizeSub, sizeMain-shift);
138 for (int i = 0; i < size; i++) {
139 sum += (*left) * (*right);
143 correlation[sizeSub+shift] = qAbs(sum);
150 std::cout << "Correlation calculated. Time taken: " << t.elapsed() << " ms." << std::endl;
152 if (out_max != NULL) {