-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspec.cpp
106 lines (97 loc) · 2.51 KB
/
spec.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
#include "spec.hpp"
#include <cassert>
#include <cmath>
#include <cstring>
#include <log/log.hpp>
#include <optional>
const auto SpectrSize = 8 * 4096;
Spec::Spec(std::span<float> wav)
: wav(wav), input(fftw_alloc_complex(SpectrSize)), output(fftw_alloc_complex(SpectrSize)), running(true), thread(std::thread(&Spec::run, this))
{
memset(input, 0, SpectrSize * sizeof(fftw_complex));
memset(output, 0, SpectrSize * sizeof(fftw_complex));
plan = fftw_plan_dft_1d(SpectrSize, input, output, FFTW_FORWARD, FFTW_MEASURE);
}
auto Spec::getSpec(int start, int end) const -> std::vector<float>
{
const auto key = std::make_pair(start, end);
std::lock_guard<std::mutex> lock(mutex);
auto it = range2Spec.find(key);
if (it != std::end(range2Spec))
{
age.erase(it->second.age);
age.push_front(key);
it->second.age = std::begin(age);
return it->second.spec;
}
jobs.insert(key);
age.push_front(key);
range2Spec.insert(std::make_pair(key, S{{}, std::begin(age)}));
if (range2Spec.size() > MaxRanges)
{
auto oldest = std::end(age);
--oldest;
range2Spec.erase(*oldest);
jobs.erase(*oldest);
age.pop_back();
}
return {};
}
auto Spec::internalGetSpec(int start, int end) const -> std::vector<float>
{
auto p = 0;
for (auto i = end - SpectrSize; i < end; ++i, ++p)
{
input[p][1] = 0;
if ((i >= static_cast<int>(wav.size()) || i < 0))
{
input[p][0] = 0;
continue;
}
if (i >= start)
input[p][0] = wav[i];
else
input[p][0] = expf(-2.5e-4f * (start - i)) * wav[i];
}
fftw_execute(plan);
std::vector<float> ret;
for (auto i = 0U; i < SpectrSize / 2; i++)
ret.push_back(
static_cast<float>(sqrt(output[i][0] * output[i][0] + output[i][1] * output[i][1]) / SpectrSize));
return ret;
}
auto Spec::run() -> void
{
while (running)
{
const auto job = [&]() -> std::optional<Range> {
std::lock_guard<std::mutex> lock(mutex);
if (jobs.empty())
return std::nullopt;
auto key = *jobs.begin();
jobs.erase(key);
return key;
}();
if (!job)
{
std::this_thread::sleep_for(std::chrono::milliseconds(20));
continue;
}
auto spec = internalGetSpec(job->first, job->second);
{
std::lock_guard<std::mutex> lock(mutex);
auto it = range2Spec.find(*job);
if (it == std::end(range2Spec))
continue;
it->second.spec = spec;
}
}
}
Spec::~Spec()
{
running = false;
thread.join();
fftw_destroy_plan(plan);
fftw_free(input);
fftw_free(output);
}