"Fossies" - the Fresh Open Source Software Archive

Member "mumble-1.3.2/3rdparty/speex-build/AGC.cpp" (9 Jul 2020, 3442 Bytes) of package /linux/misc/mumble-1.3.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include <QtCore>
    2 
    3 #ifdef Q_OS_WIN
    4 #define _WIN32_IE 0x0600
    5 #include <windows.h>
    6 #include <shellapi.h>
    7 #define CALLGRIND_START_INSTRUMENTATION
    8 #define CALLGRIND_STOP_INSTRUMENTATION
    9 #define CALLGRIND_ZERO_STATS
   10 #else
   11 #include <valgrind/callgrind.h>
   12 #endif
   13 
   14 #include <math.h>
   15 #include <speex/speex.h>
   16 #include <speex/speex_preprocess.h>
   17 
   18 #include "Timer.h"
   19 
   20 template<class T>
   21 static inline double veccomp(const QVector<T> &a, const QVector<T> &b, const char *n) {
   22     long double gdiff = 0.0;
   23     if (a.size() != b.size()) {
   24         qFatal("%s: %d <=> %d", n, a.size(), b.size());
   25     }
   26     for (int i=0;i<a.size();++i) {
   27         double diff = fabs(a[i] - b[i]);
   28         if (diff > gdiff)
   29             gdiff = diff;
   30 #ifdef EXACT
   31 
   32         if (a[i] != b[i]) {
   33 #else
   34         union { T tv;
   35             uint32_t uv;
   36         } v1, v2;
   37         v1.uv = v2.uv = 0;
   38         v1.tv = a[i];
   39         v2.tv = b[i];
   40         if (fabsf(a[i] - b[i]) > 1000) {
   41             qWarning("%08x %08x %08x", v1.uv, v2.uv, v1.uv ^ v2.uv);
   42 #endif
   43             qFatal("%s: Offset %d: %.10g <=> %.10g", n, i, static_cast<double>(a[i]), static_cast<double>(b[i]));
   44         }
   45     }
   46     return gdiff;
   47 }
   48 
   49 int main(int argc, char **argv) {
   50 
   51     CALLGRIND_STOP_INSTRUMENTATION;
   52     CALLGRIND_ZERO_STATS;
   53 
   54     QCoreApplication a(argc, argv);
   55 
   56     QFile f((argc >= 2) ? argv[1] : "wb_male.wav");
   57     if (! f.open(QIODevice::ReadOnly)) {
   58         qFatal("Failed to open file!");
   59     }
   60     f.seek(36 + 8);
   61 
   62     QFile o("output.agc");
   63     if (! o.open(QIODevice::WriteOnly))
   64         qFatal("Failed to open out file!");
   65 
   66     QFile vf("verify.agc");
   67     if (! vf.open(QIODevice::ReadOnly))
   68         qWarning("No verify!");
   69 
   70     QDataStream out(&o);
   71     QDataStream verify(&vf);
   72 
   73     static const int iFrameSize = 320;
   74     int iarg;
   75 
   76     SpeexPreprocessState *spp = speex_preprocess_state_init(iFrameSize, 16000);
   77     iarg = 0;
   78     speex_preprocess_ctl(spp, SPEEX_PREPROCESS_SET_VAD, &iarg);
   79     speex_preprocess_ctl(spp, SPEEX_PREPROCESS_SET_DENOISE, &iarg);
   80     speex_preprocess_ctl(spp, SPEEX_PREPROCESS_SET_DEREVERB, &iarg);
   81 
   82     iarg = 1;
   83     speex_preprocess_ctl(spp, SPEEX_PREPROCESS_SET_AGC, &iarg);
   84     iarg = 21747;
   85     speex_preprocess_ctl(spp, SPEEX_PREPROCESS_SET_AGC_TARGET, &iarg);
   86 
   87 
   88     QVector<QByteArray> v;
   89     while (1) {
   90         QByteArray qba = f.read(iFrameSize * sizeof(short));
   91         if (qba.size() != iFrameSize * sizeof(short))
   92             break;
   93         v.append(qba);
   94     }
   95 
   96     int nframes = v.size();
   97 
   98     qWarning("Ready to process %d frames of %d samples", nframes, iFrameSize);
   99 
  100     QVector<short *> qvIn;
  101     QVector<short> sIn(nframes * iFrameSize);
  102 
  103     for (int i=0;i<nframes;i++) {
  104         const short *ptr = reinterpret_cast<const short *>(v[i].constData());
  105         short *s = sIn.data() + i * iFrameSize;
  106         for (int j=0;j<iFrameSize;++j)
  107             s[j] = ptr[j];
  108         qvIn.append(s);
  109     }
  110 
  111 #ifdef Q_OS_WIN
  112     if (!SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS))
  113         qWarning("Application: Failed to set priority!");
  114 #endif
  115 
  116     Timer t;
  117     t.restart();
  118 
  119     CALLGRIND_START_INSTRUMENTATION;
  120 
  121     for (int i=0;i<nframes;i++) {
  122         speex_preprocess_run(spp, qvIn[i]);
  123 
  124         int v;
  125         speex_preprocess_ctl(spp, SPEEX_PREPROCESS_GET_AGC_GAIN, &v);
  126         qWarning("%d %d", i, v);
  127 
  128     }
  129     CALLGRIND_STOP_INSTRUMENTATION;
  130 
  131     quint64 e = t.elapsed();
  132 
  133 #ifdef Q_OS_WIN
  134     if (!SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS))
  135         qWarning("Application: Failed to reset priority!");
  136 #endif
  137 
  138     qWarning("Used %llu usec", e);
  139     qWarning("%.2f times realtime", (20000ULL * nframes) / (e * 1.0));
  140 
  141     if (! RUNNING_ON_VALGRIND) {
  142 
  143         out << sIn;
  144 
  145         if (vf.isOpen()) {
  146             QVector<short> vIn;
  147             verify >> vIn;
  148             veccomp(vIn, sIn, "AGC");
  149         }
  150     }
  151 
  152     return 0;
  153 }