Debian Codegraph @

source/mainwin.cc

raw
1 // ----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2003-2013 Fons Adriaensen <[email protected]>
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 3 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, see <http://www.gnu.org/licenses/>.
17 //
18 // ----------------------------------------------------------------------------
19
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include "mainwin.h"
25 #include "styles.h"
26
27
28 Mainwin::Mainwin (X_window *parent, X_resman *resman, X_callback *callb, Filewin *filewin, ITC_ctrl *itcctrl) :
29 X_window (parent, XPOS, YPOS, XDEF, YDEF, XftColors.main_bg->pixel),
30 _callb (callb),
31 _xp (XPOS),
32 _yp (YPOS),
33 _xs (XDEF),
34 _ys (YDEF),
35 _state (INIT),
36 _count (0),
37 _flock (false),
38 _doall (false),
39 _itcctrl (itcctrl),
40 _filewin (filewin),
41 _disp (0),
42 _data1 (0),
43 _data2 (0),
44 _convol (0),
45 _awfilt (0),
46 _prog0 (0),
47 _prog1 (0)
48 {
49 X_hints H;
50 char s [1024];
51
52 _xatom = XInternAtom (dpy (), "WM_DELETE_WINDOW", True);
53 XSetWMProtocols (dpy (), win (), &_xatom, 1);
54 _xatom = XInternAtom (dpy (), "WM_PROTOCOLS", True);
55
56 H.size (XDEF, YDEF);
57 H.minsize (XMIN, YMIN);
58 H.maxsize (disp ()->xsize (), disp ()->ysize ());
59 H.position (XPOS, YPOS);
60 H.rname (resman->rname ());
61 H.rclas (resman->rclas ());
62 x_apply (&H);
63
64 sprintf (s, "%s-%s [%s]", PROGNAME, VERSION, resman->rname ());
65 x_set_title (s);
66 x_add_events (StructureNotifyMask);
67
68 *_file1 = 0;
69 *_file2 = 0;
70 _base1 = _file1;
71
72 _splashw = new Splashwin (this, (_xs - 400) / 2, (_ys - 300) / 2, 400, 300);
73 _splashw->x_set_win_gravity (CenterGravity);
74 _editwin = new Editwin (this, this, LMAR, TMAR, _xs - LMAR, _ys - TMAR);
75 _filtwin = new Filtwin (this, this, LMAR, TMAR, _xs - LMAR, _ys - TMAR);
76 _exptwin = new Exptwin (parent, resman, this);
77 _progwin = new Progwin (this, resman);
78
79 makewin ();
80 _modwin = _ifwview;
81 _modwin->x_map ();
82 x_map ();
83 }
84
85
86 Mainwin::~Mainwin (void)
87 {
88 delete _data1;
89 delete _data2;
90 delete _splashw;
91 delete _editwin;
92 delete _filtwin;
93 }
94
95
96 void Mainwin::stop (void)
97 {
98 _state = TERM;
99 }
100
101
102 void Mainwin::handle_time (void)
103 {
104 if (++_count == 100) _splashw->x_unmap ();
105 switch (_state)
106 {
107 case INIT:
108 _splashw->x_mapraised ();
109 set_view ();
110 break;
111
112 case TERM:
113 _state = EXIT;
114 break;
115
116 default:
117 ;
118 }
119 }
120
121
122 void Mainwin::handle_event (XEvent *E)
123 {
124 switch (E->type)
125 {
126 case ConfigureNotify:
127 resize ((XConfigureEvent *) E);
128 break;
129 case ClientMessage:
130 xcmesg ((XClientMessageEvent *) E);
131 break;
132 }
133 }
134
135
136 void Mainwin::resize (XConfigureEvent *E)
137 {
138 if ((_xs != E->width) || (_ys != E->height))
139 {
140 _xs = E->width;
141 _ys = E->height;
142 _editwin->resize (_xs - LMAR, _ys - TMAR);
143 _filtwin->resize (_xs - LMAR, _ys - TMAR);
144 }
145 _xp = E->x;
146 _yp = E->y;
147 }
148
149
150 void Mainwin::xcmesg (XClientMessageEvent *E)
151 {
152 if (E->message_type == _xatom) _callb->handle_callb (CB_MAIN_CLOSE, 0, 0);
153 }
154
155
156 void Mainwin::handle_callb (int k, X_window *W, XEvent *E)
157 {
158 int c, e;
159
160 c = X_callback::cb_class (k);
161 e = X_callback::cb_event (k);
162 switch (c)
163 {
164 case X_callback::TEXTIP:
165 {
166 if (_flock || _prog0) break;
167 switch (e)
168 {
169 case X_textip::BUT:
170 XSetInputFocus (dpy (), W->win (), RevertToPointerRoot, CurrentTime);
171 break;
172
173 case X_textip::MODIF:
174 modif (W);
175 break;
176 }
177 }
178 case X_callback::BUTTON:
179 {
180 X_button *B = (X_button *) W;
181 XButtonEvent *X = (XButtonEvent *) E;
182
183 if (_flock || _prog0) break;
184 k = B->cbid ();
185 if (e == X_button::RELSE)
186 {
187 switch (k)
188 {
189 case B_CHAN:
190 case B_CHAN + 1:
191 case B_CHAN + 2:
192 case B_CHAN + 3:
193 settrace (k - B_CHAN, ((XButtonEvent *) E)->button);
194 break;
195
196 case B_FRMI:
197 next_sect (-1);
198 break;
199
200 case B_FRPL:
201 next_sect (+1);
202 break;
203
204 case B_SESS:
205 _flock = 1;
206 _filewin->open (this, CB_FILE_SESS, ".", "", 0);
207 break;
208
209 case B_LOAD:
210 _flock = 1;
211 _filewin->open (this, CB_FILE_LOAD, ".", ".ald", 0);
212 break;
213
214 case B_SAVE:
215 break;
216
217 case B_SONE:
218 case B_SALL:
219 _flock = true;
220 _doall = k == B_SALL;
221 switch (_state)
222 {
223 case CONV:
224 _filewin->open (this, CB_FILE_SAVE, "impresp", ".ald", _doall ? _base1 : 0);
225 break;
226 case EDIT:
227 _filewin->open (this, CB_FILE_SAVE, "edited", ".ald", _doall ? _base1 : 0);
228 break;
229 default:
230 _filewin->open (this, CB_FILE_SAVE, ".", ".ald", 0);
231 }
232 break;
233
234 case B_IMPT:
235 break;
236
237 case B_EXPT:
238 if (_data1 == 0) break;
239 _flock = 1;
240 _exptwin->show ();
241 break;
242
243 case B_SGEN:
244 _callb->handle_callb (CB_OPEN_SGEN, this, 0);
245 break;
246
247 case B_CAPT:
248 _callb->handle_callb (CB_OPEN_CAPT, this, 0);
249 break;
250
251 case B_CONV:
252 set_conv ();
253 break;
254
255 case B_EDIT:
256 set_edit ();
257 break;
258
259 case B_SINT:
260 // set_sint ();
261 break;
262
263 case B_FILT:
264 // set_filt ();
265 break;
266
267 case B_CANC:
268 cancel ();
269 break;
270
271 case B_APPL:
272 apply ();
273 break;
274
275 case B_FILE:
276 _tcfil = _tcfil1;
277 _filewin->open (this, CB_FILE_LOAD, "sweep", ".ald", 0);
278 break;
279
280 case B_TREF:
281 edit_tref (X->button == Button3);
282 break;
283
284 case B_GAIN:
285 edit_gain (X->button == Button3, X->state & ShiftMask);
286 break;
287
288 case B_TRM0:
289 edit_trim0 (X->button == Button3);
290 break;
291
292 case B_TRM1:
293 edit_trim1 (X->button == Button3);
294 break;
295
296 case B_FADE:
297 edit_fade (X->button == Button3);
298 break;
299 }
300 }
301 break;
302 }
303 /*
304 case X_callback::MENU:
305 {
306 X_menuwin *M = (X_menuwin *) W;
307 switch (e)
308 {
309 case X_menuwin::OPEN:
310 M->show ();
311 break;
312
313 case X_menuwin::SEL:
314 // M->selection ()
315 M->hide ();
316 break;
317
318 case X_menuwin::CAN:
319 M->hide ();
320 break;
321 }
322 break;
323 }
324 */
325 default:
326 switch (k)
327 {
328 case CB_FILE_CANC:
329 _flock = false;
330 break;
331
332 case CB_FILE_SESS:
333 _flock = false;
334 _callb->handle_callb (CB_FILE_SESS, 0, 0);
335 break;
336
337 case CB_FILE_LOAD:
338 _flock = false;
339 file_load ();
340 break;
341
342 case CB_FILE_SAVE:
343 _flock = false;
344 if (_doall) save_all ();
345 else save_one ();
346 break;
347
348 case CB_FILE_IMPT:
349 _flock = false;
350 break;
351
352 case CB_FILE_EXPT:
353 _flock = false;
354 file_expt ();
355 break;
356 }
357 break;
358 }
359 }
360
361
362 void Mainwin::handle_mesg (ITC_mesg *M)
363 {
364 switch (M->type ())
365 {
366 case MT_CONV_ONE:
367 conv_done_one ((M_calc_conv *) M);
368 break;
369
370 case MT_CONV_ALL:
371 conv_done_all ((M_calc_conv *) M);
372 break;
373
374 case MT_EDIT_ONE:
375 edit_done_one ((M_calc_edit *) M);
376 break;
377
378 case MT_EDIT_ALL:
379 edit_done_all ((M_calc_edit *) M);
380 break;
381
382 case MT_EXPORT:
383 M->recover ();
384 _editwin->update ();
385 break;
386 }
387 }
388
389
390 void Mainwin::handle_prog (void)
391 {
392 if (++_prog1 == _prog0)
393 {
394 _progwin->hide ();
395 _prog1 = _prog0 = 0;
396 }
397 else _progwin->prog ((float) _prog1 / (float) _prog0);
398 }
399
400
401 void Mainwin::set_view (void)
402 {
403 _modwin->x_unmap ();
404 _modwin = _ifwview;
405 _modwin->x_map ();
406 _tfile->x_mapraised ();
407 _editwin->x_map ();
408 _filtwin->x_unmap ();
409 _state = VIEW;
410 set_disp (_data1, 0, 0);
411 fileinfo ();
412 }
413
414
415 void Mainwin::set_conv (void)
416 {
417 if (_data1 == 0) return;
418 _modwin->x_unmap ();
419 _modwin = _ifwconv;
420 _modwin->x_map ();
421 _tfile->x_mapraised ();
422 _state = CONV;
423 conv_init ();
424 }
425
426
427 void Mainwin::set_edit (void)
428 {
429 if (_data1 == 0) return;
430 _modwin->x_unmap ();
431 _modwin = _ifwedit;
432 _modwin->x_map ();
433 _tfile->x_mapraised ();
434 _state = EDIT;
435 edit_init ();
436 }
437
438
439 void Mainwin::set_sint (void)
440 {
441 if (_data1 == 0) return;
442 _modwin->x_unmap ();
443 _modwin = _ifwsint;
444 _modwin->x_map ();
445 _tfile->x_mapraised ();
446 _state = SINT;
447 sint_init ();
448 }
449
450
451 void Mainwin::set_filt (void)
452 {
453 if (_data1 == 0) return;
454 _modwin->x_unmap ();
455 _modwin = _ifwfilt;
456 _modwin->x_map ();
457 _tfile->x_mapraised ();
458 _editwin->x_unmap ();
459 _filtwin->x_map ();
460 _state = FILT;
461 }
462
463
464 void Mainwin::file_load (void)
465 {
466 char name [1024];
467
468 sprintf (name, "%s/%s", _filewin->wdir (), _filewin->file ());
469 switch (_state)
470 {
471 case VIEW:
472 strcpy (_file1, name);
473 Impdata::checkext (_file1);
474 delete _data1;
475 _data1 = new Impdata;
476 if (_data1->open_read (_file1))
477 {
478 delete _data1;
479 _data1 = 0;
480 return;
481 }
482 _data1->alloc ();
483 _data1->read_sect (0);
484 set_disp (_data1, 0, 0);
485 fileinfo ();
486 break;
487
488 case CONV:
489 _tcfil->set_text (name);
490 modif (_tcfil);
491 break;
492 }
493 }
494
495
496 void Mainwin::file_save (void)
497 {
498 }
499
500
501 void Mainwin::file_impt (void)
502 {
503 }
504
505
506 void Mainwin::file_expt (void)
507 {
508 int c, s;
509 U32 m, mask;
510 const char *name;
511 M_export *M;
512
513 name = _exptwin->file ();
514 if (*name)
515 {
516 if (_exptwin->chan () == EXP_CHAN_ALL) mask = ((1u << _data1->n_chan ()) - 1);
517 else mask = _editwin->get_mask ();
518
519 switch (_exptwin->type ())
520 {
521 case EXP_TYPE_RAW:
522 case EXP_TYPE_WAV:
523 for (c = 0, m = mask; m; m >>= 1) c += m & 1;
524 break;
525 default:
526 c = 1;
527 }
528
529 switch (_exptwin->sect ())
530 {
531 case EXP_SECT_COMB:
532 case EXP_SECT_ITER:
533 s = _data1->n_sect ();
534 break;
535 default:
536 s = 1;
537 }
538
539 M = new M_export ();
540 M->_impd = _data1;
541 M->_name = name;
542 M->_type = _exptwin->type ();
543 M->_form = _exptwin->form ();
544 M->_sect = _exptwin->sect ();
545 M->_mask = mask;
546 M->_rval = 0;
547 _itcctrl->send_event (TO_CALC, M);
548 _progwin->show (_xs / 2, _ys / 2, "Exporting...");
549 _prog0 += c * s;
550 }
551 }
552
553
554 void Mainwin::set_disp (Impdata *data, int nchan1, const char **labels)
555 {
556 int i, f = 0;
557 float t0, t1;
558 char s [16];
559
560 _nchan1 = 0;
561
562 _editwin->clear_marks ();
563
564 _disp = data;
565 if (data)
566 {
567 if (nchan1)
568 {
569 _nchan1 = nchan1;
570 _cmask2 = ((1 << (data->n_chan () - _nchan1)) - 1) << _nchan1;
571 }
572 else
573 {
574 _nchan1 = data->n_chan ();
575 _cmask2 = 0;
576 }
577 for (i = 0; i < 4; i++) _bchan [i]->x_unmap ();
578 for (i = 0; i < _nchan1; i++)
579 {
580 if (_cmask2) _bchan [i]->set_text (labels [i], 0);
581 else _bchan [i]->set_text (Impdata::channame (data->type (), i), 0);
582 _bchan [i]->set_stat (1);
583 }
584 if (_cmask2)
585 {
586 _bchan [i]->set_text (labels [i], 0);
587 _bchan [i]->set_stat (0);
588 i++;
589 f |= Editwin::OVERLAY;
590 }
591 if (i > 1) while (i) _bchan [--i]->x_map ();
592 if (data->n_sect () > 1)
593 {
594 _bfrmi->x_map ();
595 _bfrpl->x_map ();
596 sprintf (s, "%d / %d", data->i_sect () + 1, data->n_sect ());
597 _tfrag->set_text (s);
598 }
599 else
600 {
601 _bfrmi->x_unmap ();
602 _bfrpl->x_unmap ();
603 _tfrag->set_text ("1 / 1");
604 }
605 if (_state == SINT) f |= Editwin::POWMODE;
606 t0 = (0 - data->dtref ()) / data->drate () - 0.1;
607 t1 = (data->n_fram () - data->dtref ()) / data->drate () + 0.1;
608 _editwin->set_data (data, (1 << _nchan1) - 1, f, t0, t1);
609 }
610 else
611 {
612 for (i = 0; i < 4; i++) _bchan [i]->x_unmap ();
613 _bfrmi->x_unmap ();
614 _bfrpl->x_unmap ();
615 _tfrag->set_text ("- / -");
616 _editwin->set_data (0, 0, 0, 0, 0);
617 }
618 }
619
620
621 void Mainwin::settrace (int k, int b)
622 {
623 int i, m;
624
625 if (k < _nchan1) m = 1 << k;
626 else m = _cmask2;
627 if (b == Button3) m ^= _editwin->get_mask ();
628 if (m)
629 {
630 _editwin->set_mask (m);
631 for (i = 0; i < _nchan1; i++)
632 {
633 _bchan [i]->set_stat (m & 1);
634 m >>= 1;
635 }
636 if (_cmask2) _bchan [_nchan1]->set_stat (m ? 1 : 0);
637 }
638 }
639
640
641 void Mainwin::modif (X_window *W)
642 {
643 switch (_state)
644 {
645 case CONV:
646 _bappl->set_stat (2);
647 if (W == _tcfil1)
648 {
649 delete _convol;
650 _convol = 0;
651 }
652 _data2->clear ();
653 _editwin->update ();
654 break;
655 }
656 }
657
658
659 void Mainwin::cancel ()
660 {
661 switch (_state)
662 {
663 case CONV:
664 delete _data2;
665 delete _convol;
666 _data2 = 0;
667 _convol = 0;
668 break;
669
670 case EDIT:
671 delete _data2;
672 _data2 = 0;
673 break;
674
675 case FILT:
676 break;
677
678 case SINT:
679 delete _data2;
680 delete _awfilt;
681 _data2 = 0;
682 _awfilt = 0;
683 break;
684 }
685 set_view ();
686 }
687
688
689 void Mainwin::apply ()
690 {
691 switch (_state)
692 {
693 case CONV:
694 _tctim1->callb_modified ();
695 _tctim2->callb_modified ();
696 _tcfil1->callb_modified ();
697 conv_calc_one (0);
698 break;
699 }
700 }
701
702
703 void Mainwin::next_sect (int d)
704 {
705 int i, j, n;;
706 char s [16];
707
708 i = _data1->i_sect ();
709 j = i + d;
710 n = _data1->n_sect () - 1;
711 if (j > n) j = n;
712 else if (j < 0) j = 0;
713 if (j != i)
714 {
715 sprintf (s, "%d / %d", j + 1, n + 1);
716 _tfrag->set_text (s);
717 switch (_state)
718 {
719 case VIEW:
720 _data1->read_sect (j);
721 _editwin->update ();
722 break;
723
724 case CONV:
725 _data1->read_sect (j);
726 conv_calc_one (0);
727 break;
728
729 case EDIT:
730 _data1->read_sect (j);
731 edit_calc_one (0);
732 break;
733
734 case SINT:
735 _data1->read_sect (j);
736 sint_exec ();
737 _editwin->update ();
738 break;
739 }
740 }
741 }
742
743
744 void Mainwin::save_one (void)
745 {
746 char name [1024];
747
748 sprintf (name, "%s/%s", _filewin->wdir (), _filewin->file ());
749 switch (_state)
750 {
751 case CONV:
752 conv_calc_one (name);
753 break;
754 case EDIT:
755 edit_calc_one (name);
756 break;
757 }
758 }
759
760
761 void Mainwin::save_all (void)
762 {
763 char name [1024];
764
765 sprintf (name, "%s/%s", _filewin->wdir (), _filewin->file ());
766 switch (_state)
767 {
768 case CONV:
769 conv_calc_all (name);
770 break;
771 case EDIT:
772 edit_calc_all (name);
773 break;
774 }
775 }
776
777
778 void Mainwin::conv_init ()
779 {
780 _bappl->set_stat (2);
781 _data2 = new Impdata ();
782 _data2->set_type (_data1->type ());
783 _data2->set_rate (_data1->rate_n (), _data1->rate_d ());
784 _data2->set_n_fram (1000);
785 _data2->set_n_sect (1);
786 _data2->alloc ();
787 _editwin->set_data (_data2, 0, 0, 0, 0);
788 }
789
790
791 int Mainwin::conv_prep (M_calc_conv *M)
792 {
793 char s1 [1024];
794 char s2 [1024];
795 float t0, t1;
796 float b [2 * 4096];
797 int c, i, n;
798 Impdata T;
799 Convdata *D;
800
801 if (! _convol)
802 {
803 *s1 = *s2 = 0;
804 if ( (sscanf (_tcfil1->text (), "%s", s1) != 1)
805 || T.open_read (s1)
806 || ((T.type () != Impdata::TYPE_SWEEP) && (T.n_chan () != 1))
807 || (T.rate_n () / T.rate_d () != _data1->rate_n () / _data1->rate_d ()))
808 {
809 askdata (_tcfil1);
810 return 1;
811 }
812 c = _data1->n_chan ();
813 _convlen = T.n_fram ();
814 D = new Convdata (4096, _convlen, 0);
815 for (i = 0; i < (int)(D->npar ()); i++)
816 {
817 n = T.read_ext (4096, b);
818 if (n < 4096) memset (b + 2 * n, 0, 2 * (4096 - n) * sizeof (float));
819 D->prepare_part (i, 1.0f, b + (T.n_chan () - 1), T.n_chan ());
820 }
821 D->prepare_done ();
822 T.close ();
823 _convol = new Convolver (4096, _convlen, 0, c, c);
824 for (i = 0; i < c; i++) _convol->set_conv (i, i, D);
825 }
826
827 if ( (sscanf (_tctim1->text (), "%f", &t0) != 1)
828 || (t0 > 0.0f))
829 {
830 askdata (_tctim1);
831 return 1;
832 }
833 if ( (sscanf (_tctim2->text (), "%f", &t1) != 1)
834 || (t1 < t0 + 0.1f)
835 || (t1 > 40.0f))
836 {
837 askdata (_tctim2);
838 return 1;
839 }
840 M->_conv = _convol;
841 M->_data1 = _data1;
842 M->_data2 = _data2;
843 M->_k0 = _convlen - 1 + (int)(floor (_data1->drate () * t0 + 0.5f));
844 M->_k1 = _convlen - 1 + (int)(floor (_data1->drate () * t1 + 0.5f));
845 if (M->_k0 < 0) M->_k0 = 0;
846 if (_data2->n_fram () != (U32)(M->_k1 - M->_k0))
847 {
848 _data2->set_n_fram ((U32)(M->_k1 - M->_k0));
849 _data2->alloc ();
850 _editwin->set_data (_data2, 0, 0, 0, 0);
851 }
852 _data2->set_tref (_convlen - 1 - M->_k0);
853 return 0;
854 }
855
856
857 int Mainwin::conv_save (const char *name, bool all)
858 {
859 _data2->set_n_sect (all ? _data1->n_sect () : 1);
860 strcpy (_file2, name);
861 Impdata::checkext (_file2);
862 if (! strcmp (_file1, _file2))
863 {
864 fprintf (stderr, "Can't save to current input '%s'.\n", _file1);
865 return 1;
866 }
867 if (_data2->open_write (_file2))
868 {
869 fprintf (stderr, "Can't create output file '%s'.\n", _file2);
870 return 1;
871 }
872 return 0;
873 }
874
875
876 void Mainwin::conv_calc_one (const char *name)
877 {
878 M_calc_conv *M;
879
880 M = new M_calc_conv (MT_CONV_ONE);
881 if (conv_prep (M) || (name && conv_save (name, false)))
882 {
883 M->recover ();
884 return;
885 }
886 M->_sect = name ? 0 : -1;
887 _progwin->show (_xs / 2, _ys / 2, "Performing convolution...");
888 _prog0 += (M->_k1 + _convol->part () - 1) / _convol->part ();
889 _itcctrl->send_event (TO_CALC, M);
890 }
891
892
893 void Mainwin::conv_done_one (M_calc_conv *M)
894 {
895 M->recover ();
896 _data2->close ();
897 _bappl->set_stat (0);
898 _editwin->update ();
899 }
900
901
902 void Mainwin::conv_calc_all (const char *name)
903 {
904 M_calc_conv *M;
905
906 M = new M_calc_conv (MT_CONV_ALL);
907 if (conv_prep (M) || (name && conv_save (name, true)))
908 {
909 M->recover ();
910 return;
911 }
912 _progwin->show (_xs / 2, _ys / 2, "Performing convolution on all sections...");
913 _prog0 += _data1->n_sect () * ((M->_k1 + _convol->part () - 1) / _convol->part ());
914 _itcctrl->send_event (TO_CALC, M);
915 }
916
917
918 void Mainwin::conv_done_all (M_calc_conv *M)
919 {
920 M->recover ();
921 _data2->close ();
922 delete _data1;
923 _data1 = _data2;
924 _data2 = 0;
925 strcpy (_file1, _file2);
926 _data1->open_read (_file1);
927 _data1->alloc ();
928 _data1->read_sect (0);
929 delete _convol;
930 _convol = 0;
931 set_view ();
932 }
933
934
935
936 void Mainwin::edit_init ()
937 {
938 _offs = 0;
939 _gain = 1.0f;
940 _slope = 0.0f;
941 _ind0 = 0;
942 _ind1 = _data1->n_fram () - 1;
943 _ttref->set_text ("0.0");
944 _tgain->set_text ("1.0");
945 _ttrm00->set_text ("--");
946 _ttrm01->set_text ("--");
947 _ttrm10->set_text ("--");
948 _ttrm11->set_text ("--");
949 _ttfade->set_text ("--");
950 _tslope->set_text ("0.0");
951 _btref->set_stat (0);
952 _bgain->set_stat (0);
953 _btrim0->set_stat (0);
954 _btrim1->set_stat (0);
955 _data2 = new Impdata ();
956 _data2->set_type (_data1->type ());
957 _data2->set_rate (_data1->rate_n (), _data1->rate_d ());
958 }
959
960
961 int Mainwin::edit_prep (M_calc_edit *M)
962 {
963 M->_data1 = _data1;
964 M->_data2 = _data2;
965 M->_offs = _offs;
966 M->_gain = _gain;
967 M->_ind0 = _ind0;
968 M->_ind1 = _ind1;
969 M->_slope = 0;
970 if (_btrim0->stat ())
971 {
972 M->_trm00 = _trm00;
973 M->_trm01 = _trm01;
974 }
975 else M->_trm00 = M->_trm01 = 0;
976 if (_btrim1->stat ())
977 {
978 M->_trm10 = _trm10;
979 M->_trm11 = _trm11;
980 }
981 else M->_trm10 = M->_trm11 = 0;
982 if (_bfade->stat ())
983 {
984 M->_tfade = _tfade;
985 M->_slope = _slope;
986 }
987 return 0;
988 }
989
990
991 int Mainwin::edit_save (const char *name, bool all)
992 {
993 _data2->set_n_sect (all ? _data1->n_sect () : 1);
994 _data2->set_n_fram (_ind1 - _ind0 + 1);
995 _data2->alloc ();
996 _data2->set_tref (_data1->tref_i () - _ind0 + _offs);
997 strcpy (_file2, name);
998 Impdata::checkext (_file2);
999 if (! strcmp (_file1, _file2))
1000 {
1001 fprintf (stderr, "Can't save to current input '%s'.\n", _file1);
1002 return 1;
1003 }
1004 if (_data2->open_write (_file2))
1005 {
1006 fprintf (stderr, "Can't create output file '%s'.\n", _file2);
1007 return 1;
1008 }
1009 return 0;
1010 }
1011
1012
1013 void Mainwin::edit_calc_one (const char *name)
1014 {
1015 M_calc_edit *M;
1016
1017 M = new M_calc_edit (MT_EDIT_ONE);
1018 if (edit_prep (M) || (name && edit_save (name, false)))
1019 {
1020 M->recover ();
1021 return;
1022 }
1023 if (name)
1024 {
1025 M->_sect = 0;
1026 _progwin->show (_xs / 2, _ys / 2, "Performing edits...");
1027 _prog0 += 1;
1028 }
1029 else M->_sect = -1;
1030 _data1->read_sect (_data1->i_sect ());
1031 _itcctrl->send_event (TO_CALC, M);
1032 }
1033
1034
1035 void Mainwin::edit_done_one (M_calc_edit *M)
1036 {
1037 M->recover ();
1038 _data2->close ();
1039 _editwin->update ();
1040 }
1041
1042
1043 void Mainwin::edit_calc_all (const char *name)
1044 {
1045 M_calc_edit *M;
1046
1047 M = new M_calc_edit (MT_EDIT_ALL);
1048 if (edit_prep (M) || (name && edit_save (name, true)))
1049 {
1050 M->recover ();
1051 return;
1052 }
1053 _progwin->show (_xs / 2, _ys / 2, "Performing edits on all sections...");
1054 _prog0 += _data1->n_sect ();
1055 _itcctrl->send_event (TO_CALC, M);
1056 }
1057
1058
1059 void Mainwin::edit_done_all (M_calc_edit *M)
1060 {
1061 M->recover ();
1062 _data2->close ();
1063 delete _data1;
1064 _data1 = _data2;
1065 _data2 = 0;
1066 strcpy (_file1, _file2);
1067 _data1->open_read (_file1);
1068 _data1->alloc ();
1069 _data1->read_sect (0);
1070 set_view ();
1071 }
1072
1073
1074 void Mainwin::edit_tref (bool undo)
1075 {
1076 int i;
1077
1078 if (undo)
1079 {
1080 if (_btref->stat ())
1081 {
1082 _btref->set_stat (0);
1083 _ttref->set_text ("0.0");
1084 _editwin->set_offs (_offs = 0);
1085 }
1086 else return;
1087 }
1088 else
1089 {
1090 i = _editwin->get_currmark ();
1091 if (i < 0) return;
1092 _btref->set_stat (2);
1093 _offs = _editwin->get_markpos (i) - _data1->tref_i ();
1094 ptime (_data1->tref_i (), _ttref);
1095 _editwin->set_offs (_offs);
1096 }
1097 if (_btrim0->stat ())
1098 {
1099 ptime (_trm00, _ttrm00);
1100 ptime (_trm01, _ttrm01);
1101 }
1102 if (_btrim1->stat ())
1103 {
1104 ptime (_trm10, _ttrm10);
1105 ptime (_trm11, _ttrm11);
1106 }
1107 }
1108
1109
1110 void Mainwin::edit_gain (bool undo, bool norm)
1111 {
1112 int c, i, j, i0, i1, k, m, n, r, s;
1113 float u, v, *p;
1114 char t [16];
1115
1116 if (undo)
1117 {
1118 if (_bgain->stat ())
1119 {
1120 _bgain->set_stat (0);
1121 _tgain->set_text ("1.0");
1122 _editwin->set_gain (_gain = 1.0f);
1123 }
1124 }
1125 else if (norm)
1126 {
1127 i = _editwin->get_currmark ();
1128 if (i < 0) return;
1129 k = _editwin->get_markpos (i);
1130 r = _data1->rate_n () / (_data1->rate_d () * 100);
1131 s = _data1->n_fram () - 1;
1132 c = _data1->n_chan ();
1133 i0 = k - r;
1134 i1 = k + r;
1135 if (i0 < 0) i0 = 0;
1136 if (i1 > s) i1 = s;
1137 m = _editwin->get_mask ();
1138 n = 0;
1139 u = 0.0f;
1140 for (j = 0; m; j++, m >>= 1)
1141 {
1142 if (m & 1)
1143 {
1144 n++;
1145 p = _data1->data (j);
1146 for (i = i0; i <= i1; i++)
1147 {
1148 v = p [c * i];
1149 u += v * v * 0.5f * (1.0f + cosf (M_PI * (i - k) / r));
1150 }
1151 }
1152 }
1153 _gain = sqrtf (n / u);
1154 sprintf (t, "%5.3lf", _gain);
1155 _tgain->set_text (t);
1156 _bgain->set_stat (2);
1157 _editwin->set_gain (_gain);
1158 }
1159 else
1160 {
1161 if (sscanf (_tgain->text (), "%f", &v) != 1) return;
1162 _gain = v;
1163 sprintf (t, "%5.3lf", _gain);
1164 _tgain->set_text (t);
1165 _bgain->set_stat (2);
1166 _editwin->set_gain (_gain);
1167 }
1168 }
1169
1170
1171 void Mainwin::edit_trim0 (bool undo)
1172 {
1173 int m0, m1;
1174
1175 if (undo)
1176 {
1177 if (_btrim0->stat ())
1178 {
1179 _ind0 = 0;
1180 _btrim0->set_stat (0);
1181 _ttrm00->set_text ("--");
1182 _ttrm01->set_text ("--");
1183 }
1184 else return;
1185 }
1186 else
1187 {
1188 m0 = _editwin->get_markpos (0);
1189 m1 = _editwin->get_markpos (1);
1190 if ((m0 == INT_MIN) || (m1 == INT_MIN)) return;
1191 _btrim0->set_stat (2);
1192 if (m0 < m1) { _trm00 = m0; _trm01 = m1; }
1193 else { _trm00 = m1; _trm01 = m0; }
1194 _ind0 = (_trm00 < 0) ? 0 : _trm00;
1195 ptime (_trm00, _ttrm00);
1196 ptime (_trm01, _ttrm01);
1197 }
1198 edit_calc_one (0);
1199 }
1200
1201
1202 void Mainwin::edit_trim1 (bool undo)
1203 {
1204 int m0, m1, n;
1205
1206 if (undo)
1207 {
1208 if (_btrim1->stat ())
1209 {
1210 _ind1 = _data1->n_fram () - 1;
1211 _btrim1->set_stat (0);
1212 _ttrm10->set_text ("--");
1213 _ttrm11->set_text ("--");
1214 }
1215 else return;
1216 }
1217 else
1218 {
1219 m0 = _editwin->get_markpos (0);
1220 m1 = _editwin->get_markpos (1);
1221 if ((m0 == INT_MIN) || (m1 == INT_MIN)) return;
1222 _btrim1->set_stat (2);
1223 if (m0 < m1) { _trm10 = m0; _trm11 = m1; }
1224 else { _trm10 = m1; _trm11 = m0; }
1225 n = _data1->n_fram () - 1;
1226 _ind1 = (_trm11 > n) ? n : _trm11;
1227 ptime (_trm10, _ttrm10);
1228 ptime (_trm11, _ttrm11);
1229 }
1230 edit_calc_one (0);
1231 }
1232
1233
1234 void Mainwin::edit_fade (bool undo)
1235 {
1236 int i, k;
1237 float v;
1238
1239 if (undo)
1240 {
1241 if (_bfade->stat ())
1242 {
1243 _bfade->set_stat (0);
1244 _ttfade->set_text ("--");
1245 }
1246 else return;
1247 }
1248 else
1249 {
1250 i = _editwin->get_currmark ();
1251 if (i < 0) return;
1252 k = _editwin->get_markpos (i);
1253 if (k < _ind0) return;
1254 if (k > _ind1) return;
1255 if (sscanf (_tslope->text (), "%f", &v) != 1) return;
1256 _slope = v;
1257 _tfade = k;
1258 _bfade->set_stat (2);
1259 ptime (_tfade, _ttfade);
1260 }
1261 edit_calc_one (0);
1262 }
1263
1264
1265 void Mainwin::sint_init ()
1266 {
1267 _subs = (U32)(_data1->drate() / 1000);
1268 _data2 = new Impdata;
1269 _data2->set_type (_data1->type ());
1270 _data2->set_rate (_data1->rate_n (),_data1->rate_d ());
1271 _data2->set_n_sect (_data1->n_sect ());
1272 _data2->set_n_fram (_ind1 - _ind0 + 1);
1273 _data2->set_tref ((U32)(_data1->dtref () / _subs));
1274 _data2->alloc ();
1275 _awfilt = new ACweight ();
1276 _awfilt->init ((int)(_data1->drate ()));
1277 set_disp (_data2, 0, 0);
1278 sint_exec ();
1279 _editwin->update ();
1280 }
1281
1282
1283 void Mainwin::sint_exec ()
1284 {
1285 int c, h, i, j, m;
1286 float *p, *q, v1, v2, v3, s1, s2;
1287
1288 m = _editwin->get_mask ();
1289 c = _data1->n_chan ();
1290 memset (_data2->data (0), 0, 2 * _data2->n_fram () * sizeof (float));
1291 for (j = 0; m; j++, m >>= 1)
1292 {
1293 if (m & 1)
1294 {
1295 p = _data1->data (j);
1296 q = _data2->data (0);
1297 for (i = 0; i < (int)(_data2->n_fram ()); i++)
1298 {
1299 s1 = s2 = 0.0f;
1300 for (h = 0; h < _subs; h++)
1301 {
1302 v1 = *p;
1303 p += c;
1304 _awfilt->process (1, &v1, &v2, &v3);
1305 s1 += v1 * v1;
1306 s2 += v2 * v2;
1307 }
1308 *q++ += s1;
1309 *q++ += s2;
1310 }
1311 }
1312 }
1313
1314 q = _data2->data (0) + 2 * _data2->n_fram ();
1315 s1 = s2 = 0.0f;
1316 for (i = 0; i < (int)(_data2->n_fram ()); i++)
1317 {
1318 s2 += *--q;
1319 *q = s2;
1320 s1 += *--q;
1321 *q = s1;
1322 }
1323 q = _data2->data (0);
1324 s1 = 1.0f / (s1 + 1e-30);
1325 s2 = 1.0f / (s2 + 1e-30);
1326 for (i = 0; i < (int)(_data2->n_fram ()); i++)
1327 {
1328 *q++ *= s1;
1329 *q++ *= s2;
1330 }
1331 }
1332
1333
1334 void Mainwin::ptime (int v, X_textip *T)
1335 {
1336 char s [16];
1337
1338 sprintf (s, "%8.6lf", (v - _offs - _data1->dtref ()) / _data1->drate ());
1339 T->set_text (s);
1340 }
1341
1342
1343 void Mainwin::makewin (void)
1344 {
1345 int i, y;
1346 X_window *W;
1347
1348 _ifwview = W = new X_window (this, 0, 0, 1200, TMAR, XftColors.main_bg->pixel);
1349 Bst0.size.x = 70;
1350 Bst0.size.y = 18;
1351 _bsess = new X_tbutton (W, this, &Bst0, 0, 0, "Session", 0, B_SESS);
1352 _bsess->x_map ();
1353 _bload = new X_tbutton (W, this, &Bst0, 70, 0, "Load", 0, B_LOAD);
1354 _bload->x_map ();
1355 _bsave = new X_tbutton (W, this, &Bst0, 140, 0, "Save", 0, B_SAVE);
1356 _bsave->x_map ();
1357 _bimpt = new X_tbutton (W, this, &Bst0, 210, 0, "Import", 0, B_IMPT);
1358 _bimpt->x_map ();
1359 _bexpt = new X_tbutton (W, this, &Bst0, 280, 0, "Export", 0, B_EXPT);
1360 _bexpt->x_map ();
1361 _bsave->set_stat (3);
1362 _bimpt->set_stat (3);
1363
1364 _tfile = new X_textip (this, 0, &Tst0, 350, 0, XMIN - 350, 18, 127);
1365 _tfile->x_mapraised ();
1366
1367 _bsgen = new X_tbutton (W, this, &Bst0, 0, 18, "Sweep", 0, B_SGEN);
1368 _bsgen->x_map ();
1369 _bcapt = new X_tbutton (W, this, &Bst0, 70, 18, "Capture", 0, B_CAPT);
1370 _bcapt->x_map ();
1371 _bconv = new X_tbutton (W, this, &Bst0, 140, 18, "Convol", 0, B_CONV);
1372 _bconv->x_map ();
1373 _bedit = new X_tbutton (W, this, &Bst0, 210, 18, "Edit", 0, B_EDIT);
1374 _bedit->x_map ();
1375 _bsint = new X_tbutton (W, this, &Bst0, 280, 18, "Integr", 0, B_SINT);
1376 _bsint->x_map ();
1377 _bfilt = new X_tbutton (W, this, &Bst0, 0, 36, "Filter", 0, B_FILT);
1378 _bfilt->x_map ();
1379 _bsint->set_stat (3);
1380 _bfilt->set_stat (3);
1381
1382 _ifwconv = W = new X_window (this, 0, 0, 1200, TMAR, XftColors.main_bg->pixel);
1383 Bst0.size.x = 100;
1384 Bst0.size.y = 18;
1385 (new X_tbutton (W, this, &Bst0, 0, 0, "Cancel", 0, B_CANC))->x_map ();
1386 (new X_tbutton (W, this, &Bst0, 100, 0, "Save section", 0, B_SONE))->x_map ();
1387 (new X_tbutton (W, this, &Bst0, 200, 0, "Apply to all", 0, B_SALL))->x_map ();
1388 addtext (W, 0, 21, 70, 18, "Start time", 1);
1389 addtext (W, 0, 40, 70, 18, "End time", 1);
1390 _tctim1 = new X_textip (W, this, &Tst1, 75, 20, 75, 18, 1023);
1391 _tctim1->set_text ("-1");
1392 _tctim1->x_map ();
1393 _tctim2 = new X_textip (W, this, &Tst1, 75, 39, 75, 18, 1023, _tctim1);
1394 _tctim2->set_text ("1");
1395 _tctim2->x_map ();
1396 addtext (W, 160, 20, 60, 18, "Sweep", 1);
1397 Bst0.size.x = 17;
1398 Bst0.size.y = 17;
1399 (new X_ibutton (W, this, &Bst0, 225, 20, disp ()->image1515 (X_display::IMG_DN), B_FILE))->x_map ();
1400 _tcfil1 = new X_textip (W, this, &Tst1, 250, 20, 250, 18, 1023, _tctim2);
1401 _tcfil1->set_align (1);
1402 _tcfil1->x_map ();
1403 Bst0.size.x = 70;
1404 Bst0.size.y = 18;
1405 _bappl = new X_tbutton (W, this, &Bst0, 430, 39, "Apply", 0, B_APPL);
1406 _bappl->x_map ();
1407
1408 _ifwedit = W = new X_window (this, 0, 0, 1200, TMAR, XftColors.main_bg->pixel);
1409 Bst0.size.x = 100;
1410 Bst0.size.y = 18;
1411 (new X_tbutton (W, this, &Bst0, 0, 0, "Cancel", 0, B_CANC))->x_map ();
1412 (new X_tbutton (W, this, &Bst0, 100, 0, "Save section", 0, B_SONE))->x_map ();
1413 (new X_tbutton (W, this, &Bst0, 200, 0, "Apply to all", 0, B_SALL))->x_map ();
1414 Bst0.size.x = 80;
1415 Bst0.size.y = 18;
1416 _ttref = new X_textip (W, 0, &Tst1, 5, 20, 90, 18, 1023, 0);
1417 _ttref->x_map ();
1418 _btref = new X_tbutton (W, this, &Bst0, 97, 20, "Time ref", 0, B_TREF);
1419 _btref->x_map ();
1420 _tgain = new X_textip (W, this, &Tst1, 5, 39, 90, 18, 1023, 0);
1421 _tgain->x_map ();
1422 _bgain = new X_tbutton (W, this, &Bst0, 97, 39, "Gain / Norm", 0, B_GAIN);
1423 _bgain->x_map ();
1424 _ttrm00 = new X_textip (W, 0, &Tst1, 200, 20, 80, 18, 1023, 0);
1425 _ttrm00->x_map ();
1426 _ttrm01 = new X_textip (W, 0, &Tst1, 281, 20, 80, 18, 1023, 0);
1427 _ttrm01->x_map ();
1428 _btrim0 = new X_tbutton (W, this, &Bst0, 363, 20, "Trim start", 0, B_TRM0);
1429 _btrim0->x_map ();
1430 _ttrm10 = new X_textip (W, 0, &Tst1, 200, 39, 80, 18, 1023, 0);
1431 _ttrm10->x_map ();
1432 _ttrm11 = new X_textip (W, 0, &Tst1, 281, 39, 80, 18, 1023, 0);
1433 _ttrm11->x_map ();
1434 _btrim1 = new X_tbutton (W, this, &Bst0, 363, 39, "Trim end", 0, B_TRM1);
1435 _btrim1->x_map ();
1436 _ttfade = new X_textip (W, 0, &Tst1, 460, 20, 80, 18, 1023, 0);
1437 _ttfade->x_map ();
1438 _tslope = new X_textip (W, this, &Tst1, 541, 20, 80, 18, 1023, 0);
1439 _tslope->x_map ();
1440 _bfade = new X_tbutton (W, this, &Bst0, 623, 20, "Fade out", 0, B_FADE);
1441 _bfade->x_map ();
1442
1443 _ifwsint = W = new X_window (this, 0, 0, 1200, TMAR, XftColors.main_bg->pixel);
1444 (new X_tbutton (W, this, &Bst0, 0, 0, "Back", 0, B_CANC))->x_map ();
1445
1446 _ifwfilt = W = new X_window (this, 0, 0, 1200, TMAR, XftColors.main_bg->pixel);
1447 (new X_tbutton (W, this, &Bst0, 0, 0, "Back", 0, B_CANC))->x_map ();
1448
1449 addtext (_editwin->bbwin (), 10, 3, 55, 18, "Section:", -1);
1450 _tfrag = new X_textip (_editwin->bbwin (), 0, &Tst0, 70, 3, 70, 17, 15);
1451 _tfrag->x_map ();
1452 _tfrag->set_text ("- / -");
1453 Bst0.size.x = 24;
1454 Bst0.size.y = 17;
1455 _bfrmi = new X_ibutton (_editwin->bbwin (), this, &Bst0, 145, 3, disp ()->image1515 (X_display::IMG_LT), B_FRMI);
1456 _bfrpl = new X_ibutton (_editwin->bbwin (), this, &Bst0, 170, 3, disp ()->image1515 (X_display::IMG_RT), B_FRPL);
1457
1458 Bst0.size.x = 35;
1459 Bst0.size.y = 18;
1460 y = 40;
1461 for (i = 0; i < 4; i++)
1462 {
1463 _bchan [i] = new X_tbutton (_editwin->rbwin (), this, &Bst0, 1, y, "-", 0, B_CHAN + i);
1464 _bchan [i]->x_set_win_gravity (NorthGravity);
1465 y += 18;
1466 }
1467 }
1468
1469
1470 void Mainwin::addtext (X_window *W, int xp, int yp, int xs, int ys, const char *text, int align)
1471 {
1472 (new X_textln (W, &Tst0, xp, yp, xs, ys, text, align))->x_map ();
1473 }
1474
1475
1476 void Mainwin::askdata (X_textip *T)
1477 {
1478 T->set_text ("???");
1479 XSetInputFocus (dpy (), T->win (), RevertToPointerRoot, CurrentTime);
1480 }
1481
1482
1483 void Mainwin::fileinfo (void)
1484 {
1485 char s [256];
1486 char *p;
1487
1488 if (!_data1) return;
1489 p = strrchr (_file1, '/');
1490 if (p) p++;
1491 else p = _file1;
1492 snprintf (s, 256, "%s %3.1lf %d * %d", p, _data1->drate (), _data1->n_sect (), _data1->n_fram ());
1493 _tfile->set_text (s);
1494 _base1 = p;
1495 }
1496