Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AGC.cpp « speex-build « 3rdparty - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b4faa19e20f2cb1be5d0641b21663c7f847d1964 (plain)
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
#include <QtCore>

#ifdef Q_OS_WIN
#define _WIN32_IE 0x0600
#include <windows.h>
#include <shellapi.h>
#define CALLGRIND_START_INSTRUMENTATION
#define CALLGRIND_STOP_INSTRUMENTATION
#define CALLGRIND_ZERO_STATS
#else
#include <valgrind/callgrind.h>
#endif

#include <math.h>
#include <speex/speex.h>
#include <speex/speex_preprocess.h>

#include "Timer.h"

template<class T>
static inline double veccomp(const QVector<T> &a, const QVector<T> &b, const char *n) {
	long double gdiff = 0.0;
	if (a.size() != b.size()) {
		qFatal("%s: %d <=> %d", n, a.size(), b.size());
	}
	for (int i=0;i<a.size();++i) {
		double diff = fabs(a[i] - b[i]);
		if (diff > gdiff)
			gdiff = diff;
#ifdef EXACT

		if (a[i] != b[i]) {
#else
		union { T tv;
			uint32_t uv;
		} v1, v2;
		v1.uv = v2.uv = 0;
		v1.tv = a[i];
		v2.tv = b[i];
		if (fabsf(a[i] - b[i]) > 1000) {
			qWarning("%08x %08x %08x", v1.uv, v2.uv, v1.uv ^ v2.uv);
#endif
			qFatal("%s: Offset %d: %.10g <=> %.10g", n, i, static_cast<double>(a[i]), static_cast<double>(b[i]));
		}
	}
	return gdiff;
}

int main(int argc, char **argv) {

	CALLGRIND_STOP_INSTRUMENTATION;
	CALLGRIND_ZERO_STATS;

	QCoreApplication a(argc, argv);

	QFile f((argc >= 2) ? argv[1] : "wb_male.wav");
	if (! f.open(QIODevice::ReadOnly)) {
		qFatal("Failed to open file!");
	}
	f.seek(36 + 8);

	QFile o("output.agc");
	if (! o.open(QIODevice::WriteOnly))
		qFatal("Failed to open out file!");

	QFile vf("verify.agc");
	if (! vf.open(QIODevice::ReadOnly))
		qWarning("No verify!");

	QDataStream out(&o);
	QDataStream verify(&vf);

	static const int iFrameSize = 320;
	int iarg;

	SpeexPreprocessState *spp = speex_preprocess_state_init(iFrameSize, 16000);
	iarg = 0;
	speex_preprocess_ctl(spp, SPEEX_PREPROCESS_SET_VAD, &iarg);
	speex_preprocess_ctl(spp, SPEEX_PREPROCESS_SET_DENOISE, &iarg);
	speex_preprocess_ctl(spp, SPEEX_PREPROCESS_SET_DEREVERB, &iarg);

	iarg = 1;
	speex_preprocess_ctl(spp, SPEEX_PREPROCESS_SET_AGC, &iarg);
	iarg = 21747;
	speex_preprocess_ctl(spp, SPEEX_PREPROCESS_SET_AGC_TARGET, &iarg);


	QVector<QByteArray> v;
	while (1) {
		QByteArray qba = f.read(iFrameSize * sizeof(short));
		if (qba.size() != iFrameSize * sizeof(short))
			break;
		v.append(qba);
	}

	int nframes = v.size();

	qWarning("Ready to process %d frames of %d samples", nframes, iFrameSize);

	QVector<short *> qvIn;
	QVector<short> sIn(nframes * iFrameSize);

	for (int i=0;i<nframes;i++) {
		const short *ptr = reinterpret_cast<const short *>(v[i].constData());
		short *s = sIn.data() + i * iFrameSize;
		for (int j=0;j<iFrameSize;++j)
			s[j] = ptr[j];
		qvIn.append(s);
	}

#ifdef Q_OS_WIN
	if (!SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS))
		qWarning("Application: Failed to set priority!");
#endif

	Timer t;
	t.restart();

	CALLGRIND_START_INSTRUMENTATION;

	for (int i=0;i<nframes;i++) {
		speex_preprocess_run(spp, qvIn[i]);

		int v;
		speex_preprocess_ctl(spp, SPEEX_PREPROCESS_GET_AGC_GAIN, &v);
		qWarning("%d %d", i, v);

	}
	CALLGRIND_STOP_INSTRUMENTATION;

	quint64 e = t.elapsed();

#ifdef Q_OS_WIN
	if (!SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS))
		qWarning("Application: Failed to reset priority!");
#endif

	qWarning("Used %llu usec", e);
	qWarning("%.2f times realtime", (20000ULL * nframes) / (e * 1.0));

	if (! RUNNING_ON_VALGRIND) {

		out << sIn;

		if (vf.isOpen()) {
			QVector<short> vIn;
			verify >> vIn;
			veccomp(vIn, sIn, "AGC");
		}
	}

	return 0;
}