|
1 | 1 | #include "../../../backend/backend.h"
|
2 | 2 | #include "../shared/circular_buffer.h"
|
3 | 3 | #include "c74_min.h"
|
| 4 | +#include <chrono> |
| 5 | +#include <semaphore> |
4 | 6 | #include <string>
|
5 | 7 | #include <thread>
|
6 | 8 | #include <vector>
|
@@ -50,8 +52,10 @@ class nn : public object<nn>, public vector_operator<> {
|
50 | 52 | std::vector<std::unique_ptr<float[]>> m_in_model, m_out_model;
|
51 | 53 |
|
52 | 54 | // AUDIO PERFORM
|
53 |
| - bool m_use_thread; |
| 55 | + bool m_use_thread, m_should_stop_perform_thread; |
54 | 56 | std::unique_ptr<std::thread> m_compute_thread;
|
| 57 | + std::binary_semaphore m_data_available_lock, m_result_available_lock; |
| 58 | + |
55 | 59 | void operator()(audio_bundle input, audio_bundle output);
|
56 | 60 | void perform(audio_bundle input, audio_bundle output);
|
57 | 61 |
|
@@ -151,14 +155,36 @@ void model_perform(nn *nn_instance) {
|
151 | 155 | in_model.push_back(nn_instance->m_in_model[c].get());
|
152 | 156 | for (int c(0); c < nn_instance->m_out_dim; c++)
|
153 | 157 | out_model.push_back(nn_instance->m_out_model[c].get());
|
| 158 | + |
154 | 159 | nn_instance->m_model->perform(in_model, out_model, nn_instance->m_buffer_size,
|
155 | 160 | nn_instance->m_method, 1);
|
156 | 161 | }
|
157 | 162 |
|
| 163 | +void model_perform_loop(nn *nn_instance) { |
| 164 | + std::vector<float *> in_model, out_model; |
| 165 | + |
| 166 | + for (auto &ptr : nn_instance->m_in_model) |
| 167 | + in_model.push_back(ptr.get()); |
| 168 | + |
| 169 | + for (auto &ptr : nn_instance->m_out_model) |
| 170 | + out_model.push_back(ptr.get()); |
| 171 | + |
| 172 | + while (!nn_instance->m_should_stop_perform_thread) { |
| 173 | + if (nn_instance->m_data_available_lock.try_acquire_for( |
| 174 | + std::chrono::milliseconds(200))) { |
| 175 | + nn_instance->m_model->perform(in_model, out_model, |
| 176 | + nn_instance->m_buffer_size, |
| 177 | + nn_instance->m_method, 1); |
| 178 | + nn_instance->m_result_available_lock.release(); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
158 | 183 | nn::nn(const atoms &args)
|
159 | 184 | : m_compute_thread(nullptr), m_in_dim(1), m_in_ratio(1), m_out_dim(1),
|
160 | 185 | m_out_ratio(1), m_buffer_size(4096), m_method("forward"),
|
161 |
| - m_use_thread(true) { |
| 186 | + m_use_thread(true), m_data_available_lock(0), m_result_available_lock(1), |
| 187 | + m_should_stop_perform_thread(false) { |
162 | 188 |
|
163 | 189 | m_model = std::make_unique<Backend>();
|
164 | 190 | m_is_backend_init = true;
|
@@ -261,9 +287,13 @@ nn::nn(const atoms &args)
|
261 | 287 | m_out_buffer[i].initialize(m_buffer_size);
|
262 | 288 | m_out_model.push_back(std::make_unique<float[]>(m_buffer_size));
|
263 | 289 | }
|
| 290 | + |
| 291 | + if (m_use_thread) |
| 292 | + m_compute_thread = std::make_unique<std::thread>(model_perform_loop, this); |
264 | 293 | }
|
265 | 294 |
|
266 | 295 | nn::~nn() {
|
| 296 | + m_should_stop_perform_thread = true; |
267 | 297 | if (m_compute_thread)
|
268 | 298 | m_compute_thread->join();
|
269 | 299 | }
|
@@ -318,24 +348,30 @@ void nn::perform(audio_bundle input, audio_bundle output) {
|
318 | 348 | }
|
319 | 349 |
|
320 | 350 | if (m_in_buffer[0].full()) { // BUFFER IS FULL
|
321 |
| - // IF USE THREAD, CHECK THAT COMPUTATION IS OVER |
322 |
| - if (m_compute_thread && m_use_thread) { |
323 |
| - m_compute_thread->join(); |
324 |
| - } |
| 351 | + if (!m_use_thread) { |
| 352 | + // TRANSFER MEMORY BETWEEN INPUT CIRCULAR BUFFER AND MODEL BUFFER |
| 353 | + for (int c(0); c < m_in_dim; c++) |
| 354 | + m_in_buffer[c].get(m_in_model[c].get(), m_buffer_size); |
325 | 355 |
|
326 |
| - // TRANSFER MEMORY BETWEEN INPUT CIRCULAR BUFFER AND MODEL BUFFER |
327 |
| - for (int c(0); c < m_in_dim; c++) |
328 |
| - m_in_buffer[c].get(m_in_model[c].get(), m_buffer_size); |
329 |
| - |
330 |
| - if (!m_use_thread) // PROCESS DATA RIGHT NOW |
| 356 | + // CALL MODEL PERFORM IN CURRENT THREAD |
331 | 357 | model_perform(this);
|
332 | 358 |
|
333 |
| - // TRANSFER MEMORY BETWEEN OUTPUT CIRCULAR BUFFER AND MODEL BUFFER |
334 |
| - for (int c(0); c < m_out_dim; c++) |
335 |
| - m_out_buffer[c].put(m_out_model[c].get(), m_buffer_size); |
| 359 | + // TRANSFER MEMORY BETWEEN OUTPUT CIRCULAR BUFFER AND MODEL BUFFER |
| 360 | + for (int c(0); c < m_out_dim; c++) |
| 361 | + m_out_buffer[c].put(m_out_model[c].get(), m_buffer_size); |
| 362 | + |
| 363 | + } else if (m_result_available_lock.try_acquire()) { |
| 364 | + // TRANSFER MEMORY BETWEEN INPUT CIRCULAR BUFFER AND MODEL BUFFER |
| 365 | + for (int c(0); c < m_in_dim; c++) |
| 366 | + m_in_buffer[c].get(m_in_model[c].get(), m_buffer_size); |
336 | 367 |
|
337 |
| - if (m_use_thread) // PROCESS DATA LATER |
338 |
| - m_compute_thread = std::make_unique<std::thread>(model_perform, this); |
| 368 | + // TRANSFER MEMORY BETWEEN OUTPUT CIRCULAR BUFFER AND MODEL BUFFER |
| 369 | + for (int c(0); c < m_out_dim; c++) |
| 370 | + m_out_buffer[c].put(m_out_model[c].get(), m_buffer_size); |
| 371 | + |
| 372 | + // SIGNAL PERFORM THREAD THAT DATA IS AVAILABLE |
| 373 | + m_data_available_lock.release(); |
| 374 | + } |
339 | 375 | }
|
340 | 376 |
|
341 | 377 | // COPY CIRCULAR BUFFER TO OUTPUT
|
|
0 commit comments