File: datamodifier.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 (480 lines) | stat: -rw-r--r-- 11,428 bytes parent folder | download | duplicates (3)
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
// datamodifier.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 "application.h"
#include "localdefs.h"
#include "controller.h"
#include "datamodifier.h"
#include "data.h"
#include "editor.h"
#include "formatrequester.h"
#include "lpcdata.h"
#include "processfun.h"
#include "range.h"
#include "request.h"
#include "sound.h"
#include "valuerequester.h"

DataModifier::DataModifier(Data* data) : myData(data), myEditor(nil) {
	myData->ref();
}

DataModifier::DataModifier(Data* data, DataEditor* editor)
		: myData(data), myEditor(editor) {
	myData->ref();
}

DataModifier::~DataModifier() {
	Resource::unref(myData);
}

int
DataModifier::apply() {
	int status = false;
	if(ok()) {
		Application::inform(message());
		status = doApply(myData);
	}
	return status;
}

void
DataModifier::doPostModifyCommands() {
	if(myEditor != nil)
		doEditorCommands(myEditor);
}

int
DataModifier::sampRate() { return myData->sRate(); }

//********

Modifier *
Reverser::create(DataEditor* ed) {
	return new Reverser(ed->currentSelection());
}

int
Reverser::doApply(Data* data) {
	data->reverse();
	return true;
}

Modifier *
Reverser::createUnDo() {
	return new Reverser(target());
}

//********

class NormalizeRequester : public TitledRequester {
	friend Normalizer;
protected:
	NormalizeRequester(Normalizer *n)
		: TitledRequester("Normalize Current Selection:"), client(n) {}
	redefined void configureRequest(Request *);
	redefined boolean confirmValues();
private:
	Normalizer* client;
};

void
NormalizeRequester::configureRequest(Request *request) {
	request->appendLabel("This will rescale the current selection's amplitudes");
	request->appendLabel("to fit between -1 and 1.");
}

boolean
NormalizeRequester::confirmValues() {
	return (
		(client->target()->dataType() > IntData)
		||
		Application::confirm(
			"Warning:  Performing this operation on non-floating-point samples",
			"will irreversibly destroy the data.",
			"Continue anyway?",
			Cancel
		)
	);
}

//********

Modifier *
Normalizer::create(DataEditor* ed) {
	return new Normalizer(ed->currentSelection());
}

Requester *
Normalizer::createRequester() {
	return new NormalizeRequester(this);
}

int
Normalizer::doApply(Data* data) {
	reverseFactor = data->maxValue();		// store inverse for undo
	data->normalize();
	return true;
}

// undo is only possible when data type is float or double

Modifier *
Normalizer::createUnDo() {
	return (target()->dataType() >= FloatData) ?
		new Scaler(target(), reverseFactor) : nil;
}

//********

class TimeInsertRequester : public TitledRequester {
	friend SpaceInsert;
protected:
	TimeInsertRequester(SpaceInsert *);
	redefined void configureRequest(Request *);
	redefined boolean confirmValues();
private:
	SpaceInsert* client;
	double timeToInsert;
};

TimeInsertRequester::TimeInsertRequester(SpaceInsert* s)
	: TitledRequester("Insert Blank Space:"), client(s), timeToInsert(0) {}

void
TimeInsertRequester::configureRequest(Request* request) {
	request->appendValue("Amount To Insert (seconds):",
	                     &timeToInsert, NonNegativeNumbers);
}

boolean
TimeInsertRequester::confirmValues() {
	client->nFrames = round(timeToInsert * client->sampRate());
	return true;
}

//********

SpaceInsert::SpaceInsert(Data* d, int nframes)
		: DataModifier(d), nFrames(nframes), insertTime(false) {
	initialize();
}

SpaceInsert::SpaceInsert(Data* d, boolean asTime)
	: DataModifier(d), nFrames(1), insertTime(asTime) {}

Modifier *
SpaceInsert::create(DataEditor* ed) {
	return new SpaceInsert(ed->currentSelection());
}

Requester *
SpaceInsert::createRequester() {
	if(insertTime)
		return new TimeInsertRequester(this);
	else
		return new RangedValueRequester<int>(
			"Insert Blank Space:", "Amount To Insert (frames):",
			this->nFrames, NonNegativeIntegers);
}

int
SpaceInsert::doApply(Data *data) {
	Range edit(0, nFrames);
	boolean was = Data::deferRescan(true);	// inserting zeros so no rescan
	int status = data->spliceIn(edit);
	Data::deferRescan(was);
	return status;
}

//********

OutSplicer::OutSplicer(Data* d, const Range& r) : DataModifier(d), region(r) {
	initialize();
}

Modifier *
OutSplicer::create(DataEditor* de) {
	return nil;
}

int
OutSplicer::doApply(Data* data) {
	if(region == data->frameRange()) {
		Application::alert("Entire file may not be spliced out.",
			"Select only a portion of the file.");
		return false;
	}
	else
		return data->spliceOut(region);
}

Modifier *
OutSplicer::createUnDo() {
	return nil;
}

//********

class LengthChangeRequester : public TitledRequester {
	friend class LengthChanger;
protected:
	LengthChangeRequester(const char* title, LengthChanger* l)
		: TitledRequester(title), client(l) {}
	redefined void configureRequest(Request *);
	redefined boolean confirmValues();
protected:
	LengthChanger* client;
};

void
LengthChangeRequester::configureRequest(Request* request) {
	Range lengths = PositiveIntegers;
	lengths.set(8, lengths.intMax());	// min length 8
	request->appendValue("New Length (in frames):",
	                     &client->newLength, lengths);
}

boolean
LengthChangeRequester::confirmValues() {
	boolean status = false;
	status = (client->newLength >= client->oldLength ||
		Application::confirm(
			"This operation will destroy any data past",
			"the new file endpoint, and is not reversible.",
			"Continue?",
			Cancel)
	);
	return status;
}

//********

class DurChangeRequester : public LengthChangeRequester {
	friend LengthChanger;
protected:
	DurChangeRequester(const char* title, LengthChanger *);
	redefined void configureRequest(Request *);
	redefined boolean confirmValues();
private:
	double newDuration;
};

DurChangeRequester::DurChangeRequester(const char* title, LengthChanger* client)
		: LengthChangeRequester(title, client) {
	newDuration = double(client->newLength)/client->sampRate();
}

void
DurChangeRequester::configureRequest(Request* request) {
	Range durations = AllNumbers;
	durations.set(8.0/client->sampRate(), durations.max());
	request->appendValue("New Duration (seconds):", &newDuration,
			     durations);
}

boolean
DurChangeRequester::confirmValues() {
	client->newLength = round(newDuration * client->sampRate());
	return LengthChangeRequester::confirmValues();
}

//********

LengthChanger::LengthChanger(Data* d, boolean asDur)
	: DataModifier(d),
	  oldLength(d->length()), newLength(oldLength), asDuration(asDur) {}

LengthChanger::LengthChanger(Data* d, int newlen)
		: DataModifier(d),
		  oldLength(d->length()), newLength(newlen), asDuration(false) {
	initialize();
}

Modifier *
LengthChanger::create(DataEditor* ed) {
	return new LengthChanger(ed->model());
}

Requester *
LengthChanger::createRequester() {
	if(asDuration)
		return new DurChangeRequester("Change File Duration:", this);
	else
		return new LengthChangeRequester("Change File Length:", this);
}

int
LengthChanger::doApply(Data *data) {
	int status = data->changeLength(newLength);
	if(status == true) {
		// defer rescan if increasing length since extending with zeros
		boolean was = Data::deferRescan(newLength >= oldLength);
		data->Notify();		// data does not automatically do this 
		Data::deferRescan(was);
	}
	return status;
}

Modifier *
LengthChanger::createUnDo() {
	return (newLength > oldLength) ?
		new LengthChanger(target(), oldLength) : nil;
}

//********

class SampRateRequester : public TitledRequester {
	friend SampRateChanger;
protected:
	SampRateRequester(SampRateChanger *s)
		: TitledRequester("Change Sampling Rate:"), client(s) {}
	redefined void configureRequest(Request *);
	redefined boolean confirmValues();
private:
	SampRateChanger* client;
};

void
SampRateRequester::configureRequest(Request* request) {
	request->appendValue("New Sampling Rate:",
	                     &client->newSrate, PositiveIntegers);
	if(client->target()->isA(Sound_Data))
		request->appendChoice(
			"Interpolate sound to preserve pitch level:",
			"|No|Yes|", &client->doInterp, true);
	else client->doInterp = False;
}

boolean
SampRateRequester::confirmValues() {
	if(client->doInterp == Requester::True)
		return Application::confirm("This operation is not reversible.",
			"Continue?");
	else
		return true;
}

//********

SampRateChanger::SampRateChanger(Data* d)
	: DataModifier(d), newSrate(sampRate()), doInterp(Requester::True) {}
		
SampRateChanger::SampRateChanger(Data* d, int newsrate, boolean interp)
		: DataModifier(d), newSrate(newsrate),
		doInterp(interp ? Requester::True : Requester::False) {
	initialize();
}

Modifier *
SampRateChanger::create(DataEditor* ed) {
	return new SampRateChanger(ed->model());
}

Requester *
SampRateChanger::createRequester() {
	return new SampRateRequester(this);
}

int
SampRateChanger::doApply(Data *data) {
	return data->changeSRate(newSrate, doInterp == Requester::True);
}

//********

class FormatChangeRequester : public FormatRequester {
	friend FormatChanger;
protected:
	FormatChangeRequester(FormatChanger *);
	redefined boolean confirmValues();
private:
	FormatChanger* client;
};

FormatChangeRequester::FormatChangeRequester(FormatChanger* f)
	: FormatRequester(nil, "Convert format to:", f->newType),
	  TitledRequester("Change Sound Sample Format:"),
	  client(f) {}

boolean
FormatChangeRequester::confirmValues() {
	boolean status = true;
	if(client->newType == client->oldType) {
		Application::alert("Samples are already in this format.");
		status = false;
	}
	return status;
}

//********

FormatChanger::FormatChanger(Data* d)
		: DataModifier(d), oldType(d->dataType()) {
	newType = (oldType == ShortData) ? FloatData : ShortData;
}
		
FormatChanger::FormatChanger(Data* d, DataType newtype)
		: DataModifier(d), oldType(d->dataType()), newType(newtype) {
	initialize();
}

Modifier *
FormatChanger::create(DataEditor* ed) {
	return new FormatChanger(ed->model());
}

Requester *
FormatChanger::createRequester() {
	return new FormatChangeRequester(this);
}

int
FormatChanger::doApply(Data *data) {
	return ((Sound *) data)->changeDataType(DataType(newType));
}

Modifier *
FormatChanger::createUnDo() {
	return (newType > oldType) ?
		new FormatChanger(target(), DataType(oldType)) : nil;
}

//********

Modifier *
FrameStabilizer::create(DataEditor* ed) {
	return new FrameStabilizer(ed->model());
}

int
FrameStabilizer::applyToLPCData(LPCData* lpc) {
	lpc->stabilizeFrames();
	return true;
}