Adding Dry/Wet controls - Alex
[terminatorX.git] / src / tX_vtt.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2003  Alexander K├Ânig
4  
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9  
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14  
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  
19     File: tX_vtt.cc
20  
21     Description: This implements the new virtual turntable class. It replaces
22                  the old turntable.c from terminatorX 3.2 and earlier. The lowpass
23                  filter is based on some sample code by Paul Kellett
24                  <paul.kellett@maxim.abel.co.uk>
25                  
26     08 Dec 1999 - Switched to the new audiofile class            
27 */    
28
29 #include "tX_vtt.h"
30 #include "tX_global.h"
31 #include <stdio.h>
32 #include "malloc.h"
33 #include <math.h>
34 #include "tX_mastergui.h"
35 #include "tX_sequencer.h"
36 #include <glib.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include "tX_loaddlg.h"
43
44 #define USE_PREFETCH 1
45
46 #ifdef USE_PREFETCH
47 #define my_prefetch(base, index); __asm__  __volatile__ ("prefetch index(%0)\n" : : "r" (base));
48 #define my_prefetchw(base, index); __asm__  __volatile__ ("prefetchw index(%0)\n" : : "r" (base));
49 #else
50 #define my_prefetch(base, index);  /* NOP */;
51 #define my_prefetchw(base, index); /* NOP */;
52 #endif
53
54 extern void build_vtt_gui(vtt_class *);
55 extern void gui_set_name(vtt_class *vtt, char *newname);
56 extern void gui_set_filename(vtt_class *vtt, char *newname);
57 extern void delete_gui(vtt_class *vtt);
58 extern void gui_update_display(vtt_class *vtt);
59 extern void gui_clear_master_button(vtt_class *vtt);
60 extern void cleanup_vtt(vtt_class *vtt);
61 extern int vg_get_current_page(vtt_class *vtt);
62 extern f_prec gui_get_audio_x_zoom(vtt_class *vtt);
63 extern void gui_set_audio_x_zoom(vtt_class *vtt, f_prec);
64
65 int vtt_class::last_sample_rate=44100;
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 int vtt_class::solo_ctr=0;
73
74 int vtt_class::samples_in_mix_buffer=0;
75 pthread_mutex_t vtt_class::render_lock=PTHREAD_MUTEX_INITIALIZER;
76 f_prec vtt_class::master_volume=1.0;
77 f_prec vtt_class::res_master_volume=1.0;
78
79 vtt_class * vtt_class::sync_master=NULL;
80 int vtt_class::master_triggered=0;
81 int vtt_class::master_triggered_at=0;
82 vtt_class * vtt_class::focused_vtt=NULL;
83 f_prec vtt_class::mix_max_l=0;
84 f_prec vtt_class::mix_max_r=0;
85 f_prec vtt_class::vol_channel_adjust=1.0;
86 int vtt_class::mix_buffer_size=0;
87
88 #define GAIN_AUTO_ADJUST 0.8
89
90 vtt_class :: vtt_class (int do_create_gui)
91 {       
92         vtt_amount++;
93         cleanup_required=false;
94         
95         sprintf (name, "Turntable %i", vtt_amount);
96         strcpy(filename, "NONE");
97         buffer=NULL;
98         samples_in_buffer=0;
99         pos_i_max=0;
100
101         pan=0;
102         rel_pitch=1; 
103         ec_volume=1; 
104         ec_pan=1; 
105         audiofile_pitch_correction=1.0;
106         ec_length=1;
107         ec_output_buffer=NULL;
108         output_buffer=NULL;
109         
110         set_volume(1);
111         set_pitch(1);
112         
113         autotrigger=1;
114         loop=1;
115         
116         is_playing=0;
117         is_sync_master=0;
118         is_sync_client=0;
119         sync_cycles=0,
120         sync_countdown=0;
121         
122         lp_enable=0;
123         lp_reso=0.8;
124         lp_freq=0.3;
125         lp_gain=1;
126         lp_setup(lp_gain, lp_reso, lp_freq);
127         lp_reset();
128         
129         ec_enable=0;
130         ec_length=0.5;
131         ec_feedback=0.3;
132         ec_clear_buffer();
133         ec_set_length(0.5);
134         ec_set_pan(0);
135         ec_set_volume(1);
136         
137         main_list.push_back(this);
138
139         /* "connecting" the seq-parameters */
140         
141         sp_speed.set_vtt((void *) this);
142         sp_volume.set_vtt((void *) this);       
143         sp_pitch.set_vtt((void *) this);        
144         sp_pan.set_vtt((void *) this);
145         sp_trigger.set_vtt((void *) this);      
146         sp_loop.set_vtt((void *) this); 
147         sp_sync_client.set_vtt((void *) this);  
148         sp_sync_cycles.set_vtt((void *) this);  
149         sp_lp_enable.set_vtt((void *) this);    
150         sp_lp_gain.set_vtt((void *) this);      
151         sp_lp_reso.set_vtt((void *) this);      
152         sp_lp_freq.set_vtt((void *) this);      
153         sp_ec_enable.set_vtt((void *) this);    
154         sp_ec_length.set_vtt((void *) this);
155         sp_ec_pan.set_vtt((void *) this);
156         sp_ec_volume.set_vtt((void *) this);
157         sp_ec_feedback.set_vtt((void *) this);          
158         sp_mute.set_vtt((void *) this);
159         sp_spin.set_vtt((void *) this);
160
161         x_par = &sp_speed;
162         y_par = &sp_lp_freq;
163         
164         lp_fx=new vtt_fx_lp();
165         lp_fx->set_vtt((void *) this);
166         fx_list.push_back(lp_fx);
167
168         ec_fx=new vtt_fx_ec();
169         ec_fx->set_vtt((void *) this);
170         fx_list.push_back(ec_fx);
171         
172         if (do_create_gui)
173         {       
174                 build_vtt_gui(this);
175                 lp_fx->set_panel_widget(gui.lp_panel->get_widget());    
176                 ec_fx->set_panel_widget(gui.ec_panel->get_widget());
177         }
178         else have_gui=0;
179                 
180         set_pan(0);     
181         set_master_volume(globals.volume);
182         set_output_buffer_size(samples_in_mix_buffer/2);
183         
184         audiofile = NULL;
185         audiofile_pitch_correction=1.0;
186         mute=0;
187         mix_solo=0;
188         mix_mute=0;
189         res_mute=mute;
190         res_mute_old=0;
191         
192         audio_hidden=false;
193         control_hidden=false;
194         
195         do_scratch=0;
196         speed_last=1;
197         speed_real=1;\r
198 }
199
200 vtt_class :: ~vtt_class()
201 {
202         vtt_fx *effect;
203         stop();
204
205         main_list.remove(this);
206         if (audiofile) delete audiofile;
207         //if (buffer) free(buffer);
208         if (output_buffer) tX_freemem(output_buffer, "output_buffer", "vtt Destructor");
209         vtt_amount--;
210         
211         if (mix_solo) solo_ctr--;
212         
213         while (fx_list.size())
214         { 
215                 effect=(*fx_list.begin());
216                 fx_list.remove(effect);
217                 delete effect;
218         }
219         
220         if (sync_master==this) {
221                 sync_master=NULL;
222         }
223         
224         delete_gui(this);
225 }
226
227 void vtt_class :: set_name(char *newname)
228 {
229         strcpy(name, newname);
230         gui_set_name(this, name);       
231 }
232
233 tX_audio_error vtt_class :: load_file(char *fname)
234 {
235         tX_audio_error res;
236         int was_playing=is_playing;
237         
238         if (is_playing) stop();
239
240         if (audiofile) delete(audiofile);
241         
242         buffer=NULL;
243         samples_in_buffer=0;
244         maxpos=0;
245         pos_i_max=0;
246         strcpy(filename,"");
247
248         audiofile=new tx_audiofile();
249         res=audiofile->load(fname);     
250         
251         if (res==TX_AUDIO_SUCCESS) {
252                 buffer=audiofile->get_buffer();
253                 double file_rate=audiofile->get_sample_rate();
254                 audiofile_pitch_correction=file_rate/((double) last_sample_rate);
255                 recalc_pitch();
256                 samples_in_buffer=audiofile->get_no_samples();
257                 pos_i_max=samples_in_buffer-1;
258                 maxpos=audiofile->get_no_samples();
259                 strcpy(filename, fname);
260                 if (was_playing) trigger();
261 //              printf("Successfully loaded %s, %08x, %i\n", fname, buffer, samples_in_buffer);
262         }
263         
264         if (have_gui)
265         {
266                 gui_update_display(this);
267         }
268         ec_set_length(ec_length);
269         
270         return(res);
271 }
272
273 int vtt_class :: set_output_buffer_size(int newsize)
274 {
275         list <vtt_fx *> :: iterator effect;
276
277         if (ec_output_buffer) tX_freemem(ec_output_buffer, "ec_output_buffer", "vtt set_output_buffer_size()");
278         tX_malloc(ec_output_buffer, "ec_output_buffer", "vtt set_output_buffer_size()", sizeof(float)*newsize, (float *));
279
280         if (output_buffer) tX_freemem(output_buffer, "output_buffer", "vtt set_output_buffer_size()");
281         tX_malloc(output_buffer, "output_buffer", "vtt set_output_buffer_size()", sizeof(float)*newsize, (float *));
282
283         end_of_outputbuffer = output_buffer + newsize; //size_t(sizeof(float)*(newsize));
284         
285         samples_in_outputbuffer=newsize;
286         inv_samples_in_outputbuffer=1.0/samples_in_outputbuffer;
287
288         for (effect=fx_list.begin(); effect != fx_list.end(); effect++)
289         {
290                 (*effect)->reconnect_buffer();
291         }
292         
293         if (output_buffer) return(0);   
294         else return(0);
295 }
296
297 void vtt_class :: set_volume(f_prec newvol)
298 {
299         rel_volume=newvol;
300         recalc_volume();
301 }
302
303 void vtt_class :: recalc_volume()
304 {
305         res_volume=rel_volume*res_master_volume;
306         f_prec ec_res_volume=res_volume*ec_volume;
307         
308         if (pan>0.0)
309         {
310                 res_volume_left=(1.0-pan)*res_volume;
311                 res_volume_right=res_volume;
312         }
313         else if (pan<0.0)
314         {
315                 res_volume_left=res_volume;
316                 res_volume_right=(1.0+pan)*res_volume;
317         }
318         else
319         {
320                 res_volume_left=res_volume_right=res_volume;
321         }
322         
323         if (ec_pan>0.0)
324         {
325                 ec_volume_left=(1.0-ec_pan)*ec_res_volume;
326                 ec_volume_right=ec_res_volume;
327         }
328         else if (ec_pan<0.0)
329         {
330                 ec_volume_left=ec_res_volume;
331                 ec_volume_right=(1.0+ec_pan)*ec_res_volume;
332         }
333         else
334         {
335                 ec_volume_left=ec_volume_right=ec_res_volume;
336         }       
337 //      printf("vtt_volume: %f, %f, l: %f, r: %f\n", rel_volume, res_volume, res_volume_left, res_volume_right);
338 }
339
340 void vtt_class :: set_pan(f_prec newpan)
341 {
342         pan=newpan;
343         recalc_volume();
344 }
345
346 void vtt_class :: set_pitch(f_prec newpitch)
347 {
348         rel_pitch=newpitch;
349         recalc_pitch();
350 }
351
352 void vtt_class :: recalc_pitch()
353 {
354         res_pitch=globals.pitch*rel_pitch;
355         res_pitch*=audiofile_pitch_correction;
356         speed=res_pitch;
357         ec_set_length(ec_length);
358 }
359
360 void vtt_class :: set_autotrigger(int newstate)
361 {
362         autotrigger=newstate;
363 }
364
365 void vtt_class :: set_loop(int newstate)
366 {
367         loop=newstate;
368 }
369
370 void vtt_class :: set_mute(int newstate)
371 {
372         mute=newstate;
373         calc_mute();
374 }
375
376 void vtt_class :: set_mix_mute(int newstate)
377 {
378         mix_mute=newstate;
379         calc_mute();
380 }
381
382 void vtt_class :: set_mix_solo(int newstate)
383 {
384         if (mix_solo && !newstate)
385         {
386                 /* turning it off */
387                 mix_solo=0;
388                 solo_ctr--;
389         }
390         else if (!mix_solo && newstate)
391         {
392                 /* turning it on */
393                 mix_solo=1;
394                 solo_ctr++;
395         }
396         calc_mute();
397
398         /* locking ? */
399         list <vtt_class *> :: iterator vtt;
400         
401         for (vtt=main_list.begin(); vtt!=main_list.end() ; vtt++)
402         {
403                 (*vtt)->calc_mute();
404         }
405 }
406
407 void vtt_class :: lp_set_enable (int newstate)
408 {
409         lp_enable=newstate;
410         lp_reset();
411 }
412
413 void vtt_class :: lp_reset()
414 {
415         lp_buf0=lp_buf1=0;
416 }
417
418 void vtt_class :: lp_set_gain (f_prec gain)
419 {
420         lp_gain=gain;
421         lp_resgain=lp_gain*lp_autogain;
422 }
423
424 void vtt_class :: lp_set_reso(f_prec reso)
425 {
426         lp_reso=reso;
427         
428         lp_b=reso*(1.0+(1.0/lp_a));
429         lp_autogain=1.0-reso*GAIN_AUTO_ADJUST;
430         lp_resgain=lp_gain*lp_autogain;
431 }
432
433 void vtt_class :: lp_set_freq(f_prec freq)
434 {
435         lp_freq=freq;
436         
437         lp_a=0.9999-freq;
438         lp_b=lp_reso*(1.0+(1.0/lp_a));
439 }
440
441 void vtt_class :: lp_setup(f_prec gain, f_prec reso, f_prec freq)
442 {
443         lp_freq=freq;
444         lp_reso=reso;
445         
446         lp_a=1.0-freq;
447         lp_b=reso*(1.0+(1.0/lp_a));
448         
449         lp_autogain=1.0-reso*GAIN_AUTO_ADJUST;
450         lp_resgain=lp_gain*lp_autogain;
451 }
452
453 void vtt_class :: ec_set_enable(int newstate)
454 {
455         ec_enable=newstate;
456         ec_clear_buffer();
457 }
458
459
460 void vtt_class :: ec_set_pan(f_prec pan)
461 {
462         ec_pan=pan;
463
464         recalc_volume();
465 }
466
467 /* Max length is 1.0 */
468
469 void vtt_class :: ec_set_length(f_prec length)
470 {
471         int delay;
472
473         ec_length=length;
474         if (res_pitch==0) 
475         {
476                 ec_res_length=length*samples_in_buffer;
477         }
478         else
479         {
480                 ec_res_length=length*samples_in_buffer/res_pitch;       
481         }
482         
483         if (ec_res_length<0) ec_res_length*=-1;
484         
485         if (ec_res_length>=EC_MAX_BUFFER)
486         {
487                 ec_res_length=EC_MAX_BUFFER*length;
488         }
489         
490         delay=(int )floor(ec_res_length);
491         delay-=2;
492         ec_delay=&ec_buffer[delay];
493 }
494
495 void vtt_class :: ec_set_feedback(f_prec feedback)
496 {
497         ec_feedback=feedback;
498 }
499
500
501 void vtt_class :: ec_set_volume(f_prec volume)
502 {
503         ec_volume=volume;
504         recalc_volume();
505 }
506
507 void vtt_class :: ec_clear_buffer()
508 {
509 /*      f_prec *ptr;
510         
511         for (ptr=ec_buffer; ptr<=ec_delay; ptr++) {
512                 *ptr=0.0;
513         } */
514         
515         memset(ec_buffer, 0, sizeof(ec_buffer));
516         ec_ptr=ec_buffer; 
517 }
518
519 void vtt_class :: render()
520 {
521         list <vtt_fx *> :: iterator effect;
522
523         if (do_scratch) {
524                 if (sense_cycles>0) {
525                         sense_cycles--;
526                         if (sense_cycles==0) sp_speed.receive_input_value(0);
527                 }
528         }
529         render_scratch();
530         
531         for (effect=fx_list.begin(); effect != fx_list.end(); effect++) {
532                 if ((*effect)->isEnabled()) (*effect)->run();
533         }
534 }
535
536 extern void vg_create_fx_gui(vtt_class *vtt, vtt_fx_ladspa *effect, LADSPA_Plugin *plugin);
537
538 vtt_fx_ladspa * vtt_class :: add_effect (LADSPA_Plugin *plugin)
539 {
540         vtt_fx_ladspa *new_effect;
541         
542         new_effect = new vtt_fx_ladspa (plugin, this);
543         pthread_mutex_lock(&render_lock);
544         fx_list.push_back(new_effect);
545         if (is_playing) new_effect->activate();
546         pthread_mutex_unlock(&render_lock);
547         vg_create_fx_gui(this, new_effect, plugin);
548         
549         return new_effect;
550 }
551
552 void vtt_class :: calc_speed()
553 {
554         do_mute=0;
555         fade_out=0;
556         fade_in=0;
557
558         if (speed != speed_target) {
559                 speed_target=speed;
560                 speed_step=speed_target-speed_real;
561                 speed_step/=globals.vtt_inertia;
562         }
563                         
564         if (speed_target != speed_real) {
565                 speed_real+=speed_step;
566                 if ((speed_step<0) && (speed_real<speed_target)) speed_real=speed_target;
567                 else if ((speed_step>0) && (speed_real>speed_target)) speed_real=speed_target;                  
568         }
569         
570         if (fade) {
571                 if ((speed_last==0) && (speed_real !=0)) {
572                         fade_in=1;
573                         fade=NEED_FADE_OUT;
574                 }
575         } else {
576                 if ((speed_last!=0) && (speed_real==0)) {
577                         fade_out=1;
578                         fade=NEED_FADE_IN;
579                 }
580         }
581
582         speed_last = speed_real;
583
584         if (res_mute != res_mute_old) {
585                 if (res_mute) {
586                         fade_out=1; fade_in=0;
587                         fade=NEED_FADE_IN;
588                 } else {
589                         fade_in=1; fade_out=0;
590                         fade=NEED_FADE_OUT;
591                 }
592                 res_mute_old=res_mute;
593         } else {
594                 if (res_mute) do_mute=1;
595         }       
596 }
597
598 void vtt_class :: render_scratch()
599 {
600         int16_t *ptr;
601         
602         int sample;
603         
604         d_prec pos_a_f;
605         
606         f_prec amount_a;
607         f_prec amount_b;
608
609         f_prec sample_a;
610         f_prec sample_b;
611         
612         f_prec sample_res;
613         
614         f_prec *out;
615         f_prec fade_vol;        
616
617         calc_speed();
618                                         
619         for (sample =0,out=output_buffer, fade_vol=0.0; sample < samples_in_outputbuffer;sample++, out++, fade_vol+=inv_samples_in_outputbuffer) {
620                 if ((speed_real!=0) || (fade_out)) {
621
622                         pos_f+=speed_real;
623
624                         if (pos_f>maxpos) {
625                                 pos_f-=maxpos;
626                                 if (res_pitch>0) {
627                                         if (loop) {
628                                                 if (is_sync_master)
629                                                 {
630                                                         master_triggered=1;
631                                                         master_triggered_at=sample;
632                                                 }
633                                         } else {
634                                                 want_stop=1;
635                                         }
636                                 }
637                         } else if (pos_f<0) {
638                                 pos_f+=maxpos;
639                                 if (res_pitch<0) {
640                                         if (loop) {
641                                                 if (is_sync_master)
642                                                 {
643                                                         master_triggered=1;
644                                                         master_triggered_at=sample;
645                                                 }
646                                         } else {
647                                                 want_stop=1;
648                                         }
649                                 }
650                         }
651                                 
652                         pos_a_f=floor(pos_f);
653                         pos_i=(unsigned int) pos_a_f;
654                                                                 
655                         amount_b=pos_f-pos_a_f;                         
656                         amount_a=1.0-amount_b;                          
657                                 
658                         if (do_mute) {
659                                 *out=0.0;
660                         } else {
661                                 ptr=&buffer[pos_i];
662                                 sample_a=(f_prec) *ptr;
663                         
664                                 if (pos_i == pos_i_max)  {
665                                         sample_b=*buffer;
666                                 } else {
667                                         ptr++;
668                                         sample_b=(f_prec) *ptr;
669                                 }
670                                 
671                                 sample_res=(sample_a*amount_a)+(sample_b*amount_b);
672                                                                 
673                                 if (fade_in) {
674                                         sample_res*=fade_vol;
675                                 } else if (fade_out) {
676                                         sample_res*=1.0-fade_vol;
677                                 }
678  
679                                 *out=sample_res;
680                         }
681                 } else {
682                                 *out=0;
683                 }
684         }
685 }       
686
687 void vtt_class :: forward_turntable()
688 {
689         int sample;
690         double pos_f_tmp;
691 #ifdef pos_f_test
692         int show=0;
693         double diff;
694 #endif
695
696         calc_speed();
697
698         if ((speed_real==0) && (!fade_out)) return;
699         
700         
701         /* following code is problematic as adding speed_real*n is
702           different from adding speed_real n times to pos_f.
703           
704           well it speeds things up quite a bit and double precision
705           seems to do a satisfying job.
706           
707           #define pos_f_test to prove that.
708         */
709         
710         pos_f_tmp=pos_f+speed_real*samples_in_outputbuffer;
711         
712         if ((pos_f_tmp > 0) && (pos_f_tmp < maxpos)) {
713 #ifdef pos_f_test
714                 show=1;
715 #else   
716                 pos_f=pos_f_tmp;
717                 return;
718 #endif          
719         }
720                                 
721         /* now the slow way ;) */
722         
723         for (sample =0; sample < samples_in_outputbuffer; sample++) {
724                 pos_f+=speed_real;
725
726                 if (pos_f>maxpos) {
727                         pos_f-=maxpos;
728                         if (res_pitch>0) {
729                                 if (loop) {
730                                         if (is_sync_master) {
731                                                 master_triggered=1;
732                                                 master_triggered_at=sample;
733                                         }
734                                 } else {
735                                         want_stop=1;
736                                 }
737                         }
738                 } else if (pos_f<0) {
739                         pos_f+=maxpos;
740                         if (res_pitch<0) {
741                                 if (loop) {
742                                         if (is_sync_master) {
743                                                 master_triggered=1;
744                                                 master_triggered_at=sample;
745                                         }
746                                 } else {
747                                         want_stop=1;
748                                 }
749                         }
750                 }
751         }
752 #ifdef pos_f_test
753         if (show) {
754                 diff=pos_f_tmp-pos_f;
755                 if (diff!=0) printf("fast: %f, slow: %f, diff: %f, tt: %s\n", pos_f_tmp, pos_f, diff, name);
756         }
757 #endif  
758 }       
759
760 /*
761         The following lowpass filter is based on some sample code by
762         Paul Kellett <paul.kellett@maxim.abel.co.uk>
763 */
764
765 void vtt_class :: render_lp()
766 {
767         f_prec *sample;
768                 
769         for (sample = output_buffer; sample<end_of_outputbuffer; sample++) {
770                 lp_buf0 = lp_a * lp_buf0 + lp_freq * ((*sample)*lp_resgain + lp_b * (lp_buf0 - lp_buf1));
771                 lp_buf1 = lp_a * lp_buf1 + lp_freq * lp_buf0;
772                 
773                 *sample=lp_buf1;
774         }
775 }
776
777 void vtt_class :: render_ec()
778 {
779         f_prec *sample;
780         f_prec *ec_sample;
781         int i;
782
783         for (i=0, sample = output_buffer, ec_sample=ec_output_buffer; i<samples_in_outputbuffer; i++, ec_sample++,sample++, ec_ptr++) {
784                 if (ec_ptr>ec_delay) ec_ptr=ec_buffer;
785                 *ec_sample=(*ec_ptr) *ec_feedback;
786                 *ec_ptr=*sample+*ec_sample;
787         }       
788 }
789
790 int vtt_class :: set_mix_buffer_size(int no_samples)
791 {
792         list <vtt_class *> :: iterator vtt;
793         int res=0;
794         
795 //      printf("vtt_class::set_mix_buffer_size(), mix_buffer: %12x, mix_out: %12x, samples: %i\n", mix_buffer, mix_out_buffer, no_samples);
796         
797         if (mix_buffer) tX_freemem(mix_buffer, "mix_buffer", "vtt set_mix_buffer_size()");
798         samples_in_mix_buffer=no_samples*2;
799
800         tX_malloc(mix_buffer, "mix_buffer", "vtt set_mix_buffer_size()", sizeof(float)*samples_in_mix_buffer, (float *));
801         mix_buffer_end=mix_buffer+samples_in_mix_buffer;
802
803 //      printf("mix_buffer: %12x\n", mix_buffer);
804 //      printf("mix_samples: %i, out_samples: %i", samples_in_mix_buffer, no_samples);
805         
806         if (mix_out_buffer) tX_freemem(mix_out_buffer, "mix_out_buffer", "vtt set_mix_buffer_size()");
807         tX_malloc(mix_out_buffer, "mix_out_buffer", "vtt set_mix_buffer_size()", sizeof(int16_t)*samples_in_mix_buffer + 4, (int16_t *));
808
809 //      printf("mix_out_buffer: %12x\n", mix_out_buffer);
810         
811         for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++) {
812                 res|=(*vtt)->set_output_buffer_size(no_samples);
813         }
814         
815         if ((!mix_buffer) || (!mix_out_buffer) || res) return(1);
816         
817         mix_buffer_size=no_samples;
818         
819         return(0);
820 }
821
822 int16_t * vtt_class :: render_all_turntables()
823 {
824         list <vtt_class *> :: iterator vtt, next;
825         int sample;
826         int mix_sample;
827         f_prec temp;
828         f_prec max;
829         f_prec min;
830         
831         pthread_mutex_lock(&render_lock);
832         
833         if (render_list.size()==0) {
834                 memset((void *) mix_out_buffer, 0, sizeof(int16_t)*samples_in_mix_buffer);
835                 /* We need to memset mix_buffer, too, as the JACK backend
836                    acesses this directly.
837                 */
838                 memset((void *) mix_buffer, 0, sizeof(float)*samples_in_mix_buffer);
839         } else {
840                         vtt=render_list.begin();
841                         (*vtt)->render();                       
842                         max=(*vtt)->max_value;
843                         min=max;
844
845                         for (sample=0, mix_sample=0; sample<(*vtt)->samples_in_outputbuffer; sample++) {                                
846                                 temp=(*vtt)->output_buffer[sample];
847                                 mix_buffer[mix_sample]=temp*(*vtt)->res_volume_left;
848                                 mix_sample++;
849                                 mix_buffer[mix_sample]=temp*(*vtt)->res_volume_right;
850                                 mix_sample++;
851                                 
852                                 if (temp>max) max=temp;
853                                 else if (temp<min) min=temp;
854                         }
855                         
856                         min*=-1.0;
857                         if (min>max) (*vtt)->max_value=min; else (*vtt)->max_value=max;
858
859                         if ((*vtt)->ec_enable) {
860                                 for (sample=0, mix_sample=0; sample<(*vtt)->samples_in_outputbuffer; sample++) {                                
861                                         temp=(*vtt)->ec_output_buffer[sample];
862                                         
863                                         mix_buffer[mix_sample]+=temp*(*vtt)->ec_volume_left;
864                                         mix_sample++;
865                                         mix_buffer[mix_sample]+=temp*(*vtt)->ec_volume_right;
866                                         mix_sample++;
867                                 }
868                         }
869                         
870                         if (master_triggered) {
871                                 for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++) {
872                                         if ((*vtt)->is_sync_client)     {
873                                                 if ((*vtt)->sync_countdown)     {
874                                                         (*vtt)->sync_countdown--;
875                                                 } else {
876                                                         (*vtt)->sync_countdown=(*vtt)->sync_cycles;
877                                                         (*vtt)->trigger(false);
878                                                 }
879                                         }
880                                 }
881                         }
882                         
883                         vtt=render_list.begin();
884                         for (vtt++; vtt!=render_list.end(); vtt++) {
885                                 (*vtt)->render();                                       
886                                 max=(*vtt)->max_value;
887                                 min=max;
888
889                                 for (sample=0, mix_sample=0; sample<(*vtt)->samples_in_outputbuffer; sample++) {
890                                         temp=(*vtt)->output_buffer[sample];
891                                         mix_buffer[mix_sample]+=temp*(*vtt)->res_volume_left;
892                                         mix_sample++;                                   
893                                         mix_buffer[mix_sample]+=temp*(*vtt)->res_volume_right;
894                                         mix_sample++;
895                                 
896                                         if (temp>max) max=temp;
897                                         else if (temp<min) min=temp;
898                                 }
899                                 
900                                 min*=-1.0;
901                                 if (min>max) (*vtt)->max_value=min; else (*vtt)->max_value=max;
902                                 
903                                 if ((*vtt)->ec_enable) {
904                                         for (sample=0, mix_sample=0; sample<(*vtt)->samples_in_outputbuffer; sample++) {
905                                                 temp=(*vtt)->ec_output_buffer[sample];
906                                                 
907                                                 mix_buffer[mix_sample]+=temp*(*vtt)->ec_volume_left;
908                                                 mix_sample++;
909                                                 mix_buffer[mix_sample]+=temp*(*vtt)->ec_volume_right;
910                                                 mix_sample++;
911                                         }
912                                 }
913                         }
914                         
915                         /* left */
916                         
917                         max=mix_max_l;
918                         min=max;
919
920                         for (sample=0; sample<samples_in_mix_buffer; sample+=2) {                               
921                                 temp=mix_buffer[sample];
922
923 #ifndef TX_DO_CLIP
924 #define FL_SHRT_MAX 32767.0
925 #define FL_SHRT_MIN -32768.0
926                                 if(temp < FL_SHRT_MIN) temp = FL_SHRT_MIN;
927                                 else if (temp > FL_SHRT_MAX) temp = FL_SHRT_MAX;
928 #endif                                  
929
930                                 mix_out_buffer[sample]=(int16_t) temp;
931                         
932                                 if (temp>max) max=temp;
933                                 else if (temp<min) min=temp;
934                         }
935                         
936                         min*=-1.0;
937                         if (min>max) mix_max_l=min; else mix_max_l=max;         
938                         
939                         /* right */
940                         
941                         max=mix_max_r;
942                         min=max;
943
944                         for (sample=1; sample<samples_in_mix_buffer; sample+=2) {                               
945                                 temp=mix_buffer[sample];
946
947 #ifndef TX_DO_CLIP
948                                 if(temp < FL_SHRT_MIN) temp = FL_SHRT_MIN;
949                                 else if (temp > FL_SHRT_MAX) temp = FL_SHRT_MAX;
950 #endif
951                                 
952                                 mix_out_buffer[sample]=(int16_t) temp;
953                         
954                                 if (temp>max) max=temp;
955                                 else if (temp<min) min=temp;
956                         }
957                         
958                         min*=-1.0;
959                         if (min>max) mix_max_r=min; else mix_max_r=max;         
960                         
961         }
962         master_triggered=0;
963                 
964         vtt=render_list.begin();
965         while (vtt!=render_list.end()) {
966                 next=vtt;
967                 next++;
968                 
969                 if ((*vtt)->want_stop) (*vtt)->stop_nolock();
970                 vtt=next;
971         }
972         pthread_mutex_unlock(&render_lock);
973         
974         return(mix_out_buffer);
975 }
976
977 void vtt_class :: forward_all_turntables()
978 {
979         list <vtt_class *> :: iterator vtt, next;
980
981         if (render_list.size()>0)
982         {
983                  vtt=render_list.begin();
984                  (*vtt)->forward_turntable();                    
985
986                  if (master_triggered)
987                  {
988                          for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++)
989                          {
990                                  if ((*vtt)->is_sync_client)
991                                  {
992                                          if ((*vtt)->sync_countdown)
993                                          {
994                                                  (*vtt)->sync_countdown--;
995                                          }
996                                          else
997                                          {
998                                                  (*vtt)->sync_countdown=(*vtt)->sync_cycles;
999                                                  (*vtt)->trigger();
1000                                          }
1001                                  }
1002                          }
1003                  }
1004
1005                  vtt=render_list.begin();
1006                  for (vtt++; vtt!=render_list.end(); vtt++)
1007                  {
1008                          (*vtt)->forward_turntable();
1009                  }
1010                  
1011         }
1012         master_triggered=0;
1013         vtt=render_list.begin();
1014         while (vtt!=render_list.end())
1015         {
1016                 next=vtt;
1017                 next++;
1018                 
1019                 if ((*vtt)->want_stop) (*vtt)->stop_nolock();
1020                 vtt=next;
1021         }
1022 }
1023
1024 void vtt_class :: retrigger() 
1025 {
1026         if (res_pitch>=0) pos_f=0;
1027         else pos_f=maxpos;
1028                 
1029         fade=NEED_FADE_OUT;
1030         speed=res_pitch;
1031         speed_real=res_pitch;
1032         speed_target=res_pitch;
1033         want_stop=0;
1034
1035         max_value=0;
1036         
1037         if (is_sync_master)     {
1038                 master_triggered=1;
1039                 master_triggered_at=0;
1040         }
1041 }
1042
1043 int vtt_class :: trigger(bool need_lock)
1044 {
1045         list <vtt_fx *> :: iterator effect;
1046
1047         if (!buffer) return 1;
1048         
1049         retrigger();
1050         
1051         if (!is_playing) {
1052                 if (need_lock) pthread_mutex_lock(&render_lock);
1053                 is_playing=1;
1054                 cleanup_required=false;
1055                 
1056                 /* activating plugins */
1057                 for (effect=fx_list.begin(); effect != fx_list.end(); effect++) {
1058                         (*effect)->activate();
1059                 }
1060                 
1061                 if (is_sync_master)  {
1062                         render_list.push_front(this);           
1063                 } else {
1064                         render_list.push_back(this);
1065                 }
1066                 
1067                 if (need_lock) pthread_mutex_unlock(&render_lock);              
1068         }
1069
1070         return 0;
1071 }
1072
1073 /* call this only when owning render_lock. */
1074 int vtt_class :: stop_nolock()
1075 {
1076         list <vtt_fx *> :: iterator effect;
1077
1078         if (!is_playing) {
1079                 return 1;
1080         }
1081         
1082         render_list.remove(this);
1083         want_stop=0;
1084         is_playing=0;
1085         max_value=0;
1086
1087         cleanup_required=true;
1088         
1089         /* deactivating plugins */
1090         for (effect=fx_list.begin(); effect != fx_list.end(); effect++) {
1091                 (*effect)->deactivate();
1092         }
1093         
1094         return 0;
1095 }
1096
1097 int vtt_class :: stop()
1098 {
1099         int res;
1100         
1101         pthread_mutex_lock(&render_lock);
1102         res=stop_nolock();
1103         pthread_mutex_unlock(&render_lock);
1104
1105         return(res);
1106 }
1107
1108 void vtt_class :: set_sync_master(int master)
1109 {
1110         if (master) {
1111                 if (sync_master) sync_master->set_sync_master(0);
1112                 sync_master=this;
1113                 is_sync_master=1;
1114         } else {
1115                 if (sync_master==this) sync_master=0;
1116                 is_sync_master=0;
1117                 gui_clear_master_button(this);
1118         }
1119 }
1120
1121 void vtt_class :: set_sync_client(int slave, int cycles)
1122 {
1123         tX_debug("vtt_class::set_sync_client() setting %i, %i.", slave, cycles);
1124         is_sync_client=slave;
1125         sync_cycles=cycles;
1126 //      sync_countdown=cycles; 
1127         sync_countdown=0;
1128 }
1129
1130 void vtt_class :: set_sync_client_ug(int slave, int cycles)
1131 {
1132         set_sync_client(slave, cycles);
1133 }
1134
1135 void vtt_class :: set_master_volume(f_prec new_volume)
1136 {
1137         list <vtt_class *> :: iterator vtt;
1138
1139         master_volume=new_volume;
1140         globals.volume=new_volume;
1141         
1142         if (main_list.size()>0) {
1143                 vol_channel_adjust=sqrt((f_prec) main_list.size());
1144                 res_master_volume=master_volume/vol_channel_adjust;             
1145         }
1146                 
1147         for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++) {
1148                 (*vtt)->recalc_volume();
1149         }
1150 }
1151
1152 void vtt_class :: set_master_pitch(f_prec new_pitch)
1153 {
1154         list <vtt_class *> :: iterator vtt;
1155         
1156         globals.pitch=new_pitch;
1157         for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++) {
1158                 (*vtt)->recalc_pitch();
1159         }
1160 }
1161
1162 void vtt_class :: focus_no(int no)
1163 {
1164         list <vtt_class *> :: iterator vtt;
1165         int i;
1166
1167         focused_vtt=NULL;
1168         
1169         for (i=0, vtt=main_list.begin(); vtt!=main_list.end(); vtt++, i++) {
1170                 if (i==no) {
1171                         focused_vtt=(*vtt);
1172                 }
1173         }
1174 }
1175
1176 void vtt_class :: focus_next()
1177 {
1178         list <vtt_class *> :: iterator vtt;
1179         
1180         if (!focused_vtt) {
1181                 if (main_list.size()) {
1182                         focused_vtt=(*main_list.begin());
1183                 }
1184                 return;
1185         }
1186         
1187         for (vtt=main_list.begin(); vtt!=main_list.end() ; vtt++) {
1188                 if ((*vtt)==focused_vtt) {
1189                         /* Ok, we found ourselves.. */
1190                         
1191                         vtt++;
1192                         while ((vtt!=main_list.end()) && ((*vtt)->audio_hidden) ) {
1193                                 vtt++;
1194                         }
1195                         
1196                         if (vtt==main_list.end()) {
1197                                 /* No other "focusable" after this vtt so we're looking for the next */
1198                                 
1199                                 for (vtt=main_list.begin(); vtt!=main_list.end() ; vtt++) {
1200                                         if (! (*vtt)->audio_hidden) {
1201                                                 focused_vtt=(*vtt);
1202                                                 return;
1203                                         }
1204                                 }
1205                                 /* When we get here there's no "focusable" vtt at all... damn */
1206                                 focused_vtt=NULL;
1207                                 return;
1208                         } else {
1209                                 focused_vtt=(*vtt);
1210                                 return;
1211                         }
1212                 }
1213         }
1214         
1215         focused_vtt=(*main_list.begin());
1216 }
1217
1218 void vtt_class :: set_scratch(int newstate)
1219 {
1220         if (newstate) {
1221                 sp_spin.receive_input_value(0);
1222                 do_scratch=1;
1223                 sense_cycles=globals.sense_cycles;
1224         } else {
1225                 sp_spin.receive_input_value(1);
1226                 do_scratch=0;
1227         }
1228 }
1229
1230
1231 void vtt_class :: unfocus()
1232 {
1233         focused_vtt=NULL;
1234 }
1235
1236 void vtt_class :: set_x_input_parameter(tX_seqpar *sp)
1237 {
1238         x_par = sp;
1239 }
1240
1241 void vtt_class :: set_y_input_parameter(tX_seqpar *sp)
1242 {
1243         y_par = sp;
1244 }
1245
1246 void vtt_class :: xy_input(f_prec x_value, f_prec y_value)
1247 {
1248         if (x_par) x_par->handle_mouse_input(x_value*globals.mouse_speed);
1249         if (y_par) y_par->handle_mouse_input(y_value*globals.mouse_speed);
1250 }
1251
1252 #define store(data); if (fwrite((void *) &data, sizeof(data), 1, output)!=1) res+=1;
1253
1254 int  vtt_class :: save(FILE *rc, gzFile rz, char *indent) {
1255         list <vtt_fx *> :: iterator effect;
1256         char tmp_xml_buffer[4096];
1257         
1258         int res=0;
1259
1260         tX_store("%s<turntable>\n", indent);
1261         strcat(indent, "\t");
1262         
1263         store_string("name", name);
1264         if (buffer) {
1265                 store_string("audiofile", filename);
1266         } else {
1267                 store_string("audiofile", "");
1268         }
1269         store_bool("sync_master", is_sync_master);
1270         store_bool("autotrigger", autotrigger);
1271         store_bool_sp("loop", loop, sp_loop);
1272
1273         store_bool_sp("sync_client", is_sync_client, sp_sync_client);
1274         store_int_sp("sync_cycles", sync_cycles, sp_sync_cycles);
1275
1276         store_float_sp("volume", rel_volume, sp_volume);
1277         store_float_sp("pitch", rel_pitch, sp_pitch);   
1278         store_bool_sp("mute", mute, sp_mute);
1279         store_float_sp("pan", pan, sp_pan);
1280         
1281         store_bool_sp("lowpass_enable", lp_enable, sp_lp_enable);
1282         store_float_sp("lowpass_gain", lp_gain, sp_lp_gain);
1283         store_float_sp("lowpass_reso", lp_reso, sp_lp_reso);
1284         store_float_sp("lowpass_freq", lp_freq, sp_lp_freq);
1285
1286         store_bool_sp("echo_enable", ec_enable, sp_ec_enable);
1287         store_float_sp("echo_length", ec_length, sp_ec_length);
1288         store_float_sp("echo_feedback", ec_feedback, sp_ec_feedback);
1289         store_float_sp("echo_pan", ec_pan, sp_ec_pan);
1290         store_float_sp("echo_volume", ec_volume, sp_ec_volume);
1291         
1292         store_id("speed", sp_speed.get_persistence_id());
1293         store_id("trigger", sp_trigger.get_persistence_id());
1294         store_id("spin", sp_spin.get_persistence_id());
1295
1296         
1297         if (x_par) {
1298                 store_int("x_axis_mapping", x_par->get_persistence_id());
1299         }
1300         
1301         if (y_par) {
1302                 store_int("y_axis_mapping", y_par->get_persistence_id());
1303         }
1304
1305         store_bool("audio_panel_hidden", audio_hidden);
1306         store_bool("control_panel_hidden", control_hidden);
1307         store_bool("main_panel_hidden", gui.main_panel->is_hidden());
1308         store_bool("trigger_panel_hidden", gui.trigger_panel->is_hidden());
1309         store_bool("lowpass_panel_hidden", gui.lp_panel->is_hidden());
1310         store_bool("echo_panel_hidden", gui.ec_panel->is_hidden());
1311         
1312         store_bool("mix_mute", mix_mute);
1313         store_bool("mix_solo", mix_solo);
1314
1315         store_float("audio_x_zoom", gui_get_audio_x_zoom(this));
1316         
1317         tX_store("%s<fx>\n", indent);
1318         strcat(indent, "\t");
1319         
1320         for (effect=fx_list.begin(); effect!=fx_list.end(); effect++) {
1321                 (*effect)->save(rc, rz, indent);
1322         }
1323         indent[strlen(indent)-1]=0;
1324         tX_store("%s</fx>\n", indent);
1325         
1326         indent[strlen(indent)-1]=0;
1327         tX_store("%s</turntable>\n", indent);
1328         
1329         return(res);
1330 }
1331
1332 #define TX_XML_SETFILE_VERSION "1.0"
1333
1334 int  vtt_class :: save_all(FILE* rc, gzFile rz) {
1335         int res=0;
1336         list <vtt_class *> :: iterator vtt;
1337         char indent[256];
1338         
1339         tX_seqpar :: create_persistence_ids();
1340
1341         tX_store("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n");
1342         tX_store("<terminatorXset version=\"%s\">\n", TX_XML_SETFILE_VERSION);
1343         
1344         strcpy(indent, "\t");
1345
1346         //store_int(vtt_amount); obsolete
1347
1348         store_float_sp("master_volume", master_volume, sp_master_volume);
1349         store_float_sp("master_pitch", globals.pitch, sp_master_pitch);
1350
1351         for (vtt=main_list.begin(); vtt!=main_list.end(); vtt++) {
1352                 res+=(*vtt)->save(rc, rz, indent);
1353         }
1354         
1355         sequencer.save(rc, rz, indent);
1356         
1357         tX_store("</terminatorXset>\n");
1358         
1359         return(res);
1360 }
1361
1362 int vtt_class :: load(xmlDocPtr doc, xmlNodePtr node) {
1363         char buffer[1024];
1364         bool hidden;
1365         int xpar_id=-1;
1366         int ypar_id=-1;
1367         int elementFound;
1368         char *pid_attr;
1369         int pid;
1370         double dvalue;
1371         double tmp;
1372         char tmp_xml_buffer[4096];
1373         
1374         for (xmlNodePtr cur=node->xmlChildrenNode; cur != NULL; cur = cur->next) {
1375                 if (cur->type == XML_ELEMENT_NODE) {
1376                         elementFound=0;
1377                         
1378                         restore_string_ac("name", buffer, set_name(buffer));
1379                         restore_string("audiofile", filename);
1380                         restore_bool("sync_master", is_sync_master);
1381                         restore_bool("autotrigger", autotrigger);
1382                         restore_bool_id("loop", loop, sp_loop, nop);
1383                         restore_bool_id("sync_client", is_sync_client, sp_sync_client, set_sync_client(is_sync_client, sync_cycles));
1384                         restore_int_id("sync_cycles", sync_cycles, sp_sync_cycles, set_sync_client(is_sync_client, sync_cycles));
1385                         restore_float_id("volume", rel_volume, sp_volume, recalc_volume());
1386                         restore_float_id("pitch", rel_pitch, sp_pitch, recalc_pitch());
1387                         restore_bool_id("mute", mute, sp_mute, set_mute(mute));
1388                         restore_float_id("pan", pan, sp_pan, set_pan(pan));
1389         
1390                         restore_bool_id("lowpass_enable", lp_enable, sp_lp_enable, lp_set_enable(lp_enable));
1391                         restore_float_id("lowpass_gain", lp_gain, sp_lp_gain, lp_set_gain(lp_gain)); 
1392                         restore_float_id("lowpass_reso", lp_reso, sp_lp_reso, lp_set_reso(lp_reso));
1393                         restore_float_id("lowpass_freq", lp_freq, sp_lp_freq, lp_set_freq(lp_freq));
1394         
1395                         restore_bool_id("echo_enable", ec_enable, sp_ec_enable, ec_set_enable(ec_enable));      
1396                         restore_float_id("echo_length", ec_length, sp_ec_length, ec_set_length(ec_length));
1397                         restore_float_id("echo_feedback", ec_feedback, sp_ec_feedback, ec_set_feedback(ec_feedback));
1398                         restore_float_id("echo_pan", ec_pan, sp_ec_pan, ec_set_pan(ec_pan));
1399                         restore_float_id("echo_volume", ec_volume, sp_ec_volume, ec_set_volume(ec_volume));             
1400                 
1401                         restore_id("speed", sp_speed);  
1402                         restore_id("trigger", sp_trigger);
1403                         restore_id("spin", sp_spin);
1404         
1405                         restore_int("x_axis_mapping", xpar_id);
1406                         restore_int("y_axis_mapping", ypar_id);
1407                         
1408                         restore_bool("mix_mute", mix_mute);
1409                         restore_bool("mix_solo", mix_solo);
1410         
1411                         restore_bool("audio_panel_hidden", audio_hidden);
1412                         restore_bool("control_panel_hidden", control_hidden);
1413                         restore_bool_ac("main_panel_hidden", hidden, gui.main_panel->hide(hidden));
1414                         restore_bool_ac("trigger_panel_hidden", hidden, gui.trigger_panel->hide(hidden));
1415                         restore_bool_ac("lowpass_panel_hidden", hidden, gui.lp_panel->hide(hidden));                    
1416                         restore_bool_ac("echo_panel_hidden", hidden, gui.ec_panel->hide(hidden));
1417                         restore_float_ac("audio_x_zoom", tmp, gui_set_audio_x_zoom(this,tmp));
1418                         vg_adjust_zoom(gui.zoom, this);
1419                         
1420                         if (xmlStrcmp(cur->name, (xmlChar *) "fx")==0) {
1421                                 xmlNodePtr fx=cur;
1422                                 elementFound=1;
1423                                 
1424                                 for (xmlNodePtr cur=fx->xmlChildrenNode; cur != NULL; cur = cur->next) {
1425                                         if (cur->type == XML_ELEMENT_NODE) {
1426                                                 int elementFound=0;
1427                                                 
1428                                                 if (xmlStrcmp(cur->name, (xmlChar *) "cutoff")==0) {
1429                                                         for (unsigned int t=0; t<fx_list.size(); t++) effect_down(lp_fx);
1430                                                         elementFound=1;
1431                                                 } else if (xmlStrcmp(cur->name, (xmlChar *) "lowpass")==0) {
1432                                                         for (unsigned int t=0; t<fx_list.size(); t++) effect_down(ec_fx);
1433                                                         elementFound=1;                                                         
1434                                                 } else if (xmlStrcmp(cur->name, (xmlChar *) "ladspa_plugin")==0) {
1435                                                         xmlNodePtr pluginNode=cur;
1436                                                         int ladspa_id=-1;
1437                                                         elementFound=1;
1438                                                         
1439                                                         for (xmlNodePtr cur=pluginNode->xmlChildrenNode; cur!=NULL; cur = cur->next) {
1440                                                                 int elementFound;
1441                                                                 if (cur->type == XML_ELEMENT_NODE) {
1442                                                                         elementFound=0;
1443
1444                                                                         restore_int("ladspa_id", ladspa_id);
1445                                                                         if (elementFound) break;
1446                                                                 }
1447                                                         }
1448                                                         
1449                                                         if (ladspa_id!=-1) {
1450                                                                 LADSPA_Plugin *plugin=LADSPA_Plugin::getPluginByUniqueID(ladspa_id);
1451                                                                 if (plugin) {
1452                                                                         vtt_fx_ladspa *ladspa_effect=add_effect(plugin);
1453                                                                         ladspa_effect->load(doc, pluginNode);
1454                                                                 } else {
1455                                                                         sprintf(buffer,"The terminatorX set file you are loading makes use of a LADSPA plugin that is not installed on this machine. The plugin's ID is [%i].", ladspa_id);
1456                                                                         tx_note(buffer, true);                                                  
1457                                                                 }
1458                                                         } else {
1459                                                                 tX_warning("ladspa_plugin section without a ladspa_id element.");
1460                                                         }
1461                                                         
1462                                                 } else {
1463                                                         tX_warning("unhandled element %s in fx section.", cur->name);
1464                                                 }
1465                                         }
1466                                 }
1467                         }
1468                         
1469                         if(!elementFound) {
1470                                 tX_warning("unhandled element %s in turntable secion.", cur->name);
1471                         }
1472                 }
1473         }
1474
1475         recalc_volume();
1476
1477         if (mix_solo) {
1478                 solo_ctr++;
1479         }
1480         
1481         if (xpar_id>=0) {
1482                 set_x_input_parameter(tX_seqpar :: get_sp_by_persistence_id(xpar_id));
1483         }
1484         else set_x_input_parameter(NULL);
1485         
1486         if (ypar_id) {
1487                 set_y_input_parameter(tX_seqpar :: get_sp_by_persistence_id(ypar_id));
1488         }
1489         else set_y_input_parameter(NULL);
1490         
1491         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gui.mute), mix_mute);
1492         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gui.solo), mix_solo);
1493         
1494         return 0;
1495 }
1496
1497 void vtt_class :: delete_all()
1498 {
1499         while (main_list.size()) {
1500                 delete((*main_list.begin()));
1501         }
1502         
1503         /* Take care of the master events.. */
1504         sequencer.delete_all_events(tX_sequencer::DELETE_ALL);
1505         
1506         /* Now reset master settings ot the default: */
1507         set_master_pitch(1.0);
1508         set_master_volume(1.0);
1509         
1510         sp_master_pitch.do_exec(1.0);
1511         sp_master_pitch.do_update_graphics();
1512
1513         sp_master_volume.do_exec(1.0);
1514         sp_master_volume.do_update_graphics();
1515         
1516         /* Remove master MIDI mappings... */
1517         sp_master_pitch.bound_midi_event.type=tX_midievent::NONE;
1518         sp_master_volume.bound_midi_event.type=tX_midievent::NONE;
1519         
1520         seq_update();
1521 }
1522
1523 int vtt_class :: load_all(xmlDocPtr doc, char *fname) {
1524         xmlNodePtr root=xmlDocGetRootElement(doc);
1525         int elementFound=0;
1526         char fn_buff[4096];
1527         double dvalue;
1528         int res=0;
1529         int restmp=0;
1530         
1531         if (!root) {
1532                 tX_error("no root element? What kind of XML document is this?");
1533                 return 1;
1534         }
1535         
1536         if (xmlStrcmp(root->name, (const xmlChar *) "terminatorXset")) {
1537                 tX_error("this is not a terminatorXset file.")
1538                 return 2;
1539         }
1540         
1541         if (xmlGetProp(root,(xmlChar *) "version")==NULL) {
1542                 tX_error("the set file lacks a version attribute.");
1543                 return 3;
1544         }
1545         
1546         if (xmlStrcmp(xmlGetProp(root, (xmlChar *) "version"), (xmlChar *) TX_XML_SETFILE_VERSION)) {
1547                 tX_warning("this set file is version %s - while this releases uses version %s - trying to load anyway.", xmlGetProp(root, (xmlChar *) "version"), TX_XML_SETFILE_VERSION);
1548         }
1549         
1550         /* delete current tables... */
1551         delete_all();
1552
1553         int table_ctr=0;
1554         
1555         /* counting turntables.. */
1556         for (xmlNodePtr cur=root->xmlChildrenNode; cur != NULL; cur = cur->next) {
1557                 if (cur->type == XML_ELEMENT_NODE) {    
1558                         if (xmlStrcmp(cur->name, (xmlChar *) "turntable")==0) {
1559                                 table_ctr++;
1560                         }
1561                 }
1562         }
1563
1564         tX_debug("Found %i turntables in set.",  table_ctr);
1565
1566         ld_create_loaddlg(TX_LOADDLG_MODE_MULTI, table_ctr);
1567         ld_set_setname(fname);
1568
1569         /* parsing all */
1570         for (xmlNodePtr cur=root->xmlChildrenNode; cur != NULL; cur = cur->next) {
1571                 if (cur->type == XML_ELEMENT_NODE) {                    
1572                         elementFound=0;
1573                 
1574                         restore_float_id("master_volume", master_volume, sp_master_volume, set_master_volume(master_volume));
1575                         restore_float_id("master_pitch", globals.pitch, sp_master_pitch, set_master_pitch(globals.pitch));
1576                         
1577                         if ((!elementFound) && (xmlStrcmp(cur->name, (xmlChar *) "turntable")==0)) {
1578                                 elementFound=1;
1579                                 vtt_class *vtt=new vtt_class(1);
1580                                 vtt->load(doc, cur);
1581                                 
1582                                 tX_debug("loading a turntable..");
1583
1584                                 if (strlen(vtt->filename)) {
1585                                         strcpy(fn_buff, vtt->filename);
1586                                         ld_set_filename(fn_buff);
1587                                 
1588                                         restmp=(int) vtt->load_file(fn_buff);
1589                                         res+=restmp;
1590                                 }
1591         
1592                                 gtk_box_pack_start(GTK_BOX(control_parent), vtt->gui.control_box, TRUE, TRUE, 0);
1593                                 gtk_box_pack_start(GTK_BOX(audio_parent), vtt->gui.audio_box, TRUE, TRUE, 0);
1594                                 if (vtt->audio_hidden) vtt->hide_audio(vtt->audio_hidden);
1595                                 if (vtt->control_hidden) vtt->hide_control(vtt->control_hidden);\r
1596                         }
1597                         if ((!elementFound) && (xmlStrcmp(cur->name, (xmlChar *) "sequencer")==0)) {
1598                                 elementFound=1;
1599                                 sequencer.load(doc, cur);
1600                         }
1601                         if (!elementFound) {
1602                                 tX_warning("unhandled element %s in setfile %s", cur->name, fname);
1603                         }
1604                 }
1605         }
1606         
1607         sp_master_volume.do_update_graphics();
1608         sp_master_pitch.do_update_graphics();
1609         
1610         ld_destroy();
1611         
1612         return(res);
1613 }
1614
1615 void add_vtt(GtkWidget *ctrl, GtkWidget *audio, char *fn)
1616 {
1617         vtt_class *new_tt;
1618         new_tt = new vtt_class(1);
1619         gtk_box_pack_start(GTK_BOX(ctrl), new_tt->gui.control_box, TRUE, TRUE, 0);
1620         gtk_box_pack_start(GTK_BOX(audio), new_tt->gui.audio_box, TRUE, TRUE, 0);
1621         if (fn) new_tt->load_file(fn);
1622 }
1623
1624 extern void vg_move_fx_panel_up(GtkWidget *wid, vtt_class *vtt);
1625 extern void vg_move_fx_panel_down(GtkWidget *wid, vtt_class *vtt);
1626
1627 //#define debug_fx_stack(); for (i=fx_list.begin(); i != fx_list.end(); i++) puts((*i)->get_info_string());
1628 #define debug_fx_stack();
1629
1630 void vtt_class :: effect_up(vtt_fx *effect)
1631 {
1632         list <vtt_fx *> :: iterator i;
1633         list <vtt_fx *> :: iterator previous;
1634         int ok=0;
1635         
1636         debug_fx_stack();
1637         
1638         if ((*fx_list.begin())==effect) return;
1639         
1640         for (previous=i=fx_list.begin(); i != fx_list.end(); i++) {
1641                 if ((*i) == effect) {
1642                         ok=1;
1643                         break;
1644                 }
1645                 previous=i;
1646         }
1647         
1648         if (ok) {       
1649                 pthread_mutex_lock(&render_lock);
1650                 fx_list.remove(effect);
1651                 fx_list.insert(previous, effect);
1652                 pthread_mutex_unlock(&render_lock);
1653
1654                 vg_move_fx_panel_up(effect->get_panel_widget(), this);
1655         }
1656         
1657         debug_fx_stack();
1658 }
1659
1660 void vtt_class :: effect_down(vtt_fx *effect)
1661 {
1662         list <vtt_fx *> :: iterator i;
1663         int ok=0;
1664
1665         debug_fx_stack();
1666                 
1667         for (i=fx_list.begin(); i != fx_list.end(); i++) {
1668                 if ((*i) == effect) {
1669                         ok=1;
1670                         break;
1671                 }
1672         }
1673         
1674         if ((ok) && (i!=fx_list.end())) {
1675                 i++;
1676                 if (i==fx_list.end()) return;
1677                 i++;
1678
1679                 pthread_mutex_lock(&render_lock);
1680                 fx_list.remove(effect);
1681                 
1682                 fx_list.insert(i, effect);
1683                 vg_move_fx_panel_down(effect->get_panel_widget(), this);
1684                 pthread_mutex_unlock(&render_lock);
1685         }
1686         
1687         debug_fx_stack();       
1688 }
1689
1690 void vtt_class ::  effect_remove(vtt_fx_ladspa *effect)
1691 {
1692         pthread_mutex_lock(&render_lock);
1693         fx_list.remove(effect);
1694         pthread_mutex_unlock(&render_lock);
1695         
1696         delete effect;
1697 }
1698
1699 extern void gui_hide_control_panel(vtt_class *vtt, bool hide);
1700 extern void gui_hide_audio_panel(vtt_class *vtt, bool hide);
1701
1702 void vtt_class :: hide_audio(bool hide) {
1703         audio_hidden=hide;
1704         gui_hide_audio_panel(this, hide);
1705 }
1706
1707 void vtt_class :: hide_control(bool hide) {
1708         control_hidden=hide;
1709         gui_hide_control_panel(this, hide);     
1710 }
1711
1712 void vtt_class :: set_sample_rate(int samplerate) {
1713         list <vtt_class *> :: iterator vtt;
1714         double sr=(double) samplerate;
1715
1716         last_sample_rate=samplerate;
1717         
1718         for (vtt=main_list.begin(); vtt!=main_list.end() ; vtt++) {
1719                 if ((*vtt)->audiofile) {
1720                         double file_rate=(*vtt)->audiofile->get_sample_rate();
1721                         (*vtt)->audiofile_pitch_correction=file_rate/sr;
1722                 } else {
1723                         (*vtt)->audiofile_pitch_correction=1.0;
1724                 }
1725                 (*vtt)->recalc_pitch();
1726         }
1727         
1728         int no_samples=(int) (sr*0.001); // Forcing 1 ms blocksize
1729         
1730         set_mix_buffer_size(no_samples);        
1731 }
1732
1733 void vtt_class :: adjust_to_master_pitch(int master_cycles, int cycles, bool create_event) {
1734         if (!sync_master) return;
1735         if (this==sync_master) return;
1736         if (!sync_master->audiofile) return;
1737         if (!audiofile) return;
1738         
1739         double master_time=((double) master_cycles)/sync_master->rel_pitch*sync_master->audiofile->get_no_samples()/((double) sync_master->audiofile->get_sample_rate());
1740         double my_rel_pitch=((audiofile->get_no_samples()/((double) audiofile->get_sample_rate()))*((double) cycles))/master_time;
1741         
1742         if (create_event) {
1743                 sp_pitch.do_exec(my_rel_pitch);
1744                 sp_pitch.record_value(my_rel_pitch);
1745         } else {
1746                 sp_pitch.do_exec(my_rel_pitch);
1747         }
1748         
1749         tX_debug("master_time: %lf, res_pitch: %lf - res time: %lf, (%lf, %lf)", master_time, my_rel_pitch, ((double) cycles)*my_rel_pitch*audiofile->get_no_samples()/((double) audiofile->get_sample_rate()), (double) sync_master->audiofile->get_sample_rate(),(double)  audiofile->get_sample_rate());
1750         
1751         sp_pitch.update_graphics();
1752 }