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