1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
#include "cavaprovider.hpp"
#include "audioprovider.hpp"
#include <QDebug>
#include <QObject>
#include <cava/cavacore.h>
#include <cmath>
#include <cstddef>
namespace caelestia {
CavaProcessor::CavaProcessor(AudioProvider* provider, QObject* parent)
: AudioProcessor(provider, parent)
, m_plan(nullptr)
, m_in(new double[static_cast<size_t>(m_chunkSize)])
, m_out(nullptr)
, m_bars(0) {};
CavaProcessor::~CavaProcessor() {
cleanup();
delete[] m_in;
}
void CavaProcessor::setBars(int bars) {
if (bars < 0) {
qWarning() << "CavaProcessor::setBars: bars must be greater than 0. Setting to 0.";
bars = 0;
}
if (m_bars != bars) {
m_bars = bars;
reload();
}
}
void CavaProcessor::reload() {
cleanup();
initCava();
}
void CavaProcessor::cleanup() {
if (!m_plan) {
return;
}
cava_destroy(m_plan);
m_plan = nullptr;
if (m_out) {
delete[] m_out;
m_out = nullptr;
}
}
void CavaProcessor::initCava() {
if (m_plan || m_bars == 0) {
return;
}
m_plan = cava_init(m_bars, static_cast<unsigned int>(m_sampleRate), 1, 1, 0.85, 50, 10000);
if (m_plan->status == -1) {
qWarning() << "CavaProcessor::initCava: failed to initialise cava plan";
cleanup();
return;
}
m_out = new double[static_cast<size_t>(m_bars)];
}
void CavaProcessor::processChunk(const QVector<double>& chunk) {
if (!m_plan || m_bars == 0) {
return;
}
std::copy(chunk.constBegin(), chunk.constEnd(), m_in);
// Process in data via cava
cava_execute(m_in, m_chunkSize, m_out, m_plan);
// Apply monstercat filter
for (int i = 0; i < m_bars; i++) {
for (int j = i - 1; j >= 0; j--) {
m_out[j] = std::max(m_out[i] / std::pow(1.5, i - j), m_out[j]);
}
for (int j = i + 1; j < m_bars; j++) {
m_out[j] = std::max(m_out[i] / std::pow(1.5, j - i), m_out[j]);
}
}
// Update values
QVector<double> values(m_bars);
std::copy(m_out, m_out + m_bars, values.begin());
if (values != m_values) {
m_values = std::move(values);
emit valuesChanged(m_values);
}
}
CavaProvider::CavaProvider(int sampleRate, int chunkSize, QObject* parent)
: AudioProvider(sampleRate, chunkSize, parent)
, m_bars(0)
, m_values(m_bars) {
m_processor = new CavaProcessor(this);
init();
connect(static_cast<CavaProcessor*>(m_processor), &CavaProcessor::valuesChanged, this, &CavaProvider::updateValues);
}
int CavaProvider::bars() const {
return m_bars;
}
void CavaProvider::setBars(int bars) {
if (bars < 0) {
qWarning() << "CavaProvider::setBars: bars must be greater than 0. Setting to 0.";
bars = 0;
}
if (m_bars == bars) {
return;
}
m_values.resize(bars);
m_bars = bars;
emit barsChanged();
emit valuesChanged();
QMetaObject::invokeMethod(m_processor, "setBars", Qt::QueuedConnection, Q_ARG(int, bars));
}
QVector<double> CavaProvider::values() const {
return m_values;
}
void CavaProvider::updateValues(QVector<double> values) {
if (values != m_values) {
m_values = std::move(values);
emit valuesChanged();
}
}
} // namespace caelestia
|