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