-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScopeWrapper.cpp
214 lines (191 loc) · 5.88 KB
/
ScopeWrapper.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <QDebug>
#include "ScopeWrapper.h"
#include "scope/scope.h"
#define SCOPE static_cast<ddb_scope_t*>(scope)
ScopeWrapper::ScopeWrapper(QObject *parent) : QObject(parent) {
scope = ddb_scope_alloc();
ddb_scope_init(SCOPE);
series.append(nullptr);
series.append(nullptr);
SCOPE->fragment_duration = 50;
SCOPE->samplerate = 44100;
SCOPE->mode = DDB_SCOPE_MULTICHANNEL;
channels_last = 2;
m_paused = false;
m_scale = 1;
m_scale_changed = false;
m_mode_changed = false;
//emit seriesWidthChanged();
//qRegisterMetaType<ScopeWrapper>("ScopeWrapper");
}
ScopeWrapper::~ScopeWrapper() {
ddb_scope_free(SCOPE);
}
void ScopeWrapper::setSeries(int pos, QAbstractSeries *s) {
if (pos >= 0 && pos <=1) {
series.replace(pos,static_cast<QXYSeries*>(s));
}
}
int ScopeWrapper::getFragmentDuration() {
return SCOPE->fragment_duration;
}
void ScopeWrapper::setFragmentDuration(int dur) {
if (dur != SCOPE->fragment_duration) {
m_fragment_duration_changed = true;
SCOPE->fragment_duration = dur;
emit fragmentDurationChanged();
emit seriesWidthChanged();
}
return;
}
int ScopeWrapper::getScopeMode() {
return SCOPE->mode;
}
void ScopeWrapper::setScopeMode(int scope_mode) {
if (SCOPE->mode != scope_mode) {
switch (scope_mode) {
case DDB_SCOPE_MONO:
case DDB_SCOPE_MULTICHANNEL:
SCOPE->mode = static_cast<ddb_scope_mode_t>(scope_mode);
m_mode_changed = true;
emit scopeModeChanged();
}
}
}
int ScopeWrapper::getSeriesWidth() {
return ((float)SCOPE->fragment_duration)/1000.0 * SCOPE->samplerate / channels_last / m_scale-1;
}
bool ScopeWrapper::isPaused() {
return m_paused;
}
void ScopeWrapper::setPaused(bool pause) {
if (m_paused != pause) {
m_paused = pause;
emit pausedChanged();
}
}
int ScopeWrapper::getScale() {
return m_scale;
}
void ScopeWrapper::setScale(int s) {
if (s != m_scale) {
m_scale = s;
m_scale_changed = true;
emit scaleChanged();
emit seriesWidthChanged();
}
}
void ScopeWrapper::process(const ddb_audio_data_t *data) {
if (m_paused) {
return;
}
int reformat_x = false;
if (SCOPE->samplerate != data->fmt->samplerate) {
emit seriesWidthChanged();
SCOPE->samplerate = data->fmt->samplerate;
reformat_x = true;
}
if (channels_last != data->fmt->channels) {
if (!reformat_x) {
emit seriesWidthChanged();
reformat_x = true;
}
channels_last = data->fmt->channels;
}
if (!data_left || m_scale_changed || m_mode_changed || m_fragment_duration_changed) {
reformat_x = true;
}
size_t total_samples = ((float)SCOPE->fragment_duration)/1000.0 * SCOPE->samplerate;
bool is_stereo = !(SCOPE->mode == DDB_SCOPE_MONO || data->fmt->channels == 1);
if (reformat_x) {
if (!data_left) {
data_left = new QVector<QPointF>();
}
size_t left_size = total_samples/m_scale;
if (is_stereo) {
left_size /=2;
}
data_left->resize(left_size);
data_left->squeeze();
if (!data_right) {
data_right = new QVector<QPointF>();
}
data_right->resize(total_samples/m_scale/2);
data_right->squeeze();
}
QPointF *ptr_l = data_left->data();
QPointF *ptr_r = data_right->data();
if (reformat_x) {
qDebug() << "reformat_x";
if (is_stereo) {
for (size_t i = 0; i < total_samples/2/m_scale; i++) {
ptr_l[i].setX(i);
ptr_r[i].setX(i);
}
}
else {
for (size_t i = 0; i < total_samples/m_scale; i++) {
ptr_l[i].setX(i);
}
}
m_scale_changed = false;
m_mode_changed = false;
m_fragment_duration_changed = false;
}
ddb_scope_process(SCOPE, data->fmt->samplerate, data->fmt->channels, data->data, data->nframes);
if (is_stereo) {
size_t i_out = 0;
size_t bail_out_at = ((int) total_samples/m_scale/2)*2*m_scale;
for (size_t i = 0; i < bail_out_at; i+=2*m_scale) {
for (size_t right_chn = 0; right_chn < 2; right_chn++) {
float offset = right_chn ? -0.5 : 0.5;
float value = SCOPE->samples[i+right_chn]*0.5 + offset;
if (!right_chn) {
data_left->replace(i_out,QPointF(i_out,value));
//ptr_l[i_out].setY(value);
}
else {
data_right->replace(i_out,QPointF(i_out,value));
//ptr_r[i_out].setY(value);
}
}
i_out++;
}
if (series.at(0)) {
QVector<QPointF> vec(*data_left);
vec.detach();
series.at(0)->replace(vec.toList());
}
if (series.at(1)) {
if (!series.at(1)->isVisible()) {
series.at(1)->setVisible();
}
QVector<QPointF> vec(*data_right);
vec.detach();
series.at(1)->replace(vec.toList());
}
}
else {
if (data->fmt->channels == 1) {
int i_out = 0;
for (size_t i = 0; i < total_samples; i+=m_scale) {
ptr_l[i_out].setY(SCOPE->samples[i]);
i_out++;
}
}
else {
// merge
int i_out = 0;
for (size_t i = 0; i < total_samples; i+=2*m_scale) {
ptr_l[i_out].setY((SCOPE->samples[i] + SCOPE->samples[i+1])/2);
i_out++;
}
}
if (series.at(0)) {
series.at(0)->replace(*data_left);
}
if (series.at(1) && series.at(1)->isVisible()) {
series.at(1)->setVisible(false);
}
}
}