]> git.sesse.net Git - kdenlive/blob - src/lib/audio/audioCorrelationInfo.cpp
use only kDebug/qDebug, no cout
[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
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     if (m_max <= 0) {
40         int width = size();
41         int64_t max = 0;
42         for (int i = 0; i < width; ++i) {
43             if (m_correlationVector[i] > max) {
44                 max = m_correlationVector[i];
45             }
46         }
47         Q_ASSERT(max > 0);
48         return max;
49     }
50     return m_max;
51 }
52
53 int AudioCorrelationInfo::maxIndex() const
54 {
55     int64_t max = 0;
56     int index = 0;
57     int width = size();
58
59     for (int i = 0; i < width; ++i) {
60         if (m_correlationVector[i] > max) {
61             max = m_correlationVector[i];
62             index = i;
63         }
64     }
65
66     return index;
67 }
68
69 int64_t* AudioCorrelationInfo::correlationVector()
70 {
71     return m_correlationVector;
72 }
73
74 QImage AudioCorrelationInfo::toImage(int height) const
75 {
76     int width = size();
77     int64_t maxVal = max();
78
79     QImage img(width, height, QImage::Format_ARGB32);
80     img.fill(qRgb(255,255,255));
81
82     if (maxVal == 0)
83         return img;
84
85     int val;
86
87     for (int x = 0; x < width; x++) {
88         val = m_correlationVector[x]/double(maxVal)*img.height();
89         for (int y = img.height()-1; y > img.height() - val - 1; y--) {
90             img.setPixel(x, y, qRgb(50, 50, 50));
91         }
92     }
93
94     return img;
95 }