2024-10-22 01:55:31 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "datatypes.h"
|
2024-10-30 14:55:24 +01:00
|
|
|
#include "graph-manager.h"
|
2024-11-01 02:38:47 +01:00
|
|
|
#include "midi-node.h"
|
2024-10-22 01:55:31 +02:00
|
|
|
#include "signal.h"
|
|
|
|
|
#include "midi.h"
|
|
|
|
|
|
|
|
|
|
#include <chrono>
|
2024-11-01 02:38:47 +01:00
|
|
|
#include <memory>
|
2024-10-22 01:55:31 +02:00
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
class SignalManager{
|
|
|
|
|
|
|
|
|
|
public:
|
2024-10-25 16:41:37 +02:00
|
|
|
SignalManager() {}
|
2024-10-31 01:43:00 +01:00
|
|
|
SignalManager(GraphManager *gm, std::mutex *mutex) : mutex(mutex), gm(gm) {}
|
2024-10-22 01:55:31 +02:00
|
|
|
|
2024-10-25 16:41:37 +02:00
|
|
|
Midi midi;
|
|
|
|
|
|
|
|
|
|
std::mutex *mutex;
|
2024-10-30 14:55:24 +01:00
|
|
|
GraphManager *gm;
|
2024-10-22 01:55:31 +02:00
|
|
|
|
2024-11-01 02:38:47 +01:00
|
|
|
std::vector<std::shared_ptr<Signal>> signals;
|
2024-10-22 01:55:31 +02:00
|
|
|
|
|
|
|
|
bool loop = true;
|
2024-10-25 02:43:41 +02:00
|
|
|
bool play = false;
|
2024-11-01 02:38:47 +01:00
|
|
|
|
2024-10-25 02:43:41 +02:00
|
|
|
void pause(){
|
|
|
|
|
play = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stop(){
|
|
|
|
|
play = false;
|
|
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void start(){
|
|
|
|
|
play = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void reset(){
|
|
|
|
|
for(auto &s : signals){
|
2024-11-01 02:38:47 +01:00
|
|
|
s->position = s->start->position + Vec2D<float>{0.1, 0.1};
|
|
|
|
|
s->next = s->start;
|
|
|
|
|
s->last = s->start;
|
2024-10-25 02:43:41 +02:00
|
|
|
}
|
2024-10-30 14:55:24 +01:00
|
|
|
gm->reset();
|
2024-10-25 02:43:41 +02:00
|
|
|
}
|
2024-10-22 01:55:31 +02:00
|
|
|
|
|
|
|
|
void startSignalThread(){
|
|
|
|
|
std::thread t1(&SignalManager::updateSignals, this);
|
|
|
|
|
t1.detach();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stopSignalThread(){
|
|
|
|
|
loop = false;
|
|
|
|
|
}
|
2024-10-25 16:41:37 +02:00
|
|
|
|
2024-11-01 02:38:47 +01:00
|
|
|
void addSignalAtNode(std::shared_ptr<Node> node){
|
2024-10-27 00:40:57 +02:00
|
|
|
Color c = {(unsigned char)(20 + (rand() % 200)), 100, (unsigned char)(20 + (int)(rand() % 200)), 200};
|
2024-11-01 02:38:47 +01:00
|
|
|
signals.emplace_back(std::make_shared<Signal>(node->position, 8.0f, 0, c)); // position + 1 or else it does not render. why? no idea!
|
|
|
|
|
signals.back()->next = node;
|
|
|
|
|
signals.back()->last = node;
|
|
|
|
|
signals.back()->start = node;
|
2024-10-22 01:55:31 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-22 03:07:11 +02:00
|
|
|
void removeSignal(int n){
|
|
|
|
|
signals.erase(signals.begin() + n);
|
2024-10-22 01:55:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateSignals(){
|
2024-10-28 02:07:47 +01:00
|
|
|
auto last = std::chrono::high_resolution_clock::now();
|
2024-10-22 01:55:31 +02:00
|
|
|
while(loop){
|
2024-10-28 02:07:47 +01:00
|
|
|
std::chrono::duration<double, std::micro> diff = std::chrono::high_resolution_clock::now() - last;
|
|
|
|
|
if((int)diff.count() >= 250){
|
2024-10-25 16:46:28 +02:00
|
|
|
mutex->lock();
|
2024-10-25 02:43:41 +02:00
|
|
|
if(play){
|
|
|
|
|
for(auto& s : signals){
|
2024-11-01 02:38:47 +01:00
|
|
|
s->direction = (s->next->position - s->position).norm();
|
|
|
|
|
s->update(diff.count());
|
|
|
|
|
if(s->position.dist(s->next->position) < 0.6f) {
|
|
|
|
|
std::shared_ptr<Node> next_node = gm->next(s->next, s->last);
|
|
|
|
|
s->next->trigger();
|
|
|
|
|
|
|
|
|
|
if(next_node != s->next && next_node != std::shared_ptr<Node>(nullptr) && s->next->getNodeType() == NodeType::MIDINODE) {
|
|
|
|
|
auto midinode = std::dynamic_pointer_cast<MidiNode>(s->next);
|
|
|
|
|
for(auto &m : midinode->midi_outs){
|
2024-10-28 02:07:47 +01:00
|
|
|
if(m.second.enabled) {
|
2024-11-01 02:38:47 +01:00
|
|
|
if(midinode->send_note) midi.note(m.second.i, midinode->midi_note_channel, midinode->midi_note, midinode->midi_velocity, midinode->midi_gate);
|
|
|
|
|
if(midinode->send_cc) midi.cc(m.second.i, midinode->midi_cc_channel, midinode->midi_cc, midinode->midi_cc_value);
|
|
|
|
|
if(midinode->send_pitch_bend) midi.pitch_bend(m.second.i, midinode->midi_pitch_bend_channel, midinode->midi_pitch_bend);
|
2024-10-28 02:07:47 +01:00
|
|
|
}
|
2024-10-25 02:43:41 +02:00
|
|
|
}
|
2024-11-01 02:38:47 +01:00
|
|
|
s->last = s->next;
|
|
|
|
|
s->next = next_node;
|
2024-10-22 01:55:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-25 16:46:28 +02:00
|
|
|
midi.update();
|
|
|
|
|
mutex->unlock();
|
2024-10-28 02:07:47 +01:00
|
|
|
last = std::chrono::high_resolution_clock::now();
|
2024-10-22 01:55:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-01 02:38:47 +01:00
|
|
|
void drawSignals(Vec2D<float> mouse_pos, Camera2D camera){
|
|
|
|
|
for(auto s : signals){
|
2024-10-28 02:07:47 +01:00
|
|
|
mutex->lock();
|
2024-11-01 02:38:47 +01:00
|
|
|
s->draw(mouse_pos, camera);
|
2024-10-28 02:07:47 +01:00
|
|
|
mutex->unlock();
|
2024-10-22 01:55:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|