]> git.sesse.net Git - kdenlive/blob - src/lib/audio/audioCorrelationInfo.cpp
2313fe2bffa7cdd5431433307b32a15ed27f7263
[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
15 AudioCorrelationInfo::AudioCorrelationInfo(int mainSize, int subSize) :
16     m_mainSize(mainSize),
17     m_subSize(subSize),
18     m_max(-1)
19 {
20     m_correlationVector = new int64_t[m_mainSize+m_subSize+1];
21 }
22
23 AudioCorrelationInfo::~AudioCorrelationInfo()
24 {
25     delete[] m_correlationVector;
26 }
27
28 int AudioCorrelationInfo::size() const
29 {
30     return m_mainSize+m_subSize+1;
31 }
32
33 void AudioCorrelationInfo::setMax(int64_t max)
34 {
35     m_max = max;
36 }
37
38 int64_t AudioCorrelationInfo::max() const
39 {
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     if (maxVal == 0)
84         return img;
85
86     int val;
87
88     for (int x = 0; x < width; x++) {
89         val = m_correlationVector[x]/double(maxVal)*img.height();
90         for (int y = img.height()-1; y > img.height() - val - 1; y--) {
91             img.setPixel(x, y, qRgb(50, 50, 50));
92         }
93     }
94
95     return img;
96 }