File: analyzer.cc

package info (click to toggle)
imms 2.0.3-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 736 kB
  • ctags: 901
  • sloc: cpp: 5,532; ansic: 1,096; sh: 186; makefile: 113
file content (161 lines) | stat: -rw-r--r-- 3,613 bytes parent folder | download
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
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <string.h>
#include <fftw3.h>
#include <stdint.h>
#include <math.h>

#include <utils.h>
#include <song.h>
#include <immsdb.h>

#include "spectrum.h"
#include "strmanip.h"

#define SCALE       100000.0

using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::ostringstream;
using std::fstream;

typedef uint16_t sample_t;

int usage()
{
    cout << "usage: analyzer <filename>" << endl;
    return -1;
}

double infftdata[WINDOWSIZE];
fftw_complex outfftdata[NFREQS];
fftw_plan plan;

int analyze(const string &path)
{
    if (access(path.c_str(), R_OK))
    {
        cerr << "analyzer: Could not open file " << path << endl;
        return -4;
    }

    string epath = rex.replace(path, "'", "'\"'\"'", Regexx::global);
    ostringstream command;
    command << "nice -n 15 sox \'" << epath << "\' -t .raw -w -u -c 1 -r "
        << SAMPLERATE << " -";
#ifdef DEBUG
    cout << "analyzer: Executing: " << command.str() << endl;
#endif
    FILE *p = popen(command.str().c_str(), "r");

    if (!p)
    {
        cerr << "analyzer: Could not open pipe!" << endl;
        return -2;
    }

    sample_t indata[WINDOWSIZE];
    double outdata[NFREQS];

    double maxes[NFREQS];
    memset(maxes, 0, sizeof(maxes));

#ifdef DEBUG
    StackTimer t;
#endif

    int r = fread(indata, sizeof(sample_t), OVERLAP, p);

    if (r != OVERLAP)
        return -3;

    try {
        SpectrumAnalyzer analyzer(path);

        if (analyzer.is_known())
        {
            cout << "analyzer: Already analyzed - skipping." << endl;
            return 1;
        }

        while (fread(indata + OVERLAP, sizeof(sample_t), READSIZE, p)
                == READSIZE)
        {
            for (int i = 0; i < WINDOWSIZE; ++i)
                infftdata[i] = (double)indata[i];

            fftw_execute(plan);

            for (int i = 0; i < NFREQS; ++i)
            {
                outdata[i] = sqrt(pow(outfftdata[i][0] / SCALE, 2)
                        + pow(outfftdata[i][1] / SCALE, 2));
                if (outdata[i] > maxes[i])
                    maxes[i] = outdata[i];
            }

            analyzer.integrate_spectrum(outdata);

            memmove(indata, indata + READSIZE, OVERLAP * sizeof(sample_t));
        }

    }
    catch (std::string &s) { cerr << s << endl; }

    pclose(p);

    return 0;
}

int main(int argc, char *argv[])
{
    if (argc < 2)
        return usage();

    StackLockFile lock(get_imms_root() + ".analyzer_lock");
    if (!lock.isok())
    {
        cerr << "analyzer: Another instance already active - exiting." << endl;
        return -7;
    }

    nice(15);

    bool shouldexport = true;
    FILE *wisdom = fopen(get_imms_root(".fftw_wisdom").c_str(), "r");
    if (wisdom)
    {
        shouldexport = !fftw_import_wisdom_from_file(wisdom);
        fclose(wisdom);
    }
    else
        cerr << "analyzer: Growing wiser. This may take a while." << endl;

    plan = fftw_plan_dft_r2c_1d(WINDOWSIZE, infftdata, outfftdata,
            FFTW_MEASURE | FFTW_PATIENT | FFTW_EXHAUSTIVE);

    if (shouldexport)
    {
        FILE *wisdom = fopen(get_imms_root(".fftw_wisdom").c_str(), "w");
        if (wisdom)
        {
            fftw_export_wisdom_to_file(wisdom);
            fclose(wisdom);
        }
        else
            cerr << "analyzer: Could not write to wisdom file!" << endl;

    }

    ImmsDb immsdb;
    for (int i = 1; i < argc; ++i)
        analyze(path_normalize(argv[i]));

    fftw_destroy_plan(plan);
}