1 /*
2 * din.h
3 * DIN Is Noise is copyright (c) 2006-2021 Jagannathan Sampath
4 * DIN Is Noise is released under GNU Public License 2.0
5 * For more information, please visit https://dinisnoise.org/
6 */
7 
8 
9 #ifndef __din__
10 #define __din__
11 
12 #define RED_PSD RED << din::PSD
13 #define RED_A2D RED << din::A2D
14 #define TOGO " to go!"
15 
16 #define CLEAR_SELECTED_DRONES if (SHIFT || CTRL) ; else clear_selected_drones ();
17 
18 #define RESETALLCHUCKTRAILS if (drone::chuckt::autoresettrails) din0.resetallchucktrails ();
19 
20 #include "curve_editor.h"
21 #include "listeners.h"
22 #include "range.h"
23 #include "ui.h"
24 #include "audio.h"
25 #include "play.h"
26 #include "oscilloscope.h"
27 #include "curve_library.h"
28 #include "beat2value.h"
29 #include "help.h"
30 #include "phrasor.h"
31 #include "input.h"
32 #include "command.h"
33 #include "globals.h"
34 #include "random.h"
35 #include "drone.h"
36 #include "gravity.h"
37 #include "mesh.h"
38 #include "scale_info.h"
39 #include "instrument.h"
40 #include "alarm.h"
41 #include "textboard.h"
42 #include "spinner.h"
43 #include "din_info.h"
44 #include <list>
45 
46 extern int BOTTOM;
47 extern float VOLUME;
48 extern float DELTA_VOLUME;
49 extern char BUFFER[];
50 
51 struct checkbutton;
52 
53 struct din : instrument, scale_listener, region_listener, pointgotlis {
54 
55   // voice
56   multi_curve wave;
57   curve_editor waved;
58   wave_listener wavlis;
59   solver wavsol;
60   play wavplay;
61 	float step; // represents pitch (see note.h)
62 	float delta; // 0 - left of range, 1 - right of range
63 	std::string pitch_volume_info;
64 
65 	// noise
66 	noiser nsr;
67 
68   // ranges
69   std::vector<range> ranges;
70   int num_ranges, last_range, current_range;
71   range *firstr, *lastr;
72   int visl, visr;
73   void find_visible_ranges (int dir = 0);
74   int load_ranges ();
75 	void save_ranges ();
76   void create_ranges (int n);
77   void setup_ranges (int last_num_octaves, int load = 1);
78   void calc_all_range_notes ();
79 	void calc_added_range_notes (int last_num_octaves, int first_added_range);
80 	void reset_all_ranges ();
81   void delselran ();
82   void ranchkdro ();
83   void initranpar (int n);
84 
85 	// range ops
86   //
87   int range_right_changed (int r, int dx, int mr);
88   int range_left_changed (int r, int dx, int mr);
89 
90   struct adjustrangest {
91     int active;
92     int others;
adjustrangestdin::adjustrangest93     adjustrangest () {unset ();}
setdin::adjustrangest94     void set () {active = 1; others = 1;}
unsetdin::adjustrangest95     void unset () {active = 0; others = 1;}
96   } adjustranges;
97 
98   void set_range_width (int r, int w);
99   void set_range_width (int s, int t, int w);
100 	void set_range_height (int r, int h);
101 	void set_range_height (int s, int t, int h);
102   void default_range_to_all (int h = 0);
103   void default_range_to_selected (int h = 0);
104   void selected_range_to_all (int h = 0);
105 	int find_range (float x, int r);
106 	void find_current_range ();
107 	void notate_all_ranges ();
108 	void change_range_note (int i, int d);
109 
110 	// range modulation
111 	void init_range_mod (int s, int t);
112 	void pause_resume_ran_mod ();
113 	void set_ran_mod (int w);
114 	void toggle_ran_mod ();
115 	void update_range_mod_solvers (int type, multi_curve& mix);
116 	void update_drone_mod_solvers (int type, multi_curve& mix);
117 	void all_ranges_width_changed ();
118 	void all_ranges_height_changed ();
119 	void modulate_ranges ();
120 	void browse_range (int dr);
121 
122   int find_tone_and_volume ();
123   float get_note_value (const std::string& s);
124   void tuning_changed ();
125 
126   // microtonal_keyboard
127   //
128   box<int> win;
129   void scroll (int dx, int dy, int warp_mouse = 1);
130   int win_mousex, win_mousey;
131 	int prev_win_mousex, prev_win_mousey;
132 	int tonex, toney;
133   int delta_mousex, delta_mousey;
134   void calc_win_mouse ();
135   void set_key_to_pitch_at_cursor ();
136   void mouse2tonic ();
137   void height_changed (int r, int dh);
138 	void window_resized (int w, int h);
139 
140   //
141   // drones
142   //
143 
144   int num_drones;
145   std::string num_drones_info;
146   std::list<drone*> drones;
147 
148   std::vector<drone*> selected_drones;
149 	int num_selected_drones;
150 	void load_selected_drones (ifstream& f);
151 	void save_selected_drones (ofstream& f);
152 
153 	std::vector<drone*> browsed_drones;
154 	int num_browsed_drones;
155 	int browsed_drone;
156 	int last_browseable_drone;
157 	void select_all_browsed_drones (int bd);
158 
159   std::list<drone*> risers; // rising drones
160   std::list<drone*> fallers; // falling drones
161   std::list<drone*> launchers; // launch new drones
162   std::list<drone*> trackers; // drones that track other drones
163   std::list<drone*> attractors; // drones that attract other drones
164 	std::list<drone*> satellites; // launched drones due to orbit another drone
165 	std::list<drone*> gravitated; // drones that move under gravity
166 
167   // for mute/unmute drones and for drone <> noise conversions
168   struct gabber {
169     std::string what;
170     std::list<drone*> drones;
171     int n;
gabberdin::gabber172     gabber() : n(0) {}
173     void set (din* dd, float t, const string& w, float tdiv = 1.0f, int fid = 0);
174     void eval ();
175     void erase (drone* d);
176     void setgabt ();
177     void abort ();
178   } gab;
179 
180   void drone2noise ();
181   void noise2drone ();
182 
183   void setdronemastervolume (float d);
184   void setdronevol (spinner<float>& s);
185 
186   // drone waveform
187   multi_curve drone_wave;
188   curve_editor droneed;
189   wave_listener dronelis;
190 
191   // drone add
192   int adding;
193   void toggle_adding_drones ();
194   drone* add_drone (float wx, float wy, int fromlauncher = 0);
195 
196   // drone wand
197   int wanding;
198   point<int> wand;
199   int stopwanding ();
200 
201   // drone move
202   int moving_drones;
203   void start_moving_drones ();
204   void toggle_moving_drones ();
205   void set_moving_drones (int what);
206 
207   // drone set
208   void set_drone (drone& d, float wx, float wy);
209 	void movedrone (drone& d);
210 
211 	// drone delete
212 	void delete_drone (drone& ds);
213 	void delete_selected_drones ();
214   int delete_all_drones ();
215 
216 	// drone load & save
217 	void load_drones ();
218 	void save_drones ();
219 
220   // drone selection and browsing
221   //
222 	void clear_selected_drones ();
223   int select_all_drones ();
224   void invert_selected_drones ();
225   void add_drone_to_selection (drone* d);
226   void remove_drone_from_selection (drone* d);
227   std::string get_selected_drones ();
228 	void browse_drone (int db);
229   int is_drone_hit (drone& di, const box<float>& _rgn);
230   void calc_selector_range (const box<float>& _rgn, int& sell, int& selr);
231 	void find_selected_drones (const box<float>& _rgn);
232   void print_selected_drones ();
print_num_dronesdin233   inline void print_num_drones () {
234     sprintf (BUFFER, "%d drones", num_drones);
235     num_drones_info = BUFFER;
236   }
237 	int select_launchers ();
238   void seloncre (drone* d);
239 
240 	// scale/rotate drones
241 	//
242 
243 	std::vector< point<float> > rvec, svec; // rotation, scale vectors
244 	float angle; // rotate by angle
245 	point<float> scl; // scale x, y factors
246   void gotpoint (); // for center of scaling/rotation
247 	void calc_drones_centroid ();
248 	void calc_xform_vectors (std::vector<point<float> >& V, int n);
249   void calc_xform_vectors ();
250 	int prep_rotate_drones ();
251 	void rotate_drones (float da);
252 	void rotate_drones ();
253 	int prep_scale_drones ();
254 	void scale_drones (float ds);
255 	void scale_drones ();
256 	enum {NONE, SCALE, ROTATE};
257 	int xforming;
258 	void resize_xform_vectors ();
259 
260   // drone tone
261   void update_drone_tone ();
262   void update_drone_x (int i, int j);
263   void update_drone_players ();
264   void update_drone_solvers (multi_curve& crv);
265   void update_drone_ranges ();
266   void update_drone_anchors ();
267 	void refresh_all_drones ();
268 	void refresh_drones (int r1, int r2);
269 	void refresh_drones (int r);
270 
271   // drone orbit
272   void orbit_selected_drones ();
273   void attract_drones ();
274   void select_attractors ();
275   void select_attractees ();
276   void remove_attractee (drone* d);
277 	void change_drone_vel (spinner<float>& s);
278 	void rotate_drone_vel (spinner<float>& s);
279   void change_drone_accel (spinner<float>& s);
280 
281 	// drone modulation
282   enum {MODULATE_DRONES, MODULATE_VOICE}; // target
283   enum {AM, FM}; // type
284 	int modulate_what;
285   void change_drone_bpm (int what, float delta);
286 	void change_drone_bpm (int what, spinner<float>& s);
287   void change_drone_depth (int what, float delta);
288 	void change_drone_depth (int what, spinner<float>& s);
289 	void modulate_drones ();
290 
291   // auto rotation of velocity/acceleration vectors used as AM/FM direction
292   void setautorot (int which, int state, int tog = 0); // velocity/acceleration, yes/no, toggle?
293   void setautorotparam (int which, int what);
294   void setautorotdir (int which, int dir);
295   void setautoflip (int which, int state, int tog = 0);
296   void setautoflipangle (int which);
297 
298   // drone death
299   void mortalize_drones (int reincarnate = 0);
300   void immortalize_drones ();
301 
302 	// drone visuals
303   void change_drone_trail_points (spinner<int>& s);
304   void change_drone_handle_size (spinner<int>& s);
305   void change_drone_arrow (spinner<float>& s, int w);
306   void capdronearrows (int c);
307 	void reset_drone_arrows ();
308 	void trail_drones ();
309 
310 	// drone ops
311 	//
312   int rising, falling;
313   void rise_drones ();
314   void fall_drones ();
315 
316 	void sync_drones ();
317 	int freeze_drones ();
318 	int thaw_drones ();
319 	int freeze_orbiters ();
320 	int thaw_orbiters ();
321 	void toggle_freeze_drones ();
322 
323 	void snap_drones (int v);
324 	void pos_afx_vel (int v);
325 
326 	// drone connections
327 	vector<int> stepz;
328 	int nstepz;
329 	void calc_stepz (const std::string& fld);
330 	int connect_drones ();
331 	int disconnect_drones ();
332 	void remove_connections (drone* d);
333 	int can_connect (drone* d1, drone* d2);
334 	drone *ec;
335 
336 	// connection drawing
337 	int con_size;
338 	float* con_clr;
339 	float* con_pts;
340 	int totcon, _2totcon;
341 	void alloc_conns ();
342 	void draw_connections ();
343 
344   // drone > noise > drone
345 	void noise_interpolator_changed ();
346 
347   // launchers
348   //
349   void launch_drones ();
350 	void make_launcher (drone* pd);
351   void make_launchers ();
352   void destroy_launchers ();
353   void toggle_launchers ();
354   void change_drones_per_min (spinner<float>& s);
355   void balloon (drone* d);
356 
357   void make_trackers ();
358   void remove_tracker (drone* ptd);
359   void track_drones ();
360   void select_tracked_drones ();
361 
362 	void set_targets ();
363 	void clear_targets ();
364 	void remove_drone_from_targets (drone* T);
365 	void carry_satellites_to_orbit ();
366 	void kill_old_drones ();
367 	void change_drone_lifetime (spinner<float>& s);
368 	void change_orbit_insertion_time (spinner<float>& s);
369 
370   // gravity
371   //
372   void set_drones_under_gravity ();
373   void move_drones_under_gravity ();
374   void evalgravity ();
375 
376   // drone mesh
377   //
378 	struct _meshh { // settings
379 		int create;
380 		int draw;
381 		int num;
_meshhdin::_meshh382 		_meshh () : create (0), draw (1), num (0) {}
383 	} meshh;
384   void toggle_create_mesh ();
385   void create_drone_mesh ();
386 	void stop_creating_mesh ();
387   std::list<mesh> meshes;
388   void remove_drone_from_mesh (drone* pd);
389 	void remove_drone_from_pre_mesh (drone* d);
390 	add_mesh_drone amd;
391 
392 	// drone pendulum
393 	//
394 	std::vector<drone_pendulum_group*> drone_pendulums;
395 	int create_drone_pend;
396 	void create_drone_pendulum ();
397 	void toggle_create_drone_pendulum ();
398 	void stop_creating_drone_pendulum ();
399 	void update_drone_pendulums ();
400 	void update_drone_pendulum (drone_pendulum_group& g);
401 	void remove_from_groups (drone* d);
402 	void load_drone_pendulum_groups (ifstream& f);
403 	void save_drone_pendulum_groups (ofstream& f);
404 
405 	void toggle_create_this (); // mesh or pendulum?
406 
407 	// visuals
408 	textboard tb_hz_vol;
409   void draw_drones ();
410 	void draw_vol_dist ();
411 	void draw_pitch_dist ();
412   void set_drone_volume (int i, float v);
413 	void color_selected_drones ();
414 
415 	// utils
416   drone* get_drone (int id);
417 	int get_selected_drone_id (drone* d);
418 
419   // modulation
420   //
421 
422 	// voice
423 	float fm_step, fm_depth;
424   float am_depth, am_vol;
425   beat2value fm, am;
426 	void switch_modulation (); // voice or drones
427 	void prep_modulate (int op_code);
428 	void change_am_depth (float d);
429 	void change_fm_depth (float d);
430 	void change_am_bpm (float b);
431 	void change_fm_bpm (float b);
432 
433 	// voice + drones
434 	void change_depth (int type, float d);
435 	void change_bpm (beat2value& which, float amt);
436 	void change__bpm (int type, beat2value& bv2, float amt);
437 	void change__depth (int drone_arg1, float amt1, int voice_arg2, float amt2);
438 
439   // am & fm modulation editors
440   curve_editor moded;
441   beat2value_listener fmlis, amlis;
442 
443   // for deltas of FM/AM
444   struct delta_t {
445     float depth, min_depth;
446     float bpm, min_bpm;
447     delta_t (float _depth = 1, float _bpm = 1);
448   };
449 
450   delta_t am_delta, fm_delta, gater_delta;
451   delta_t dam_delta, dfm_delta;
452   delta_t* p_am_delta;
453   delta_t os_delta;
454   void raise_delta (float& d, float dd, const std::string& mesg);
455   void lower_delta (float& d, float dd, const std::string& mesg, float minn = 1);
456 
457 	// gater
458 	//
459 	beat2value gatr;
460   int calc_am_fm_gater ();
461 
462   // gater editor
463   curve_editor gated;
464   curve_library gatlib;
465   beat2value_listener gatrlis;
466 
467 	float fdr_gater_prev_amount;
468 
469   // phrasor - record/play/scratch a phrase
470   phrasor phrasor0;
471   void do_phrase_recording ();
472   int finish_phrase_recording ();
473   void clear_all_phrases ();
474 
475 
476   din (cmdlist& cmdlst);
477   ~din ();
478 
479   void setup ();
480   int handle_input ();
481 
482 	// scale handling
483 	void tonic_changed ();
484 	void scale_changed ();
485 	void scale_loaded ();
486   void load_scale (int _load_drones = 0, int _load_ranges = 1);
487   void save_scale ();
488 
489 	// audio handling
490   void sample_rate_changed ();
491   void samples_per_channel_changed ();
492   int render_audio (float* out0, float* out1);
493 
494   // OpenGL
495   float* dvap; int n_dvap;
496   float* dcol; int n_dcol;
497   float* dap; int n_dap;
498   int gl_pts[8];
499 
500   // ui
501   void toggle_this (int& what, checkbutton& cb);
502 	box<float> rgn;
503 	void region_begin ();
504 	void region_end ();
505 	void region_abort ();
506 	const box<float>& region_update ();
507 
508 	static const std::string PSD, A2D;
509 
510 	// din saved info
511 	din_info dinfo;
512 
513 	// help
514 	help helptext;
515 
516 	scale_info* ptr_scaleinfo;
517 
518 	void enter ();
519 	void bg (); // background exec
520 	void draw ();
521 
522   rnd<float> rndr, rndg, rndb;
523   void set_random_color ();
524 
525 #ifdef __SVG__
526   void write_trail ();
527 #endif
528 
529   void chuck ();
530   void evalchuck ();
531   void changechucklength (spinner<float>&);
532   void changechuckspeed (spinner<float>&);
533   void flipchuckspeed ();
534   void togchuckspeed ();
535   void resetchucktrails (drone& d);
536   void resetallchucktrails ();
537   void drawchuck ();
538 
539   void setmoddir (int w, int d);
540 
541   void setvelaccel (int w, int id, int neg);
542 
543 
544 	/*void dirty_connection (drone* d);
545 	void dirty_connections ();
546 	vector<connect> conns;*/
547 
548 	/*struct ringt {
549 		float x, y;
550 		float r;
551 	} ring;
552 	float butt;
553 	float inter_butt;
554 	int butting;
555 	void butt_drones ();
556 
557   point<int> wand0; */
558 
559 };
560 
561 #define NUM_ORDERERS 7
562 #define LAST_ORDERER (NUM_ORDERERS - 1)
563 
564 struct mkb_selector_t : box_selector, mesh_data {
565 
566 	ascending_rows_orderer of_asc;
567 	ascending_cols_orderer of_asc_cols;
568 	descending_rows_orderer of_desc;
569 	descending_cols_orderer of_desc_cols;
570 	random_orderer of_rnd;
571 	proximity_orderer of_prox_near, of_prox_far;
572 	orderer_t* orderers [NUM_ORDERERS];
573 	mkb_selector_t ();
574 
575   int lmb_clicked;
576   int handle_input ();
577 	void draw (const box<float>& rgn);
get_mesh_dronemkb_selector_t578   drone* get_mesh_drone (unsigned int i) {
579     if (i < meshd.size ()) return meshd[i];
580     return 0;
581   }
582 
583 };
584 
585 extern mkb_selector_t mkb_selector;
586 extern din din0;
587 
588 void make_arrow (float* A, int k, int cap, int& n, float x, float y, float ux, float uy, float vx, float vy, float u, float v, float t = 1.0f);
589 
590 #endif
591