-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmentercpu.cpp
106 lines (94 loc) · 3.18 KB
/
segmentercpu.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 <cstring>
#include <stdexcept>
#include "segmentercpu.h"
static inline unsigned int ceil2(unsigned int v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
void SegmenterCPU::segment_data(float * data_out, int window_count)
{
//if (debug_mode & AFET_DEBUG_SAVE_BUFFERS)
// export_c_buffer(m_populate, (window_count - 1) * m_shift + m_window_size, 1, sizeof(short), "wave.dat");
for (int i = 0; i < window_count; i++)
{
for (int j = 0; j < m_window_size; j++)
{
data_out[m_window_size2 * i + j] = m_window[j] * m_tmpbuffer[i * m_shift + j];
}
}
}
void SegmenterCPU::init(int window_size, int shift, int window_limit, int deltasize)
{
m_window_size = window_size;
m_shift = shift;
m_deltasize = deltasize;
m_remaining_samples = 0;
m_samples = 0;
m_flushed = true;
m_last_calc_flushed = false;
m_window_size2 = ceil2((float)m_window_size);
m_buffer_size = window_limit * shift + window_size - shift;
m_tmpbuffer = new short[m_buffer_size];
m_window = new float[m_window_size];
}
void SegmenterCPU::cleanup()
{
delete[] m_tmpbuffer;
delete[] m_window;
}
void SegmenterCPU::set_window(const float * window)
{
memcpy(m_window, window, m_window_size * sizeof(float));
}
void SegmenterCPU::set_input(const short * data_in, float * data_out, int samples, int & window_count, int & window_count_no_delta)
{
m_last_calc_flushed = m_flushed;
if (m_last_calc_flushed)
{
memcpy(m_tmpbuffer, data_in, samples * sizeof(short));
window_count_no_delta = estimated_window_count(samples);
window_count = window_count_no_delta - m_deltasize;
if (window_count <= 0)
throw std::runtime_error("Can't process data, window count is too small");
segment_data(data_out, window_count_no_delta);
int processed_samples = (window_count - m_deltasize) * m_shift + m_window_size - m_shift;
if (processed_samples <= 0)
throw std::runtime_error("Processed samples <= 0, this should never happen");
m_remaining_samples = samples - processed_samples + m_window_size - m_shift;
memcpy(m_tmpbuffer, m_tmpbuffer + samples - m_remaining_samples, m_remaining_samples * sizeof(short));
m_flushed = false;
}
else
{
memcpy(m_tmpbuffer + m_remaining_samples, data_in, samples * sizeof(short));
samples += m_remaining_samples;
window_count_no_delta = estimated_window_count(samples);
window_count = window_count_no_delta - 2 * m_deltasize;
if (window_count > 0)
{
segment_data(data_out, window_count_no_delta);
}
else
window_count = 0;
int processed_samples = window_count * m_shift + m_window_size - m_shift;
m_remaining_samples = samples - processed_samples + m_window_size - m_shift;
memcpy(m_tmpbuffer, m_tmpbuffer + samples - m_remaining_samples, m_remaining_samples * sizeof(short));
}
m_samples = samples;
}
void SegmenterCPU::flush(float * data_out, int & window_count, int & window_count_no_delta)
{
m_flushed = true;
window_count_no_delta = estimated_window_count(m_remaining_samples);
window_count = window_count_no_delta - m_deltasize;
if (window_count <= 0)
return;
segment_data(data_out, window_count_no_delta);
}