File: editor.C

package info (click to toggle)
mixviews 1.10-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,440 kB
  • ctags: 6,314
  • sloc: cpp: 31,647; ansic: 2,100; makefile: 1,782; sh: 17
file content (1218 lines) | stat: -rw-r--r-- 27,268 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
// editor.C

/******************************************************************************
 *
 *  MiXViews - an X window system based sound & data editor/processor
 *
 *  Copyright (c) 1993, 1994 Regents of the University of California
 *
 *  Author:     Douglas Scott
 *  Date:       December 13, 1994
 *
 *  Permission to use, copy and modify this software and its documentation
 *  for research and/or educational purposes and without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and that
 *  both that copyright notice and this permission notice appear in
 *  supporting documentation. The author reserves the right to distribute this
 *  software and its documentation.  The University of California and the author
 *  make no representations about the suitability of this software for any 
 *  purpose, and in no event shall University of California be liable for any
 *  damage, loss of data, or profits resulting from its use.
 *  It is provided "as is" without express or implied warranty.
 *
 ******************************************************************************/


#ifdef __GNUG__
#pragma implementation
#endif

#include <InterViews/button.h>
#include <InterViews/world.h>
#include <X11/keysym.h>
#include "localdefs.h"
#include <sys/time.h>
#include <assert.h>
#include "datafile.h"
#include "application.h"
#include "header.h"
#include "editor.h"
#include "comment.h"
#include "controller.h"
#include "data.h"
#include "delay.h"
#include "filename.h"
#include "filecommand.h"
#include "sound.h"
#include "lpcdata.h"
#include "formantfilter.h"
#include "pchdata.h"
#include "fftdata.h"
#include "envelope.h"
#include "datamodifier.h"
#include "analysisfun.h"
#include "optionsetter.h"
#include "phraser.h"
#include "ellipfilt.h"
#include "fftfun.h"
#include "rescaler.h"
#include "interpolater.h"
#include "lowpassfilter.h"
#include "pulsegen.h"
#include "statusaction.h"
#include "textwindow.h"
#include "pvoceditor.h"
#include "soundeditor.h"
#include "curvegen.h"
#include "crossfader.h"
#include "commenteditor.h"
#include "soundheader.h"

const char *
scat(const char *s1, const char *s2) {
	static char str[1024];
	sprintf(str, "%s%s", s1, s2);
	return str;
}

const char *
scat(const char *s1, const char *s2, const char *s3) {
	static char str[1024];
	sprintf(str, "%s%s%s", s1, s2, s3);
	return str;
}

Data *
DataEditor::internalBuffer = nil;

DataEditor::C_Constructor 
DataEditor::ctor_table1[] = {
	SoundEditor::new_DataEditor1,
	LPCEditor::new_DataEditor1,
	PCHEditor::new_DataEditor1,
	FFTEditor::new_DataEditor1,
	EnvelopeEditor::new_DataEditor1,
	PvocEditor::new_DataEditor1
};

DataEditor::C_D_Constructor 
DataEditor::ctor_table2[] = {
	SoundEditor::new_DataEditor2,
	LPCEditor::new_DataEditor2,
	PCHEditor::new_DataEditor2,
	FFTEditor::new_DataEditor2,
	EnvelopeEditor::new_DataEditor2,
	PvocEditor::new_DataEditor2
};

DataEditor::DataEditor(Controller *c, const Data *d)
		: controller(c), data(nil), selection(nil) {
	init((Data *) d);
}

inline int typeToIndex(FileType type) {
	return powerOfTwoToLinearEnum(int(type)) - 2;
}

DataEditor *
DataEditor::create(Controller *c, const char* name) {
	int type_index = typeToIndex(FileName::fileType(name));
	static const int tableEnd = sizeof(ctor_table1) / sizeof(*ctor_table1);
	assert(0 < type_index && type_index < tableEnd);
	return (*DataEditor::ctor_table1[type_index])(c);
}

DataEditor *
DataEditor::create(Controller *c, const Data *d) {
	int type_index = typeToIndex(d->fileType());
	static const int tableEnd = sizeof(ctor_table2) / sizeof(*ctor_table2);
	assert(0 < type_index && type_index < tableEnd);
	return (*DataEditor::ctor_table2[type_index])(c, d);
}

void
DataEditor::init(Data* d) {
	reset();
	if(d)
		setData(d);
}

DataEditor::~DataEditor() {
	setCopyBuffer(nil);
	setSelection(nil);
	setData(nil);
}

void
DataEditor::setData(Data* d) {
	if(d != data) {
		Resource::unref(data);
		data = d;
		if(data != nil) {
			data->ref();
			location = data->frameRange();
			channels = data->channelRange();
		}
	}
}

Data *
DataEditor::createModel(DataFile *f) {
	boolean status = false;
	Data *d = newModel();
	if(!f->readable())
		Application::error(scat("Unable to read file ", f->name()));
	else {
		Application::inform(scat("Reading ", f->name(), "..."));
		Header *header = d->createHeader(f, true);
		header->ref();
		if(!(status = d->read(f, header))) {
			if(d->length() == 0)
				/* non-read error */ ;
			else if(Application::globalResourceIsTrue("ReadRawFiles")) {
				if(header->configure(controller)) {
					f->clear();
					status = d->read(f, header);
				}
			}
		}
		Resource::unref(header);
	}
	f->failif(status != true);		// calling routine checks file state
	return d;
}

const char *
DataEditor::defaultDir() {
	const char* sfd = getenv("SFDIR");
	const char *a = 
		Application::getGlobalResource(data->defaultDirAttribute());
	return a ? a : sfd ? sfd : ".";
}

boolean
DataEditor::readFile(DataFile* file) {
	boolean status = true;
	file->ref();
	setData(createModel(file));
	status = file->good();
	Resource::unref(file);
	return status;
}

void
DataEditor::setInsertPoint(int point, int chan) {
	if(selectionMade() || chan < 0) // for first call after setting
		reset();
	setLocation(Range(point, max(point, data->length() - 1)));
	addChannel(chan);
	setSelection(nil);
}

void
DataEditor::setEditRegion(const Range &region, int chan) {
	if(!selectionMade()) {	// for first call after unsetting
		reset();
		isSelected = true;
	}
	setLocation(region);
	addChannel(chan);
	setSelection(nil);
	// when new region set in any window, it becomes default target rather
	// than any previously copied internal buffer.  Not so for insert point.
	setCopyBuffer(nil);
}

void
DataEditor::setLocation(const Range& newloc) {
	setTimeStamp();
	if(newloc != location)
		location = newloc;
}

void
DataEditor::addChannel(int chan) {
	if(chan >= 0) {
		int first = channels.intMin();
		int last = channels.intMax();
		int nfirst = (first < 0 || chan < first) ? chan : first;
		setChannels(nfirst, chan > last ? chan : last);
	}
}

Range
DataEditor::currentChannels() {
	// for now, either just one, or all channels
	// set to all if none chosen (-1 is flag for this)
	if(nchans() > 1 || channels.includes(-1))
		setChannels(model()->channelRange());
	return channels;
}

void
DataEditor::setTimeStamp() {
	struct timeval tp;
	struct timezone tzp;
	gettimeofday(&tp,&tzp);
	timestamp = tp.tv_sec;
}

void
DataEditor::setSelection(Data *s) {
	if(selection != s) {
		Resource::unref(selection);
		if((selection = s) != nil)
			selection->ref();
	}
}

Status
DataEditor::setCopyBuffer(Data *b) {
	if(internalBuffer != b) {
		Resource::unref(internalBuffer);
		if((internalBuffer = b) != nil)
			internalBuffer->ref();
	}
	return internalBuffer != nil;
}

Data *
DataEditor::currentSelection() {
	if(selection == nil)
		setSelection(data->clone(currentRegion(), currentChannels()));
	return selection;
}

Data *
DataEditor::currentSource() {
	Data *source = (copyBuffer() != nil) ?
		copyBuffer() : controller->findSelection();
	if(source == nil)
		Application::alert(
			"You must select a source to use this operation", 
			"or copy a selection into the internal buffer."
		);
	else if(*source == *data) {
		// must make deep copy if source is same as target
		Data *copyOfSource = source->copyOf();
		Resource::unref(source);
		source = copyOfSource;
		source->ref();
	}
	else
		source->ref();
	return source;
}

void
DataEditor::freeSource(Data *sel) {
	if(sel != internalBuffer)	// only unref source if it was not internal
		Resource::unref(sel);
}

Data *
DataEditor::getSelection(long &timestamp) {
	timestamp = timeStamp();
	Data *sel = nil;
	if (selectionMade())
		sel = data->clone(currentRegion(), currentChannels());
	return sel;
}

// re-select edit region (after reverting to saved, for instance)
// unless that region is no longer within the data bounds

void
DataEditor::reselect() {
	if(selectionMade() && data->frameRange().includes(currentRegion()))
			setSelection(data->clone(currentRegion(), currentChannels()));
	else {
		controller->unselectView();	// do to reset even if no selection made
		setSelection(nil);
	}
}

// static method -- used by "static constructor" methods

boolean
DataEditor::applyModifierUsing(Modifier &mod, Controller* controller) {
	boolean status = false;
	if(mod.configure(controller))
		status = mod.apply();
	return status;
}

boolean
DataEditor::applyModifier(Modifier &mod) {
	return applyModifierUsing(mod, controller);
}

boolean
DataEditor::applyModifierToNew(Modifier &mod) {
	boolean status;
	if((status = applyModifier(mod)) == true)
		displayInternalBuffer();
	setCopyBuffer(nil);
	return status;
}

void
DataEditor::markEditRegion(int size) {
	controller->showEditRegion(
		Range(currentInsert(), currentInsert() + size - 1),
		currentChannels()
	);
}

// keysym is checked first by controller, then by view, then by editor
// subclass, and lastly by editor base class.  Checking stops when matched.

boolean
DataEditor::keyCommand(unsigned long sym) {
	boolean interested = true;
	switch (sym) {
	case XK_period:
		setDefaultDir();
		break;
	case XK_n:
		newFile();
		break;
	case XK_nobreakspace:
		newLPCFile();
		break;
	case XK_exclamdown:
		newSoundFile();
		break;
	case XK_cent:
		newEnvelopeFile();
		break;
	case XK_currency:
		newPvocFile();
		break;
	case XK_o:
		openNewFile();
		break;
	case XK_s:
		saveFile();
		break;
	case XK_S:
		saveToFile();
		break;
	case XK_U:
		revertToSaved();
		break;
	case XK_quotedbl:
		changeComment();
		break;
	case XK_question:
		information();
		break;
	case XK_braceright:
		dataDump();
		break;
	case XK_I:
		displayInternalBuffer();
		break;
	case XK_c:
		copy();
		break;
	case XK_C:
		copyToNew();
		break;
	case XK_r:
		remove();
		break;
	case XK_y:
		removeToNew();
		break;
	case XK_e:
		erase();
		break;
	case XK_x:
		spliceOut();
		break;
	case XK_X:
		spliceOutToNew();
		break;
	case XK_d:
		zap();
		break;
	case XK_m:
		mix();
		break;
	case XK_R:
		replace();
		break;
	case XK_g:
		crossfade();
		break;
	case XK_v:
		spliceIn();
		break;
	case XK_P:
		scaleValues();
		break;
	case XK_b:
		reverse();
		break;
	case XK_i:
		insertSpace();
		break;
	case XK_E:
		applyEnvelope();
		break;
	case XK_O:
		vertOffset();
		break;
	case XK_bracketright:
		rescale();
		break;
	case XK_D:
		delay();
		break;
	case XK_t:
		interpolate();
		break;
	case XK_L:
		lowPassFilter();
		break;
	case XK_bracketleft:
		normalize();
		break;
	case XK_l:
		changeLength();
		break;
	case XK_0:
		findZeroCrossing();
		break;
	case XK_1:
		findSlopeChange();
		break;
	case XK_2:
		showPeakFrame();
		break;
	case XK_3:
		extractEnvelope();
		break;
	case XK_degree:
		setDataOptions();
		break;
	default:
		interested = false;
		break;
	}
	return interested;
}

Controller*
DataEditor::openNewFile(const char* windowtitle) {
	FileOpener fo(defaultDir(), windowtitle);
	applyModifier(fo);
	return fo.getNewController();
}

// the following are the menu-called functions

Status
DataEditor::openNewFile() {
	return (openNewFile("Select File to Open:") != nil);
}

Status
DataEditor::newLPCFile() {
	return (LPCEditor::new_File(controller) != nil);
}

Status
DataEditor::newSoundFile() {
	return (SoundEditor::new_File(controller) != nil);
}

Status
DataEditor::newEnvelopeFile() {
	return (EnvelopeEditor::new_File(controller) != nil);
}

Status
DataEditor::newPvocFile() {
	return (PvocEditor::new_File(controller) != nil);
}

Response
DataEditor::closeFile() {
	Response r = Yes;
	// warn if file modified and if this is the last open view of it
	if(data->modified() && data->currentViews() == 1) {
		r = Application::choice("Do you wish to save changes to",
			controller->fileName(), "before closing?", Cancel);
		switch(r) {
		case Yes:
			saveFile();
			if(data->modified())
				r = Cancel;	// assumes cancel or error during save
			else
				r = No;	// indicates warning was issued
			break;
		case No:
		case Cancel:
			break;
  		default:
			break;
		}
	}
	return r;	// defaults to Yes indicating no alert issued
}
	
Status
DataEditor::saveFile() {
	FileSaver fs(controller->fileName(), defaultDir(), model());
	return applyModifier(fs);
}

Status
DataEditor::saveToFile() {
	FileSaver fs(defaultDir(), model());
	return applyModifier(fs);
}

Status
DataEditor::revertToSaved() {
	const char* filename = controller->fileName();
	Status status = Fail;
	if(!data->modified())
		Application::alert("File has not been edited since last save.");
	else if(FileName::isTempName(filename))
		Application::alert("This is a temporary file and does not",
			"exist yet on disk.  You must save it first.");
	else if(Application::confirm(
			"Are you sure you wish to revert to the saved version?")) {
		DataFile f(filename, "r");
		if(f.readable()) {
			Application::inform("Re-reading file from disk...");
			Header *header = data->createHeader(&f, true);
			header->ref();
			status = data->read(&f, header);
			Resource::unref(header);
		}
		controller->resetScaleTimes();
		reselect();
	}
	return status;
}

Status
DataEditor::displayInternalBuffer() {
	Status status = Fail;
	if(copyBuffer() != nil) {
		if(copyBuffer()->length() > 3) {
			Controller *newCtlr = new Controller(copyBuffer());
			newCtlr->display(controller->world());
			copyBuffer()->Notify();	// to assure rescan of amps
			status = Succeed;
		}
		else
			Application::alert("Cannot display data with length < 4 frames.");
		// once displayed, not available internally to avoid confusion
		setCopyBuffer(nil);
	}
	else Application::alert("Internal buffer is currently empty.");
	return status;
}

Status
DataEditor::changeComment() {
	TextWindow* commentWindow = new TextWindow(
		scat(controller->fileName(), ": Comment"),
		new CommentEditor(new ButtonState, model())
	); 
	commentWindow->display(controller->world());
	return Succeed;
}

Status
DataEditor::dataDump() {
	DataDumper dumper(defaultDir(), currentSelection());
	return applyModifier(dumper);
}

Status
DataEditor::information() {
	data->information(controller);
	return Succeed;
}

// previous internal buffer is kept until actual copy of new buffer takes place

Status
DataEditor::copy() {
	Application::inform("Copying...");
	boolean was = Data::deferRescan(true);
	Status status = setCopyBuffer(currentSelection()->copyOf());
	Data::deferRescan(was);
	return status;
}

Status
DataEditor::copyToNew() {
	return copy() && displayInternalBuffer();
}

Status
DataEditor::remove() {
	return copy() && erase();
}

Status
DataEditor::removeToNew() {
	return remove() && displayInternalBuffer();
}

Status
DataEditor::erase() {
	currentSelection()->erase();
	return Succeed;
}

Status
DataEditor::spliceOut() {
	return copy() && zap();
}

Status
DataEditor::spliceOutToNew() {
	return spliceOut() && displayInternalBuffer();
}

Status
DataEditor::zap() {
	Status status = Fail;
	if(selectionMade()
		|| Application::confirm("Splice out everything from this point",
			"to the end of the file?")) {
		OutSplicer splicer(model(), currentRegion());
		if((status = applyModifier(splicer)))
			controller->showInsertPoint(currentInsert(), currentChannels());
	}
	return status;
}

Status
DataEditor::mix() {
	// for now this mixes at unity gain
	Data* src = nil;
	Status status = Fail;
	if((src = currentSource()) != nil) {
		Mixer mixer(currentSelection(), src);
		if((status = applyModifier(mixer)))
			markEditRegion(min(currentSelection()->length(), src->length()));
	}
	freeSource(src);
	return status;
}

Status
DataEditor::replace() {
	Data* src = nil;
	Status status = Fail;
	if((src = currentSource()) != nil) {
		Replacer replacer(currentSelection(), src);
		if((status = applyModifier(replacer)))
			markEditRegion(min(currentSelection()->length(), src->length()));
	}
	freeSource(src);
	return status;
}

Status
DataEditor::crossfade() {
	Data* src = nil;
	Status status = Fail;
	if((src = currentSource()) != nil) {
		Crossfader crossfader(currentSelection(), src);
		if((status = applyModifier(crossfader)))
			markEditRegion(min(currentSelection()->length(), src->length()));
	}
	freeSource(src);
	return status;
}

Status
DataEditor::spliceIn() {
	Data* src = nil;
	Status status = Fail;
	if((src = currentSource()) != nil) {
		Splicer splicer(currentSelection(), src);
		if((status = applyModifier(splicer))) {
			Range edit = src->frameRange();
			edit += currentInsert();		// offset for highlight display
			controller->showEditRegion(edit, data->channelRange());
		}
	}
	freeSource(src);
	return status;
}

Status
DataEditor::scaleValues() {
	Scaler s(currentSelection());
	return applyModifier(s);
}

Status
DataEditor::insertSpace() {
	SpaceInsert s(currentSelection());
	return applyModifier(s);
}

Status
DataEditor::reverse() {
	Reverser r(currentSelection());
	return applyModifier(r);
}

Status
DataEditor::vertOffset() {
	Offset o(currentSelection());
	return applyModifier(o);
}

Status
DataEditor::rescale() {
	Rescaler r(currentSelection());
	return applyModifier(r);
}

Status
DataEditor::interpolate() {
	// create new buffer which will be resized by Interpolater
	setCopyBuffer(currentSelection()->newData(1));
	Interpolater i(currentSelection(), copyBuffer());
	return applyModifierToNew(i);
}

Status
DataEditor::changeLength() {
	LengthChanger l(model());
	Status status = applyModifier(l);
	reselect();
	return status;
}

Status
DataEditor::delay() {
	Delay d(currentSelection());
	return applyModifier(d);
}

Status
DataEditor::lowPassFilter() {
	LowPassFilter l(currentSelection(), 0.5);
	return applyModifier(l);
}

Status
DataEditor::applyEnvelope() {
	Envelope *envelope = nil;
	Data *source = nil;
	Controller* ctlr = nil;
	if((source = currentSource()) != nil) {
		;
	}
	else if((ctlr = openNewFile("Select Envelope File to Apply:")) != nil) {
		source = ctlr->model();
		source->ref();
	}
	Status status = Fail;
	if(source) {
		// make copy of source so that it may be normalized, etc.
		envelope = new Envelope(source->length());
		envelope->copyFrom(source);
		envelope->ref();
		Application::inform("Scaling envelope between -1 and 1...");
		envelope->normalize();
		Phraser p(currentSelection(), envelope);
		if(p.configure(controller))
			status = p.apply();
		Resource::unref(envelope);
	}
	freeSource(source);
	return status;
}

Status
DataEditor::normalize() {
	Normalizer n(currentSelection());
	return applyModifier(n);
}

Status
DataEditor::changeSampleRate() {
	SampRateChanger s(model());
	Status status;
	if((status = applyModifier(s))) {
		controller->resetScaleTimes();
		reselect();
	}
	return status;
}

Status
DataEditor::showPeakFrame() {
	int peakchan, peakloc;
	data->maxValue(&peakchan, &peakloc);
	Range chan(peakchan, peakchan);
	controller->showInsertPoint(peakloc, chan);
	return Succeed;
}

Status
DataEditor::findZeroCrossing() {
	int offset = currentSelection()->zeroCrossing();
	Status status = Succeed;
	if(offset > 0) {
		Range chan(currentChannels().intMin(), currentChannels().intMin());
		controller->showInsertPoint(currentInsert() + offset, chan);
	}
	else {
		Application::alert("No zero crossings found.");
		status = Fail;
	}
	return status;
}

// for now, this just displays the location of the first discontinuity found, though the
// analysis data is an array of offsets to all such locations within the current selection

Status
DataEditor::findSlopeChange() {
	SlopeChangeDetecter s(currentSelection());
	Status status;
	if((status = applyModifier(s))) {
		int offset = s.getAnalysis()->get(0);
		Range chan(currentChannels().intMin(), currentChannels().intMin());
		controller->showInsertPoint(currentInsert() + offset, chan);
	}
	else {
		Application::alert("No slope changes of that magnitude found.");
		status = Fail;
	}
	return status;
}

Status
DataEditor::extractEnvelope() {
	EnvelopeExtracter e(currentSelection());
	Status status;
	if((status = applyModifier(e))) {
		Controller *newctlr = new Controller(e.getAnalysis());
		newctlr->display(controller->world());
	}
	return status;
}

//**************

Data *
LPCEditor::newModel() { return new LPCData(); }

Range
LPCEditor::currentChannels() {
	if(nchans() == 1 && !channels.includes(-1))
		;
	else if(channels.includesZero() || channels.includes(-1))  // default
		setChannels(model()->channelRange());
	else if(channels.intMin() >= 4)		// coefficient display
		setChannels(4, model()->channels() - 1);
	return channels;
}

boolean
LPCEditor::keyCommand(unsigned long sym) {
	boolean interested = true;
	switch (sym) {
	case XK_asterisk:
		stabilizeFrames();
		break;
	case XK_A:
		displayFrameAmplitudes();
		break;
	case XK_F:
		displayFrameFormants();
		break;
	case XK_k:
		changeSampleRate();
		break;
	case XK_M:
		mergePitchData();
		break;
	case XK_p:
		adjustPitchDeviation();
		break;
	default:
		interested = DataEditor::keyCommand(sym);
		break;
	}
	return interested;
}

Status
LPCEditor::newFile() {
	return (LPCEditor::new_File(controller) != nil);
}

Status
LPCEditor::saveFile() {
	LPCFileSaver lfs(controller->fileName(), defaultDir(), model());
	return applyModifier(lfs);
}

Status
LPCEditor::saveToFile() {
	LPCFileSaver lfs(defaultDir(), model());
	return applyModifier(lfs);
}

// static constructor

Controller *
LPCEditor::new_File(Controller* controller) {
	LPCFileCreator lfc;
	applyModifierUsing(lfc, controller);
	return lfc.getNewController();
}

Status
LPCEditor::stabilizeFrames() {
	FrameStabilizer fs(model());
	return applyModifier(fs);
}

Status
LPCEditor::displayFrameAmplitudes() {
	const int pulseFrameSize = 1000;
	const int pulsePerFrame = 10;
	LPCData* selected = (LPCData *) currentSelection();
	Application::inform("Creating test pattern...");
	int lpcLen = selected->length();
	int srate = selected->sRate();
	Sound* pulses = new Sound(lpcLen * pulseFrameSize, srate, 1, FloatData);
	pulses->ref();
	// add pulsePerFrame pulses per original LPC frame
	PulseGenerator pgen(pulses, pulseFrameSize/pulsePerFrame);
	pgen.apply();
	FormantFilter filter(pulses, pulses, selected, 1.0);
	filter.apply();
	Envelope* amplitudes = new Envelope(lpcLen * pulsePerFrame);
	amplitudes->setFrameRangeLabel("LPC Analysis Frames");
	amplitudes->setRangeFactor(1.0/pulsePerFrame);
	Application::inform("Extracting amplitudes...");
	pulses->getEnvelope(amplitudes, 0, AbsoluteMagnitude);
	Resource::unref(pulses);
	Controller* ampDisplay = new Controller(amplitudes, 
		scat(controller->windowName(), ":  Frame Amplitudes"));
	ampDisplay->display(controller->world());
	return Succeed;
}

Status
LPCEditor::displayFrameFormants() {
	const int pulseFrameSize = 1024;
	LPCData* selected = (LPCData *) currentSelection();
	int lpcLen = selected->length();
	int srate = selected->sRate();
	Sound* pulses = new Sound(lpcLen * pulseFrameSize, srate, 1, FloatData);
	pulses->ref();
	PulseGenerator pgen(pulses, pulseFrameSize);
	Application::inform("Creating test pattern...");
	pgen.apply();
	FormantFilter filter(pulses, pulses, selected, 1.0);
	filter.apply();
	Application::inform("Analyzing formants...");
	// fft size depends on npoles
	FFT_Function analyzer(
		pulses, selected->nPoles() > 32 ? 128 : 64, pulseFrameSize
	);
	Status status;
	if((status = analyzer.apply())) {
		Controller* fftDisplay = new Controller(
			analyzer.getAnalysis(), 
			scat(controller->windowName(), ":  Formant Frequencies"));
		fftDisplay->display(controller->world());
	}
	Resource::unref(pulses);
	return status;
}

Status
LPCEditor::mergePitchData() {
	// regardless of selection, select only channel 3 for merge
	const int pitchChannel = 3;
	setChannels(pitchChannel, pitchChannel);
	LPCData *lpc = (LPCData *) currentSelection();
	Data *pitches = nil;
	Controller* newController = nil;
	Status status = Succeed;
	if((pitches = currentSource()) != nil) {
		;
	}
	else if((newController = openNewFile("Select Pitch File for Merge:")) != nil)
		pitches = newController->model();
	if(pitches && Application::confirm("Please confirm merge operation.")) {
		Application::inform("Merging pitch data...");
		lpc->mergePitchData(pitches);	
		markEditRegion(lpc->length());
	}
	else status = Fail;
	freeSource(pitches);
	return status;
}

#if 0

Status
LPCEditor::warpFrames() {
	Data *source = nil;
	Controller* ctlr = nil;
	if((source = currentSource()) != nil) {
		copy();
		Envelope* evp = new Envelope(1);
		evp->ref();
		Interpolater i(source, evp, copyBuffer()->length());
		i.apply();		// stretch source to equal current sel's length
		FrameWarper f(evp, copyBuffer());
		if(applyModifierToNew(f)) {
			Application::alert("A new file of filter modifying values",
				"is being created, and will be needed for any",
				"subsequent warped LPC formant filtering.");
			ctlr = new Controller(evp, "new_warp.evp");
			ctlr->display(controller->world());
		}
		Resource::unref(evp);
	}
	freeSource(source);
	return Succeed;
}

#endif /* 0 */

Status
LPCEditor::setDataOptions() {
	LPCOptionSetter setter;
	return applyModifier(setter);
}

// these static functions have their addresses loaded into a ctor array
// in the DataEditor base class

DataEditor *
LPCEditor::new_DataEditor1(Controller *c) {
	return new LPCEditor(c);
}

DataEditor *
LPCEditor::new_DataEditor2(Controller *c, const Data *d) {
	return new LPCEditor(c, d);
}

//**************

Data *
PCHEditor::newModel() { return new PCHData(); }

// these static functions have their addresses loaded into a ctor array
// in the DataEditor base class

DataEditor *
PCHEditor::new_DataEditor1(Controller *c) {
	return new PCHEditor(c);
}

DataEditor *
PCHEditor::new_DataEditor2(Controller *c, const Data *d) {
	return new PCHEditor(c, d);
}

//**************

Data *
FFTEditor::newModel() { return new FFTData(); }

Status
FFTEditor::setDataOptions() { return Succeed; }

// these static functions have their addresses loaded into a ctor array
// in the DataEditor base class

DataEditor *
FFTEditor::new_DataEditor1(Controller *c) {
	return new FFTEditor(c);
}

DataEditor *
FFTEditor::new_DataEditor2(Controller *c, const Data *d) {
	return new FFTEditor(c, d);
}

//**************

Data *
EnvelopeEditor::newModel() { return new Envelope(); }

boolean
EnvelopeEditor::keyCommand(unsigned long sym) {
	boolean interested = true;
	switch (sym) {
	case XK_underscore:
		createLinearCurve();
		break;
	case XK_asterisk:
		createExponentialCurve();
		break;
	default:
		interested = DataEditor::keyCommand(sym);
		break;
	}
	return interested;
}

Status
EnvelopeEditor::newFile() {
	return (EnvelopeEditor::new_File(controller) != nil);
}

Status
EnvelopeEditor::setDataOptions() { return Succeed; }

// static constructor

Controller *
EnvelopeEditor::new_File(Controller* controller) {
	EnvelopeFileCreator efc;
	applyModifierUsing(efc, controller);
	return efc.getNewController();
}

Status
EnvelopeEditor::createLinearCurve() {
	LinearCurveGenerator l(currentSelection());
	return applyModifier(l);
}

Status
EnvelopeEditor::createExponentialCurve() {
	ExponentialCurveGenerator e(currentSelection());
	return applyModifier(e);
}

// these static functions have their addresses loaded into a ctor array
// in the DataEditor base class

DataEditor *
EnvelopeEditor::new_DataEditor1(Controller *c) {
	return new EnvelopeEditor(c);
}

DataEditor *
EnvelopeEditor::new_DataEditor2(Controller *c, const Data *d) {
	return new EnvelopeEditor(c, d);
}