-
Notifications
You must be signed in to change notification settings - Fork 1
/
audioinfo_t.cpp
78 lines (65 loc) · 1.65 KB
/
audioinfo_t.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
#include "audioinfo_t.h"
#include <stdlib.h>
#include <math.h>
#include <QtCore/qendian.h>
#include <QDebug>
audioinfo_t::audioinfo_t(const QAudioFormat &init_format, QObject *parent):
QIODevice(parent),
format(init_format),
maxAmplitude(32767),
value(0),
level(0.0),
output_size(0),
output(NULL)
{
}
audioinfo_t::~audioinfo_t()
{
}
void audioinfo_t::start()
{
open(QIODevice::WriteOnly);
}
void audioinfo_t::stop()
{
close();
}
qint64 audioinfo_t::readData(char *data, qint64 maxlen)
{
Q_UNUSED(data)
Q_UNUSED(maxlen)
return 0;
}
void audioinfo_t::set_output_size(quint32 init_size)
{
output_size = init_size;
output = new quint16[init_size];
}
qint64 audioinfo_t::writeData(const char *data, qint64 len)
{
if (maxAmplitude) {
const int numSamples = len / 2;
quint16 maxValue = 0;
const unsigned char *ptr = reinterpret_cast<const unsigned char *>(data);
for (int i = 0; i < numSamples; ++i) {
/*format.setChannels(1);
format.setSampleSize(16);
format.setSampleType(QAudioFormat::SignedInt);
format.setByteOrder(QAudioFormat::LittleEndian);*/
value = qAbs(qFromLittleEndian<qint16>(ptr));
if (output_size)
{
output_size--;
output[output_size] = value;
if (!output_size)
emit output_ready(output);
}
maxValue = qMax(value, maxValue);
ptr += 2;
}
maxValue = qMin(maxValue, maxAmplitude);
level = qreal(maxValue) / maxAmplitude;
}
emit update();
return len;
}