]> git.sesse.net Git - kdenlive/blobdiff - src/lib/audio/audioCorrelation.cpp
use only kDebug/qDebug, no cout
[kdenlive] / src / lib / audio / audioCorrelation.cpp
index 0a308c1c8368e1af7bb208aa12a9d54d282e8026..1d2464ef1277b49f39d03d4e570b3b859c0a312b 100644 (file)
@@ -1,19 +1,22 @@
-/***************************************************************************
- *   Copyright (C) 2012 by Simon Andreas Eugster (simon.eu@gmail.com)      *
- *   This file is part of kdenlive. See www.kdenlive.org.                  *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- ***************************************************************************/
+/*
+Copyright (C) 2012  Simon A. Eugster (Granjow)  <simon.eu@gmail.com>
+This file is part of kdenlive. See www.kdenlive.org.
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+*/
 
 #include "audioCorrelation.h"
+#include "fftCorrelation.h"
 
+#include <QDebug>
 #include <QTime>
 #include <cmath>
 #include <iostream>
 
+
 AudioCorrelation::AudioCorrelation(AudioEnvelope *mainTrackEnvelope) :
     m_mainTrackEnvelope(mainTrackEnvelope)
 {
@@ -26,21 +29,75 @@ AudioCorrelation::~AudioCorrelation()
     foreach (AudioEnvelope *envelope, m_children) {
         delete envelope;
     }
+    foreach (AudioCorrelationInfo *info, m_correlations) {
+        delete info;
+    }
+
+    qDebug() << "Envelope deleted.";
 }
 
-int AudioCorrelation::addChild(AudioEnvelope *envelope)
+int AudioCorrelation::addChild(AudioEnvelope *envelope, bool useFFT)
 {
     envelope->normalizeEnvelope();
 
     const int sizeMain = m_mainTrackEnvelope->envelopeSize();
     const int sizeSub = envelope->envelopeSize();
 
-
     AudioCorrelationInfo *info = new AudioCorrelationInfo(sizeMain, sizeSub);
     int64_t *correlation = info->correlationVector();
 
     const int64_t *envMain = m_mainTrackEnvelope->envelope();
     const int64_t *envSub = envelope->envelope();
+    int64_t max = 0;
+
+    if (useFFT) {
+        FFTCorrelation::correlate(envMain, sizeMain,
+                                  envSub, sizeSub,
+                                  correlation);
+    } else {
+        correlate(envMain, sizeMain,
+                  envSub, sizeSub,
+                  correlation,
+                  &max);
+        info->setMax(max);
+    }
+
+
+    m_children.append(envelope);
+    m_correlations.append(info);
+
+    Q_ASSERT(m_correlations.size() == m_children.size());
+
+    return m_children.indexOf(envelope);
+}
+
+int AudioCorrelation::getShift(int childIndex) const
+{
+    Q_ASSERT(childIndex >= 0);
+    Q_ASSERT(childIndex < m_correlations.size());
+
+    int indexOffset = m_correlations.at(childIndex)->maxIndex();
+    indexOffset -= m_children.at(childIndex)->envelopeSize();
+
+    return indexOffset;
+}
+
+AudioCorrelationInfo const* AudioCorrelation::info(int childIndex) const
+{
+    Q_ASSERT(childIndex >= 0);
+    Q_ASSERT(childIndex < m_correlations.size());
+
+    return m_correlations.at(childIndex);
+}
+
+
+void AudioCorrelation::correlate(const int64_t *envMain, int sizeMain,
+                                 const int64_t *envSub, int sizeSub,
+                                 int64_t *correlation,
+                                 int64_t *out_max)
+{
+    Q_ASSERT(correlation != NULL);
+
     int64_t const* left;
     int64_t const* right;
     int size;
@@ -79,45 +136,21 @@ int AudioCorrelation::addChild(AudioEnvelope *envelope)
         }
 
         sum = 0;
-        for (int i = 0; i < size; i++) {
+        for (int i = 0; i < size; ++i) {
             sum += (*left) * (*right);
             left++;
             right++;
         }
-        correlation[sizeSub+shift] = std::abs(sum);
+        correlation[sizeSub+shift] = qAbs(sum);
 
         if (sum > max) {
             max = sum;
         }
 
     }
-    info->setMax(max);
-    std::cout << "Correlation calculated. Time taken: " << t.elapsed() << " ms." << std::endl;
-
+    qDebug() << "Correlation calculated. Time taken: " << t.elapsed() << " ms.";
 
-    m_children.append(envelope);
-    m_correlations.append(info);
-
-    Q_ASSERT(m_correlations.size() == m_children.size());
-
-    return m_children.indexOf(envelope);
-}
-
-int AudioCorrelation::getShift(int childIndex) const
-{
-    Q_ASSERT(childIndex >= 0);
-    Q_ASSERT(childIndex < m_correlations.size());
-
-    int indexOffset = m_correlations.at(childIndex)->maxIndex();
-    indexOffset -= m_children.at(childIndex)->envelopeSize();
-
-    return indexOffset;
-}
-
-AudioCorrelationInfo const* AudioCorrelation::info(int childIndex) const
-{
-    Q_ASSERT(childIndex >= 0);
-    Q_ASSERT(childIndex < m_correlations.size());
-
-    return m_correlations.at(childIndex);
+    if (out_max != NULL) {
+        *out_max = max;
+    }
 }