-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInputDetector.h
More file actions
192 lines (149 loc) · 5.66 KB
/
Copy pathInputDetector.h
File metadata and controls
192 lines (149 loc) · 5.66 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#pragma once
#include "Commons.h"
#include "SquareWaveOscillator.h"
#include <array>
extern PatchProcessor* getInitialisingPatchProcessor();
class InputDetector
{
private:
PatchCtrls* patchCtrls_;
PatchState* patchState_;
SquareWaveOscillator* osc_;
std::array<bool, 32> sequence = {
1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1,
0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0
};
std::array<bool, 5> read;
int p = 0;
float score_;
float lp_coefficient_;
float threshold_;
bool state_;
int normalization_detection_count_;
int normalization_detection_mismatches_;
uint32_t normalization_probe_state_;
const int kProbeSequenceDuration = 32;
public:
InputDetector(PatchCtrls* patchCtrls, PatchState* patchState)
{
patchCtrls_ = patchCtrls;
patchState_ = patchState;
osc_ = SquareWaveOscillator::create(patchState_->sampleRate);
score_ = 0.0f;
state_ = false;
threshold_ = 0;
lp_coefficient_ = 0;
normalization_detection_count_ = 0;
normalization_probe_state_ = 0;
normalization_detection_mismatches_ = 0;
}
~InputDetector()
{
SquareWaveOscillator::destroy(osc_);
}
static InputDetector* create(PatchCtrls* patchCtrls, PatchState* patchState)
{
return new InputDetector(patchCtrls, patchState);
}
static void destroy(InputDetector* obj)
{
delete obj;
}
/*
int findPattern(InfiniteStream* stream, vector<int>& pattern)
{
// Initialize hash values for the two halves of the pattern
long long firstHalfHash = 0;
long long secondHalfHash = 0;
int patternLength = pattern.size();
int halfLength = patternLength >> 1; // Divide by 2 using bit shift
// Create bit masks for the two halves
// These masks ensure we only keep the relevant bits when updating hashes
long long firstHalfMask = (1LL << halfLength) - 1;
long long secondHalfMask = (1LL << (patternLength - halfLength)) - 1;
// Build the hash for the first half of the pattern
// Each bit is shifted to its position in the hash
for (int i = 0; i < halfLength; ++i) {
firstHalfHash |= (long long)pattern[i] << (halfLength - 1 - i);
}
// Build the hash for the second half of the pattern
for (int i = halfLength; i < patternLength; ++i) {
secondHalfHash |= (long long)pattern[i] << (patternLength - 1 - i);
}
// Rolling hash values for the current window in the stream
long long currentFirstHalf = 0;
long long currentSecondHalf = 0;
// Process the stream bit by bit
for (int position = 1; ; ++position) {
// Get the next bit from the stream
int currentBit = stream->next();
// Update the second half hash by shifting left and adding the new bit
currentSecondHalf = (currentSecondHalf << 1) | currentBit;
// Extract the bit that will overflow from second half to first half
int overflowBit = (int)((currentSecondHalf >> (patternLength - halfLength)) & 1);
// Keep only the relevant bits in the second half
currentSecondHalf &= secondHalfMask;
// Update the first half hash with the overflow bit
currentFirstHalf = (currentFirstHalf << 1) | overflowBit;
currentFirstHalf &= firstHalfMask;
// Check if we have read enough bits and if the pattern matches
if (position >= patternLength &&
firstHalfHash == currentFirstHalf &&
secondHalfHash == currentSecondHalf) {
// Return the starting index of the pattern (0-indexed)
return position - patternLength;
}
}
}
*/
void Process(float x, float y)
{
// x is supposed to be centered!
score_ += lp_coefficient_ * (x * y - score_);
float hysteresis = state_ ? -0.05f * threshold_ : 0.0f;
state_ = score_ >= (threshold_ + hysteresis);
}
void Decode(FloatArray buffer)
{
}
void Encode(size_t size)
{
for (size_t i = 0; i < 32; i++)
{
for (size_t j = 0; j < size; j++)
{
float freq = sequence[i] * 2000 + 2000;
osc_->setFrequency(freq);
bool out = osc_->getSample() > 0;
getInitialisingPatchProcessor()->patch->setButton(IN_DETEC, out, 0);
}
}
}
inline void Process(AudioBuffer &buffer)
{
FloatArray left = buffer.getSamples(LEFT_CHANNEL);
Decode(left);
size_t size = buffer.getSize();
/*
for (size_t i = 0; i < size; i++)
{
bool expected_value = normalization_probe_state_ >> (size - 1);
bool read_value = left[i] > 0.25f;
if (expected_value != read_value)
{
++normalization_detection_mismatches_;
}
++normalization_detection_count_;
if (i == size - 1)
{
normalization_detection_count_ = 0;
bool destination = normalization_detection_mismatches_ >= 2;
normalization_detection_mismatches_ = 0;
}
normalization_probe_state_ = 1103515245 * normalization_probe_state_ + 12345;
getInitialisingPatchProcessor()->patch->setButton(IN_DETEC, normalization_probe_state_ >> (size - 1), 0);
}
*/
//Encode(size);
}
};