ef7187dfe945086738c57a746501a1c4cb836f0e
[terminatorX.git] / src / tX_vtt.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2002  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 #include <glib.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #ifdef USE_3DNOW
43 #include "3dnow.h"
44 #endif
45
46
47 #ifdef DEBUG
48 #define tX_freemem(ptr, varname, comment); fprintf(stderr, "** free() [%s] at %08x. %s.\n", varname, ptr, comment); free(ptr);
49 #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);
50 #else
51 #define tX_freemem(ptr, varname, comment); free(ptr);
52 #define tX_malloc(ptr, varname, comment, size, type); ptr=type malloc(size);
53 #endif
54
55 #include "tX_loaddlg.h"
56
57 #define USE_PREFETCH 1
58
59 #ifdef USE_PREFETCH
60 #define my_prefetch(base, index); __asm__  __volatile__ ("prefetch index(%0)\n" : : "r" (base));
61 #define my_prefetchw(base, index); __asm__  __volatile__ ("prefetchw index(%0)\n" : : "r" (base));
62 #else
63 #define my_prefetch(base, index);  /* NOP */;
64 #define my_prefetchw(base, index); /* NOP */;
65 #endif
66
67 extern void build_vtt_gui(vtt_class *);
68 extern void gui_set_name(vtt_class *vtt, char *newname);
69 extern void gui_set_filename(vtt_class *vtt, char *newname);
70 extern void delete_gui(vtt_class *vtt);
71 extern void gui_update_display(vtt_class *vtt);
72 extern void gui_clear_master_button(vtt_class *vtt);
73 extern void cleanup_vtt(vtt_class *vtt);
74 extern int vg_get_current_page(vtt_class *vtt);
75
76 int vtt_class::vtt_amount=0;
77 list <vtt_class *> vtt_class::main_list;
78 list <vtt_class *> vtt_class::render_list;
79 int16_t* vtt_class::mix_out_buffer=NULL;
80 f_prec * vtt_class::mix_buffer=NULL;
81 f_prec * vtt_class::mix_buffer_end=NULL;
82 int vtt_class::solo_ctr=0;
83
84 int vtt_class::samples_in_mix_buffer=0;
85 pthread_mutex_t vtt_class::render_lock=PTHREAD_MUTEX_INITIALIZER;
86 f_prec vtt_class::master_volume=1.0;
87 f_prec vtt_class::res_master_volume=1.0;
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         lp_enable=0;
121         lp_reso=0.8;
122         lp_freq=0.3;
123         lp_gain=1;
124         lp_setup(lp_gain, lp_reso, lp_freq);
125         lp_reset();
126         
127         ec_enable=0;
128         ec_length=0.5;
129         ec_feedback=0.3;
130         ec_clear_buffer();
131         ec_set_length(0.5);
132         ec_set_pan(0);
133         ec_set_volume(1);
134         
135         main_list.push_back(this);
136
137         /* "connecting" the seq-parameters */
138         
139         sp_speed.set_vtt((void *) this);
140         sp_volume.set_vtt((void *) this);       
141         sp_pitch.set_vtt((void *) this);        
142         sp_pan.set_vtt((void *) this);
143         sp_trigger.set_vtt((void *) this);      
144         sp_loop.set_vtt((void *) this); 
145         sp_sync_client.set_vtt((void *) this);  
146         sp_sync_cycles.set_vtt((void *) this);  
147         sp_lp_enable.set_vtt((void *) this);    
148         sp_lp_gain.set_vtt((void *) this);      
149         sp_lp_reso.set_vtt((void *) this);      
150         sp_lp_freq.set_vtt((void *) this);      
151         sp_ec_enable.set_vtt((void *) this);    
152         sp_ec_length.set_vtt((void *) this);
153         sp_ec_pan.set_vtt((void *) this);
154         sp_ec_volume.set_vtt((void *) this);
155         sp_ec_feedback.set_vtt((void *) this);          
156         sp_mute.set_vtt((void *) this);
157         sp_spin.set_vtt((void *) this);
158
159         x_par = &sp_speed;
160         y_par = &sp_lp_freq;
161         
162         lp_fx=new vtt_fx_lp();
163         lp_fx->set_vtt((void *) this);
164         fx_list.push_back(lp_fx);
165
166         ec_fx=new vtt_fx_ec();
167         ec_fx->set_vtt((void *) this);
168         fx_list.push_back(ec_fx);
169         
170         if (do_create_gui)
171         {       
172                 build_vtt_gui(this);
173                 lp_fx->set_panel_widget(gui.lp_panel->get_widget());    
174                 ec_fx->set_panel_widget(gui.ec_panel->get_widget());
175         }
176         else have_gui=0;
177                 
178         set_pan(0);     
179         set_master_volume(globals.volume);
180         set_output_buffer_size(samples_in_mix_buffer/2);
181         
182         audiofile = NULL;
183         mix_solo=0;
184         mix_mute=0;
185         res_mute=mute;
186         res_mute_old=0;
187         
188         audio_hidden=false;
189         control_hidden=false;\r
190 }
191
192 vtt_class :: ~vtt_class()
193 {
194         vtt_fx *effect;
195         stop();
196
197         main_list.remove(this);
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         tX_malloc(output_buffer, "output_buffer", "vtt set_output_buffer_size()", sizeof(float)*newsize, (float *));
264
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         if (ec_pan>0.0)
306         {
307                 ec_volume_left=(1.0-ec_pan)*ec_res_volume;
308                 ec_volume_right=ec_res_volume;
309         }
310         else if (ec_pan<0.0)
311         {
312                 ec_volume_left=ec_res_volume;
313                 ec_volume_right=(1.0+ec_pan)*ec_res_volume;
314         }
315         else
316         {
317                 ec_volume_left=ec_volume_right=ec_res_volume;
318         }       
319 //      printf("vtt_volume: %f, %f, l: %f, r: %f\n", rel_volume, res_volume, res_volume_left, res_volume_right);
320         
321 #ifdef USE_3DNOW
322         mm_res_volume.s[0]=mm_res_volume.s[1]=res_volume;
323 #endif  
324 }
325
326 void vtt_class :: set_pan(f_prec newpan)
327 {
328         pan=newpan;
329         recalc_volume();
330 }
331
332 void vtt_class :: set_pitch(f_prec newpitch)
333 {
334         rel_pitch=newpitch;
335 //      res_pitch=fabs(globals.pitch)*rel_pitch;
336         res_pitch=globals.pitch*rel_pitch;
337         speed=res_pitch;
338         ec_set_length(ec_length);
339 }
340
341 void vtt_class :: recalc_pitch()
342 {
343 //      res_pitch=fabs(globals.pitch)*rel_pitch;
344         res_pitch=globals.pitch*rel_pitch;
345         speed=res_pitch;
346         ec_set_length(ec_length);
347 }
348
349 void vtt_class :: set_autotrigger(int newstate)
350 {
351         autotrigger=newstate;
352 }
353
354 void vtt_class :: set_loop(int newstate)
355 {
356         loop=newstate;
357 }
358
359 void vtt_class :: set_mute(int newstate)
360 {
361         mute=newstate;
362         calc_mute();
363 }
364
365 void vtt_class :: set_mix_mute(int newstate)
366 {
367         mix_mute=newstate;
368         calc_mute();
369 }
370
371 void vtt_class :: set_mix_solo(int newstate)
372 {
373         if (mix_solo && !newstate)
374         {
375                 /* turning it off */
376                 mix_solo=0;
377                 solo_ctr--;
378         }
379         else if (!mix_solo && newstate)
380         {
381                 /* turning it on */
382                 mix_solo=1;
383                 solo_ctr++;
384         }
385         calc_mute();
386
387         /* locking ? */
388         list <vtt_class *> :: iterator vtt;
389         
390         for (vtt=main_list.begin(); vtt!=main_list.end() ; vtt++)
391         {
392                 (*vtt)->calc_mute();
393         }
394 }
395
396 void vtt_class :: lp_set_enable (int newstate)
397 {
398         lp_enable=newstate;
399         lp_reset();
400 }
401
402 void vtt_class :: lp_reset()
403 {
404         lp_buf0=lp_buf1=0;
405 }
406
407 void vtt_class :: lp_set_gain (f_prec gain)
408 {
409         lp_gain=gain;
410         lp_resgain=lp_gain*lp_autogain;
411 }
412
413 void vtt_class :: lp_set_reso(f_prec reso)
414 {
415         lp_reso=reso;
416         
417         lp_b=reso*(1.0+(1.0/lp_a));
418         lp_autogain=1.0-reso*GAIN_AUTO_ADJUST;
419         lp_resgain=lp_gain*lp_autogain;
420 }
421
422 void vtt_class :: lp_set_freq(f_prec freq)
423 {
424         lp_freq=freq;
425         
426         lp_a=0.9999-freq;
427         lp_b=lp_reso*(1.0+(1.0/lp_a));
428 }
429
430 void vtt_class :: lp_setup(f_prec gain, f_prec reso, f_prec freq)
431 {
432         lp_freq=freq;
433         lp_reso=reso;
434         
435         lp_a=1.0-freq;
436         lp_b=reso*(1.0+(1.0/lp_a));
437         
438         lp_autogain=1.0-reso*GAIN_AUTO_ADJUST;
439         lp_resgain=lp_gain*lp_autogain;
440 }
441
442 void vtt_class :: ec_set_enable(int newstate)
443 {
444         ec_enable=newstate;
445         ec_clear_buffer();
446 }
447
448
449 void vtt_class :: ec_set_pan(f_prec pan)
450 {
451         ec_pan=pan;
452
453         recalc_volume();
454 }
455
456 /* Max length is 1.0 */
457
458 void vtt_class :: ec_set_length(f_prec length)
459 {
460         int delay;
461         int i=0;
462         
463         ec_length=length;
464         if (res_pitch==0) 
465         {
466                 ec_res_length=length*samples_in_buffer;
467         }
468         else
469         {
470                 ec_res_length=length*samples_in_buffer/res_pitch;       
471         }
472         
473         if (ec_res_length<0) ec_res_length*=-1;
474         
475         if (ec_res_length>=EC_MAX_BUFFER)
476         {
477                 ec_res_length=EC_MAX_BUFFER*length;
478         }
479         
480         delay=(int )floor(ec_res_length);
481         delay-=2;
482         ec_delay=&ec_buffer[delay];
483 }
484
485 void vtt_class :: ec_set_feedback(f_prec feedback)
486 {
487         ec_feedback=feedback;
488 }
489
490
491 void vtt_class :: ec_set_volume(f_prec volume)
492 {
493         ec_volume=volume;
494         recalc_volume();
495 }
496
497 void vtt_class :: ec_clear_buffer()
498 {
499         f_prec *ptr;
500         
501         for (ptr=ec_buffer; ptr<=ec_delay; ptr++)
502         {
503                 *ptr=0.0;
504         }
505         ec_ptr=ec_buffer;
506 }
507
508 void vtt_class :: render()
509 {
510         list <vtt_fx *> :: iterator effect;
511
512         if (do_scratch)
513         {
514                 if (sense_cycles>0)
515                 {
516                         sense_cycles--;
517                         if (sense_cycles==0) sp_speed.receive_input_value(0);
518                 }
519         }
520         render_scratch();
521         
522         for (effect=fx_list.begin(); effect != fx_list.end(); effect++)
523         {
524                 if ((*effect)->isEnabled()) (*effect)->run();
525         }
526 }
527
528 extern void vg_create_fx_gui(vtt_class *vtt, vtt_fx_ladspa *effect, LADSPA_Plugin *plugin);
529
530 vtt_fx_ladspa * vtt_class :: add_effect (LADSPA_Plugin *plugin)
531 {
532         vtt_fx_ladspa *new_effect;
533         
534         new_effect = new vtt_fx_ladspa (plugin, this);
535         pthread_mutex_lock(&render_lock);
536         fx_list.push_back(new_effect);
537         if (is_playing) new_effect->activate();
538         pthread_mutex_unlock(&render_lock);
539         vg_create_fx_gui(this, new_effect, plugin);
540         
541         return new_effect;
542 }
543
544 void vtt_class :: calc_speed()
545 {
546         do_mute=fade_out=fade_in=0;
547
548         if (speed != speed_target)
549         {
550                 speed_target=speed;
551                 speed_step=speed_target-speed_real;
552                 speed_step/=10.0;
553         }
554                         
555         if (speed_target != speed_real)
556         {
557                 speed_real+=speed_step;
558                 if ((speed_step<0) && (speed_real<speed_target)) speed_real=speed_target;
559                 else
560                 if ((speed_step>0) && (speed_real>speed_target)) speed_real=speed_target;                       
561         }
562         
563         if (fade)
564         {
565                 if ((speed_last==0) && (speed_real !=0))
566                 {
567                         fade_in=1;
568                         fade=NEED_FADE_OUT;
569                 }
570         }
571         else
572         {
573                 if ((speed_last!=0) && (speed_real==0))
574                 {
575                         fade_out=1;
576                         fade=NEED_FADE_IN;
577                 }
578         }
579
580         speed_last = speed_real;
581
582         if (res_mute != res_mute_old)
583         {
584                 if (res_mute)
585                 {
586                         fade_out=1; fade_in=0;
587                         fade=NEED_FADE_IN;
588                 }
589                 else
590                 {
591                         fade_in=1; fade_out=0;
592                         fade=NEED_FADE_OUT;
593                 }
594                 res_mute_old=res_mute;
595         }
596         else
597         {
598                 if (res_mute) do_mute=1;
599         }       
600 }
601
602 void vtt_class :: render_scratch()
603 {
604         int16_t *ptr;
605         
606         int sample;
607         
608         f_prec pos_a_f;
609         
610         f_prec amount_a;
611         f_prec amount_b;
612
613         f_prec sample_a;
614         f_prec sample_b;
615         
616         f_prec sample_res;
617         
618         f_prec *out;
619         f_prec fade_vol;        
620
621         calc_speed();
622                                         
623         for (sample =0,out=output_buffer, fade_vol=0.0; sample < samples_in_outputbuffer;sample++, out++, fade_vol+=inv_samples_in_outputbuffer)
624         {
625                 if ((speed_real!=0) || (fade_out))
626                 {
627
628                         pos_f+=speed_real;
629
630                         if (pos_f>maxpos)
631                         {
632                                 pos_f-=maxpos;
633                                 if (res_pitch>0)
634                                 {
635                                         if (loop)
636                                         {
637                                         if (is_sync_master)
638                                         {
639                                                 master_triggered=1;
640                                                 master_triggered_at=sample;
641                                         }
642                                         }
643                                         else
644                                         {
645                                                 want_stop=1;
646                                         }
647                                         
648                                 }
649                         }
650                         else if (pos_f<0)
651                         {
652                                 pos_f+=maxpos;
653                                 if (res_pitch<0)
654                                 {
655                                         if (loop)
656                                         {
657                                         if (is_sync_master)
658                                         {
659                                                 master_triggered=1;
660                                                 master_triggered_at=sample;
661                                         }
662                                         }
663                                         else
664                                         {
665                                                 want_stop=1;
666                                         }
667                                 }
668                         }
669                                 
670                         pos_a_f=floor(pos_f);
671                         pos_i=(unsigned int) pos_a_f;
672                                                                 
673                         amount_b=pos_f-pos_a_f;                         
674                         amount_a=1.0-amount_b;                          
675                                 
676                         if (do_mute)
677                         {
678                                 *out=0.0;
679                         }
680                         else
681                         {
682                                 ptr=&buffer[pos_i];
683                                 sample_a=(f_prec) *ptr;
684                         
685                                 if (pos_i == samples_in_buffer) 
686                                 {
687                                         sample_b=*buffer;
688                                 }
689                                 else
690                                 {
691                                         ptr++;
692                                         sample_b=(f_prec) *ptr;
693                                 }
694                                 
695                                 sample_res=(sample_a*amount_a)+(sample_b*amount_b);
696                                                                 
697                                 if (fade_in)
698                                 {
699                                         sample_res*=fade_vol;
700                                 }
701                                 else
702                                 if (fade_out)
703                                 {
704                                         sample_res*=1.0-fade_vol;
705                                 }
706  
707                                 *out=sample_res;
708                         }
709                 }
710                 else
711                 {
712                                 *out=0;
713                 }
714         }
715 }       
716
717 void vtt_class :: forward_turntable()
718 {
719         int sample;
720         double pos_f_tmp;
721 #ifdef pos_f_test
722         int show=0;
723         double diff;
724 #endif
725
726         calc_speed();
727
728         if ((speed_real==0) && (!fade_out)) return;
729         
730         
731         /* following code is problematic as adding speed_real*n is
732           different from adding speed_real n times to pos_f.
733           
734           well it speeds things up quite a bit and double precision
735           seems to do a satisfying job.
736           
737           #define pos_f_test to prove that.
738         */
739         
740         pos_f_tmp=pos_f+speed_real*samples_in_outputbuffer;
741         
742         if ((pos_f_tmp > 0) && (pos_f_tmp < maxpos))
743         {
744 #ifdef pos_f_test
745                 show=1;
746 #else   
747                 pos_f=pos_f_tmp;
748                 return;
749 #endif          
750         }
751                                 
752         /* now the slow way ;) */
753         
754         for (sample =0; sample < samples_in_outputbuffer; sample++)
755         {
756                         pos_f+=speed_real;
757
758                         if (pos_f>maxpos)
759                         {
760                                 pos_f-=maxpos;
761                                 if (res_pitch>0)
762                                 {
763                                         if (loop)
764                                         {
765                                         if (is_sync_master)
766                                         {
767                                                 master_triggered=1;
768                                                 master_triggered_at=sample;
769                                         }
770                                         }
771                                         else
772                                         {
773                                                 want_stop=1;
774                                         }
775                                         
776                                 }
777                         }
778                         else if (pos_f<0)
779                         {
780                                 pos_f+=maxpos;
781                                 if (res_pitch<0)
782                                 {
783                                         if (loop)
784                                         {
785                                         if (is_sync_master)
786                                         {
787                                                 master_triggered=1;
788                                                 master_triggered_at=sample;
789                                         }
790                                         }
791                                         else
792                                         {
793                                                 want_stop=1;
794                                         }
795                                 }
796                         }
797                 
798         }
799 #ifdef pos_f_test
800         if (show)
801         {
802                 diff=pos_f_tmp-pos_f;
803                 if (diff!=0) printf("fast: %f, slow: %f, diff: %f, tt: %s\n", pos_f_tmp, pos_f, diff, name);
804         }
805 #endif  
806 }       
807
808 /*
809         The following lowpass filter is based on some sample code by
810         Paul Kellett <paul.kellett@maxim.abel.co.uk>
811 */
812
813 void vtt_class :: render_lp()
814 {
815         f_prec *sample;
816                 
817         for (sample = output_buffer; sample<end_of_outputbuffer; sample++)
818         {
819                 lp_buf0 = lp_a * lp_buf0 + lp_freq * ((*sample)*lp_resgain + lp_b * (lp_buf0 - lp_buf1));
820                 lp_buf1 = lp_a * lp_buf1 + lp_freq * lp_buf0;
821                 
822                 *sample=lp_buf1;
823         }
824 }
825
826 void vtt_class :: render_ec()
827 {
828         f_prec *sample;
829         f_prec *ec_sample;
830         int i;
831
832         for (i=0, sample = output_buffer, ec_sample=ec_output_buffer; i<samples_in_outputbuffer; i++, ec_sample++,sample++, ec_ptr++)
833         {
834                 if (ec_ptr>ec_delay) ec_ptr=ec_buffer;
835                 *ec_sample=(*ec_ptr) *ec_feedback;
836                 *ec_ptr=*sample+*ec_sample;
837         }       
838 }
839
840 int vtt_class :: set_mix_buffer_size(int no_samples)
841 {
842         list <vtt_class *> :: iterator vtt;
843         int res=0;
844         
845 //      printf("vtt_class::set_mix_buffer_size(), mix_buffer: %12x, mix_out: %12x, samples: %i\n", mix_buffer, mix_out_buffer, no_samples);
846         
847         if (mix_buffer) tX_freemem(mix_buffer, "mix_buffer", "vtt set_mix_buffer_size()");
848         samples_in_mix_buffer=no_samples*2;
849
850         tX_malloc(mix_buffer, "mix_buffer", "vtt set_mix_buffer_size()", sizeof(float)*samples_in_mix_buffer, (float *));
851         mix_buffer_end=mix_buffer+samples_in_mix_buffer;
852
853 //      printf("mix_buffer: %12x\n", mix_buffer);
854 //      printf("mix_samples: %i, out_samples: %i", samples_in_mix_buffer, no_samples);
855         
856         if (mix_out_buffer) tX_freemem(mix_out_buffer, "mix_out_buffer", "vtt set_mix_buffer_size()");
857         tX_malloc(mix_out_buffer, "mix_out_buffer", "vtt set_mix_buffer_size()", sizeof(int16_t)*samples_in_mix_buffer + 4, (int16_t *));
858
859 //      printf("mix_out_buffer: %12x\n", mix_out_buffer);
860         
861         for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
862         {
863                 res|=(*vtt)->set_output_buffer_size(no_samples);
864         }
865         
866         if ((!mix_buffer) || (!mix_out_buffer) || res) return(1);
867         return(0);
868 }
869
870 int16_t * vtt_class :: render_all_turntables()
871 {
872         list <vtt_class *> :: iterator vtt, next;
873         int sample;
874         int mix_sample;
875         f_prec temp;
876
877 #ifdef USE_3DNOW
878         mmx_t *mix;
879         mmx_t *vtt_buffer;
880         int32_t *mix_int;
881 #endif
882
883 #ifdef USE_FLASH
884         f_prec max;
885         f_prec min;
886 #ifdef USE_3DNOW
887         mmx_t mm_max;
888         mmx_t mm_min;
889         mmx_t mm_volume;
890         mmx_t mm_src1;
891         mmx_t mm_src2;
892
893 #ifndef OVERRIDE_MOVQ_AUTODETECT
894 #ifndef GCC_VERSION
895 #define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
896 #endif /* GCC_VERSION */
897
898 #if (GCC_VERSION < 2096)
899 #warning "*************************"
900 #warning "* gcc < 2.96            *"
901 #warning "* assuming working movq *"
902 #warning "*************************"
903 #undef GCC_MOVQ_BUG_WORKAROUND
904 #else
905 #warning "*************************"
906 #warning "* gcc >= 2.96           *"
907 #warning "* using movq-workaround *"
908 #warning "*************************"
909 #define GCC_MOVQ_BUG_WORKAROUND 1
910 #endif /* GCC < 2.96 */
911 #endif /* OVERRIDE MOVQ AUTODETECVT */
912         
913 #ifdef GCC_MOVQ_BUG_WORKAROUND
914         /* REQUIRED DUE TO GCC BUG (2.96-3.0.2) */
915         mmx_t *mm_src1_ptr=&mm_src1;
916         mmx_t *mm_src2_ptr=&mm_src2;
917         mmx_t *mm_volume_ptr=&mm_volume;
918         mmx_t *mm_max_ptr=&mm_max;
919         mmx_t *mm_min_ptr=&mm_min;
920         
921 #define MM_VAR_ACC(var) (* var ## _ptr)
922 #define MM_VAR_MOVQ(var) * var ## _ptr
923 #else
924 #define MM_VAR_ACC(var) var
925 #define MM_VAR_MOVQ(var) var
926 #endif  
927         int32_t *temp_int=&mm_max.d[1];
928 #endif  
929 #endif  
930         
931         pthread_mutex_lock(&render_lock);
932         
933         if (render_list.size()==0)
934         {
935                 for (sample=0; sample<samples_in_mix_buffer; sample++)
936                 {
937                         mix_out_buffer[sample]=0;
938                 }
939         }
940         else
941         {
942                         vtt=render_list.begin();
943                         (*vtt)->render();                       
944                         max=(*vtt)->max_value;
945                         min=max;
946
947 #ifndef USE_3DNOW
948                         for (sample=0, mix_sample=0; sample<(*vtt)->samples_in_outputbuffer; sample++)
949                         {                               
950                                 temp=(*vtt)->output_buffer[sample];
951                                 mix_buffer[mix_sample]=temp*(*vtt)->res_volume_left;
952                                 mix_sample++;
953                                 mix_buffer[mix_sample]=temp*(*vtt)->res_volume_right;
954                                 mix_sample++;
955                                 
956                                 if (temp>max) max=temp;
957                                 else if (temp<min) min=temp;
958                         }
959 #else
960                         MM_VAR_ACC(mm_volume).s[0]=(*vtt)->res_volume_left;
961                         MM_VAR_ACC(mm_volume).s[1]=(*vtt)->res_volume_right;
962
963                         MM_VAR_ACC(mm_max).s[1]=MM_VAR_ACC(mm_max).s[0]=max;
964                         MM_VAR_ACC(mm_min).s[1]=MM_VAR_ACC(mm_min).s[0]=min;
965                         
966                         movq_m2r(MM_VAR_MOVQ(mm_max), mm1);
967                         movq_m2r(MM_VAR_MOVQ(mm_min), mm2);
968                                                         
969                         movq_m2r(MM_VAR_MOVQ(mm_volume), mm0);
970                         
971                         mix=(mmx_t*)mix_buffer;
972                         
973                         for (f_prec* src=(*vtt)->output_buffer; mix < (mmx_t*) mix_buffer_end;)
974                         {
975                                 /* first sample */
976                                 MM_VAR_ACC(mm_src1).s[0]=*src;
977                                 MM_VAR_ACC(mm_src1).s[1]=*src;
978                                         
979                                 /* sample * l/r volume */
980                                 movq_m2r(MM_VAR_MOVQ(mm_src1), mm3);
981                                 pfmul_r2r(mm0, mm3);
982                                 movq_r2m(mm3, *mix);
983                                         
984                                 /* next sample */
985                                 src++, mix++;
986                                 MM_VAR_ACC(mm_src2).s[0]=*src;
987                                 MM_VAR_ACC(mm_src2).s[1]=*src;
988                                         
989                                 /* sample * l/r volume */
990                                 movq_m2r(MM_VAR_MOVQ(mm_src2), mm3);
991                                 pfmul_r2r(mm0, mm3);
992                                 movq_r2m(mm3, *mix);
993                                         
994                                 /* calculating min/max */
995                                 MM_VAR_ACC(mm_src1).s[1]=MM_VAR_ACC(mm_src2).s[0];
996                                 movq_m2r(mm_src1, mm3);
997                                 pfmax_r2r(mm3, mm1);
998                                 pfmin_r2r(mm3, mm2);
999                                 
1000                                 src++, mix++;
1001                         }
1002
1003                         movq_r2m(mm1, MM_VAR_MOVQ(mm_max));
1004                         movq_r2m(mm2, MM_VAR_MOVQ(mm_min));
1005                         
1006                         femms();
1007                         
1008                         if (MM_VAR_ACC(mm_max).s[0]>MM_VAR_ACC(mm_max).s[1]) max=MM_VAR_ACC(mm_max).s[0]; else max=MM_VAR_ACC(mm_max).s[1];
1009                         if (MM_VAR_ACC(mm_min).s[0]<MM_VAR_ACC(mm_min).s[0]) min=MM_VAR_ACC(mm_min).s[0]; else min=MM_VAR_ACC(mm_min).s[1];
1010 #endif                  
1011                         
1012                         min*=-1.0;
1013                         if (min>max) (*vtt)->max_value=min; else (*vtt)->max_value=max;
1014
1015                         if ((*vtt)->ec_enable)
1016                         {
1017 #ifndef USE_3DNOW                       
1018                                 for (sample=0, mix_sample=0; sample<(*vtt)->samples_in_outputbuffer; sample++)
1019                                 {                               
1020                                         temp=(*vtt)->ec_output_buffer[sample];
1021                                         
1022                                         mix_buffer[mix_sample]+=temp*(*vtt)->ec_volume_left;
1023                                         mix_sample++;
1024                                         mix_buffer[mix_sample]+=temp*(*vtt)->ec_volume_right;
1025                                         mix_sample++;
1026                                 }
1027 #else
1028                                 MM_VAR_ACC(mm_volume).s[0]=(*vtt)->ec_volume_left;
1029                                 MM_VAR_ACC(mm_volume).s[1]=(*vtt)->ec_volume_right;
1030                                                 
1031                                 movq_m2r(MM_VAR_MOVQ(mm_volume), mm0);
1032                                 mix =(mmx_t*)mix_buffer;
1033
1034                                 for (f_prec* src=(*vtt)->ec_output_buffer; mix < (mmx_t*) mix_buffer_end; src++, mix++)
1035                                 {
1036                                         /* first sample */
1037                                         MM_VAR_ACC(mm_src1).s[0]=*src;
1038                                         MM_VAR_ACC(mm_src1).s[1]=*src;
1039                                 
1040                                         /* sample * l/r volume */
1041                                         movq_m2r(MM_VAR_MOVQ(mm_src1), mm3);
1042                                         pfmul_r2r(mm0, mm3);
1043                                 
1044                                         /* accumulating complete mix */
1045                                         movq_m2r(*mix, mm4);
1046                                         pfadd_r2r(mm4, mm3);
1047                                         movq_r2m(mm3, *mix);
1048                                 }
1049                                 femms();
1050 #endif                          
1051                         }
1052                         
1053                         if (master_triggered)
1054                         {
1055                                 pthread_mutex_unlock(&render_lock);
1056                                 for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
1057                                 {
1058                                         if ((*vtt)->is_sync_client)
1059                                         {
1060                                                 if ((*vtt)->sync_countdown)
1061                                                 {
1062                                                         (*vtt)->sync_countdown--;
1063                                                 }
1064                                                 else
1065                                                 {
1066                                                         (*vtt)->sync_countdown=(*vtt)->sync_cycles;
1067                                                         (*vtt)->trigger();
1068                                                 }
1069                                         }
1070                                 }
1071                                 pthread_mutex_lock(&render_lock);
1072                         }
1073                         
1074                         vtt=render_list.begin();
1075                         for (vtt++; vtt!=render_list.end(); vtt++)
1076                         {
1077                                 (*vtt)->render();                                       
1078                                 max=(*vtt)->max_value;
1079                                 min=max;
1080
1081 #ifndef USE_3DNOW
1082                                 for (sample=0, mix_sample=0; sample<(*vtt)->samples_in_outputbuffer; sample++)
1083                                 {                               
1084                                         temp=(*vtt)->output_buffer[sample];
1085                                         mix_buffer[mix_sample]+=temp*(*vtt)->res_volume_left;
1086                                         mix_sample++;                                   
1087                                         mix_buffer[mix_sample]+=temp*(*vtt)->res_volume_right;
1088                                         mix_sample++;
1089                                 
1090                                         if (temp>max) max=temp;
1091                                         else if (temp<min) min=temp;
1092                                 }
1093 #else
1094                                 MM_VAR_ACC(mm_volume).s[0]=(*vtt)->res_volume_left;
1095                                 MM_VAR_ACC(mm_volume).s[1]=(*vtt)->res_volume_right;
1096
1097                                 MM_VAR_ACC(mm_max).s[1]=MM_VAR_ACC(mm_max).s[0]=max;
1098                                 MM_VAR_ACC(mm_min).s[1]=MM_VAR_ACC(mm_min).s[0]=min;
1099                         
1100                                 movq_m2r(MM_VAR_MOVQ(mm_max), mm1);
1101                                 movq_m2r(MM_VAR_MOVQ(mm_min), mm2);
1102                                                         
1103                                 movq_m2r(MM_VAR_MOVQ(mm_volume), mm0);
1104                                 mix=(mmx_t*)mix_buffer;
1105
1106                                 for (f_prec* src=(*vtt)->output_buffer; mix < (mmx_t*) mix_buffer_end;)
1107                                 {
1108                                         /* first sample */
1109                                         MM_VAR_ACC(mm_src1).s[0]=*src;
1110                                         MM_VAR_ACC(mm_src1).s[1]=*src;
1111                                         
1112                                         /* sample * l/r volume */
1113                                         movq_m2r(MM_VAR_MOVQ(mm_src1), mm3);
1114                                         pfmul_r2r(mm0, mm3);
1115                                         
1116                                         /* accumulating complete mix */
1117                                         movq_m2r(*mix, mm4);
1118                                         pfadd_r2r(mm4, mm3);
1119                                         movq_r2m(mm3, *mix);
1120                                         
1121                                         /* next sample */
1122                                         src++, mix++;
1123                                         MM_VAR_ACC(mm_src2).s[0]=*src;
1124                                         MM_VAR_ACC(mm_src2).s[1]=*src;
1125                                         
1126                                         /* sample * l/r volume */
1127                                         movq_m2r(MM_VAR_MOVQ(mm_src2), mm3);
1128                                         pfmul_r2r(mm0, mm3);
1129
1130                                         /* accumulating complete mix */
1131                                         movq_m2r(*mix, mm4);
1132                                         pfadd_r2r(mm4, mm3);
1133                                         movq_r2m(mm3, *mix);
1134                                         
1135                                         /* calculating min/max */
1136                                         MM_VAR_ACC(mm_src1).s[1]=MM_VAR_ACC(mm_src2).s[0];
1137                                         movq_m2r(MM_VAR_MOVQ(mm_src1), mm3);
1138                                         pfmax_r2r(mm3, mm1);
1139                                         pfmin_r2r(mm3, mm2);
1140                                         
1141                                         src++, mix++;
1142                                 }
1143
1144                                 movq_r2m(mm1, MM_VAR_MOVQ(mm_max));
1145                                 movq_r2m(mm2, MM_VAR_MOVQ(mm_min));
1146                                 
1147                                 femms();
1148         
1149                                 if (MM_VAR_ACC(mm_max).s[0]>MM_VAR_ACC(mm_max).s[1]) max=MM_VAR_ACC(mm_max).s[0]; else max=MM_VAR_ACC(mm_max).s[1];
1150                                 if (MM_VAR_ACC(mm_min).s[0]<MM_VAR_ACC(mm_min).s[0]) min=MM_VAR_ACC(mm_min).s[0]; else min=MM_VAR_ACC(mm_min).s[1];
1151 #endif
1152                                 
1153                                 min*=-1.0;
1154                                 if (min>max) (*vtt)->max_value=min; else (*vtt)->max_value=max;
1155                                 
1156                                 if ((*vtt)->ec_enable)
1157                                 {
1158 #ifndef USE_3DNOW
1159                                         for (sample=0, mix_sample=0; sample<(*vtt)->samples_in_outputbuffer; sample++)
1160                                         {                               
1161                                                 temp=(*vtt)->ec_output_buffer[sample];
1162                                                 
1163                                                 mix_buffer[mix_sample]+=temp*(*vtt)->ec_volume_left;
1164                                                 mix_sample++;
1165                                                 mix_buffer[mix_sample]+=temp*(*vtt)->ec_volume_right;
1166                                                 mix_sample++;
1167                                         }
1168 #else
1169                                         MM_VAR_ACC(mm_volume).s[0]=(*vtt)->ec_volume_left;
1170                                         MM_VAR_ACC(mm_volume).s[1]=(*vtt)->ec_volume_right;
1171                                                 
1172                                         movq_m2r(MM_VAR_MOVQ(mm_volume), mm0);
1173                                         mix =(mmx_t*)mix_buffer;
1174
1175                                         for (f_prec* src=(*vtt)->ec_output_buffer; mix < (mmx_t*) mix_buffer_end; src++, mix++)
1176                                         {
1177                                                 /* first sample */
1178                                                 MM_VAR_ACC(mm_src1).s[0]=*src;
1179                                                 MM_VAR_ACC(mm_src1).s[1]=*src;
1180                                 
1181                                                 /* sample * l/r volume */
1182                                                 movq_m2r(MM_VAR_MOVQ(mm_src1), mm3);
1183                                                 pfmul_r2r(mm0, mm3);
1184                                 
1185                                                 /* accumulating complete mix */
1186                                                 movq_m2r(*mix, mm4);
1187                                                 pfadd_r2r(mm4, mm3);
1188                                                 movq_r2m(mm3, *mix);
1189                                         }
1190
1191                                         femms();
1192 #endif                                  
1193                                 }
1194                         }
1195                         
1196                         /* left */
1197                         
1198                         max=mix_max_l;
1199                         min=max;
1200
1201                         for (sample=0; sample<samples_in_mix_buffer; sample+=2)
1202                         {                               
1203                                 temp=mix_buffer[sample];
1204                                 mix_out_buffer[sample]=(int16_t) temp;
1205                         
1206                                 if (temp>max) max=temp;
1207                                 else if (temp<min) min=temp;
1208                         }
1209                         
1210                         min*=-1.0;
1211                         if (min>max) mix_max_l=min; else mix_max_l=max;         
1212                         
1213                         /* right */
1214                         
1215                         max=mix_max_r;
1216                         min=max;
1217
1218                         for (sample=1; sample<samples_in_mix_buffer; sample+=2)
1219                         {                               
1220                                 temp=mix_buffer[sample];
1221                                 mix_out_buffer[sample]=(int16_t) temp;
1222                         
1223                                 if (temp>max) max=temp;
1224                                 else if (temp<min) min=temp;
1225                         }
1226                         
1227                         min*=-1.0;
1228                         if (min>max) mix_max_r=min; else mix_max_r=max;         
1229                         
1230         }
1231         master_triggered=0;
1232                 
1233         vtt=render_list.begin();
1234         while (vtt!=render_list.end())
1235         {
1236                 next=vtt;
1237                 next++;
1238                 
1239                 if ((*vtt)->want_stop) (*vtt)->stop_nolock();
1240                 vtt=next;
1241         }
1242         pthread_mutex_unlock(&render_lock);
1243         
1244         return(mix_out_buffer);
1245 }
1246
1247 void vtt_class :: forward_all_turntables()
1248 {
1249         list <vtt_class *> :: iterator vtt, next;
1250
1251         if (render_list.size()>0)
1252         {
1253                  vtt=render_list.begin();
1254                  (*vtt)->forward_turntable();                    
1255
1256                  if (master_triggered)
1257                  {
1258                          for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
1259                          {
1260                                  if ((*vtt)->is_sync_client)
1261                                  {
1262                                          if ((*vtt)->sync_countdown)
1263                                          {
1264                                                  (*vtt)->sync_countdown--;
1265                                          }
1266                                          else
1267                                          {
1268                                                  (*vtt)->sync_countdown=(*vtt)->sync_cycles;
1269                                                  (*vtt)->trigger();
1270                                          }
1271                                  }
1272                          }
1273                  }
1274
1275                  vtt=render_list.begin();
1276                  for (vtt++; vtt!=render_list.end(); vtt++)
1277                  {
1278                          (*vtt)->forward_turntable();
1279                  }
1280                  
1281         }
1282         master_triggered=0;
1283         vtt=render_list.begin();
1284         while (vtt!=render_list.end())
1285         {
1286                 next=vtt;
1287                 next++;
1288                 
1289                 if ((*vtt)->want_stop) (*vtt)->stop_nolock();
1290                 vtt=next;
1291         }
1292 }
1293
1294
1295 int vtt_class :: trigger()
1296 {
1297         list <vtt_fx *> :: iterator effect;
1298
1299         if (!buffer) return (1);
1300         
1301         if (!is_playing) pthread_mutex_lock(&render_lock);
1302         
1303         if (res_pitch>=0) pos_f=0;
1304         else pos_f=maxpos;
1305         fade=NEED_FADE_OUT;
1306         speed=res_pitch;
1307         speed_real=res_pitch;
1308         speed_target=res_pitch;
1309         want_stop=0;
1310
1311         /* activating plugins */
1312         for (effect=fx_list.begin(); effect != fx_list.end(); effect++)
1313         {
1314                 (*effect)->activate();
1315         }
1316
1317
1318 #ifdef USE_FLASH
1319         max_value=0;
1320 #endif
1321         
1322         if (is_sync_master)
1323         {
1324                 master_triggered=1;
1325                 master_triggered_at=0;
1326         }
1327         
1328         if (!is_playing)
1329         {
1330                 is_playing=1;
1331         
1332                 if (is_sync_master) 
1333                 {
1334                         render_list.push_front(this);           
1335                 }               
1336                 else
1337                 {
1338                         render_list.push_back(this);
1339                 }
1340                 pthread_mutex_unlock(&render_lock);
1341         }
1342         return(0);
1343 }
1344
1345 int vtt_class :: stop_nolock()
1346 {
1347         list <vtt_fx *> :: iterator effect;
1348
1349         if (!is_playing) 
1350         {
1351                 pthread_mutex_unlock(&render_lock);
1352                 return(1);
1353         }
1354         render_list.remove(this);
1355         want_stop=0;
1356
1357         is_playing=0;
1358
1359 #ifdef USE_FLASH
1360         max_value=0;
1361 #endif
1362         cleanup_vtt(this);
1363         sync_countdown=0;
1364         
1365         /* deactivating plugins */
1366         for (effect=fx_list.begin(); effect != fx_list.end(); effect++)
1367         {
1368                 (*effect)->deactivate();
1369         }
1370         
1371         return(0);
1372 }
1373
1374 int vtt_class :: stop()
1375 {
1376         int res;
1377         
1378         pthread_mutex_lock(&render_lock);
1379
1380         res=stop_nolock();
1381
1382         pthread_mutex_unlock(&render_lock);
1383
1384         return(res);
1385 }
1386
1387 void vtt_class :: set_sync_master(int master)
1388 {
1389         if (master)
1390         {
1391                 if (sync_master) sync_master->set_sync_master(0);
1392                 sync_master=this;
1393                 is_sync_master=1;
1394         }
1395         else
1396         {
1397                 if (sync_master==this) sync_master=0;
1398                 is_sync_master=0;
1399                 gui_clear_master_button(this);
1400         }
1401 }
1402
1403 void vtt_class :: set_sync_client(int slave, int cycles)
1404 {
1405         is_sync_client=slave;
1406         sync_cycles=cycles;
1407 //      sync_countdown=cycles; 
1408         sync_countdown=0;
1409 }
1410
1411 void vtt_class :: set_sync_client_ug(int slave, int cycles)
1412 {
1413         set_sync_client(slave, cycles);
1414 }
1415
1416 void vtt_class :: set_master_volume(f_prec new_volume)
1417 {
1418         list <vtt_class *> :: iterator vtt;
1419
1420         master_volume=new_volume;
1421         globals.volume=new_volume;
1422         
1423         if (main_list.size()>0)
1424         {
1425                 vol_channel_adjust=sqrt((f_prec) main_list.size());
1426                 res_master_volume=master_volume/vol_channel_adjust;             
1427         }
1428                 
1429         for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
1430         {
1431                 (*vtt)->recalc_volume();
1432         }
1433 }
1434
1435 void vtt_class :: set_master_pitch(f_prec new_pitch)
1436 {
1437         list <vtt_class *> :: iterator vtt;
1438         
1439         globals.pitch=new_pitch;
1440         for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
1441         {
1442                 (*vtt)->recalc_pitch();
1443         }
1444 }
1445
1446 void vtt_class :: enable_saturate (int newstate)
1447 {
1448         do_saturate=newstate;
1449 }
1450
1451 void vtt_class :: focus_no(int no)
1452 {
1453         list <vtt_class *> :: iterator vtt;
1454         int i;
1455
1456         for (i=0, vtt=main_list.begin(); vtt!=main_list.end(); vtt++, i++)
1457         {
1458                 if (i==no)
1459                 {
1460                         focused_vtt=(*vtt);
1461                 }
1462         }
1463 }
1464
1465 void vtt_class :: focus_next()
1466 {
1467         list <vtt_class *> :: iterator vtt;
1468         
1469         if (!focused_vtt)
1470         {
1471                 if (main_list.size())
1472                 {
1473                         focused_vtt=(*main_list.begin());
1474                 }
1475                 return;
1476         }
1477         
1478         for (vtt=main_list.begin(); vtt!=main_list.end() ; vtt++) {
1479                 if ((*vtt)==focused_vtt) {
1480                         /* Ok, we found ourselves.. */
1481                         
1482                         vtt++;
1483                         while ((vtt!=main_list.end()) && ((*vtt)->audio_hidden) ) {
1484                                 vtt++;
1485                         }
1486                         
1487                         if (vtt==main_list.end()) {
1488                                 /* No other "focusable" after this vtt so we're looking for the next */
1489                                 
1490                                 for (vtt=main_list.begin(); vtt!=main_list.end() ; vtt++) {
1491                                         if (! (*vtt)->audio_hidden) {
1492                                                 focused_vtt=(*vtt);
1493                                                 return;
1494                                         }
1495                                 }
1496                                 /* When we get here there's no "focusable" vtt at all... damn */
1497                                 focused_vtt=NULL;
1498                                 return;
1499                         } else {
1500                                 focused_vtt=(*vtt);
1501                                 return;
1502                         }
1503                 }
1504         }
1505         
1506         focused_vtt=(*main_list.begin());
1507 }
1508
1509 void vtt_class :: set_scratch(int newstate)
1510 {
1511         if (newstate)
1512         {
1513                 sp_spin.receive_input_value(0);
1514                 do_scratch=1;
1515                 sense_cycles=globals.sense_cycles;
1516         }
1517         else
1518         {
1519                 sp_spin.receive_input_value(1);
1520                 do_scratch=0;
1521         }
1522 }
1523
1524
1525 void vtt_class :: unfocus()
1526 {
1527         focused_vtt=NULL;
1528 }
1529
1530 void vtt_class :: set_x_input_parameter(tX_seqpar *sp)
1531 {
1532         x_par = sp;
1533 }
1534
1535 void vtt_class :: set_y_input_parameter(tX_seqpar *sp)
1536 {
1537         y_par = sp;
1538 }
1539
1540 void vtt_class :: xy_input(f_prec x_value, f_prec y_value)
1541 {
1542         if (x_par) x_par->handle_mouse_input(x_value*globals.mouse_speed);
1543         if (y_par) y_par->handle_mouse_input(y_value*globals.mouse_speed);
1544 }
1545
1546 #define store(data); if (fwrite((void *) &data, sizeof(data), 1, output)!=1) res+=1;
1547
1548 int  vtt_class :: save(FILE * output)
1549 {
1550         list <vtt_fx *> :: iterator effect;
1551
1552         int res=0;
1553         guint32 pid;
1554         int32_t counter;
1555         guint8 hidden;
1556         
1557         store(name);
1558         store(filename);
1559         store(is_sync_master);
1560         store(is_sync_client);
1561         store(sync_cycles);
1562         store(rel_volume);
1563         store(rel_pitch);
1564         
1565         store(autotrigger);
1566         store(loop);
1567         
1568         store(mute);
1569         store(pan);
1570         
1571         store(lp_enable);
1572         store(lp_gain);
1573         store(lp_reso);
1574         store(lp_freq);
1575         
1576         store(ec_enable);
1577         store(ec_length);
1578         store(ec_feedback);
1579         store(ec_pan);
1580         store(ec_volume);
1581         
1582         store(audio_hidden);
1583         store(control_hidden);
1584
1585         pid=sp_speed.get_persistence_id();
1586         store(pid);
1587         pid=sp_volume.get_persistence_id();
1588         store(pid);
1589         pid=sp_pitch.get_persistence_id();
1590         store(pid);
1591         pid=sp_trigger.get_persistence_id();
1592         store(pid);
1593         pid=sp_loop.get_persistence_id();
1594         store(pid);
1595         pid=sp_sync_client.get_persistence_id();
1596         store(pid);
1597         pid=sp_sync_cycles.get_persistence_id();
1598         store(pid);
1599         pid=sp_lp_enable.get_persistence_id();
1600         store(pid);
1601         pid=sp_lp_gain.get_persistence_id();
1602         store(pid);
1603         pid=sp_lp_reso.get_persistence_id();
1604         store(pid);
1605         pid=sp_lp_freq.get_persistence_id();
1606         store(pid);
1607         pid=sp_ec_enable.get_persistence_id();
1608         store(pid);
1609         pid=sp_ec_length.get_persistence_id();
1610         store(pid);
1611         pid=sp_ec_feedback.get_persistence_id();
1612         store(pid);
1613         pid=sp_ec_volume.get_persistence_id();
1614         store(pid);
1615         pid=sp_ec_pan.get_persistence_id();
1616         store(pid);
1617         pid=sp_mute.get_persistence_id();
1618         store(pid);
1619         pid=sp_spin.get_persistence_id();
1620         store(pid);
1621         pid=sp_pan.get_persistence_id();
1622         store(pid);
1623                 
1624         counter=fx_list.size();
1625         store(counter);
1626
1627         for (effect=fx_list.begin(); effect!=fx_list.end(); effect++)
1628         {
1629                 (*effect)->save(output);
1630         }
1631         
1632         if (x_par)
1633         {
1634                 pid=1;
1635                 store(pid);
1636                 pid=x_par->get_persistence_id();
1637                 store(pid);
1638         }
1639         else
1640         {
1641                 pid=0;
1642                 store(pid);
1643         }
1644
1645         if (y_par)
1646         {
1647                 pid=1;
1648                 store(pid);
1649                 pid=y_par->get_persistence_id();
1650                 store(pid);
1651         }
1652         else
1653         {
1654                 pid=0;
1655                 store(pid);
1656         }
1657                 
1658         hidden=gui.main_panel->is_hidden();
1659         store(hidden);
1660
1661         hidden=gui.trigger_panel->is_hidden();
1662         store(hidden);
1663
1664         hidden=gui.lp_panel->is_hidden();
1665         store(hidden);
1666
1667         hidden=gui.ec_panel->is_hidden();
1668         store(hidden);
1669         
1670         return(res);
1671 }
1672
1673 #define atload(data); if (fread((void *) &data, sizeof(data), 1, input)!=1) res+=1;
1674
1675 int vtt_class :: load_10(FILE * input)
1676 {
1677         int res=0;
1678         int obsolete_int;
1679         
1680         atload(name);
1681         atload(filename);
1682         atload(is_sync_master);
1683         atload(is_sync_client);
1684         atload(sync_cycles);
1685         atload(rel_volume);
1686         recalc_volume();
1687         atload(rel_pitch);
1688         recalc_pitch();
1689         
1690         atload(autotrigger);
1691         atload(loop);
1692         
1693         atload(mute);
1694         atload(obsolete_int); //x_control
1695         atload(obsolete_int); //y_control
1696         
1697         atload(lp_enable);
1698         atload(lp_gain);
1699         atload(lp_reso);
1700         atload(lp_freq);
1701         lp_setup(lp_gain, lp_reso, lp_freq);
1702         
1703         atload(ec_enable);
1704         atload(ec_length);
1705         ec_set_length(ec_length);
1706         atload(ec_feedback);
1707         ec_set_feedback(ec_feedback);
1708         
1709         return(res);
1710 }
1711
1712
1713 int vtt_class :: load_11(FILE * input)
1714 {
1715         int res=0;
1716         guint32 pid;
1717         int32_t gui_page;
1718         int obsolete_int;
1719         
1720         atload(name);
1721         atload(filename);
1722         atload(is_sync_master);
1723         atload(is_sync_client);
1724         atload(sync_cycles);
1725         atload(rel_volume);
1726         recalc_volume();
1727         atload(rel_pitch);
1728         recalc_pitch();
1729         
1730         atload(autotrigger);
1731         atload(loop);
1732         
1733         atload(mute);
1734         atload(obsolete_int); //x_control
1735         atload(obsolete_int); //y_control
1736         
1737         atload(lp_enable);
1738         atload(lp_gain);
1739         atload(lp_reso);
1740         atload(lp_freq);
1741         lp_setup(lp_gain, lp_reso, lp_freq);
1742         
1743         atload(ec_enable);
1744         atload(ec_length);
1745         ec_set_length(ec_length);
1746         atload(ec_feedback);
1747         ec_set_feedback(ec_feedback);
1748
1749         atload(pid);
1750         sp_speed.set_persistence_id(pid);
1751         atload(pid);
1752         sp_volume.set_persistence_id(pid);
1753         atload(pid);
1754         sp_pitch.set_persistence_id(pid);
1755         atload(pid);
1756         sp_trigger.set_persistence_id(pid);
1757         atload(pid);
1758         sp_loop.set_persistence_id(pid);
1759         atload(pid);
1760         sp_sync_client.set_persistence_id(pid);
1761         atload(pid);
1762         sp_sync_cycles.set_persistence_id(pid);
1763         atload(pid);
1764         sp_lp_enable.set_persistence_id(pid);
1765         atload(pid);
1766         sp_lp_gain.set_persistence_id(pid);
1767         atload(pid);
1768         sp_lp_reso.set_persistence_id(pid);
1769         atload(pid);
1770         sp_lp_freq.set_persistence_id(pid);
1771         atload(pid);
1772         sp_ec_enable.set_persistence_id(pid);
1773         atload(pid);
1774         sp_ec_length.set_persistence_id(pid);
1775         atload(pid);
1776         sp_ec_feedback.set_persistence_id(pid);
1777         atload(pid);
1778         sp_mute.set_persistence_id(pid);
1779         atload(pid);
1780         sp_spin.set_persistence_id(pid);
1781         
1782         atload(gui_page);
1783         
1784         return(res);
1785 }
1786
1787 int vtt_class :: load_12(FILE * input)
1788 {
1789         int res=0;
1790         guint32 pid;
1791         int32_t counter;
1792         int32_t type;
1793         long id;
1794         int i,t;
1795         LADSPA_Plugin *plugin;
1796         char buffer[256];
1797         vtt_fx_ladspa *ladspa_effect;
1798         guint8 hidden;
1799         
1800         atload(buffer);
1801         this->set_name(buffer);
1802         atload(filename);
1803         atload(is_sync_master);
1804         atload(is_sync_client);
1805         atload(sync_cycles);
1806         atload(rel_volume);
1807         recalc_volume();
1808         atload(rel_pitch);
1809         recalc_pitch();
1810         
1811         atload(autotrigger);
1812         atload(loop);
1813         
1814         atload(mute);
1815         
1816         atload(lp_enable);
1817         atload(lp_gain);
1818         atload(lp_reso);
1819         atload(lp_freq);
1820         lp_setup(lp_gain, lp_reso, lp_freq);
1821         
1822         atload(ec_enable);
1823         atload(ec_length);
1824         ec_set_length(ec_length);
1825         atload(ec_feedback);
1826         ec_set_feedback(ec_feedback);
1827
1828         atload(pid);
1829         sp_speed.set_persistence_id(pid);
1830         atload(pid);
1831         sp_volume.set_persistence_id(pid);
1832         atload(pid);
1833         sp_pitch.set_persistence_id(pid);
1834         atload(pid);
1835         sp_trigger.set_persistence_id(pid);
1836         atload(pid);
1837         sp_loop.set_persistence_id(pid);
1838         atload(pid);
1839         sp_sync_client.set_persistence_id(pid);
1840         atload(pid);
1841         sp_sync_cycles.set_persistence_id(pid);
1842         atload(pid);
1843         sp_lp_enable.set_persistence_id(pid);
1844         atload(pid);
1845         sp_lp_gain.set_persistence_id(pid);
1846         atload(pid);
1847         sp_lp_reso.set_persistence_id(pid);
1848         atload(pid);
1849         sp_lp_freq.set_persistence_id(pid);
1850         atload(pid);
1851         sp_ec_enable.set_persistence_id(pid);
1852         atload(pid);
1853         sp_ec_length.set_persistence_id(pid);
1854         atload(pid);
1855         sp_ec_feedback.set_persistence_id(pid);
1856         atload(pid);
1857         sp_mute.set_persistence_id(pid);
1858         atload(pid);
1859         sp_spin.set_persistence_id(pid);
1860                 
1861         atload(counter);
1862         
1863         for (i=0; i<counter; i++)
1864         {
1865                 atload(type);
1866                 switch(type)
1867                 {
1868                         case TX_FX_BUILTINCUTOFF:
1869                                 for (t=0; t<fx_list.size(); t++) effect_down(lp_fx);
1870                         break;
1871                         
1872                         case TX_FX_BUILTINECHO:
1873                                 for (t=0; t<fx_list.size(); t++) effect_down(ec_fx);
1874                         break;
1875                         
1876                         case TX_FX_LADSPA:
1877                                 atload(id);
1878                                 plugin=LADSPA_Plugin::getPluginByUniqueID(id);
1879                                 if (plugin)
1880                                 {
1881                                         ladspa_effect=add_effect(plugin);
1882                                         ladspa_effect->load(input);
1883                                 }
1884                                 else
1885                                 {
1886                                         sprintf(buffer,"Fatal Error: Couldn't find required plugin with ID [%i].", id);
1887                                         tx_note(buffer);
1888                                         res++;
1889                                 }
1890                         break;
1891                         
1892                         default:
1893                                 tx_note("Fatal Error loading set: unknown effect type!");
1894                                 res++;
1895                 }               
1896         }
1897
1898         atload(pid);
1899         
1900         if (pid)
1901         {
1902                 atload(pid);
1903                 set_x_input_parameter(tX_seqpar :: get_sp_by_persistence_id(pid));
1904         }
1905         else set_x_input_parameter(NULL);
1906         
1907         atload(pid);
1908         
1909         if (pid)
1910         {
1911                 atload(pid);
1912                 set_y_input_parameter(tX_seqpar :: get_sp_by_persistence_id(pid));
1913         }
1914         else set_y_input_parameter(NULL);
1915
1916         atload(hidden);
1917         gui.main_panel->hide(hidden);
1918
1919         atload(hidden);
1920         gui.trigger_panel->hide(hidden);
1921
1922         atload(hidden);
1923         gui.lp_panel->hide(hidden);
1924
1925         atload(hidden);
1926         gui.ec_panel->hide(hidden);
1927         
1928         return(res);
1929 }
1930
1931 int vtt_class :: load_13(FILE * input)
1932 {
1933         int res=0;
1934         guint32 pid;
1935         int32_t counter;
1936         int32_t type;
1937         long id;
1938         int i,t;
1939         LADSPA_Plugin *plugin;
1940         char buffer[256];
1941         vtt_fx_ladspa *ladspa_effect;
1942         guint8 hidden;
1943         
1944         atload(buffer);
1945         this->set_name(buffer);
1946         atload(filename);
1947         atload(is_sync_master);
1948         atload(is_sync_client);
1949         atload(sync_cycles);
1950         atload(rel_volume);
1951         atload(rel_pitch);
1952         recalc_pitch();
1953         
1954         atload(autotrigger);
1955         atload(loop);
1956         
1957         atload(mute);
1958         atload(pan);
1959         
1960         atload(lp_enable);
1961         atload(lp_gain);
1962         atload(lp_reso);
1963         atload(lp_freq);
1964         lp_setup(lp_gain, lp_reso, lp_freq);
1965         
1966         atload(ec_enable);
1967         atload(ec_length);
1968         ec_set_length(ec_length);
1969         atload(ec_feedback);
1970         ec_set_feedback(ec_feedback);
1971         atload(ec_pan);
1972         ec_set_pan(ec_pan);
1973         atload(ec_volume);
1974         ec_set_volume(ec_volume);
1975
1976         recalc_volume();
1977
1978         atload(pid);
1979         sp_speed.set_persistence_id(pid);
1980         atload(pid);
1981         sp_volume.set_persistence_id(pid);
1982         atload(pid);
1983         sp_pitch.set_persistence_id(pid);
1984         atload(pid);
1985         sp_trigger.set_persistence_id(pid);
1986         atload(pid);
1987         sp_loop.set_persistence_id(pid);
1988         atload(pid);
1989         sp_sync_client.set_persistence_id(pid);
1990         atload(pid);
1991         sp_sync_cycles.set_persistence_id(pid);
1992         atload(pid);
1993         sp_lp_enable.set_persistence_id(pid);
1994         atload(pid);
1995         sp_lp_gain.set_persistence_id(pid);
1996         atload(pid);
1997         sp_lp_reso.set_persistence_id(pid);
1998         atload(pid);
1999         sp_lp_freq.set_persistence_id(pid);
2000         atload(pid);
2001         sp_ec_enable.set_persistence_id(pid);
2002         atload(pid);
2003         sp_ec_length.set_persistence_id(pid);
2004         atload(pid);
2005         sp_ec_feedback.set_persistence_id(pid);
2006         atload(pid);
2007         sp_ec_volume.set_persistence_id(pid);
2008         atload(pid);
2009         sp_ec_pan.set_persistence_id(pid);
2010         atload(pid);
2011         sp_mute.set_persistence_id(pid);
2012         atload(pid);
2013         sp_spin.set_persistence_id(pid);
2014         atload(pid);
2015         sp_pan.set_persistence_id(pid);
2016                 
2017         atload(counter);
2018         
2019         for (i=0; i<counter; i++)
2020         {
2021                 atload(type);
2022                 switch(type)
2023                 {
2024                         case TX_FX_BUILTINCUTOFF:
2025                                 for (t=0; t<fx_list.size(); t++) effect_down(lp_fx);
2026                         break;
2027                         
2028                         case TX_FX_BUILTINECHO:
2029                                 for (t=0; t<fx_list.size(); t++) effect_down(ec_fx);
2030                         break;
2031                         
2032                         case TX_FX_LADSPA:
2033                                 atload(id);
2034                                 plugin=LADSPA_Plugin::getPluginByUniqueID(id);
2035                                 if (plugin)
2036                                 {
2037                                         ladspa_effect=add_effect(plugin);
2038                                         ladspa_effect->load(input);
2039                                 }
2040                                 else
2041                                 {
2042                                         sprintf(buffer,"Fatal Error: Couldn't find required plugin with ID [%i].", id);
2043                                         tx_note(buffer);
2044                                         res++;
2045                                 }
2046                         break;
2047                         
2048                         default:
2049                                 tx_note("Fatal Error loading set: unknown effect type!");
2050                                 res++;
2051                 }               
2052         }
2053
2054         atload(pid);
2055         
2056         if (pid)
2057         {
2058                 atload(pid);
2059                 set_x_input_parameter(tX_seqpar :: get_sp_by_persistence_id(pid));
2060         }
2061         else set_x_input_parameter(NULL);
2062         
2063         atload(pid);
2064         
2065         if (pid)
2066         {
2067                 atload(pid);
2068                 set_y_input_parameter(tX_seqpar :: get_sp_by_persistence_id(pid));
2069         }
2070         else set_y_input_parameter(NULL);
2071
2072         atload(hidden);
2073         gui.main_panel->hide(hidden);
2074
2075         atload(hidden);
2076         gui.trigger_panel->hide(hidden);
2077
2078         atload(hidden);
2079         gui.lp_panel->hide(hidden);
2080
2081         atload(hidden);
2082         gui.ec_panel->hide(hidden);
2083         
2084         return(res);
2085 }
2086
2087 int vtt_class :: load_14(FILE * input)
2088 {
2089         int res=0;
2090         guint32 pid;
2091         int32_t counter;
2092         int32_t type;
2093         long id;
2094         int i,t;
2095         LADSPA_Plugin *plugin;
2096         char buffer[256];
2097         vtt_fx_ladspa *ladspa_effect;
2098         guint8 hidden;
2099         
2100         atload(buffer);
2101         this->set_name(buffer);
2102         atload(filename);
2103         atload(is_sync_master);
2104         atload(is_sync_client);
2105         atload(sync_cycles);
2106         atload(rel_volume);
2107         atload(rel_pitch);
2108         recalc_pitch();
2109         
2110         atload(autotrigger);
2111         atload(loop);
2112         
2113         atload(mute);
2114         atload(pan);
2115         
2116         atload(lp_enable);
2117         atload(lp_gain);
2118         atload(lp_reso);
2119         atload(lp_freq);
2120         lp_setup(lp_gain, lp_reso, lp_freq);
2121         
2122         atload(ec_enable);
2123         atload(ec_length);
2124         ec_set_length(ec_length);
2125         atload(ec_feedback);
2126         ec_set_feedback(ec_feedback);
2127         atload(ec_pan);
2128         ec_set_pan(ec_pan);
2129         atload(ec_volume);
2130         ec_set_volume(ec_volume);
2131
2132         atload(audio_hidden);
2133         atload(control_hidden);
2134         
2135         recalc_volume();
2136
2137         atload(pid);
2138         sp_speed.set_persistence_id(pid);
2139         atload(pid);
2140         sp_volume.set_persistence_id(pid);
2141         atload(pid);
2142         sp_pitch.set_persistence_id(pid);
2143         atload(pid);
2144         sp_trigger.set_persistence_id(pid);
2145         atload(pid);
2146         sp_loop.set_persistence_id(pid);
2147         atload(pid);
2148         sp_sync_client.set_persistence_id(pid);
2149         atload(pid);
2150         sp_sync_cycles.set_persistence_id(pid);
2151         atload(pid);
2152         sp_lp_enable.set_persistence_id(pid);
2153         atload(pid);
2154         sp_lp_gain.set_persistence_id(pid);
2155         atload(pid);
2156         sp_lp_reso.set_persistence_id(pid);
2157         atload(pid);
2158         sp_lp_freq.set_persistence_id(pid);
2159         atload(pid);
2160         sp_ec_enable.set_persistence_id(pid);
2161         atload(pid);
2162         sp_ec_length.set_persistence_id(pid);
2163         atload(pid);
2164         sp_ec_feedback.set_persistence_id(pid);
2165         atload(pid);
2166         sp_ec_volume.set_persistence_id(pid);
2167         atload(pid);
2168         sp_ec_pan.set_persistence_id(pid);
2169         atload(pid);
2170         sp_mute.set_persistence_id(pid);
2171         atload(pid);
2172         sp_spin.set_persistence_id(pid);
2173         atload(pid);
2174         sp_pan.set_persistence_id(pid);
2175                 
2176         atload(counter);
2177         
2178         for (i=0; i<counter; i++)
2179         {
2180                 atload(type);
2181                 switch(type)
2182                 {
2183                         case TX_FX_BUILTINCUTOFF:
2184                                 for (t=0; t<fx_list.size(); t++) effect_down(lp_fx);
2185                         break;
2186                         
2187                         case TX_FX_BUILTINECHO:
2188                                 for (t=0; t<fx_list.size(); t++) effect_down(ec_fx);
2189                         break;
2190                         
2191                         case TX_FX_LADSPA:
2192                                 atload(id);
2193                                 plugin=LADSPA_Plugin::getPluginByUniqueID(id);
2194                                 if (plugin)
2195                                 {
2196                                         ladspa_effect=add_effect(plugin);
2197                                         ladspa_effect->load(input);
2198                                 }
2199                                 else
2200                                 {
2201                                         sprintf(buffer,"Fatal Error: Couldn't find required plugin with ID [%i].", id);
2202                                         tx_note(buffer);
2203                                         res++;
2204                                 }
2205                         break;
2206                         
2207                         default:
2208                                 tx_note("Fatal Error loading set: unknown effect type!");
2209                                 res++;
2210                 }               
2211         }
2212
2213         atload(pid);
2214         
2215         if (pid)
2216         {
2217                 atload(pid);
2218                 set_x_input_parameter(tX_seqpar :: get_sp_by_persistence_id(pid));
2219         }
2220         else set_x_input_parameter(NULL);
2221         
2222         atload(pid);
2223         
2224         if (pid)
2225         {
2226                 atload(pid);
2227                 set_y_input_parameter(tX_seqpar :: get_sp_by_persistence_id(pid));
2228         }
2229         else set_y_input_parameter(NULL);
2230
2231         atload(hidden);
2232         gui.main_panel->hide(hidden);
2233
2234         atload(hidden);
2235         gui.trigger_panel->hide(hidden);
2236
2237         atload(hidden);
2238         gui.lp_panel->hide(hidden);
2239
2240         atload(hidden);
2241         gui.ec_panel->hide(hidden);
2242         
2243         return(res);
2244 }
2245
2246
2247 int  vtt_class :: save_all(FILE* output)
2248 {
2249         int res=0;
2250         list <vtt_class *> :: iterator vtt;
2251         guint32 pid;
2252         
2253         tX_seqpar :: create_persistence_ids();
2254         
2255         store(vtt_amount);
2256         store(master_volume);
2257         store(globals.pitch);
2258         pid=sp_master_volume.get_persistence_id();
2259         store(pid);
2260         pid=sp_master_pitch.get_persistence_id();
2261         store(pid);
2262
2263         for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
2264         {
2265                 res+=(*vtt)->save(output);
2266         }
2267         
2268         sequencer.save(output);
2269         
2270         return(res);
2271 }
2272
2273 int  vtt_class :: load_all_10(FILE* input, char *fname)
2274 {
2275         int res=0, restmp=0;
2276         list <vtt_class *> :: iterator vtt;
2277         unsigned int i, max, size;
2278         int16_t *newbuffer;
2279         vtt_class *newvtt;
2280         char ftmp[PATH_MAX];
2281         
2282         while (main_list.size())
2283         {
2284                 delete((*main_list.begin()));
2285         }
2286                 
2287         atload(max);
2288         atload(master_volume);
2289         set_master_volume(master_volume);
2290         globals.volume=master_volume;
2291         atload(globals.pitch);  
2292         set_master_pitch(globals.pitch);
2293
2294         ld_create_loaddlg(TX_LOADDLG_MODE_MULTI, max);
2295         ld_set_setname(fname);
2296
2297         for (i=0; i<max; i++)
2298         {
2299                 newvtt=new vtt_class(1);
2300                 res+=newvtt->load_10(input);
2301                 
2302                 if (strlen(newvtt->filename))
2303                 {
2304                         /* ftmp IS NECESSARY !!! */
2305                         strcpy(ftmp, newvtt->filename);
2306                         ld_set_filename(ftmp);
2307                         
2308                         //restmp=load_wav(newvtt->filename, &newbuffer, &size);
2309                         restmp=newvtt->load_file(ftmp);
2310                         res+=restmp;
2311                 }
2312                 gtk_box_pack_start(GTK_BOX(control_parent), newvtt->gui.control_box, TRUE, TRUE, 0);
2313                 gtk_box_pack_start(GTK_BOX(audio_parent), newvtt->gui.audio_box, TRUE, TRUE, 0);
2314         }
2315         
2316         sequencer.clear();
2317         
2318         ld_destroy();
2319         
2320         return(res);
2321 }
2322
2323
2324 int  vtt_class :: load_all_11(FILE* input, char *fname)
2325 {
2326         int res=0, restmp=0;
2327         list <vtt_class *> :: iterator vtt;
2328         unsigned int i, max, size;
2329         int16_t *newbuffer;
2330         vtt_class *newvtt;
2331         char ftmp[PATH_MAX];
2332         guint32 pid;
2333         
2334         while (main_list.size())
2335         {
2336                 delete((*main_list.begin()));
2337         }
2338                 
2339         atload(max);
2340         atload(master_volume);
2341         set_master_volume(master_volume);
2342         globals.volume=master_volume;
2343         atload(globals.pitch);  
2344         set_master_pitch(globals.pitch);
2345         atload(pid);
2346         sp_master_volume.set_persistence_id(pid);
2347         atload(pid);
2348         sp_master_pitch.set_persistence_id(pid);
2349
2350         ld_create_loaddlg(TX_LOADDLG_MODE_MULTI, max);
2351         ld_set_setname(fname);
2352
2353         for (i=0; i<max; i++)
2354         {
2355                 newvtt=new vtt_class(1);
2356                 res+=newvtt->load_11(input);
2357                 
2358                 if (strlen(newvtt->filename))
2359                 {
2360                         /* ftmp IS NECESSARY !!! */
2361                         strcpy(ftmp, newvtt->filename);
2362                         ld_set_filename(ftmp);
2363                         
2364                         //restmp=load_wav(newvtt->filename, &newbuffer, &size);
2365                         restmp=newvtt->load_file(ftmp);
2366                         res+=restmp;
2367                 }
2368                 gtk_box_pack_start(GTK_BOX(control_parent), newvtt->gui.control_box, TRUE, TRUE, 0);
2369                 gtk_box_pack_start(GTK_BOX(audio_parent), newvtt->gui.audio_box, TRUE, TRUE, 0);
2370                 
2371         }
2372         
2373         sequencer.load(input);
2374         
2375         ld_destroy();
2376         
2377         return(res);
2378 }
2379
2380
2381 int  vtt_class :: load_all_12(FILE* input, char *fname)
2382 {
2383         int res=0, restmp=0;
2384         list <vtt_class *> :: iterator vtt;
2385         unsigned int i, max, size;
2386         int16_t *newbuffer;
2387         vtt_class *newvtt;
2388         char ftmp[PATH_MAX];
2389         guint32 pid;
2390         
2391         while (main_list.size())
2392         {
2393                 delete((*main_list.begin()));
2394         }
2395                 
2396         atload(max);
2397         atload(master_volume);
2398         set_master_volume(master_volume);
2399         globals.volume=master_volume;
2400         atload(globals.pitch);  
2401         set_master_pitch(globals.pitch);
2402         atload(pid);
2403         sp_master_volume.set_persistence_id(pid);
2404         atload(pid);
2405         sp_master_pitch.set_persistence_id(pid);
2406
2407         ld_create_loaddlg(TX_LOADDLG_MODE_MULTI, max);
2408         ld_set_setname(fname);
2409
2410         for (i=0; i<max; i++)
2411         {
2412                 newvtt=new vtt_class(1);
2413                 res+=newvtt->load_12(input);
2414                 
2415                 if (strlen(newvtt->filename))
2416                 {
2417                         /* ftmp IS NECESSARY !!! */
2418                         strcpy(ftmp, newvtt->filename);
2419                         ld_set_filename(ftmp);
2420                         
2421                         //restmp=load_wav(newvtt->filename, &newbuffer, &size);
2422                         restmp=newvtt->load_file(ftmp);
2423                         res+=restmp;
2424                 }
2425                 gtk_box_pack_start(GTK_BOX(control_parent), newvtt->gui.control_box, TRUE, TRUE, 0);
2426                 gtk_box_pack_start(GTK_BOX(audio_parent), newvtt->gui.audio_box, TRUE, TRUE, 0);
2427                 
2428         }
2429         
2430         sequencer.load(input);
2431         
2432         ld_destroy();
2433         
2434         return(res);
2435 }
2436
2437 int  vtt_class :: load_all_13(FILE* input, char *fname)
2438 {
2439         int res=0, restmp=0;
2440         list <vtt_class *> :: iterator vtt;
2441         unsigned int i, max, size;
2442         int16_t *newbuffer;
2443         vtt_class *newvtt;
2444         char ftmp[PATH_MAX];
2445         guint32 pid;
2446         
2447         while (main_list.size())
2448         {
2449                 delete((*main_list.begin()));
2450         }
2451                 
2452         atload(max);
2453         atload(master_volume);
2454         set_master_volume(master_volume);
2455         globals.volume=master_volume;
2456         atload(globals.pitch);  
2457         set_master_pitch(globals.pitch);
2458         atload(pid);
2459         sp_master_volume.set_persistence_id(pid);
2460         atload(pid);
2461         sp_master_pitch.set_persistence_id(pid);
2462
2463         ld_create_loaddlg(TX_LOADDLG_MODE_MULTI, max);
2464         ld_set_setname(fname);
2465
2466         for (i=0; i<max; i++)
2467         {
2468                 newvtt=new vtt_class(1);
2469                 res+=newvtt->load_13(input);
2470                 
2471                 if (strlen(newvtt->filename))
2472                 {
2473                         /* ftmp IS NECESSARY !!! */
2474                         strcpy(ftmp, newvtt->filename);
2475                         ld_set_filename(ftmp);
2476                         
2477                         //restmp=load_wav(newvtt->filename, &newbuffer, &size);
2478                         restmp=newvtt->load_file(ftmp);
2479                         res+=restmp;
2480                 }
2481                 gtk_box_pack_start(GTK_BOX(control_parent), newvtt->gui.control_box, TRUE, TRUE, 0);
2482                 gtk_box_pack_start(GTK_BOX(audio_parent), newvtt->gui.audio_box, TRUE, TRUE, 0);
2483                 
2484         }
2485         
2486         sequencer.load(input);
2487         
2488         ld_destroy();
2489         
2490         return(res);
2491 }
2492
2493 int  vtt_class :: load_all_14(FILE* input, char *fname)
2494 {
2495         int res=0, restmp=0;
2496         list <vtt_class *> :: iterator vtt;
2497         unsigned int i, max, size;
2498         int16_t *newbuffer;
2499         vtt_class *newvtt;
2500         char ftmp[PATH_MAX];
2501         guint32 pid;
2502         
2503         while (main_list.size())
2504         {
2505                 delete((*main_list.begin()));
2506         }
2507                 
2508         atload(max);
2509         atload(master_volume);
2510         set_master_volume(master_volume);
2511         globals.volume=master_volume;
2512         atload(globals.pitch);  
2513         set_master_pitch(globals.pitch);
2514         atload(pid);
2515         sp_master_volume.set_persistence_id(pid);
2516         atload(pid);
2517         sp_master_pitch.set_persistence_id(pid);
2518
2519         ld_create_loaddlg(TX_LOADDLG_MODE_MULTI, max);
2520         ld_set_setname(fname);
2521
2522         for (i=0; i<max; i++)
2523         {
2524                 newvtt=new vtt_class(1);
2525                 res+=newvtt->load_14(input);
2526                 
2527                 if (strlen(newvtt->filename))
2528                 {
2529                         /* ftmp IS NECESSARY !!! */
2530                         strcpy(ftmp, newvtt->filename);
2531                         ld_set_filename(ftmp);
2532                         
2533                         //restmp=load_wav(newvtt->filename, &newbuffer, &size);
2534                         restmp=newvtt->load_file(ftmp);
2535                         res+=restmp;
2536                 }
2537                 gtk_box_pack_start(GTK_BOX(control_parent), newvtt->gui.control_box, TRUE, TRUE, 0);
2538                 gtk_box_pack_start(GTK_BOX(audio_parent), newvtt->gui.audio_box, TRUE, TRUE, 0);
2539             if (newvtt->audio_hidden) newvtt->hide_audio(newvtt->audio_hidden);
2540             if (newvtt->control_hidden) newvtt->hide_control(newvtt->control_hidden);\r
2541         }
2542         
2543         sequencer.load(input);
2544         
2545         ld_destroy();
2546         
2547         return(res);
2548 }
2549
2550
2551 void add_vtt(GtkWidget *ctrl, GtkWidget *audio, char *fn)
2552 {
2553         vtt_class *hmmpg;
2554         hmmpg = new vtt_class(1);
2555         gtk_box_pack_start(GTK_BOX(ctrl), hmmpg->gui.control_box, TRUE, TRUE, 0);
2556         gtk_box_pack_start(GTK_BOX(audio), hmmpg->gui.audio_box, TRUE, TRUE, 0);
2557         if (fn) hmmpg->load_file(fn);
2558 }
2559
2560 extern void vg_move_fx_panel_up(GtkWidget *wid, vtt_class *vtt);
2561 extern void vg_move_fx_panel_down(GtkWidget *wid, vtt_class *vtt);
2562
2563 //#define debug_fx_stack(); for (i=fx_list.begin(); i != fx_list.end(); i++) puts((*i)->get_info_string());
2564 #define debug_fx_stack();
2565
2566 void vtt_class :: effect_up(vtt_fx *effect)
2567 {
2568         list <vtt_fx *> :: iterator i;
2569         list <vtt_fx *> :: iterator previous;
2570         int ok=0;
2571         
2572         debug_fx_stack();
2573         
2574         if ((*fx_list.begin())==effect) return;
2575         
2576         for (previous=i=fx_list.begin(); i != fx_list.end(); i++)
2577         {
2578                 if ((*i) == effect)
2579                 {
2580                         ok=1;
2581                         break;
2582                 }
2583                 previous=i;
2584         }
2585         
2586         if (ok)
2587         {       
2588                 pthread_mutex_lock(&render_lock);
2589                 fx_list.remove(effect);
2590                 fx_list.insert(previous, effect);
2591                 pthread_mutex_unlock(&render_lock);
2592
2593                 vg_move_fx_panel_up(effect->get_panel_widget(), this);
2594         }
2595         
2596         debug_fx_stack();
2597 }
2598
2599 void vtt_class :: effect_down(vtt_fx *effect)
2600 {
2601         list <vtt_fx *> :: iterator i;
2602         int ok=0;
2603
2604         debug_fx_stack();
2605                 
2606         for (i=fx_list.begin(); i != fx_list.end(); i++)
2607         {
2608                 if ((*i) == effect)
2609                 {
2610                         ok=1;
2611                         break;
2612                 }
2613         }
2614         
2615         if ((ok) && (i!=fx_list.end()))
2616         {
2617                 i++;
2618                 if (i==fx_list.end()) return;
2619                 i++;
2620
2621                 pthread_mutex_lock(&render_lock);
2622                 fx_list.remove(effect);
2623                 
2624                 fx_list.insert(i, effect);
2625                 vg_move_fx_panel_down(effect->get_panel_widget(), this);
2626                 pthread_mutex_unlock(&render_lock);
2627         }
2628         
2629 debug_fx_stack();       
2630 }
2631
2632 void vtt_class ::  effect_remove(vtt_fx_ladspa *effect)
2633 {
2634         pthread_mutex_lock(&render_lock);
2635         fx_list.remove(effect);
2636         pthread_mutex_unlock(&render_lock);
2637         
2638         delete effect;
2639 }
2640
2641 extern void gui_hide_control_panel(vtt_class *vtt, bool hide);
2642 extern void gui_hide_audio_panel(vtt_class *vtt, bool hide);
2643
2644 void vtt_class :: hide_audio(bool hide) {
2645         audio_hidden=hide;
2646         gui_hide_audio_panel(this, hide);
2647 }
2648
2649 void vtt_class :: hide_control(bool hide) {
2650         control_hidden=hide;
2651         gui_hide_control_panel(this, hide);     
2652 }