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