more echo params, doc updates and more - alex
[terminatorX.git] / src / tX_vtt.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999, 2000  Alexander K├Ânig
4  
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9  
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14  
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  
19     File: tX_vtt.cc
20  
21     Description: This implements the new virtual turntable class. It replaces
22                  the old turntable.c from terminatorX 3.2 and earlier. The lowpass
23                  filter is based on some sample code by Paul Kellett
24                  <paul.kellett@maxim.abel.co.uk>
25                  
26     08 Dec 1999 - Switched to the new audiofile class            
27 */    
28    
29 #include "tX_vtt.h"
30 #include "tX_global.h"
31 #include <stdio.h>
32 #include "malloc.h"
33 #include <math.h>
34 #include "tX_mastergui.h"
35 #include "tX_sequencer.h"
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #ifdef USE_3DNOW
41 #include "3dnow.h"
42 #endif
43
44
45 #ifdef DEBUG
46 #define tX_freemem(ptr, varname, comment); fprintf(stderr, "** free() [%s] at %08x. %s.\n", varname, ptr, comment); free(ptr);
47 #define tX_malloc(ptr, varname, comment, size, type); fprintf(stderr, "**[1/2] malloc() [%s]. Size: %i. %s.\n", varname, size, comment); ptr=type malloc(size); fprintf(stderr, "**[2/2] malloc() [%s]. ptr: %08x.\n", varname, ptr);
48 #else
49 #define tX_freemem(ptr, varname, comment); free(ptr);
50 #define tX_malloc(ptr, varname, comment, size, type); ptr=type malloc(size);
51 #endif
52
53 #include "tX_loaddlg.h"
54
55 #define USE_PREFETCH 1
56
57 #ifdef USE_PREFETCH
58 #define my_prefetch(base, index); __asm__  __volatile__ ("prefetch index(%0)\n" : : "r" (base));
59 #define my_prefetchw(base, index); __asm__  __volatile__ ("prefetchw index(%0)\n" : : "r" (base));
60 #else
61 #define my_prefetch(base, index);  /* NOP */;
62 #define my_prefetchw(base, index); /* NOP */;
63 #endif
64
65 extern void build_vtt_gui(vtt_class *);
66 extern void gui_set_name(vtt_class *vtt, char *newname);
67 extern void gui_set_filename(vtt_class *vtt, char *newname);
68 extern void delete_gui(vtt_class *vtt);
69 extern void gui_update_display(vtt_class *vtt);
70 extern void gui_clear_master_button(vtt_class *vtt);
71 extern void cleanup_vtt(vtt_class *vtt);
72 extern int vg_get_current_page(vtt_class *vtt);
73 extern void vg_set_current_page(vtt_class *vtt, int page);
74
75 int vtt_class::vtt_amount=0;
76 list <vtt_class *> vtt_class::main_list;
77 list <vtt_class *> vtt_class::render_list;
78 int16_t* vtt_class::mix_out_buffer=NULL;
79 f_prec * vtt_class::mix_buffer=NULL;
80 f_prec * vtt_class::mix_buffer_end=NULL;
81
82 int vtt_class::samples_in_mix_buffer=0;
83 pthread_mutex_t vtt_class::render_lock=PTHREAD_MUTEX_INITIALIZER;
84 pthread_mutex_t vtt_class::main_lock=PTHREAD_MUTEX_INITIALIZER;
85 f_prec vtt_class::master_volume=1.0;
86 f_prec vtt_class::res_master_volume=1.0;
87 //f_prec vtt_class::saturate_fac=((f_prec) SAMPLE_MAX-SAMPLE_BORDER)*1.0/FLT_MAX;
88 f_prec vtt_class::saturate_fac=0.1;
89 int vtt_class::do_saturate=0;
90 vtt_class * vtt_class::sync_master=NULL;
91 int vtt_class::master_triggered=0;
92 int vtt_class::master_triggered_at=0;
93 vtt_class * vtt_class::focused_vtt=NULL;
94 f_prec vtt_class::mix_max_l=0;
95 f_prec vtt_class::mix_max_r=0;
96 f_prec vtt_class::vol_channel_adjust=1.0;
97
98 #define GAIN_AUTO_ADJUST 0.8
99
100 vtt_class :: vtt_class (int do_create_gui)
101 {       
102         vtt_amount++;
103         sprintf (name, "Turntable %i", vtt_amount);
104         strcpy(filename, "NONE");
105         buffer=NULL;
106         samples_in_buffer=0;
107         
108         set_volume(1);
109         set_pitch(1);
110         
111         autotrigger=1;
112         loop=1;
113         
114         is_playing=0;
115         is_sync_master=0;
116         is_sync_client=0;
117         sync_cycles=0,
118         sync_countdown=0;
119         
120         x_control=CONTROL_SCRATCH;
121         y_control=CONTROL_CUTOFF;
122         
123         lp_enable=0;
124         lp_reso=0.8;
125         lp_freq=0.3;
126         lp_gain=1;
127         lp_setup(lp_gain, lp_reso, lp_freq);
128         lp_reset();
129         
130         ec_enable=0;
131         ec_length=0.5;
132         ec_feedback=0.3;
133         ec_clear_buffer();
134         ec_set_length(0.5);
135         ec_set_pan(0);
136         ec_set_volume(1);
137         
138 //      pthread_mutex_lock(&main_lock);
139         main_list.push_back(this);
140 //      pthread_mutex_unlock(&main_lock);
141
142
143         /* "connecting" the seq-parameters */
144         
145         sp_speed.set_vtt((void *) this);
146         sp_volume.set_vtt((void *) this);       
147         sp_pitch.set_vtt((void *) this);        
148         sp_pan.set_vtt((void *) this);
149         sp_trigger.set_vtt((void *) this);      
150         sp_loop.set_vtt((void *) this); 
151         sp_sync_client.set_vtt((void *) this);  
152         sp_sync_cycles.set_vtt((void *) this);  
153         sp_lp_enable.set_vtt((void *) this);    
154         sp_lp_gain.set_vtt((void *) this);      
155         sp_lp_reso.set_vtt((void *) this);      
156         sp_lp_freq.set_vtt((void *) this);      
157         sp_ec_enable.set_vtt((void *) this);    
158         sp_ec_length.set_vtt((void *) this);
159         sp_ec_pan.set_vtt((void *) this);
160         sp_ec_volume.set_vtt((void *) this);
161         sp_ec_feedback.set_vtt((void *) this);          
162         sp_mute.set_vtt((void *) this);
163         sp_spin.set_vtt((void *) this);
164
165         x_par = &sp_speed;
166         y_par = &sp_lp_freq;
167         
168         lp_fx=new vtt_fx_lp();
169         lp_fx->set_vtt((void *) this);
170         fx_list.push_back(lp_fx);
171
172         ec_fx=new vtt_fx_ec();
173         ec_fx->set_vtt((void *) this);
174         fx_list.push_back(ec_fx);
175         
176         if (do_create_gui)
177         {       
178                 build_vtt_gui(this);
179                 lp_fx->set_panel_widget(gui.lp_panel->get_widget());    
180                 ec_fx->set_panel_widget(gui.ec_panel->get_widget());
181         }
182         else have_gui=0;
183                 
184         set_pan(0);     
185         set_master_volume(globals.volume);
186         set_output_buffer_size(samples_in_mix_buffer/2);
187         
188         audiofile = NULL;       
189 }
190
191 vtt_class :: ~vtt_class()
192 {
193         vtt_fx *effect;
194         stop();
195 //      pthread_mutex_lock(&main_lock);
196         main_list.remove(this);
197 //      pthread_mutex_unlock(&main_lock);
198         if (audiofile) delete audiofile;
199         //if (buffer) free(buffer);
200         if (output_buffer) tX_freemem(output_buffer, "output_buffer", "vtt Destructor");
201         vtt_amount--;
202         
203         while (fx_list.size())
204         { 
205                 effect=(*fx_list.begin());
206                 fx_list.remove(effect);
207                 delete effect;
208         }
209         
210         delete_gui(this);
211 }
212
213 void vtt_class :: set_name(char *newname)
214 {
215         strcpy(name, newname);
216         gui_set_name(this, name);       
217 }
218
219 int vtt_class :: load_file(char *fname)
220 {
221         int res;
222         int was_playing=is_playing;
223         
224         if (is_playing) stop();
225
226         if (audiofile) delete(audiofile);
227         
228         buffer=NULL;
229         samples_in_buffer=0;
230         maxpos=0;
231         strcpy(filename,"");
232
233         audiofile=new tx_audiofile();
234         res=audiofile->load(fname);     
235         
236         if (res==TX_AUDIO_SUCCESS)
237         {
238                 buffer=audiofile->get_buffer();
239                 samples_in_buffer=audiofile->get_no_samples();
240                 maxpos=audiofile->get_no_samples();
241                 strcpy(filename, fname);
242                 if (was_playing) trigger();
243 //              printf("Successfully loaded %s, %08x, %i\n", fname, buffer, samples_in_buffer);
244         }
245         
246         if (have_gui)
247         {
248                 gui_update_display(this);
249         }
250         ec_set_length(ec_length);
251         
252         return(res);
253 }
254
255 int vtt_class :: set_output_buffer_size(int newsize)
256 {
257         list <vtt_fx *> :: iterator effect;
258
259         if (ec_output_buffer) tX_freemem(ec_output_buffer, "ec_output_buffer", "vtt set_output_buffer_size()");
260         tX_malloc(ec_output_buffer, "ec_output_buffer", "vtt set_output_buffer_size()", sizeof(float)*newsize, (float *));
261
262         if (output_buffer) tX_freemem(output_buffer, "output_buffer", "vtt set_output_buffer_size()");
263         //output_buffer = (float *) malloc (sizeof(float)*newsize);
264         tX_malloc(output_buffer, "output_buffer", "vtt set_output_buffer_size()", sizeof(float)*newsize, (float *));
265         end_of_outputbuffer = output_buffer + newsize; //size_t(sizeof(float)*(newsize));
266         
267         samples_in_outputbuffer=newsize;
268         inv_samples_in_outputbuffer=1.0/samples_in_outputbuffer;
269
270         for (effect=fx_list.begin(); effect != fx_list.end(); effect++)
271         {
272                 (*effect)->reconnect_buffer();
273         }
274         
275         if (output_buffer) return(0);   
276         else return(0);
277 }
278
279 void vtt_class :: set_volume(f_prec newvol)
280 {
281         rel_volume=newvol;
282         recalc_volume();
283 }
284
285 void vtt_class :: recalc_volume()
286 {
287         res_volume=rel_volume*res_master_volume;
288         f_prec ec_res_volume=res_volume*ec_volume;
289         
290         if (pan>0.0)
291         {
292                 res_volume_left=(1.0-pan)*res_volume;
293                 res_volume_right=res_volume;
294         }
295         else if (pan<0.0)
296         {
297                 res_volume_left=res_volume;
298                 res_volume_right=(1.0+pan)*res_volume;
299         }
300         else
301         {
302                 res_volume_left=res_volume_right=res_volume;
303         }
304         
305         
306         if (ec_pan>0.0)
307         {
308                 ec_volume_left=(1.0-ec_pan)*ec_res_volume;
309                 ec_volume_right=ec_res_volume;
310         }
311         else if (ec_pan<0.0)
312         {
313                 ec_volume_left=ec_res_volume;
314                 ec_volume_right=(1.0+ec_pan)*ec_res_volume;
315         }
316         else
317         {
318                 ec_volume_left=ec_volume_right=ec_res_volume;
319         }       
320 //      printf("vtt_volume: %f, %f, l: %f, r: %f\n", rel_volume, res_volume, res_volume_left, res_volume_right);
321         
322 #ifdef USE_3DNOW
323         mm_res_volume.s[0]=mm_res_volume.s[1]=res_volume;
324 #endif  
325 }
326
327 void vtt_class :: set_pan(f_prec newpan)
328 {
329         pan=newpan;
330         recalc_volume();
331 }
332
333 void vtt_class :: set_pitch(f_prec newpitch)
334 {
335         rel_pitch=newpitch;
336 //      res_pitch=fabs(globals.pitch)*rel_pitch;
337         res_pitch=globals.pitch*rel_pitch;
338         speed=res_pitch;
339         ec_set_length(ec_length);
340 }
341
342 void vtt_class :: recalc_pitch()
343 {
344 //      res_pitch=fabs(globals.pitch)*rel_pitch;
345         res_pitch=globals.pitch*rel_pitch;
346         speed=res_pitch;
347         ec_set_length(ec_length);
348 }
349
350 void vtt_class :: set_autotrigger(int newstate)
351 {
352         autotrigger=newstate;
353 }
354
355 void vtt_class :: set_loop(int newstate)
356 {
357         loop=newstate;
358 }
359
360 void vtt_class :: set_controls (int x, int y)
361 {
362         x_control=x;
363         y_control=y;
364 }
365
366 void vtt_class :: set_mute(int newstate)
367 {
368         mute=newstate;
369 }
370
371 void vtt_class :: lp_set_enable (int newstate)
372 {
373         lp_enable=newstate;
374         lp_reset();
375 }
376
377 void vtt_class :: lp_reset()
378 {
379         lp_buf0=lp_buf1=0;
380 }
381
382 void vtt_class :: lp_set_gain (f_prec gain)
383 {
384         lp_gain=gain;
385         lp_resgain=lp_gain*lp_autogain;
386 }
387
388 void vtt_class :: lp_set_reso(f_prec reso)
389 {
390         lp_reso=reso;
391         
392         lp_b=reso*(1.0+(1.0/lp_a));
393         lp_autogain=1.0-reso*GAIN_AUTO_ADJUST;
394         lp_resgain=lp_gain*lp_autogain;
395 }
396
397 void vtt_class :: lp_set_freq(f_prec freq)
398 {
399         lp_freq=freq;
400         
401         lp_a=0.9999-freq;
402         lp_b=lp_reso*(1.0+(1.0/lp_a));
403         
404         //printf("a %f, b%f\n", lp_a, lp_b);
405 }
406
407 void vtt_class :: lp_setup(f_prec gain, f_prec reso, f_prec freq)
408 {
409         lp_freq=freq;
410         lp_reso=reso;
411         
412         lp_a=1.0-freq;
413         lp_b=reso*(1.0+(1.0/lp_a));
414         
415         lp_autogain=1.0-reso*GAIN_AUTO_ADJUST;
416         lp_resgain=lp_gain*lp_autogain;
417 }
418
419 void vtt_class :: ec_set_enable(int newstate)
420 {
421         ec_enable=newstate;
422         ec_clear_buffer();
423 }
424
425
426 void vtt_class :: ec_set_pan(f_prec pan)
427 {
428         ec_pan=pan;
429
430         recalc_volume();
431 }
432
433 /* Max length is 1.0 */
434
435 void vtt_class :: ec_set_length(f_prec length)
436 {
437         int delay;
438         int i=0;
439         
440         ec_length=length;
441         if (res_pitch==0) 
442         {
443                 ec_res_length=length*samples_in_buffer;
444         }
445         else
446         {
447                 ec_res_length=length*samples_in_buffer/res_pitch;       
448         }
449         
450         if (ec_res_length<0) ec_res_length*=-1;
451         
452         if (ec_res_length>=EC_MAX_BUFFER)
453         {
454                 ec_res_length=EC_MAX_BUFFER*length;
455         }
456         
457         delay=(int )floor(ec_res_length);
458         delay-=2;
459         ec_delay=&ec_buffer[delay];
460 }
461
462 void vtt_class :: ec_set_feedback(f_prec feedback)
463 {
464         ec_feedback=feedback;
465 }
466
467
468 void vtt_class :: ec_set_volume(f_prec volume)
469 {
470         ec_volume=volume;
471         recalc_volume();
472 }
473
474 void vtt_class :: ec_clear_buffer()
475 {
476         f_prec *ptr;
477         
478         for (ptr=ec_buffer; ptr<=ec_delay; ptr++)
479         {
480                 *ptr=0.0;
481         }
482         ec_ptr=ec_buffer;
483 }
484
485 void vtt_class :: render()
486 {
487         list <vtt_fx *> :: iterator effect;
488
489         if (do_scratch)
490         {
491                 if (sense_cycles>0)
492                 {
493                         sense_cycles--;
494                         if (sense_cycles==0) sp_speed.receive_input_value(0);
495                 }
496         }
497         render_scratch();
498         
499         for (effect=fx_list.begin(); effect != fx_list.end(); effect++)
500         {
501                 if ((*effect)->isEnabled()) (*effect)->run();
502         }
503 }
504
505 extern void vg_create_fx_gui(vtt_class *vtt, vtt_fx_ladspa *effect, LADSPA_Plugin *plugin);
506
507 vtt_fx_ladspa * vtt_class :: add_effect (LADSPA_Plugin *plugin)
508 {
509         vtt_fx_ladspa *new_effect;
510         
511         new_effect = new vtt_fx_ladspa (plugin, this);
512         pthread_mutex_lock(&render_lock);
513         fx_list.push_back(new_effect);
514         if (is_playing) new_effect->activate();
515         pthread_mutex_unlock(&render_lock);
516         vg_create_fx_gui(this, new_effect, plugin);
517         
518         return new_effect;
519 }
520
521 void vtt_class :: calc_speed()
522 {
523         do_mute=fade_out=fade_in=0;
524
525         if (speed != speed_target)
526         {
527                 speed_target=speed;
528                 speed_step=speed_target-speed_real;
529                 speed_step/=10.0;
530         }
531                         
532         if (speed_target != speed_real)
533         {
534                 speed_real+=speed_step;
535                 if ((speed_step<0) && (speed_real<speed_target)) speed_real=speed_target;
536                 else
537                 if ((speed_step>0) && (speed_real>speed_target)) speed_real=speed_target;                       
538         }
539         
540         if (fade)
541         {
542                 if ((speed_last==0) && (speed_real !=0))
543                 {
544                         fade_in=1;
545                         fade=NEED_FADE_OUT;
546                 }
547         }
548         else
549         {
550                 if ((speed_last!=0) && (speed_real==0))
551                 {
552                         fade_out=1;
553                         fade=NEED_FADE_IN;
554                 }
555         }
556
557         speed_last = speed_real;
558
559         if (mute != mute_old)
560         {
561                 if (mute)
562                 {
563                         fade_out=1; fade_in=0;
564                         fade=NEED_FADE_IN;
565                 }
566                 else
567                 {
568                         fade_in=1; fade_out=0;
569                         fade=NEED_FADE_OUT;
570                 }
571                 mute_old=mute;
572         }
573         else
574         {
575                 if (mute) do_mute=1;
576         }       
577 }
578
579 void vtt_class :: render_scratch()
580 {
581         int16_t *ptr;
582         
583         int sample;
584         
585         f_prec pos_a_f;
586         
587         f_prec amount_a;
588         f_prec amount_b;
589
590         f_prec sample_a;
591         f_prec sample_b;
592         
593         f_prec sample_res;
594         
595         f_prec *out;
596         f_prec fade_vol;        
597
598         calc_speed();
599                                         
600         for (sample =0,out=output_buffer, fade_vol=0.0; sample < samples_in_outputbuffer;sample++, out++, fade_vol+=inv_samples_in_outputbuffer)
601         {
602                 if ((speed_real!=0) || (fade_out))
603                 {
604
605                         pos_f+=speed_real;
606
607                         if (pos_f>maxpos)
608                         {
609                                 pos_f-=maxpos;
610                                 if (res_pitch>0)
611                                 {
612                                         if (loop)
613                                         {
614                                         if (is_sync_master)
615                                         {
616                                                 master_triggered=1;
617                                                 master_triggered_at=sample;
618                                         }
619                                         }
620                                         else
621                                         {
622                                                 want_stop=1;
623                                         }
624                                         
625                                 }
626                         }
627                         else if (pos_f<0)
628                         {
629                                 pos_f+=maxpos;
630                                 if (res_pitch<0)
631                                 {
632                                         if (loop)
633                                         {
634                                         if (is_sync_master)
635                                         {
636                                                 master_triggered=1;
637                                                 master_triggered_at=sample;
638                                         }
639                                         }
640                                         else
641                                         {
642                                                 want_stop=1;
643                                         }
644                                 }
645                         }
646                                 
647                         pos_a_f=floor(pos_f);
648                         pos_i=(unsigned int) pos_a_f;
649                                                                 
650                         amount_b=pos_f-pos_a_f;                         
651                         amount_a=1.0-amount_b;                          
652                                 
653                         if (do_mute)
654                         {
655                                 *out=0.0;
656                         }
657                         else
658                         {
659                                 ptr=&buffer[pos_i];
660                                 sample_a=(f_prec) *ptr;
661                         
662                                 if (pos_i == samples_in_buffer) 
663                                 {
664                                         sample_b=*buffer;
665                                 }
666                                 else
667                                 {
668                                         ptr++;
669                                         sample_b=(f_prec) *ptr;
670                                 }
671                                 
672                                 sample_res=(sample_a*amount_a)+(sample_b*amount_b);
673                                                                 
674                                 if (fade_in)
675                                 {
676                                         sample_res*=fade_vol;
677                                 }
678                                 else
679                                 if (fade_out)
680                                 {
681                                         sample_res*=1.0-fade_vol;
682                                 }
683  
684                                 *out=sample_res;
685                         }
686                 }
687                 else
688                 {
689                                 *out=0;
690                 }
691         }
692 }       
693
694 void vtt_class :: forward_turntable()
695 {
696         int sample;
697         double pos_f_tmp;
698 #ifdef pos_f_test
699         int show=0;
700         double diff;
701 #endif
702
703         calc_speed();
704
705         if ((speed_real==0) && (!fade_out)) return;
706         
707         
708         /* following code is problematic as adding speed_real*n is
709           different from adding speed_real n times to pos_f.
710           
711           well it speeds things up quite a bit and double precision
712           seems to do a satisfying job.
713           
714           #define pos_f_test to prove that.
715         */
716         
717         pos_f_tmp=pos_f+speed_real*samples_in_outputbuffer;
718         
719         if ((pos_f_tmp > 0) && (pos_f_tmp < maxpos))
720         {
721 #ifdef pos_f_test
722                 show=1;
723 #else   
724                 pos_f=pos_f_tmp;
725                 return;
726 #endif          
727         }
728                                 
729         /* now the slow way ;) */
730         
731         for (sample =0; sample < samples_in_outputbuffer; sample++)
732         {
733                         pos_f+=speed_real;
734
735                         if (pos_f>maxpos)
736                         {
737                                 pos_f-=maxpos;
738                                 if (res_pitch>0)
739                                 {
740                                         if (loop)
741                                         {
742                                         if (is_sync_master)
743                                         {
744                                                 master_triggered=1;
745                                                 master_triggered_at=sample;
746                                         }
747                                         }
748                                         else
749                                         {
750                                                 want_stop=1;
751                                         }
752                                         
753                                 }
754                         }
755                         else if (pos_f<0)
756                         {
757                                 pos_f+=maxpos;
758                                 if (res_pitch<0)
759                                 {
760                                         if (loop)
761                                         {
762                                         if (is_sync_master)
763                                         {
764                                                 master_triggered=1;
765                                                 master_triggered_at=sample;
766                                         }
767                                         }
768                                         else
769                                         {
770                                                 want_stop=1;
771                                         }
772                                 }
773                         }
774                 
775         }
776 #ifdef pos_f_test
777         if (show)
778         {
779                 diff=pos_f_tmp-pos_f;
780                 if (diff!=0) printf("fast: %f, slow: %f, diff: %f, tt: %s\n", pos_f_tmp, pos_f, diff, name);
781         }
782 #endif  
783 }       
784
785 /*
786         The following lowpass filter is based on some sample code by
787         Paul Kellett <paul.kellett@maxim.abel.co.uk>
788 */
789
790 void vtt_class :: render_lp()
791 {
792         f_prec *sample;
793                 
794         for (sample = output_buffer; sample<end_of_outputbuffer; sample++)
795         {
796                 lp_buf0 = lp_a * lp_buf0 + lp_freq * ((*sample)*lp_resgain + lp_b * (lp_buf0 - lp_buf1));
797                 lp_buf1 = lp_a * lp_buf1 + lp_freq * lp_buf0;
798                 
799                 *sample=lp_buf1;
800         }
801 }
802
803 void vtt_class :: render_ec()
804 {
805         f_prec *sample;
806         f_prec *ec_sample;
807         int i;
808
809         for (i=0, sample = output_buffer, ec_sample=ec_output_buffer; i<samples_in_outputbuffer; i++, ec_sample++,sample++, ec_ptr++)
810         {
811                 if (ec_ptr>ec_delay) ec_ptr=ec_buffer;
812                 *ec_sample=(*ec_ptr) *ec_feedback;
813                 *ec_ptr=*sample+*ec_sample;
814         }       
815 }
816
817 int vtt_class :: set_mix_buffer_size(int no_samples)
818 {
819         list <vtt_class *> :: iterator vtt;
820         int res=0;
821         
822 //      printf("vtt_class::set_mix_buffer_size(), mix_buffer: %12x, mix_out: %12x, samples: %i\n", mix_buffer, mix_out_buffer, no_samples);
823         
824         if (mix_buffer) tX_freemem(mix_buffer, "mix_buffer", "vtt set_mix_buffer_size()");
825         samples_in_mix_buffer=no_samples*2;
826         //mix_buffer=(float *) malloc (sizeof(float)*samples_in_mix_buffer);
827         tX_malloc(mix_buffer, "mix_buffer", "vtt set_mix_buffer_size()", sizeof(float)*samples_in_mix_buffer, (float *));
828         mix_buffer_end=mix_buffer+samples_in_mix_buffer;
829 //      printf("mix_buffer: %12x\n", mix_buffer);
830         
831 //      printf("mix_samples: %i, out_samples: %i", samples_in_mix_buffer, no_samples);
832         
833         if (mix_out_buffer) tX_freemem(mix_out_buffer, "mix_out_buffer", "vtt set_mix_buffer_size()");
834         //mix_out_buffer=(int16_t *) malloc (sizeof(int16_t)*samples_in_mix_buffer + 4); /* extra 4 for 3DNow! */
835         tX_malloc(mix_out_buffer, "mix_out_buffer", "vtt set_mix_buffer_size()", sizeof(int16_t)*samples_in_mix_buffer + 4, (int16_t *));
836 //      printf("mix_out_buffer: %12x\n", mix_out_buffer);
837         
838         for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
839         {
840                 res|=(*vtt)->set_output_buffer_size(no_samples);
841         }
842         
843         if ((!mix_buffer) || (!mix_out_buffer) || res) return(1);
844         return(0);
845 }
846
847 int16_t * vtt_class :: render_all_turntables()
848 {
849         list <vtt_class *> :: iterator vtt, next;
850         int sample;
851         int mix_sample;
852         f_prec temp;
853
854 #ifdef USE_3DNOW
855         mmx_t *mix;
856         mmx_t *vtt_buffer;
857         int32_t *mix_int;
858 #endif
859
860 #ifdef USE_FLASH
861         f_prec max;
862         f_prec min;
863 #ifdef USE_3DNOW        
864         mmx_t mm_max;
865         mmx_t mm_min;
866         int32_t *temp_int=&mm_max.d[1];
867 #endif  
868 #endif  
869         
870         pthread_mutex_lock(&render_lock);
871         
872         if (render_list.size()==0)
873         {
874                 for (sample=0; sample<samples_in_mix_buffer; sample++)
875                 {
876                         mix_out_buffer[sample]=0;
877                 }
878         }
879         else
880         {
881                         vtt=render_list.begin();
882                         (*vtt)->render();                       
883                         max=(*vtt)->max_value;
884                         min=max;
885
886                         for (sample=0, mix_sample=0; sample<(*vtt)->samples_in_outputbuffer; sample++)
887                         {                               
888                                 temp=(*vtt)->output_buffer[sample];
889                                 mix_buffer[mix_sample]=temp*(*vtt)->res_volume_left;
890                                 mix_sample++;
891                                 mix_buffer[mix_sample]=temp*(*vtt)->res_volume_right;
892                                 mix_sample++;
893                                 
894                                 if (temp>max) max=temp;
895                                 else if (temp<min) min=temp;
896                         }               
897                         
898                         min*=-1.0;
899                         if (min>max) (*vtt)->max_value=min; else (*vtt)->max_value=max;
900
901                         if ((*vtt)->ec_enable)
902                         {
903                                 for (sample=0, mix_sample=0; sample<(*vtt)->samples_in_outputbuffer; sample++)
904                                 {                               
905                                         temp=(*vtt)->ec_output_buffer[sample];
906                                         
907                                         mix_buffer[mix_sample]+=temp*(*vtt)->ec_volume_left;
908                                         mix_sample++;
909                                         mix_buffer[mix_sample]+=temp*(*vtt)->ec_volume_right;
910                                         mix_sample++;
911                                 }               
912                         }
913                         
914                         if (master_triggered)
915                         {
916                                 pthread_mutex_unlock(&render_lock);
917 //                              pthread_mutex_lock(&main_lock);
918                                 for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
919                                 {
920                                         if ((*vtt)->is_sync_client)
921                                         {
922                                                 if ((*vtt)->sync_countdown)
923                                                 {
924                                                         (*vtt)->sync_countdown--;
925                                                 }
926                                                 else
927                                                 {
928                                                         (*vtt)->sync_countdown=(*vtt)->sync_cycles;
929                                                         (*vtt)->trigger();
930                                                 }
931                                         }
932                                 }
933 //                              pthread_mutex_unlock(&main_lock);
934                                 pthread_mutex_lock(&render_lock);
935                         }
936                         
937                         vtt=render_list.begin();
938                         for (vtt++; vtt!=render_list.end(); vtt++)
939                         {
940                                 (*vtt)->render();                                       
941                                 max=(*vtt)->max_value;
942                                 min=max;
943
944                                 for (sample=0, mix_sample=0; sample<(*vtt)->samples_in_outputbuffer; sample++)
945                                 {                               
946                                         temp=(*vtt)->output_buffer[sample];
947                                         mix_buffer[mix_sample]+=temp*(*vtt)->res_volume_left;
948                                         mix_sample++;                                   
949                                         mix_buffer[mix_sample]+=temp*(*vtt)->res_volume_right;
950                                         mix_sample++;
951                                 
952                                         if (temp>max) max=temp;
953                                         else if (temp<min) min=temp;
954                                 }
955                                 
956                                 min*=-1.0;
957                                 if (min>max) (*vtt)->max_value=min; else (*vtt)->max_value=max;
958                                 
959                                 if ((*vtt)->ec_enable)
960                                 {
961                                         for (sample=0, mix_sample=0; sample<(*vtt)->samples_in_outputbuffer; sample++)
962                                         {                               
963                                                 temp=(*vtt)->ec_output_buffer[sample];
964                                                 
965                                                 mix_buffer[mix_sample]+=temp*(*vtt)->ec_volume_left;
966                                                 mix_sample++;
967                                                 mix_buffer[mix_sample]+=temp*(*vtt)->ec_volume_right;
968                                                 mix_sample++;
969                                         }               
970                                 }
971                         }
972                         
973                         /* left */
974                         
975                         max=mix_max_l;
976                         min=max;
977
978                         for (sample=0; sample<samples_in_mix_buffer; sample+=2)
979                         {                               
980                                 temp=mix_buffer[sample];
981                                 mix_out_buffer[sample]=(int16_t) temp;
982                         
983                                 if (temp>max) max=temp;
984                                 else if (temp<min) min=temp;
985                         }
986                         
987                         min*=-1.0;
988                         if (min>max) mix_max_l=min; else mix_max_l=max;         
989                         
990                         /* right */
991                         
992                         max=mix_max_r;
993                         min=max;
994
995                         for (sample=1; sample<samples_in_mix_buffer; sample+=2)
996                         {                               
997                                 temp=mix_buffer[sample];
998                                 mix_out_buffer[sample]=(int16_t) temp;
999                         
1000                                 if (temp>max) max=temp;
1001                                 else if (temp<min) min=temp;
1002                         }
1003                         
1004                         min*=-1.0;
1005                         if (min>max) mix_max_r=min; else mix_max_r=max;         
1006                         
1007         }
1008         master_triggered=0;
1009                 
1010         vtt=render_list.begin();
1011         while (vtt!=render_list.end())
1012         {
1013                 next=vtt;
1014                 next++;
1015                 
1016                 if ((*vtt)->want_stop) (*vtt)->stop_nolock();
1017                 vtt=next;
1018         }
1019         pthread_mutex_unlock(&render_lock);
1020         
1021         return(mix_out_buffer);
1022 }
1023
1024 void vtt_class :: forward_all_turntables()
1025 {
1026         list <vtt_class *> :: iterator vtt, next;
1027
1028         if (render_list.size()>0)
1029         {
1030                  vtt=render_list.begin();
1031                  (*vtt)->forward_turntable();                    
1032
1033                  if (master_triggered)
1034                  {
1035                          for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
1036                          {
1037                                  if ((*vtt)->is_sync_client)
1038                                  {
1039                                          if ((*vtt)->sync_countdown)
1040                                          {
1041                                                  (*vtt)->sync_countdown--;
1042                                          }
1043                                          else
1044                                          {
1045                                                  (*vtt)->sync_countdown=(*vtt)->sync_cycles;
1046                                                  (*vtt)->trigger();
1047                                          }
1048                                  }
1049                          }
1050                  }
1051
1052                  vtt=render_list.begin();
1053                  for (vtt++; vtt!=render_list.end(); vtt++)
1054                  {
1055                          (*vtt)->forward_turntable();
1056                  }
1057                  
1058         }
1059         master_triggered=0;
1060         vtt=render_list.begin();
1061         while (vtt!=render_list.end())
1062         {
1063                 next=vtt;
1064                 next++;
1065                 
1066                 if ((*vtt)->want_stop) (*vtt)->stop_nolock();
1067                 vtt=next;
1068         }
1069 }
1070
1071
1072 int vtt_class :: trigger()
1073 {
1074         list <vtt_fx *> :: iterator effect;
1075
1076         if (!buffer) return (1);
1077         
1078         if (!is_playing) pthread_mutex_lock(&render_lock);
1079         
1080         if (res_pitch>=0) pos_f=0;
1081         else pos_f=maxpos;
1082         fade=NEED_FADE_OUT;
1083         speed=res_pitch;
1084         speed_real=res_pitch;
1085         speed_target=res_pitch;
1086 /*      mute=0;
1087         mute_old=0;*/
1088         want_stop=0;
1089
1090         /* activating plugins */
1091         for (effect=fx_list.begin(); effect != fx_list.end(); effect++)
1092         {
1093                 (*effect)->activate();
1094         }
1095
1096
1097 #ifdef USE_FLASH
1098         max_value=0;
1099 #endif
1100         
1101         if (is_sync_master)
1102         {
1103                 master_triggered=1;
1104                 master_triggered_at=0;
1105         }
1106         
1107         if (!is_playing)
1108         {
1109                 is_playing=1;
1110         
1111                 if (is_sync_master) 
1112                 {
1113                         render_list.push_front(this);           
1114                 }               
1115                 else
1116                 {
1117                         render_list.push_back(this);
1118                 }
1119                 pthread_mutex_unlock(&render_lock);
1120         }
1121         return(0);
1122 }
1123
1124 int vtt_class :: stop_nolock()
1125 {
1126         list <vtt_fx *> :: iterator effect;
1127
1128         if (!is_playing) 
1129         {
1130                 pthread_mutex_unlock(&render_lock);
1131                 return(1);
1132         }
1133         render_list.remove(this);
1134         want_stop=0;
1135
1136         is_playing=0;
1137
1138 #ifdef USE_FLASH
1139         max_value=0;
1140 #endif
1141         cleanup_vtt(this);
1142         sync_countdown=0;
1143         
1144         /* deactivating plugins */
1145         for (effect=fx_list.begin(); effect != fx_list.end(); effect++)
1146         {
1147                 (*effect)->deactivate();
1148         }
1149         
1150         return(0);
1151 }
1152
1153 int vtt_class :: stop()
1154 {
1155         int res;
1156         
1157         pthread_mutex_lock(&render_lock);
1158
1159         res=stop_nolock();
1160
1161         pthread_mutex_unlock(&render_lock);
1162
1163         return(res);
1164 }
1165
1166 void vtt_class :: set_sync_master(int master)
1167 {
1168         if (master)
1169         {
1170                 if (sync_master) sync_master->set_sync_master(0);
1171                 sync_master=this;
1172                 is_sync_master=1;
1173         }
1174         else
1175         {
1176                 if (sync_master==this) sync_master=0;
1177                 is_sync_master=0;
1178                 gui_clear_master_button(this);
1179         }
1180 }
1181
1182 void vtt_class :: set_sync_client(int slave, int cycles)
1183 {
1184         is_sync_client=slave;
1185         sync_cycles=cycles;
1186 //      sync_countdown=cycles; 
1187         sync_countdown=0;
1188 }
1189
1190 void vtt_class :: set_sync_client_ug(int slave, int cycles)
1191 {
1192         set_sync_client(slave, cycles);
1193 }
1194
1195 void vtt_class :: set_master_volume(f_prec new_volume)
1196 {
1197         list <vtt_class *> :: iterator vtt;
1198
1199         master_volume=new_volume;
1200         globals.volume=new_volume;
1201         
1202         if (main_list.size()>0)
1203         {
1204 //              res_master_volume=master_volume/((f_prec) main_list.size());
1205                 vol_channel_adjust=sqrt((f_prec) main_list.size());
1206                 res_master_volume=master_volume/vol_channel_adjust;
1207                 
1208         }
1209                 
1210         for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
1211         {
1212                 (*vtt)->recalc_volume();
1213         }
1214 }
1215
1216 void vtt_class :: set_master_pitch(f_prec new_pitch)
1217 {
1218         list <vtt_class *> :: iterator vtt;
1219         
1220         globals.pitch=new_pitch;
1221         for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
1222         {
1223                 (*vtt)->recalc_pitch();
1224         }
1225 }
1226
1227 void vtt_class :: enable_saturate (int newstate)
1228 {
1229         do_saturate=newstate;
1230 }
1231
1232 void vtt_class :: focus_no(int no)
1233 {
1234         list <vtt_class *> :: iterator vtt;
1235         int i;
1236
1237         for (i=0, vtt=main_list.begin(); vtt!=main_list.end(); vtt++, i++)
1238         {
1239                 if (i==no)
1240                 {
1241                         focused_vtt=(*vtt);
1242                 }
1243         }
1244 }
1245
1246 void vtt_class :: focus_next()
1247 {
1248         list <vtt_class *> :: iterator vtt;
1249         
1250         if (!focused_vtt)
1251         {
1252                 if (main_list.size())
1253                 {
1254                         focused_vtt=(*main_list.begin());
1255                 }
1256                 return;
1257         }
1258         
1259         for (vtt=main_list.begin(); vtt!=main_list.end() ; vtt++)
1260         {
1261                 if ((*vtt)==focused_vtt)
1262                 {
1263                         vtt++;
1264                         if (vtt==main_list.end())
1265                         {                       
1266                                 focused_vtt=(*main_list.begin());
1267                                 return;
1268                         }
1269                         else
1270                         {
1271                                 focused_vtt=(*vtt);
1272                                 return;
1273                         }
1274                 }
1275         }
1276         
1277         focused_vtt=(*main_list.begin());
1278 }
1279
1280 void vtt_class :: set_scratch(int newstate)
1281 {
1282         if (newstate)
1283         {
1284                 sp_spin.receive_input_value(0);
1285                 do_scratch=1;
1286                 sense_cycles=globals.sense_cycles;
1287         }
1288         else
1289         {
1290                 sp_spin.receive_input_value(1);
1291                 do_scratch=0;
1292         }
1293 }
1294
1295 #define MAGIC 0.05
1296
1297 void vtt_class :: handle_input(int control, f_prec value)
1298 {
1299         f_prec temp;
1300         
1301         switch (control)
1302         {
1303                 case CONTROL_SCRATCH:
1304                 if (do_scratch) sp_speed.receive_input_value(value*globals.mouse_speed);
1305                 sense_cycles=globals.sense_cycles;
1306                 break;
1307                 
1308                 case CONTROL_VOLUME:
1309                 temp=rel_volume+MAGIC*value*globals.mouse_speed;
1310                 if (temp>2.0) temp=2.0;
1311                 else if (temp<0) temp=0;
1312                 sp_volume.receive_input_value(temp);
1313                 break;
1314                 
1315                 case CONTROL_CUTOFF:
1316                 temp=lp_freq+MAGIC*value*globals.mouse_speed;
1317                 if (temp>0.99) temp=0.99;
1318                 else if (temp<0) temp=0;
1319                 sp_lp_freq.receive_input_value(temp);
1320                 break;
1321                 
1322                 case CONTROL_FEEDBACK:
1323                 temp=ec_feedback+MAGIC*value*globals.mouse_speed;
1324                 if (temp>1.0) temp=1.0;
1325                 else if (temp<0) temp=0;
1326                 sp_ec_feedback.receive_input_value(temp);
1327                 break;
1328         }
1329 }
1330
1331 void vtt_class :: unfocus()
1332 {
1333         focused_vtt=NULL;
1334 }
1335
1336 extern void vg_display_ycontrol(vtt_class *vtt);
1337 extern void vg_display_xcontrol(vtt_class *vtt);
1338
1339 void vtt_class :: set_x_input_parameter(tX_seqpar *sp)
1340 {
1341         x_par = sp;
1342         vg_display_xcontrol(this);
1343 }
1344
1345 void vtt_class :: set_y_input_parameter(tX_seqpar *sp)
1346 {
1347         y_par = sp;
1348         vg_display_ycontrol(this);
1349 }
1350
1351 void vtt_class :: xy_input(f_prec x_value, f_prec y_value)
1352 {
1353         if (x_par) x_par->handle_mouse_input(x_value*globals.mouse_speed);
1354         if (y_par) y_par->handle_mouse_input(y_value*globals.mouse_speed);
1355         
1356 /*      handle_input(x_control, x_value);
1357         handle_input(y_control, y_value);*/
1358 }
1359
1360
1361 #define store(data); if (fwrite((void *) &data, sizeof(data), 1, output)!=1) res+=1;
1362
1363 int  vtt_class :: save(FILE * output)
1364 {
1365         list <vtt_fx *> :: iterator effect;
1366
1367         int res=0;
1368         u_int32_t pid;
1369         int32_t counter;
1370         u_int8_t hidden;
1371         
1372         store(name);
1373         store(filename);
1374         store(is_sync_master);
1375         store(is_sync_client);
1376         store(sync_cycles);
1377         store(rel_volume);
1378         store(rel_pitch);
1379         
1380         store(autotrigger);
1381         store(loop);
1382         
1383         store(mute);
1384         store(pan);
1385         
1386         store(lp_enable);
1387         store(lp_gain);
1388         store(lp_reso);
1389         store(lp_freq);
1390         
1391         store(ec_enable);
1392         store(ec_length);
1393         store(ec_feedback);
1394         store(ec_pan);
1395         store(ec_volume);
1396
1397         pid=sp_speed.get_persistence_id();
1398         store(pid);
1399         pid=sp_volume.get_persistence_id();
1400         store(pid);
1401         pid=sp_pitch.get_persistence_id();
1402         store(pid);
1403         pid=sp_trigger.get_persistence_id();
1404         store(pid);
1405         pid=sp_loop.get_persistence_id();
1406         store(pid);
1407         pid=sp_sync_client.get_persistence_id();
1408         store(pid);
1409         pid=sp_sync_cycles.get_persistence_id();
1410         store(pid);
1411         pid=sp_lp_enable.get_persistence_id();
1412         store(pid);
1413         pid=sp_lp_gain.get_persistence_id();
1414         store(pid);
1415         pid=sp_lp_reso.get_persistence_id();
1416         store(pid);
1417         pid=sp_lp_freq.get_persistence_id();
1418         store(pid);
1419         pid=sp_ec_enable.get_persistence_id();
1420         store(pid);
1421         pid=sp_ec_length.get_persistence_id();
1422         store(pid);
1423         pid=sp_ec_feedback.get_persistence_id();
1424         store(pid);
1425         pid=sp_ec_volume.get_persistence_id();
1426         store(pid);
1427         pid=sp_ec_pan.get_persistence_id();
1428         store(pid);
1429         pid=sp_mute.get_persistence_id();
1430         store(pid);
1431         pid=sp_spin.get_persistence_id();
1432         store(pid);
1433         pid=sp_pan.get_persistence_id();
1434         store(pid);
1435                 
1436         counter=fx_list.size();
1437         store(counter);
1438
1439         for (effect=fx_list.begin(); effect!=fx_list.end(); effect++)
1440         {
1441                 (*effect)->save(output);
1442         }
1443         
1444         if (x_par)
1445         {
1446                 pid=1;
1447                 store(pid);
1448                 pid=x_par->get_persistence_id();
1449                 store(pid);
1450         }
1451         else
1452         {
1453                 pid=0;
1454                 store(pid);
1455         }
1456
1457         if (y_par)
1458         {
1459                 pid=1;
1460                 store(pid);
1461                 pid=y_par->get_persistence_id();
1462                 store(pid);
1463         }
1464         else
1465         {
1466                 pid=0;
1467                 store(pid);
1468         }
1469                 
1470         hidden=gui.main_panel->is_hidden();
1471         store(hidden);
1472
1473         hidden=gui.trigger_panel->is_hidden();
1474         store(hidden);
1475
1476         hidden=gui.lp_panel->is_hidden();
1477         store(hidden);
1478
1479         hidden=gui.ec_panel->is_hidden();
1480         store(hidden);
1481         
1482         return(res);
1483 }
1484
1485 #define atload(data); if (fread((void *) &data, sizeof(data), 1, input)!=1) res+=1;
1486
1487 int vtt_class :: load_10(FILE * input)
1488 {
1489         int res=0;
1490         
1491         atload(name);
1492         atload(filename);
1493         atload(is_sync_master);
1494         atload(is_sync_client);
1495         atload(sync_cycles);
1496         atload(rel_volume);
1497         recalc_volume();
1498         atload(rel_pitch);
1499         recalc_pitch();
1500         
1501         atload(autotrigger);
1502         atload(loop);
1503         
1504         atload(mute);
1505         atload(x_control);
1506         atload(y_control);      
1507         
1508         atload(lp_enable);
1509         atload(lp_gain);
1510         atload(lp_reso);
1511         atload(lp_freq);
1512         lp_setup(lp_gain, lp_reso, lp_freq);
1513         
1514         atload(ec_enable);
1515         atload(ec_length);
1516         ec_set_length(ec_length);
1517         atload(ec_feedback);
1518         ec_set_feedback(ec_feedback);
1519         
1520         return(res);
1521 }
1522
1523
1524 int vtt_class :: load_11(FILE * input)
1525 {
1526         int res=0;
1527         u_int32_t pid;
1528         int32_t gui_page;
1529         
1530         atload(name);
1531         atload(filename);
1532         atload(is_sync_master);
1533         atload(is_sync_client);
1534         atload(sync_cycles);
1535         atload(rel_volume);
1536         recalc_volume();
1537         atload(rel_pitch);
1538         recalc_pitch();
1539         
1540         atload(autotrigger);
1541         atload(loop);
1542         
1543         atload(mute);
1544         atload(x_control);
1545         atload(y_control);      
1546         
1547         atload(lp_enable);
1548         atload(lp_gain);
1549         atload(lp_reso);
1550         atload(lp_freq);
1551         lp_setup(lp_gain, lp_reso, lp_freq);
1552         
1553         atload(ec_enable);
1554         atload(ec_length);
1555         ec_set_length(ec_length);
1556         atload(ec_feedback);
1557         ec_set_feedback(ec_feedback);
1558
1559         atload(pid);
1560         sp_speed.set_persistence_id(pid);
1561         atload(pid);
1562         sp_volume.set_persistence_id(pid);
1563         atload(pid);
1564         sp_pitch.set_persistence_id(pid);
1565         atload(pid);
1566         sp_trigger.set_persistence_id(pid);
1567         atload(pid);
1568         sp_loop.set_persistence_id(pid);
1569         atload(pid);
1570         sp_sync_client.set_persistence_id(pid);
1571         atload(pid);
1572         sp_sync_cycles.set_persistence_id(pid);
1573         atload(pid);
1574         sp_lp_enable.set_persistence_id(pid);
1575         atload(pid);
1576         sp_lp_gain.set_persistence_id(pid);
1577         atload(pid);
1578         sp_lp_reso.set_persistence_id(pid);
1579         atload(pid);
1580         sp_lp_freq.set_persistence_id(pid);
1581         atload(pid);
1582         sp_ec_enable.set_persistence_id(pid);
1583         atload(pid);
1584         sp_ec_length.set_persistence_id(pid);
1585         atload(pid);
1586         sp_ec_feedback.set_persistence_id(pid);
1587         atload(pid);
1588         sp_mute.set_persistence_id(pid);
1589         atload(pid);
1590         sp_spin.set_persistence_id(pid);
1591         
1592         atload(gui_page);
1593         vg_set_current_page(this, gui_page);
1594         
1595         return(res);
1596 }
1597
1598 int vtt_class :: load_12(FILE * input)
1599 {
1600         int res=0;
1601         u_int32_t pid;
1602         int32_t counter;
1603         int32_t type;
1604         long id;
1605         int i,t;
1606         LADSPA_Plugin *plugin;
1607         char buffer[256];
1608         vtt_fx_ladspa *ladspa_effect;
1609         u_int8_t hidden;
1610         
1611         atload(buffer);
1612         this->set_name(buffer);
1613         atload(filename);
1614         atload(is_sync_master);
1615         atload(is_sync_client);
1616         atload(sync_cycles);
1617         atload(rel_volume);
1618         recalc_volume();
1619         atload(rel_pitch);
1620         recalc_pitch();
1621         
1622         atload(autotrigger);
1623         atload(loop);
1624         
1625         atload(mute);
1626         
1627         atload(lp_enable);
1628         atload(lp_gain);
1629         atload(lp_reso);
1630         atload(lp_freq);
1631         lp_setup(lp_gain, lp_reso, lp_freq);
1632         
1633         atload(ec_enable);
1634         atload(ec_length);
1635         ec_set_length(ec_length);
1636         atload(ec_feedback);
1637         ec_set_feedback(ec_feedback);
1638
1639         atload(pid);
1640         sp_speed.set_persistence_id(pid);
1641         atload(pid);
1642         sp_volume.set_persistence_id(pid);
1643         atload(pid);
1644         sp_pitch.set_persistence_id(pid);
1645         atload(pid);
1646         sp_trigger.set_persistence_id(pid);
1647         atload(pid);
1648         sp_loop.set_persistence_id(pid);
1649         atload(pid);
1650         sp_sync_client.set_persistence_id(pid);
1651         atload(pid);
1652         sp_sync_cycles.set_persistence_id(pid);
1653         atload(pid);
1654         sp_lp_enable.set_persistence_id(pid);
1655         atload(pid);
1656         sp_lp_gain.set_persistence_id(pid);
1657         atload(pid);
1658         sp_lp_reso.set_persistence_id(pid);
1659         atload(pid);
1660         sp_lp_freq.set_persistence_id(pid);
1661         atload(pid);
1662         sp_ec_enable.set_persistence_id(pid);
1663         atload(pid);
1664         sp_ec_length.set_persistence_id(pid);
1665         atload(pid);
1666         sp_ec_feedback.set_persistence_id(pid);
1667         atload(pid);
1668         sp_mute.set_persistence_id(pid);
1669         atload(pid);
1670         sp_spin.set_persistence_id(pid);
1671                 
1672         atload(counter);
1673         
1674         for (i=0; i<counter; i++)
1675         {
1676                 atload(type);
1677                 switch(type)
1678                 {
1679                         case TX_FX_BUILTINCUTOFF:
1680                                 for (t=0; t<fx_list.size(); t++) effect_down(lp_fx);
1681                         break;
1682                         
1683                         case TX_FX_BUILTINECHO:
1684                                 for (t=0; t<fx_list.size(); t++) effect_down(ec_fx);
1685                         break;
1686                         
1687                         case TX_FX_LADSPA:
1688                                 atload(id);
1689                                 plugin=LADSPA_Plugin::getPluginByUniqueID(id);
1690                                 if (plugin)
1691                                 {
1692                                         ladspa_effect=add_effect(plugin);
1693                                         ladspa_effect->load(input);
1694                                 }
1695                                 else
1696                                 {
1697                                         sprintf(buffer,"Fatal Error: Couldn't find required plugin with ID [%i].", id);
1698                                         tx_note(buffer);
1699                                         res++;
1700                                 }
1701                         break;
1702                         
1703                         default:
1704                                 tx_note("Fatal Error loading set: unknown effect type!");
1705                                 res++;
1706                 }               
1707         }
1708
1709         atload(pid);
1710         
1711         if (pid)
1712         {
1713                 atload(pid);
1714                 set_x_input_parameter(tX_seqpar :: get_sp_by_persistence_id(pid));
1715         }
1716         else set_x_input_parameter(NULL);
1717         
1718         atload(pid);
1719         
1720         if (pid)
1721         {
1722                 atload(pid);
1723                 set_y_input_parameter(tX_seqpar :: get_sp_by_persistence_id(pid));
1724         }
1725         else set_y_input_parameter(NULL);
1726
1727         atload(hidden);
1728         gui.main_panel->hide(hidden);
1729
1730         atload(hidden);
1731         gui.trigger_panel->hide(hidden);
1732
1733         atload(hidden);
1734         gui.lp_panel->hide(hidden);
1735
1736         atload(hidden);
1737         gui.ec_panel->hide(hidden);
1738         
1739         return(res);
1740 }
1741
1742 int vtt_class :: load_13(FILE * input)
1743 {
1744         int res=0;
1745         u_int32_t pid;
1746         int32_t counter;
1747         int32_t type;
1748         long id;
1749         int i,t;
1750         LADSPA_Plugin *plugin;
1751         char buffer[256];
1752         vtt_fx_ladspa *ladspa_effect;
1753         u_int8_t hidden;
1754         
1755         atload(buffer);
1756         this->set_name(buffer);
1757         atload(filename);
1758         atload(is_sync_master);
1759         atload(is_sync_client);
1760         atload(sync_cycles);
1761         atload(rel_volume);
1762         atload(rel_pitch);
1763         recalc_pitch();
1764         
1765         atload(autotrigger);
1766         atload(loop);
1767         
1768         atload(mute);
1769         atload(pan);
1770         
1771         atload(lp_enable);
1772         atload(lp_gain);
1773         atload(lp_reso);
1774         atload(lp_freq);
1775         lp_setup(lp_gain, lp_reso, lp_freq);
1776         
1777         atload(ec_enable);
1778         atload(ec_length);
1779         ec_set_length(ec_length);
1780         atload(ec_feedback);
1781         ec_set_feedback(ec_feedback);
1782         atload(ec_pan);
1783         ec_set_pan(ec_pan);
1784         atload(ec_volume);
1785         ec_set_volume(ec_volume);
1786
1787         recalc_volume();
1788
1789         atload(pid);
1790         sp_speed.set_persistence_id(pid);
1791         atload(pid);
1792         sp_volume.set_persistence_id(pid);
1793         atload(pid);
1794         sp_pitch.set_persistence_id(pid);
1795         atload(pid);
1796         sp_trigger.set_persistence_id(pid);
1797         atload(pid);
1798         sp_loop.set_persistence_id(pid);
1799         atload(pid);
1800         sp_sync_client.set_persistence_id(pid);
1801         atload(pid);
1802         sp_sync_cycles.set_persistence_id(pid);
1803         atload(pid);
1804         sp_lp_enable.set_persistence_id(pid);
1805         atload(pid);
1806         sp_lp_gain.set_persistence_id(pid);
1807         atload(pid);
1808         sp_lp_reso.set_persistence_id(pid);
1809         atload(pid);
1810         sp_lp_freq.set_persistence_id(pid);
1811         atload(pid);
1812         sp_ec_enable.set_persistence_id(pid);
1813         atload(pid);
1814         sp_ec_length.set_persistence_id(pid);
1815         atload(pid);
1816         sp_ec_feedback.set_persistence_id(pid);
1817         atload(pid);
1818         sp_ec_volume.set_persistence_id(pid);
1819         atload(pid);
1820         sp_ec_pan.set_persistence_id(pid);
1821         atload(pid);
1822         sp_mute.set_persistence_id(pid);
1823         atload(pid);
1824         sp_spin.set_persistence_id(pid);
1825         atload(pid);
1826         sp_pan.set_persistence_id(pid);
1827                 
1828         atload(counter);
1829         
1830         for (i=0; i<counter; i++)
1831         {
1832                 atload(type);
1833                 switch(type)
1834                 {
1835                         case TX_FX_BUILTINCUTOFF:
1836                                 for (t=0; t<fx_list.size(); t++) effect_down(lp_fx);
1837                         break;
1838                         
1839                         case TX_FX_BUILTINECHO:
1840                                 for (t=0; t<fx_list.size(); t++) effect_down(ec_fx);
1841                         break;
1842                         
1843                         case TX_FX_LADSPA:
1844                                 atload(id);
1845                                 plugin=LADSPA_Plugin::getPluginByUniqueID(id);
1846                                 if (plugin)
1847                                 {
1848                                         ladspa_effect=add_effect(plugin);
1849                                         ladspa_effect->load(input);
1850                                 }
1851                                 else
1852                                 {
1853                                         sprintf(buffer,"Fatal Error: Couldn't find required plugin with ID [%i].", id);
1854                                         tx_note(buffer);
1855                                         res++;
1856                                 }
1857                         break;
1858                         
1859                         default:
1860                                 tx_note("Fatal Error loading set: unknown effect type!");
1861                                 res++;
1862                 }               
1863         }
1864
1865         atload(pid);
1866         
1867         if (pid)
1868         {
1869                 atload(pid);
1870                 set_x_input_parameter(tX_seqpar :: get_sp_by_persistence_id(pid));
1871         }
1872         else set_x_input_parameter(NULL);
1873         
1874         atload(pid);
1875         
1876         if (pid)
1877         {
1878                 atload(pid);
1879                 set_y_input_parameter(tX_seqpar :: get_sp_by_persistence_id(pid));
1880         }
1881         else set_y_input_parameter(NULL);
1882
1883         atload(hidden);
1884         gui.main_panel->hide(hidden);
1885
1886         atload(hidden);
1887         gui.trigger_panel->hide(hidden);
1888
1889         atload(hidden);
1890         gui.lp_panel->hide(hidden);
1891
1892         atload(hidden);
1893         gui.ec_panel->hide(hidden);
1894         
1895         return(res);
1896 }
1897
1898
1899 int  vtt_class :: save_all(FILE* output)
1900 {
1901         int res=0;
1902         list <vtt_class *> :: iterator vtt;
1903         u_int32_t pid;
1904         
1905         tX_seqpar :: create_persistence_ids();
1906         
1907         store(vtt_amount);
1908         store(master_volume);
1909         store(globals.pitch);
1910         pid=sp_master_volume.get_persistence_id();
1911         store(pid);
1912         pid=sp_master_pitch.get_persistence_id();
1913         store(pid);
1914
1915         for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
1916         {
1917                 res+=(*vtt)->save(output);
1918         }
1919         
1920         sequencer.save(output);
1921         
1922         return(res);
1923 }
1924
1925 int  vtt_class :: load_all_10(FILE* input, char *fname)
1926 {
1927         int res=0, restmp=0;
1928         list <vtt_class *> :: iterator vtt;
1929         unsigned int i, max, size;
1930         int16_t *newbuffer;
1931         vtt_class *newvtt;
1932         char ftmp[PATH_MAX];
1933         
1934         while (main_list.size())
1935         {
1936                 delete((*main_list.begin()));
1937         }
1938                 
1939         atload(max);
1940         atload(master_volume);
1941         set_master_volume(master_volume);
1942         globals.volume=master_volume;
1943         atload(globals.pitch);  
1944         set_master_pitch(globals.pitch);
1945
1946         ld_create_loaddlg(TX_LOADDLG_MODE_MULTI, max);
1947         ld_set_setname(fname);
1948
1949         for (i=0; i<max; i++)
1950         {
1951                 newvtt=new vtt_class(1);
1952                 res+=newvtt->load_10(input);
1953                 
1954                 if (strlen(newvtt->filename))
1955                 {
1956                         /* ftmp IS NECESSARY !!! */
1957                         strcpy(ftmp, newvtt->filename);
1958                         ld_set_filename(ftmp);
1959                         
1960                         //restmp=load_wav(newvtt->filename, &newbuffer, &size);
1961                         restmp=newvtt->load_file(ftmp);
1962                         res+=restmp;
1963                 }
1964                 gtk_box_pack_start(GTK_BOX(control_parent), newvtt->gui.control_box, TRUE, TRUE, 0);
1965                 gtk_box_pack_start(GTK_BOX(audio_parent), newvtt->gui.audio_box, TRUE, TRUE, 0);
1966         
1967         }
1968         
1969         sequencer.clear();
1970         
1971         ld_destroy();
1972         
1973         return(res);
1974 }
1975
1976
1977 int  vtt_class :: load_all_11(FILE* input, char *fname)
1978 {
1979         int res=0, restmp=0;
1980         list <vtt_class *> :: iterator vtt;
1981         unsigned int i, max, size;
1982         int16_t *newbuffer;
1983         vtt_class *newvtt;
1984         char ftmp[PATH_MAX];
1985         u_int32_t pid;
1986         
1987         while (main_list.size())
1988         {
1989                 delete((*main_list.begin()));
1990         }
1991                 
1992         atload(max);
1993         atload(master_volume);
1994         set_master_volume(master_volume);
1995         globals.volume=master_volume;
1996         atload(globals.pitch);  
1997         set_master_pitch(globals.pitch);
1998         atload(pid);
1999         sp_master_volume.set_persistence_id(pid);
2000         atload(pid);
2001         sp_master_pitch.set_persistence_id(pid);
2002
2003         ld_create_loaddlg(TX_LOADDLG_MODE_MULTI, max);
2004         ld_set_setname(fname);
2005
2006         for (i=0; i<max; i++)
2007         {
2008                 newvtt=new vtt_class(1);
2009                 res+=newvtt->load_11(input);
2010                 
2011                 if (strlen(newvtt->filename))
2012                 {
2013                         /* ftmp IS NECESSARY !!! */
2014                         strcpy(ftmp, newvtt->filename);
2015                         ld_set_filename(ftmp);
2016                         
2017                         //restmp=load_wav(newvtt->filename, &newbuffer, &size);
2018                         restmp=newvtt->load_file(ftmp);
2019                         res+=restmp;
2020                 }
2021                 gtk_box_pack_start(GTK_BOX(control_parent), newvtt->gui.control_box, TRUE, TRUE, 0);
2022                 gtk_box_pack_start(GTK_BOX(audio_parent), newvtt->gui.audio_box, TRUE, TRUE, 0);
2023                 
2024         }
2025         
2026         sequencer.load(input);
2027         
2028         ld_destroy();
2029         
2030         return(res);
2031 }
2032
2033
2034 int  vtt_class :: load_all_12(FILE* input, char *fname)
2035 {
2036         int res=0, restmp=0;
2037         list <vtt_class *> :: iterator vtt;
2038         unsigned int i, max, size;
2039         int16_t *newbuffer;
2040         vtt_class *newvtt;
2041         char ftmp[PATH_MAX];
2042         u_int32_t pid;
2043         
2044         while (main_list.size())
2045         {
2046                 delete((*main_list.begin()));
2047         }
2048                 
2049         atload(max);
2050         atload(master_volume);
2051         set_master_volume(master_volume);
2052         globals.volume=master_volume;
2053         atload(globals.pitch);  
2054         set_master_pitch(globals.pitch);
2055         atload(pid);
2056         sp_master_volume.set_persistence_id(pid);
2057         atload(pid);
2058         sp_master_pitch.set_persistence_id(pid);
2059
2060         ld_create_loaddlg(TX_LOADDLG_MODE_MULTI, max);
2061         ld_set_setname(fname);
2062
2063         for (i=0; i<max; i++)
2064         {
2065                 newvtt=new vtt_class(1);
2066                 res+=newvtt->load_12(input);
2067                 
2068                 if (strlen(newvtt->filename))
2069                 {
2070                         /* ftmp IS NECESSARY !!! */
2071                         strcpy(ftmp, newvtt->filename);
2072                         ld_set_filename(ftmp);
2073                         
2074                         //restmp=load_wav(newvtt->filename, &newbuffer, &size);
2075                         restmp=newvtt->load_file(ftmp);
2076                         res+=restmp;
2077                 }
2078                 gtk_box_pack_start(GTK_BOX(control_parent), newvtt->gui.control_box, TRUE, TRUE, 0);
2079                 gtk_box_pack_start(GTK_BOX(audio_parent), newvtt->gui.audio_box, TRUE, TRUE, 0);
2080                 
2081         }
2082         
2083         sequencer.load(input);
2084         
2085         ld_destroy();
2086         
2087         return(res);
2088 }
2089
2090 int  vtt_class :: load_all_13(FILE* input, char *fname)
2091 {
2092         int res=0, restmp=0;
2093         list <vtt_class *> :: iterator vtt;
2094         unsigned int i, max, size;
2095         int16_t *newbuffer;
2096         vtt_class *newvtt;
2097         char ftmp[PATH_MAX];
2098         u_int32_t pid;
2099         
2100         while (main_list.size())
2101         {
2102                 delete((*main_list.begin()));
2103         }
2104                 
2105         atload(max);
2106         atload(master_volume);
2107         set_master_volume(master_volume);
2108         globals.volume=master_volume;
2109         atload(globals.pitch);  
2110         set_master_pitch(globals.pitch);
2111         atload(pid);
2112         sp_master_volume.set_persistence_id(pid);
2113         atload(pid);
2114         sp_master_pitch.set_persistence_id(pid);
2115
2116         ld_create_loaddlg(TX_LOADDLG_MODE_MULTI, max);
2117         ld_set_setname(fname);
2118
2119         for (i=0; i<max; i++)
2120         {
2121                 newvtt=new vtt_class(1);
2122                 res+=newvtt->load_13(input);
2123                 
2124                 if (strlen(newvtt->filename))
2125                 {
2126                         /* ftmp IS NECESSARY !!! */
2127                         strcpy(ftmp, newvtt->filename);
2128                         ld_set_filename(ftmp);
2129                         
2130                         //restmp=load_wav(newvtt->filename, &newbuffer, &size);
2131                         restmp=newvtt->load_file(ftmp);
2132                         res+=restmp;
2133                 }
2134                 gtk_box_pack_start(GTK_BOX(control_parent), newvtt->gui.control_box, TRUE, TRUE, 0);
2135                 gtk_box_pack_start(GTK_BOX(audio_parent), newvtt->gui.audio_box, TRUE, TRUE, 0);
2136                 
2137         }
2138         
2139         sequencer.load(input);
2140         
2141         ld_destroy();
2142         
2143         return(res);
2144 }
2145
2146 void add_vtt(GtkWidget *ctrl, GtkWidget *audio, char *fn)
2147 {
2148         vtt_class *hmmpg;
2149         hmmpg = new vtt_class(1);
2150         gtk_box_pack_start(GTK_BOX(ctrl), hmmpg->gui.control_box, TRUE, TRUE, 0);
2151         gtk_box_pack_start(GTK_BOX(audio), hmmpg->gui.audio_box, TRUE, TRUE, 0);
2152         if (fn) hmmpg->load_file(fn);
2153 }
2154
2155 extern void vg_move_fx_panel_up(GtkWidget *wid, vtt_class *vtt);
2156 extern void vg_move_fx_panel_down(GtkWidget *wid, vtt_class *vtt);
2157
2158 //#define debug_fx_stack(); for (i=fx_list.begin(); i != fx_list.end(); i++) puts((*i)->get_info_string());
2159 #define debug_fx_stack();
2160
2161 void vtt_class :: effect_up(vtt_fx *effect)
2162 {
2163         list <vtt_fx *> :: iterator i;
2164         list <vtt_fx *> :: iterator previous;
2165         int ok=0;
2166         
2167         debug_fx_stack();
2168         
2169         if ((*fx_list.begin())==effect) return;
2170         
2171         for (previous=i=fx_list.begin(); i != fx_list.end(); i++)
2172         {
2173                 if ((*i) == effect)
2174                 {
2175                         ok=1;
2176                         break;
2177                 }
2178                 previous=i;
2179         }
2180         
2181         if (ok)
2182         {       
2183                 pthread_mutex_lock(&render_lock);
2184                 fx_list.remove(effect);
2185                 fx_list.insert(previous, effect);
2186                 pthread_mutex_unlock(&render_lock);
2187
2188                 vg_move_fx_panel_up(effect->get_panel_widget(), this);
2189         }
2190         
2191         debug_fx_stack();
2192 }
2193
2194 void vtt_class :: effect_down(vtt_fx *effect)
2195 {
2196         list <vtt_fx *> :: iterator i;
2197         int ok=0;
2198
2199         debug_fx_stack();
2200                 
2201         for (i=fx_list.begin(); i != fx_list.end(); i++)
2202         {
2203                 if ((*i) == effect)
2204                 {
2205                         ok=1;
2206                         break;
2207                 }
2208         }
2209         
2210         if ((ok) && (i!=fx_list.end()))
2211         {
2212                 i++;
2213                 if (i==fx_list.end()) return;
2214                 i++;
2215
2216                 pthread_mutex_lock(&render_lock);
2217                 fx_list.remove(effect);
2218                 
2219                 fx_list.insert(i, effect);
2220                 vg_move_fx_panel_down(effect->get_panel_widget(), this);
2221                 pthread_mutex_unlock(&render_lock);
2222         }
2223         
2224 debug_fx_stack();       
2225 }
2226
2227 void vtt_class ::  effect_remove(vtt_fx_ladspa *effect)
2228 {
2229         pthread_mutex_lock(&render_lock);
2230         fx_list.remove(effect);
2231         pthread_mutex_unlock(&render_lock);
2232         
2233         delete effect;
2234 }
2235
2236
2237