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