]> git.sesse.net Git - kdenlive/blob - src/lib/audio/audioCorrelationInfo.cpp
Audio alignment works.
[kdenlive] / src / lib / audio / audioCorrelationInfo.cpp
1 /***************************************************************************
2  *   Copyright (C) 2012 by Simon Andreas Eugster (simon.eu@gmail.com)      *
3  *   This file is part of kdenlive. See www.kdenlive.org.                  *
4  *                                                                         *
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  ***************************************************************************/
10
11 #include "audioCorrelationInfo.h"
12 #include <iostream>
13
14 AudioCorrelationInfo::AudioCorrelationInfo(int mainSize, int subSize) :
15     m_mainSize(mainSize),
16     m_subSize(subSize),
17     m_max(-1)
18 {
19     m_correlationVector = new int64_t[m_mainSize+m_subSize+1];
20 }
21
22 AudioCorrelationInfo::~AudioCorrelationInfo()
23 {
24     delete m_correlationVector;
25 }
26
27 int AudioCorrelationInfo::size() const
28 {
29     return m_mainSize+m_subSize+1;
30 }
31
32 void AudioCorrelationInfo::setMax(int64_t max)
33 {
34     m_max = max;
35 }
36
37 int64_t AudioCorrelationInfo::max() const
38 {
39     Q_ASSERT(m_max > 0);
40     if (m_max <= 0) {
41         int width = size();
42         int64_t max = 0;
43         for (int i = 0; i < width; i++) {
44             if (m_correlationVector[i] > max) {
45                 max = m_correlationVector[i];
46             }
47         }
48         Q_ASSERT(max > 0);
49         return max;
50     }
51     return m_max;
52 }
53
54 int AudioCorrelationInfo::maxIndex() const
55 {
56     int64_t max = 0;
57     int index = 0;
58     int width = size();
59
60     for (int i = 0; i < width; i++) {
61         if (m_correlationVector[i] > max) {
62             max = m_correlationVector[i];
63             index = i;
64         }
65     }
66
67     return index;
68 }
69
70 int64_t* AudioCorrelationInfo::correlationVector()
71 {
72     return m_correlationVector;
73 }
74
75 QImage AudioCorrelationInfo::toImage(int height) const
76 {
77     int width = size();
78     int64_t maxVal = max();
79
80     QImage img(width, height, QImage::Format_ARGB32);
81     img.fill(qRgb(255,255,255));
82
83     int val;
84
85     for (int x = 0; x < width; x++) {
86         val = m_correlationVector[x]/double(maxVal)*img.height();
87         for (int y = img.height()-1; y > img.height() - val - 1; y--) {
88             img.setPixel(x, y, qRgb(50, 50, 50));
89         }
90     }
91
92     return img;
93 }