-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensorToAudio.cpp
More file actions
223 lines (194 loc) · 6.92 KB
/
SensorToAudio.cpp
File metadata and controls
223 lines (194 loc) · 6.92 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <thread>
#include <vector>
#include <array>
#include <cmath>
#include <algorithm>
#include <condition_variable>
#include <mutex>
#include <atomic>
#include <cassert>
#include <juce_audio_basics/juce_audio_basics.h>
#include "MovementIn.h"
#include "circular_buffer.h"
#include "GaussianNoise.cpp"
#include "Logger.h"
class SensorToAudio
{
public:
SensorToAudio(MovementIn& mi) : movementIn(mi)
{}
~SensorToAudio()
{
stopPolling();
// In case the thread is sleeping, waiting for data, wake them up
// TODO: make sure this can end in any order of execution
shouldStop = true;
dataAvailable.notify_one();
}
void startPolling()
{
if (!bufferThread.joinable())
{
shouldStop = false;
bufferThread = std::thread([this] { fillBuffer(); });
}
}
void stopPolling()
{
auto logger = Logger::getLogger();
static auto spid = Logger::createSignpost();
os_signpost_interval_begin(logger, spid, "SensorToAudio stop polling", "");
shouldStop = true;
dataAvailable.notify_one(); // Exit waitFlushBuffer
movementIn.stopPolling();
if (bufferThread.joinable())
bufferThread.join(); // Ensure the thread is joined before exiting
os_signpost_interval_end(logger, spid, "SensorToAudio stop polling", "");
}
void prepareToPlay(double sr, int smw, int abns)
{
sampleRate = sr;
sizeModelWindow = smw;
audioBufferNumSamples = abns;
// allocate cirtcular buffers, set freezing mode
sensorDataBuffer = std::make_unique<circular_buffer<float>[]>(channels);
for (size_t c(0); c < channels; c++)
{
// initialize to length of ceiling(audioBufferNumSamples/sizeModelWindow)
// integer arithmetic: (numerator + denominator - 1) / denominator
int bufferSize = (audioBufferNumSamples + sizeModelWindow - 1) / sizeModelWindow;
assert(bufferSize > 0);
sensorDataBuffer[c].initialize(static_cast<size_t>(bufferSize));
sensorDataBuffer[c].freezing = true;
}
}
void readBuffer(juce::AudioBuffer<float>& audioBuffer, int numSamples)
{
std::lock_guard<std::mutex> lock(bufferLock);
for (size_t c(0); c < channels; c++)
{
float* cPtr = audioBuffer.getWritePointer(static_cast<int>(c));
sensorDataBuffer[c].get(cPtr, numSamples);
}
}
bool full()
{
if (sensorDataBuffer)
{
std::lock_guard<std::mutex> lock(bufferLock);
return sensorDataBuffer[0].full();
}
return 0;
}
void shave(size_t newSize)
{
if (sensorDataBuffer)
{
std::lock_guard<std::mutex> lock(bufferLock);
for (size_t c(0); c < channels; c++)
sensorDataBuffer[c].shave(newSize);
}
}
void waitFlushBuffer(juce::AudioBuffer<float>& audioBuffer, int& samplesRead, std::atomic<bool>& shouldStopBackend)
{
std::unique_lock<std::mutex> lock(bufferLock);
if (!sensorDataBuffer) // buffer hasn't been created yet, problem
{
samplesRead = 0;
return;
}
if (sensorDataBuffer[0].empty()) // wait for data to arrive
{
dataAvailable.wait(lock, [&] {
return !sensorDataBuffer[0].empty() || shouldStop;
});
}
if (shouldStop)
{
shouldStopBackend = true;
return;
}
samplesRead = static_cast<int>(sensorDataBuffer[0].size());
for (size_t c(0); c < channels; c++)
{
float* cPtr = audioBuffer.getWritePointer(static_cast<int>(c));
sensorDataBuffer[c].get(cPtr, samplesRead);
}
}
private:
std::thread bufferThread;
std::atomic<bool> shouldStop{false};
// For signalling the backend of data availability
std::condition_variable dataAvailable;
MovementIn& movementIn;
double sampleRate;
int sizeModelWindow;
int audioBufferNumSamples;
static constexpr size_t channels = 16;
GaussianNoise noise{0.0f, 0.3f};
std::unique_ptr<circular_buffer<float>[]> sensorDataBuffer;
std::mutex bufferLock;
inline void sampleData()
{
// sample current sensor values to circular buffer
// retrieve JoyCon sensor data
auto [ljc, rjc] = movementIn.getJoyConData();
// Convert to range
std::array<float, 12> fjc;
auto tof = [](float val) { return val / 8000.0f; };
std::transform(&ljc.accelerometer[0], &ljc.accelerometer[0] + 6, fjc.begin(), tof);
std::transform(&rjc.accelerometer[0], &rjc.accelerometer[0] + 6, fjc.begin() + 6, tof);
// Construct sensor data array
std::array<float, channels> data = {
fjc[0], fjc[1], fjc[2],
fjc[6], fjc[7], fjc[8],
fjc[4], fjc[9], fjc[3],
fjc[10], fjc[11], fjc[5],
};
// fill last four channels with gaussian noise
for (size_t c(12); c < channels; c++)
data[c] = noise.generate();
// write to circular buffers
{
std::lock_guard<std::mutex> lock(bufferLock);
for (size_t c(0); c < channels; c++)
{
sensorDataBuffer[c].put(data[c]);
}
}
dataAvailable.notify_one(); // Wake up the backend thread
}
void fillBuffer()
{
auto logger = Logger::getLogger();
auto spid = Logger::createSignpost();
// time this so that one data point per model window is sampled
// NOTE: if this drifts, we could have a buffer of polling times produced by the audio callback
const auto pollingInterval = std::chrono::duration<double>(sizeModelWindow / sampleRate);
// weird way of getting types to match
auto nextTime = std::chrono::steady_clock::now() + pollingInterval;
nextTime -= pollingInterval;
while (!shouldStop)
{
os_signpost_interval_begin(logger, spid, "Wait for unmute", "");
if (movementIn.outputMuted())
{
// use some waiting mechanism to wait for mute button
shouldStop = movementIn.waitForUnmute();
}
os_signpost_interval_end(logger, spid, "Wait for unmute", "");
if (shouldStop)
break;
nextTime = std::chrono::steady_clock::now();
os_signpost_interval_begin(logger, spid, "Sample data", "");
while (!movementIn.outputMuted())
{
sampleData();
while (nextTime <= std::chrono::steady_clock::now())
nextTime += pollingInterval;
std::this_thread::sleep_until(nextTime);
}
os_signpost_interval_end(logger, spid, "Sample data", "");
}
}
};