-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbookedit.m
1897 lines (1668 loc) · 71.8 KB
/
bookedit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = bookedit( book, channel, bwfactor )
% Usage :
% bookedit
% bookedit(‘mptkBook.bin’)
% bookedit(book,chan)
%
% Synopsis :
% Interface for plotting and editing a Matching Pursuit book
%
% Detailed description :
% * bookedit asks the user for a MPTK binary book file and plots it. You can set a ‘.mat’
% variable called ‘MPTKdir.mat’ containing the full path to your book directory:
% – bookdir = ‘/pathToMptk/book/’;
% – save ‘MPTKdir.mat’ bookdir
% * bookedit(‘mptkBook.bin’) loads the book“mptkbook.bin”
% * bookedit(book,chan) reads the channel number “chan” of a “book” structure in the
% current axes. If it is a string, it is understood as a filename and the book is read
% from the corresponding file. Books can be read separately using the bookread utility.
%
% Notes :
% The patches delimit the support of the atoms. Their color is proportional to the atom’s
% amplitudes, mapped to the current colormap and the current caxis.
%% Authors:
% Gilles Gonon
% Remi Gribonval
% Copyright (C) 2008 IRISA
%
% This script is part of the Matching Pursuit Library package,
% distributed under the General Public License.
%
%% SVN log:
% $Author: gonon $
% $Date: 2008-02-14 18:00:30 +0100 (Wed, 14 Feb 2008) $
% $Revision: 783 $
%
%% INITIALIZATIONS
% - Load a book
% - Define Figure Menus - toolbar
% - define data structure stored in GUI figure
%
% Check if a bookDir has already been provided bu user
bdf = 'MPTKdir.mat'; % book dir file
if (exist(bdf,'file')==2)
bd = load(bdf);
else
disp([ 'You can set a default directory for opening books in a variable called ' ...
'''MPTKdir.mat'' containing' 10 ...
'the full path to your book directory: ' 10 'bookdir = ''/my/path/to/mptk/book/'';' 10 ...
'save ''MPTKdir.mat'' bookdir' ]);
bd.bookdir = pwd;
end
if nargin<1
loadBook(bd.bookdir);
return;
end
%% Test input args
if ischar(book),
disp('Loading the book...');
[p,n] = fileparts(book);
data.loadBookDir = p;
book = bookread( book );
% Record path of the book for open book
disp('Done.');
end;
% Add Fields in structure for graphical / selection information to atoms
book = addMatlabBookFields(book);
if nargin < 2,
channel = 1;
end;
if channel > book.numChans,
error('Book has %d channels. Can''t display channel number %d.', ...
channel, book.numChans );
end;
if nargin < 3,
bwfactor = 2;
end;
% ------------------------
% General Variables
nS = book.numSamples;
fs = book.sampleRate;
%----------------------------
% General GUI variables
bHeight = 0.05;
vSpace = 0.01;
bgColor = [0.7 0.8 0.9];
fgColor = [0.8 0.9 1];
%% ------------------------------
% Main window
% -----------
figH = figure( ...
'Name','MPTK - Visualisation and edition of books', ...
'NumberTitle','off', ...
'Backingstore','off', ...
'Units','normalized', ...
'Position',[ .1 .15 .8 .7 ], ...
'Color',bgColor, ...
'MenuBar','none',...
'Visible','on');
if (nargout == 1)
varargout(1) = { figH };
end
%% ---------------------
% Figure menus
% ------------
item = 1;
% menuItem(1) : 'File' menu
menuItem(item) = uimenu(figH,'Label','&File','Callback','');
item = item + 1;
% menuItem(2) : 'Edit' menu
menuItem(item) = uimenu(figH,'Label','&Edit','Callback','');
item = item + 1;
% menuItem(3) : 'Transform' menu
menuItem(item) = uimenu(figH,'Label','&Tranform','Callback','');
item = item + 1;
% menuItem(4) : 'Help' menu
menuItem(item) = uimenu(figH,'Label','&Help','Callback','');
item = item + 1;
% 'File' sub items
menuItem(item) = uimenu(menuItem(1),'Label','&Open book','Callback',@loadBook,'Separator','off','Accelerator','O');
item = item + 1;
menuItem(item) = uimenu(menuItem(1),'Label','&Save selection to book','Callback',@saveSelectedBook,'Separator','off','Accelerator','S');
item = item + 1;
menuItem(item) = uimenu(menuItem(1),'Label','&Save visible to book','Callback',@saveVisibleBook,'Separator','off','Accelerator','V');
item = item + 1;
menuItem(item) = uimenu(menuItem(1),'Label','&Close window','Callback','close(gcf)','Separator','on','Accelerator','W');
item = item + 1;
% 'Edit' subitems
menuItem(item) = uimenu(menuItem(2),'Label','Select &All','Callback',@selectAll,'Separator','off','Accelerator','A');
item = item + 1;
menuItem(item) = uimenu(menuItem(2),'Label','Select &None (clear selection)','Callback',@selectNone,'Separator','off','Accelerator','N');
item = item + 1;
menuItem(item) = uimenu(menuItem(2),'Label','Cut selected atoms','Callback',@cutSelection,'Separator','on','Accelerator','X');
item = item + 1;
menuItem(item) = uimenu(menuItem(2),'Label','&Keep only selected atoms','Callback',@keepSelection,'Separator','off','Accelerator','K');
item = item + 1;
menuItem(item) = uimenu(menuItem(2),'Label','&Move selected atoms','Callback',@moveSelection,'Separator','off','Accelerator','B');
item = item + 1;
menuItem(item) = uimenu(menuItem(2),'Label','&Export selection to Anywave','Callback',@exportAnywave,'Separator','on','Accelerator','E','Enable','off');
item = item + 1;
menuItem(item) = uimenu(menuItem(2),'Label','&Refresh figure','Callback',@refreshFigure,'Separator','on','Accelerator','R');
item = item + 1;
% 'Transform' subitems
menuItem(item) = uimenu(menuItem(3),'Label','&Pitch Shift ...','Callback',@pitchShift,'Separator','off','Accelerator','P');
item = item + 1;
menuItem(item) = uimenu(menuItem(3),'Label','&Time Scale ...','Callback',@timeStretch,'Separator','off','Accelerator','T');
item = item + 1;
menuItem(item) = uimenu(menuItem(3),'Label','Apply &Gain on selection ...','Callback',@applyGain,'Separator','off','Accelerator','G');
item = item + 1;
menuItem(item) = uimenu(menuItem(3),'Label','T&ime Reverse selection ...','Callback',@timeReverse,'Separator','off','Accelerator','I','Enable','off');
item = item + 1;
menuItem(item) = uimenu(menuItem(3),'Label','&Freq reverse selection ...','Callback',@freqReverse,'Separator','off','Accelerator','F','Enable','off');
item = item + 1;
menuItem(item) = uimenu(menuItem(3),'Label','Te&mpo detection ...','Callback',@tempoDetect,'Separator','on','Accelerator','M','Enable','off');
item = item + 1;
% 'Help subitems'
menuItem(item) = uimenu(menuItem(4),'Label','Help','Callback','doc bookedit_exp','Separator','off');
item = item + 1;
menuItem(item) = uimenu(menuItem(4),'Label','About BOOKEDIT','Callback',@aboutBookedit,'Separator','on');
item = item + 1;
%% ------------------------------
% Add toolbar buttons (icons)
% --------
if exist('MPtoolbaricons.mat','file')==2
icons = load('MPtoolbaricons.mat');
else
icons.playsound = rand(16,16,3);
icons.zoomx = rand(16,16,3);
icons.zoomy = rand(16,16,3);
icons.zoomplus = rand(16,16,3);
icons.fullview = rand(16,16,3);
icons.playselected = rand(16,16,3);
icons.selectadd = rand(16,16,3);
icons.selectremove = rand(16,16,3);
icons.open_hand = rand(16,16,3);
end
toolH(1) = uitoolbar(figH);
% Play visible atoms
toolH(end+1) = uipushtool(toolH(1),'CData',icons.playsound,'TooltipString','Play visible atom sound',...
'ClickedCallback',@playvisiblesound);
% Play selected atoms
toolH(end+1) = uipushtool(toolH(1),'CData',icons.playselected,'TooltipString','Play slected atoms sound',...
'ClickedCallback',@playselectedsound);
% Select atoms
toolH(end+1) = uitoggletool(toolH(1),'CData',icons.selectadd,'TooltipString','Select Atoms',...
'OnCallback',@toggleOnSelectAtoms,...
'OffCallback',@clearMouseFcn);
% Unselect atoms
%toolH(end+1) = uitoggletool(toolH(1),'CData',icons.selectremove,'TooltipString','UnSelect Atoms',...
% 'Enable','off',...
% 'OnCallback',@toggleOnUnSelectAtoms,...
% 'OffCallback',@clearMouseFcn);
% Zoom out full
toolH(end+1) = uipushtool(toolH(1),'CData',icons.fullview,'TooltipString','Zoom out full',...
'ClickedCallback',@zoomOutFull);
% Zoom horizontal
toolH(end+1) = uitoggletool(toolH(1),'CData',icons.zoomx,'TooltipString','Zoom horizontal',...
'OnCallback',@zoomHorizontal,...
'OffCallback','zoom off','Separator','on');
% Zoom vertical
toolH(end+1) = uitoggletool(toolH(1),'CData',icons.zoomy,'TooltipString','Zoom vertical',...
'OnCallback',@zoomVertical,...
'OffCallback','zoom off');
% Zoom in
toolH(end+1) = uitoggletool(toolH(1),'CData',icons.zoomplus,'TooltipString','Zoom',...
'OnCallback',@zoomIn,...
'OffCallback','zoom off');
% Drag plot
toolH(end+1) = uitoggletool(toolH(1),'CData',icons.open_hand,'TooltipString','Grab and pan',...
'OnCallback',@panPlot,...
'OffCallback','pan off');
%% -------------------------------
% Main Axes
% ---------
axH = zeros(1,book.numChans);
vspace = 0.05; % vertical space between 2 axes
axHeight = (1-(book.numChans+1)*vspace ) / book.numChans; % axes Height
data.colorAmpdB = [-150 150]; % Min and Max atom amplitudes in dB
colorTick = linspace(data.colorAmpdB(1),data.colorAmpdB(2),5);
for ac = 1:book.numChans
axH(ac) = axes( ...
'Units','normalized', ...
'Position',[0.2675 ( vspace*ac +axHeight*(ac-1) ) 0.7 axHeight ], ...
'Drawmode','fast', ...
'Layer','top', ...
'Visible','on');
colorMapH(ac) = colorbar('peer',axH(ac));
set(colorMapH(ac),'ButtonDownFcn','colormapeditor');
end
%% -----------------------------------
% UI FRAME for BOOK INFO - QUERIES
% ----------------------
leftPanelH = uipanel('Title','Book Info/Selection','FontSize',12,...
'BackgroundColor',bgColor,...
'Position',[0.015 0.05 0.2 0.9]);
%---------------------------------
% Check boxes with type of atoms
%---------------------------------
data.typeHandles = addCheckBoxTypes(book,figH);
%-----------------------------------------------------
% Plot the atoms and get the handles to all the atoms
% -----------------
data.atomHandles = plotBook(book,axH);
for ac = 1:book.numChans
axH(ac);
axis([0 nS/fs 0 fs/2]);
end
%% ---------------------------------------------------------
% Application data : saving all needed variables in figure
% -----------------
data.selectAtoms = 0; % Bool for start drag selection of atoms
data.begSelectAtoms = []; % Begining of rectangle selection
data.endSelectAtoms = []; % End of rectangle selection
data.atomSelection = []; % Stack of rectangle selections (for undo)
data.atomUnSelection = []; % Stack of rectangle selections (for undo)
data.indAtomsSelected = []; % Indexes of selected atoms in book
data.curRectH = -1; % Current rectangle handle
data.rectSelH = []; % Vector of Selection handles
data.toolH = toolH; % Vector of toolbar icons handles
data.book = book; % Book data
data.axH = axH; % Vector of channel axes handles
data.colorMapH = colorMapH; % Vector of colormap handles
data.colorAmpdB = [-150 150]; % Min and Max atom amplitudes in dB
if (~isfield(data,'loadBookDir'))
data.loadBookDir = pwd;
end
data.saveBookDir = data.loadBookDir;
set(figH,'UserData',data)
% END OF INITIALIZATIONS
%-------------------------
%% CALLBACK FUNCTIONS
% -------------------
%% FIGURE MENUS CALLBACKS
% -------------------------
%% OPEN AND LOAD A BOOK
% This load function can be called at different times:
% - At Initialisation with a directory (possibly saved in MPTKbookdir.mat)
% - from uimenu 'Open Book ...'
% - From a subfunction (typically after an operation on atoms)
function loadBook(varargin)
curFig = gcf;
data = get(gcf,'UserData');
curdir = pwd; % Save current directory
% Just check if interface is already created or start program
if (~isfield(data,'book'))
close(curFig);
cd(varargin{1})
[filename, pathname] = uigetfile({'*.bin';'*.txt';'*.xml'},'Open a book file');
else
if (nargin==2) % Call from uimenu 'open book'
% Ask the user a book name
if (exist(data.loadBookDir,'dir') == 7) % sometimes this variable is lost ...
cd(data.loadBookDir);
end
[filename, pathname] = uigetfile({'*.bin';'*.txt';'*.xml'},'Open a book file');
else % Call from a subfunction
[pathname,fn,e] = fileparts(varargin{1});
filename = [ fn e ];
end
end
cd(curdir); % Back to current directory
if (filename)
if (~isempty(pathname))
data.loadBookDir = pwd;
else
data.loadBookDir = pathname;
end
bookname = fullfile(pathname,filename);
if (exist(bookname,'file')==2)
disp('Loading the book...');
newFig = bookedit_exp( bookname );
data = get(newFig,'UserData');
data.loadBookDir = pathname;
set(newFig,'UserData',data);
if (curFig ~= newFig)
close(curFig);
end
else % Bookname does not exists, save book directory path (the rest is unchanged)
set(gcf,'UserData',data);
end
end
end
%% SAVE VISIBLE ATOMS AS A NEW BOOK
function saveVisibleBook(varargin)
data = get(gcf,'UserData');
data.book.index(4,:) = 0;
% Copy visible info into index
for t = 1:length(data.book.atom)
data.book.index(4,data.book.index(2,:)==t) = data.book.atom(t).visible;
end
% Write book
newSaveDir = writeBook(data.book,data.saveBookDir);
if (~isempty(newSaveDir))
data.saveBookDir = newSaveDir;
set(gcf,'UserData',data)
end
end
%% SAVE SELECTED ATOMS AS A NEW BOOK
function saveSelectedBook(varargin)
data = get(gcf,'UserData');
data.book.index(4,:) = 0;
% Copy selected info into index
for t = 1:length(data.book.atom)
data.book.index(4,data.book.index(2,:)==t) = data.book.atom(t).selected;
end
% Write book
newSaveDir = writeBook(data.book,data.saveBookDir);
if (~isempty(newSaveDir))
data.saveBookDir = newSaveDir;
set(gcf,'UserData',data)
end
end
%%
function selectAll(varargin)
data = get(gcf,'UserData');
% Set atom.selected field to one
for t = 1:length(data.book.atom)
[n,m] = size(data.book.atom(t).selected);
data.book.atom(t).selected = ones(n,m);
end
set(gcf,'UserData',data)
end
function selectNone(varargin)
data = get(gcf,'UserData');
% Clear selection rectangles
delete(data.rectSelH(ishandle(data.rectSelH)));
% Erase data info
data.selectAtoms = 0; % Bool for start drag selection of atoms
data.begSelectAtoms = []; % Begining of rectangle selection
data.endSelectAtoms = []; % End of rectangle selection
data.atomSelection = []; % Stack of rectangle selections (for undo)
data.atomUnSelection = []; % Stack of rectangle selections (for undo)
data.indAtomsSelected = []; % Indexes of selected atoms in book
data.curRectH = -1; % Current rectangle handle
data.rectSelH = []; % Vector of Selection handles
data.rectUnSelH = []; % Vector of Selection handles
% Set book selected to 0
% Copy visible info into index
for t = 1:length(data.book.atom)
[n,m] = size(data.book.atom(t).selected);
data.book.atom(t).selected = zeros(n,m);
end
set(gcf,'UserData',data)
end
function cutSelection(varargin)
data = get(gcf,'UserData');
% Copy selected info into index
for t = 1:length(data.book.atom)
data.book.index(4,data.book.index(2,:)==t) = data.book.atom(t).selected;
end
% Get selected atom entries in index (atoms to remove)
selectIndex = find(data.book.index(4,:)==1);
% Remove atoms given index
data.book = removeBookAtom(data.book,selectIndex);
set(gcf,'UserData',data);
refreshFigure();
% Clear selection
selectNone();
end
function keepSelection(varargin)
% disp('keepSelection() - Not implemented')
data = get(gcf,'UserData');
% Copy selected info into index
for t = 1:length(data.book.atom)
data.book.index(4,data.book.index(2,:)==t) = data.book.atom(t).selected;
end
% Get non selected atom entries in index
selectIndex = find(data.book.index(4,:)~=1);
% Remove atoms given index
data.book = removeBookAtom(data.book,selectIndex);
set(gcf,'UserData',data);
refreshFigure();
% Clear selection
selectNone();
end
function moveSelection(varargin)
% Get figure data
data = get(gcf,'UserData');
% Disable toolbar function
toggleToolbar();
zoom off;
% Set drag function in rectangle handles
for k = 1:length(data.rectSelH)
set(data.rectSelH,'ButtonDownFcn',@startDragRect)
end
end
function exportAnywave(varargin)
disp('exportAnywave() - Not implemented')
end
function aboutBookedit(varargin)
disp('aboutBookedit() - Not implemented')
end
%% TOOLBAR ICONS CALLBACKS
% -------------------------
function playvisiblesound(varargin)
data = get(gcf,'UserData');
data.book.index(4,:) = 0;
% Copy visible info into index
for t = 1:length(data.book.atom)
data.book.index(4,data.book.index(2,:)==t) = data.book.atom(t).visible;
end
playBook(data.book);
end
function playselectedsound(varargin)
data = get(gcf,'UserData');
data.book.index(4,:) = 0;
% Copy selected info into index
for t = 1:length(data.book.atom)
data.book.index(4,data.book.index(2,:)==t) = data.book.atom(t).selected;
end
playBook(data.book);
end
function toggleOnSelectAtoms(varargin)
toggleToolbar();
zoom off;
set(gcf,'WindowButtonDownFcn',@startSelectRect);
set(gcf,'WindowButtonUpFcn',@stopSelectRect);
set(gcf,'WindowButtonMotionFcn',@dragSelectRect);
end
%% Remove action affected to mouse events
function clearMouseFcn(varargin)
set(gcf,'WindowButtonDownFcn',[]);
set(gcf,'WindowButtonUpFcn',[]);
set(gcf,'WindowButtonMotionFcn',[]);
end
%% Toolbar callback horizontal zoom In
function zoomHorizontal(varargin)
toggleToolbar();
zoom xon;
end
%% Toolbar callback vertical zoom In
function zoomVertical(varargin)
toggleToolbar();
zoom yon;
end
%% Toolbar callback grab plot
function panPlot(varargin)
toggleToolbar();
pan on;
end
%% Toolbar callback zoom in
function zoomIn(varargin)
toggleToolbar();
zoom on;
end
%% Toolbar callback zoom out full
function zoomOutFull(varargin)
toggleToolbar();
zoom out;
end
%% Checkbox callback for show/hide all atom types
function toggleViewAllAtom(varargin)
data = get(gcf,'UserData');
val = get(data.typeHandles(1),'Value'); % Get checkbox 'hide/show all' value
for th=2:length(data.typeHandles)
set(data.typeHandles(th),'Value',val);
toggleViewAtomLength(data.typeHandles(th));
end
end
%% Checkbox callback for show/hide an atom type
function toggleViewAtomType(varargin)
cbH = gcbo; % Chekbx handles
data = get(gcf,'UserData');
type = get(varargin{1},'Tag');
val = get(varargin{1},'Value');
idx = getTypeIndex(data.book,type);
indCbH = find(data.typeHandles==cbH); % Index of current handle in vector of handles
if (length(idx)==1)
set(data.typeHandles(indCbH),'Value',val);
toggleViewAtomLength(data.typeHandles(indCbH));
else
for i=1:length(idx) % type found at idx
set(data.typeHandles(indCbH+i),'Value',val);
toggleViewAtomLength(data.typeHandles(indCbH+i));
end
end
end
%% Checkbox callback for show/hide an atom type
function toggleViewAtomLength(varargin)
data = get(gcf,'UserData');
type = get(varargin{1},'Tag');
len = get(varargin{1},'String');
val = get(varargin{1},'Value');
% In case there is only one scale
if (isempty(str2num(len)))
idx = getTypeIndex(data.book,type);
else
idx = getTypeIndex(data.book,type,str2num(len));
end
for i = 1:length(idx) % type found at idx
[nA nC] = size(data.book.atom(idx(i)).params.amp);
if (val) % Show atom with type
state = 'on';
data.book.atom(idx(i)).visible = ones(nA,nC);
else % Hide atom with type
state = 'off';
data.book.atom(idx(i)).visible = zeros(nA,nC);
end
% If the atom was rendered, set is visible status
if (ishandle(data.atomHandles(idx(i))) )
set(data.atomHandles(idx(i)),'Visible',state);
end
set(gcf,'UserData',data);
end
end
%% Transform Menu callbacks (TODO)
% -----------------------------
%% Apply a Gain to selection
function applyGain(varargin)
prompt = {'Enter the gain to apply to selected atom in dB:'};
name = 'Apply gain to selection';
numlines = 1;
defaultanswer = {'3'};
val = inputdlg(prompt,name,numlines,defaultanswer);
gaindB=str2double(val);
if (~isempty(gaindB))
if isnan(gaindB),
errordlg('You must enter a numeric value','Bad Input','modal');
else
data = get(gcf,'UserData');
% Convert gain in dB to linear scale
gain = 10.^(gaindB/20);
% Browse atoms type and apply gain on selected atoms
for t = 1:length(data.book.atom)
data.book.atom(t).params.amp(data.book.atom(t).selected==1,:) = data.book.atom(t).params.amp(data.book.atom(t).selected==1,:) * gain;
end
set(gcf,'UserData',data);
refreshFigure();
end
end
end
%% Pitch Shift transformation (NOTE: rewrite inputPitchShift() and applyPitchShift() as for TimeStretch )
% This function only opens a GUI which ask the user for the transform parameters
function pitchShift(varargin)
figBookedit = gcbf;
% Get input arguments
d = inputPitchShift();
uiwait(d);
if (ishandle(d)) % OK button pushed
args = get(d,'UserData');
close(d);
% Core function for applying time Stretch
data = get(figBookedit,'UserData');
data.book = applyPitchShift(data.book,args);
set(figBookedit,'UserData',data);
refreshFigure();
end
end
%% Time stretch transformation
% This function only opens a GUI which ask the user for transform parameters
function timeStretch(varargin)
figBookedit = gcbf;
% Get input arguments
d = inputTimeStretch();
uiwait(d);
if (ishandle(d)) % OK button pushed
args = get(d,'UserData');
close(d);
% Core function for applying time Stretch
data = get(figBookedit,'UserData');
data.book = applyTimeStretch(data.book,args);
set(figBookedit,'UserData',data);
refreshFigure();
end
end
%% Reverse time for selected atoms in each rectangle
function timeReverse(varargin)
disp('timeReverse() - not implemented')
end
%% Reverse frequency for the selected atoms in each rectangle
function freqReverse(varargin)
disp( 'freqReverse() - not implemented')
end
%% Detect tempo - todo
% Open a gui with a slidebar for interaction with the user
% Hint : use histograms on atoms position occurence shows a clear view of note occurences
function tempoDetect(varargin)
disp( 'tempoDetect() - not implemented')
end
%% OTHER SUB FUNCTIONS
% These functions are not direct callbacks but are called by the different
% callbacks
% ----------------------
%% Get index entries of visible atoms (in all atom type)
function index = indexOfVisible(book)
index = zeros(1,book.numAtoms);
for t = 1:length(book.atom)
index(1,book.index(2,:)==t) = book.atom(t).visible;
end
end
%% Get index entries of selected atoms (in all atom type)
function index = indexOfSelected(book)
index = zeros(1,book.numAtoms);
for t = 1:length(book.atom)
index(1,book.index(2,:)==t) = book.atom(t).visible;
end
end
%% Write a MPTK binary book file - user is asked for the book name
function newsavedir = writeBook(book,defaultDir)
newsavedir = [];
nAtom = sum(book.index(4,:));
if (nAtom) % Check that there is non zero atom in book
curDir = cd; % save current directory
cd(defaultDir);
[filename, pathname] = uiputfile( {'*.bin;*.txt','MPTK book-files (*.bin)'}, ...
'Save Atoms in book', [ 'book_' num2str(nAtom) 'atoms.bin']);
cd(curDir); % return in current directory
if (filename)
newsavedir = pathname;
bookwrite(book,fullfile(pathname,filename));
end
else
warndlg('No Atom is selected, nothing to save in book', 'Book save info', 'modal');
end
end
%% Reconstruct and Play Book as a sound
function playBook(book)
if (book.index(4,:)== 0)
disp('There are no selected atoms for reconstruction')
else
% signal = mpReconstruct(book);
signal = mprecons(book);
if (~isempty(signal))
soundsc(signal,book.sampleRate);
if(exist('PlotSoundJava'))
PlotSoundJava(signal,book.sampleRate);
else
disp('Warning: PlotSoundJava not found');
end
end
end
end
%% Convert Current point coordinates in Figure to axis cartesian position
function [x,y] = figToAxe(curpoint)
pa = get(gca,'Position'); % Must be normalized position
lim = axis(gca);
x = (curpoint(1)-pa(1)) / pa(3);
y = (curpoint(2)-pa(2)) / pa(4);
x = x * (lim(2)-lim(1)) + lim(1);
y = y * (lim(4)-lim(3)) + lim(3);
end
%% START TO SELECT ATOMS RECTANGLE (MOUSE CLICK)
function startSelectRect(varargin)
data = get(gcf,'UserData');
if (gco==gca) % Click on axis or one of its children
data.selectAtoms = 1;
% Get and check current mouse coordinates in axis
xy = get(varargin{1},'CurrentPoint');
[x,y] = figToAxe(xy);
data.begSelectAtoms = [x y];
axl = axis(gca);
rpos = [ x y 1e-6 1e-6 ];
data.curRectH = rectangle('Position', rpos,'faceColor',[0.9 0.6 0.9]);
% Swap Depth of plot (set rectangle==axChild(1) at the end of the
% gcf children handles)
axChild = get(gca,'Children');
set(gca,'Children',[axChild(axChild~=data.curRectH); axChild(axChild==data.curRectH)] );
end
set(gcf,'UserData',data)
end
%% STOP TO SELECT ATOMS RECTANGLE (MOUSE RELEASE)
function stopSelectRect(varargin)
axChild = get(gca,'Children');
if ( (gco==gca) || sum(gco==axChild) )
data = get(gcf,'UserData');
% Update userdata
data.selectAtoms = 0; % realeased
% Copy rectangle handle to vector of selection handles
if (ishandle(data.curRectH))
data.rectSelH(end+1) = data.curRectH;
data.curRectH = -1;
end
% Get and check current mouse coordinates in axis
xy = get(varargin{1},'CurrentPoint');
[x,y] = figToAxe(xy);
% Update data information
rpos = [ min(data.begSelectAtoms(1),x) max(data.begSelectAtoms(1),x) ...
min(data.begSelectAtoms(2),y) max(data.begSelectAtoms(2),y)];
data.atomSelection(end+1,:) = rpos;
set(gcf,'UserData',data);
updateAtomSelection(rpos);
end
end
%% DRAG SELECTION ATOMS RECTANGLE - (MOUSE DRAG)
function dragSelectRect(varargin)
if (~isempty(gco))
data = get(gcf,'UserData');
axChild = get(gca,'Children');
if ( (gco==gca) || sum(gco==axChild) )
if (data.selectAtoms==1)
rpos = get(data.curRectH,'Position');
xy = get(varargin{1},'CurrentPoint');
[x,y] = figToAxe(xy);
axlim = axis();
if (x~=data.begSelectAtoms(1) && y~=data.begSelectAtoms(2) && ...
x>=(axlim(1)-0.1) && x<=(axlim(2)+0.1) && y>=(axlim(3)-10) && y<=(axlim(4)+10) )
newrpos = [ min(data.begSelectAtoms(1),x) min(data.begSelectAtoms(2),y) ...
abs(data.begSelectAtoms(1)-x) abs(data.begSelectAtoms(2)-y) ];
set(data.curRectH,'Position',newrpos);
end
end
end % if gco==gca
end
end
%% START DRAG SELECTION RECTANGLE AND THE ATOMS INSIDE
function startDragRect(varargin)
% Save current clicked object handle (rectangle if this function
% is called)
rH = gcbo;
data = get(gcbf,'UserData');
data.curRectH = rH;
xy = get(gcbf,'CurrentPoint');
[x,y] = figToAxe(xy);
rpos = get(rH,'Position');
data.mouseInitPos = [ x y ]; % Store mouse initial position to get dX and dY of moves
data.rectInitPos = [rpos(1) rpos(2)]; % Initial rectangle position
data.textH = text(rpos(1),data.book.sampleRate*.49,1000,'dT = 0, dF = 0','Fontsize',14,'BackgroundColor',[ 1 0.7 0.9 ],'EdgeColor',[ 0 0 0 ]);
% Find atom in selected rectangle and plot them
set(gcbf,'UserData',data);
[data.indAtomsSelected,atomBounds] = findAtomInRect(rpos); % THIS FUNCTION DOES NOT WORK !!!
if ~isempty(atomBounds)
pX = [ atomBounds(1,:)' atomBounds(1,:)' atomBounds(2,:)' atomBounds(2,:)' ]';
pY = [ atomBounds(3,:)' atomBounds(4,:)' atomBounds(4,:)' atomBounds(3,:)' ]';
pZ = 1000*ones(size(pX));
pC = ones(size(pX));
data.curAtomsH = patch(pX,pY,pZ,'k');
data.initPX = pX;
data.initPY = pY;
set(gcbf,'UserData',data);
set(gcf,'WindowButtonUpFcn',@stopDragRect);
set(gcf,'WindowButtonMotionFcn',@moveDragRect);
else
warndlg('Selection contains no atoms', 'Warning - empty selection', 'modal');
end
end
%% MOVE SELECTION RECTANGLE AND THE ATOMS INSIDE
function moveDragRect(varargin)
data = get(gcbf,'UserData');
xy = get(gcbf,'CurrentPoint');
[x,y] = figToAxe(xy);
axlim = axis();
rpos = get(data.curRectH,'Position');
% Update X & Y position of rectangle
dMove = [x y] - data.mouseInitPos;
rpos(1) = data.rectInitPos(1) + dMove(1);
rpos(2) = data.rectInitPos(2) + dMove(2);
pX = get(data.curAtomsH,'XData');
pY = get(data.curAtomsH,'YData');
set(data.curAtomsH,'XData',data.initPX+dMove(1));
set(data.curAtomsH,'YData',data.initPY+dMove(2));
set(data.textH,'String',['dT = ' num2str(dMove(1)) ', dF = ' num2str(dMove(2)) ])
set(data.curRectH,'Position',rpos);
end
%% END OF DRAG RECTANGLE SELECTION
function stopDragRect(varargin)
set(gcbf,'WindowButtonUpFcn',[]);
set(gcbf,'WindowButtonMotionFcn',[]);
data = get(gcbf,'UserData');
% Get Delta X and delta Y at mouse release
xy = get(gcbf,'CurrentPoint');
[x,y] = figToAxe(xy);
dMove = [x y] - data.mouseInitPos;
% Normalize dMove units
fs = data.book.sampleRate;
dMove = [ round(dMove(1)*fs) dMove(2)/fs ];
% Translate atoms frequency and position
ind = find(data.indAtomsSelected==1);
for i = 1:length(ind)
idx = ind(i);
aType = data.book.index(2,idx);
aNum = data.book.index(3,idx);
% Shift time 'pos'
data.book.atom(aType).params.pos(aNum,:) = data.book.atom(aType).params.pos(aNum,:) + dMove(1);
data.book.index(5:end,idx) = data.book.atom(aType).params.pos(aNum,:)';
% Shift Frequency for atoms who have a frequency
if (isfield(data.book.atom(aType).params,'freq'))
data.book.atom(aType).params.freq(aNum,:) = data.book.atom(aType).params.freq(aNum,:) + dMove(2);
end
end
% Delete black atoms
delete(data.curAtomsH);
delete(data.textH);
data.curAtomsH = [];
data.textH = [];
set(gcbf,'UserData',data);
% Replot book
refreshFigure();
end
%% Toggle toolbar icons to 'off' state when a icon is pressed
function toggleToolbar(varargin)
co = gcbo; % Remember current object (should be an icons)
data = get(gcf,'UserData');
% Untoggle all icons excpet current object
for k=2:length(data.toolH)
if ( strcmp(get(data.toolH(k),'Type'),'uitoggletool') && data.toolH(k)~=co )
set(data.toolH(k),'State','off')
end
end
end
%% RETURN THE INDEXES TO THE CORREPONDING ATOM TYPE and optional LENGTH in the book,
% return empty matrix[] if type is not found
function idx = getTypeIndex(book,type,varargin)
idx = [];
nt = length(book.atom);
for t=1:nt,
if( strcmp(book.atom(t).type,type) )
if (nargin==3)
if( book.atom(t).params.len(1,1) == varargin{1})
idx = [idx t];
end
else
idx = [idx t];
end
end
end
end
%% Update Atom selection when a new rectangle has been added
function updateAtomSelection(rpos)
data = get(gcf,'UserData');
% Init atom Handles vector
nC = data.book.numChans;
nA = data.book.numAtoms;
fs = data.book.sampleRate;
nT = length(data.book.atom);
% Channel from current axis;
chan = 1; %find(data.axH == gca);
% Get Last Rectangle of selection coordinates
%rpos = data.atomSelection(end,:); % xmin xmax ymin ymax
nAS = 0; % Counter for the number of atoms selected
for k = 1:nT,
% Process any visible atom
ind = find( data.book.atom(k).visible(:,chan) == 1 );
for nv = 1:length(ind),
a = ind(nv);
% Get atom bounds
% Temporal position
t0 = data.book.atom(k).params.pos(a,chan)/fs; % Position in seconds
dt0 = data.book.atom(k).params.len(a,chan)/fs; % Length in seconds
xmin = max(0,t0); % limit xmin to 0
xmax = min(data.book.numSamples, xmin + dt0); % limit xmax to numSaples