2 #include <Engine/Node/SimpleApi.hpp>
4 #include <ossia/detail/hash_map.hpp>
5 #include <ossia/detail/math.hpp>
6 #include <ossia/detail/ssize.hpp>
9 #if !defined(NDEBUG) && !defined(_MSC_VER) && !defined(__clang__)
10 #include <debug/vector>
11 #define debug_vector_t __gnu_debug::vector
13 #define debug_vector_t std::vector
53 namespace FactorOracle2MIDI
86 int suffix_transition_;
88 void singleTransitionResize() {
transition_.resize(10); }
95 int phi, k, fo_iter, current_state = 1;
96 std::vector<T> input_values;
98 std::vector<std::vector<int>>
102 this->states_.resize(2);
103 this->states_[0].state_ = 0;
104 this->states_[0].lrs_ = 0;
105 this->states_[0].suffix_transition_ = -1;
106 this->RevSuffix.resize(2);
117 this->states_.resize(i + 1);
118 this->RevSuffix.resize(i + 1);
119 T alpha = word[i - 1];
120 this->AddState(i - 1);
121 int state_m_plus_one = i;
122 this->AddTransition(i - 1, i, alpha);
123 k = this->states_[i - 1].suffix_transition_;
125 int flag = 0, iter = 0;
132 while(k > -1 && flag == 0)
134 while(iter < this->states_[k].transition_.size())
136 if(this->states_[k].transition_[iter].symbol_ == alpha)
144 this->AddTransition(k, state_m_plus_one, alpha);
146 k = this->states_[k].suffix_transition_;
152 this->states_[state_m_plus_one].suffix_transition_ = 0;
153 this->states_[state_m_plus_one].lrs_ = 0;
158 if(this->states_[k].transition_[iter].symbol_ == alpha)
161 this->states_[state_m_plus_one].suffix_transition_
162 = this->states_[k].transition_[iter].last_state_;
163 this->states_[state_m_plus_one].lrs_
164 = this->LengthCommonSuffix(
165 phi, this->states_[state_m_plus_one].suffix_transition_ - 1)
168 while(iter < this->states_[k].transition_.size() && flag == 0)
170 if(this->states_[k].transition_[iter].symbol_ == alpha)
173 this->states_[state_m_plus_one].suffix_transition_
174 = this->states_[k].transition_[iter].last_state_;
175 this->states_[state_m_plus_one].lrs_
176 = this->LengthCommonSuffix(
177 phi, this->states_[state_m_plus_one].suffix_transition_ - 1)
185 T temp_word = word[state_m_plus_one - this->states_[state_m_plus_one].lrs_ - 1];
186 k = this->FindBetter(state_m_plus_one, temp_word, word);
189 this->states_[state_m_plus_one].lrs_ = this->states_[state_m_plus_one].lrs_ + 1;
190 this->states_[state_m_plus_one].suffix_transition_ = k;
192 RevSuffix[this->states_[state_m_plus_one].suffix_transition_].push_back(
196 int LengthCommonSuffix(
int phi_one,
int phi_two)
198 if(phi_two == this->states_[phi_one].suffix_transition_)
199 return this->states_[phi_one].lrs_;
202 while(this->states_[phi_one].suffix_transition_
203 != this->states_[phi_two].suffix_transition_)
204 phi_two = this->states_[phi_two].suffix_transition_;
206 if(this->states_[phi_one].lrs_ <= this->states_[phi_two].lrs_)
207 return this->states_[phi_one].lrs_;
209 return this->states_[phi_two].lrs_;
221 int len_t = this->RevSuffix[this->states_[i].suffix_transition_].size();
222 int statei = this->states_[i].suffix_transition_;
225 sort(this->RevSuffix[statei].begin(), this->RevSuffix[statei].end());
226 for(
int j = 0; j < len_t; j++)
228 if(this->states_[this->RevSuffix[this->states_[i].suffix_transition_][j]].lrs_
229 == this->states_[i].lrs_
231 [this->RevSuffix[this->states_[i].suffix_transition_][j]
232 - this->states_[i].lrs_ - 1]
235 int output_midi = RevSuffix[this->states_[i].suffix_transition_][j];
250 std::random_device rd;
251 std::mt19937 gen(rd());
252 std::uniform_real_distribution<> dis(0.0, 1.0);
254 if(this->states_.size() == 2 || this->states_.size() == 1)
256 v.push_back(this->states_[0].transition_[0].symbol_);
263 int len = this->states_.size();
266 T w = this->states_[i].transition_[0].symbol_;
272 = this->states_[this->states_[i].suffix_transition_].transition_.size() - 1;
273 std::random_device rd;
274 std::mt19937 gen(rd());
275 std::uniform_int_distribution<> dis_int(0, lenSuffix);
276 int rand_alpha = dis_int(gen);
277 T alpha = this->states_[this->states_[i].suffix_transition_]
278 .transition_[rand_alpha]
280 i = this->states_[this->states_[i].suffix_transition_]
281 .transition_[rand_alpha]
298 int len = std::ssize(word);
299 this->states_.resize(2);
300 this->states_[0].state_ = 0;
301 this->states_[0].lrs_ = 0;
302 this->states_[0].suffix_transition_ = -1;
303 this->RevSuffix.resize(2);
305 void AddState(
int first_state) { this->states_[first_state].state_ = first_state; };
306 void AddTransition(
int first_state,
int last_state, T symbol)
308 SingleTransition<T> transition_i;
309 transition_i.first_state_ = first_state;
310 transition_i.last_state_ = last_state;
311 transition_i.symbol_ = symbol;
312 this->states_[first_state].transition_.push_back(transition_i);
314 std::vector<T> CallGenerate(
int len,
float q)
317 std::vector<T> oracle = {};
319 for(
int x = 0; x < len; x++)
321 oracle = this->FOGenerate(fo_iter, oracle, q);
329 namespace Nodes::FactorOracle2MIDI
335 static const constexpr
auto prettyName =
"New Factor Oracle MIDI";
336 static const constexpr
auto objectKey =
"New Factor Oracle MIDI";
337 static const constexpr
auto category =
"Control/Impro";
338 static const constexpr
auto author =
"Maria Paula Carrero Rivas";
339 static const constexpr
auto kind = Process::ProcessCategory::Mapping;
340 static const constexpr
auto description =
"Factor Oracle algorithm .";
341 static const constexpr
auto tags = std::array<const char*, 0>{};
342 static const uuid_constexpr
auto uuid
343 = make_uuid(
"C87B5326-56C2-4489-8E08-AA9E1EF27359");
345 static const constexpr
auto controls
347 static const constexpr midi_in midi_ins[]{
"input_midi"};
348 static const constexpr midi_out midi_outs[]{
"output_midi"};
349 static const constexpr value_in value_ins[]{
"regen",
"bang"};
356 debug_vector_t<libremidi::midi_bytes> sequence;
357 debug_vector_t<libremidi::midi_bytes> midi_bytes;
358 std::size_t sequence_idx{};
361 using control_policy = ossia::safe_nodes::last_tick;
363 run(
const ossia::midi_port& input_midi,
const ossia::value_port& regen,
364 const ossia::value_port& bangs,
int seq_len, ossia::midi_port& output_midi,
365 ossia::token_request, ossia::exec_state_facade,
State&
self)
369 for(
auto val : input_midi.messages)
372 temp.pitch = val.bytes[1];
373 self.oracle.input_values.push_back(val.bytes[1]);
374 self.oracle.AddLetter(
self.oracle.current_state,
self.oracle.input_values);
375 self.oracle.current_state =
self.oracle.current_state + 1;
378 if(!regen.get_data().empty())
380 if(!
self.oracle.input_values.empty())
382 std::vector<int> temp_vec =
self.oracle.CallGenerate(seq_len, 0.6);
384 self.midi_bytes.push_back({144, 0, 40});
385 self.midi_bytes[seq_len][1] = temp_vec[seq_len];
386 self.sequence =
self.midi_bytes;
390 if(!
self.sequence.empty() && !
self.oracle.input_values.empty())
392 for(
auto& bang : bangs.get_data())
394 self.sequence_idx = ossia::clamp<int64_t>(
395 (int64_t)
self.sequence_idx, 0, (int64_t)
self.sequence.size() - 1);
396 libremidi::message tmp;
397 tmp.bytes =
self.sequence[
self.sequence_idx];
398 tmp.timestamp = bang.timestamp;
399 output_midi.messages.push_back(tmp);
400 self.sequence_idx = (
self.sequence_idx + 1) %
self.sequence.size();
405 #undef debug_vector_t
Definition: FactorOracle2MIDI.hpp:93
std::vector< State< T > > states_
Definition: FactorOracle2MIDI.hpp:97
int FindBetter(int i, T alpha, std::vector< T > word)
Definition: FactorOracle2MIDI.hpp:211
FactorOracle2MIDI()
Definition: FactorOracle2MIDI.hpp:100
void FactorOracleStart(std::vector< T > word)
Definition: FactorOracle2MIDI.hpp:292
std::vector< std::vector< int > > RevSuffix
Definition: FactorOracle2MIDI.hpp:99
void AddLetter(int i, std::vector< T > word)
Definition: FactorOracle2MIDI.hpp:108
std::vector< T > FOGenerate(int &i, std::vector< T > v, float q)
Definition: FactorOracle2MIDI.hpp:241
Definition: FactorOracle2MIDI.hpp:58
Definition: FactorOracle2MIDI.hpp:67
int first_state_
Definition: FactorOracle2MIDI.hpp:69
T symbol_
Definition: FactorOracle2MIDI.hpp:71
int last_state_
Definition: FactorOracle2MIDI.hpp:70
std::vector< SingleTransition< T > > transition_
denotes the number of the state
Definition: FactorOracle2MIDI.hpp:85
int state_
denotes the number of the state
Definition: FactorOracle2MIDI.hpp:84
Utilities for OSSIA data structures.
Definition: DeviceInterface.hpp:33
Definition: score-lib-process/Control/Widgets.hpp:178
Definition: FactorOracle2MIDI.hpp:332