File: filecommand.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 (549 lines) | stat: -rw-r--r-- 14,481 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
// filecommand.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 "controller.h"
#include "data.h"
#include "editor.h"
#include "datafile.h"
#include "envelope.h"
#include "filecommand.h"
#include "filename.h"
#include "lpcdata.h"
#include "pvocdata.h"
#include "query.h"
#include "request.h"
#include "formatrequester.h"
#include "sndconfig.h"
#include "sound.h"
#include "soundheader.h"

FileCommand::FileCommand(const char* filename)
	: theFileName(filename), theController(nil) {}

boolean
FileCommand::configure(Controller *c) {
	theController = c;		// cache a ref to this for use
	return Super::configure(c);
}

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

//********

class FileCreateRequester : public TitledRequester {
	friend FileCreator;
	friend EnvelopeFileCreator;
protected:
	FileCreateRequester(const char* title, FileCreator* fc)
		: TitledRequester(title), client(fc) {}
	redefined void configureRequest(Request *);
protected:
	FileCreator* client;
};

void
FileCreateRequester::configureRequest(Request* request) {
	String& name = client->fileName();
	request->appendValue("File name:", &name);
	request->appendValue("Length (in frames):", &client->newLength,
	                     PositiveIntegers);
}

//********

FileCreator::FileCreator(const char* filename, int length)
	: FileCommand(filename),
	  newController(nil), theData(nil), newLength(length) {}

// this could do length() != newLength

boolean
FileCreator::setData(Data *d) {
	theData = d;
	return (theData->length() != 0);
}

Data *
FileCreator::newData() {
	return theData;
}

int
FileCreator::doApply() {
	newController = new Controller(newData(), fileName());
	newController->display(controller()->world());
	return true;
}

//********

class SoundCreateRequester : public FormatRequester {
	friend SoundCreator;
protected:
	SoundCreateRequester(SoundCreator *);
	redefined void configureRequest(Request *);
private:
	SoundCreator* client;
};

SoundCreateRequester::SoundCreateRequester(SoundCreator* sc)
	: FormatRequester(nil, "Sample format:", sc->format),
	  TitledRequester("Create New Soundfile:"),
	  client(sc) {}

void
SoundCreateRequester::configureRequest(Request* request) {
	String& name = client->fileName();
	request->appendValue("File name:", &name);
	request->appendValue("Duration (seconds):", &client->duration,
		 PositiveIntegers);
	request->appendValue("Sample rate:", &client->sampleRate, PositiveIntegers);
	request->appendChoice("Channels:", "|1|2|4|", &client->channels, true);
	FormatRequester::configureRequest(request);
}

//********

SoundCreator::SoundCreator()
	: FileCreator(FileName::untitledName(Sound::defaultFileSuffix()), 0),
	  duration(1.0),
	  sampleRate(Sound::defaultSampleRate()),
	  channels(1),
	  format(Sound::defaultDataType()) {
}

Requester *
SoundCreator::createRequester() {
	return new SoundCreateRequester(this);
}

void
SoundCreator::initialize() {
	Super::initialize();
	duration = max(0.001, duration); // duration never zero
	okIf(setData(new Sound(duration, sampleRate, channels, DataType(format))));
}

//********

class LPCFileCreateRequester : public FileCreateRequester {
	friend LPCFileCreator;
protected:
	LPCFileCreateRequester(LPCFileCreator *);
	redefined void configureRequest(Request *);
private:
	LPCFileCreator* creator() { return (LPCFileCreator *) client; }
};

LPCFileCreateRequester::LPCFileCreateRequester(LPCFileCreator* lfc)
	: FileCreateRequester("Create New LPC Datafile:", lfc) {}

void
LPCFileCreateRequester::configureRequest(Request* request) {
	FileCreateRequester::configureRequest(request);
	request->appendValue("Number of filter poles:", &creator()->nPoles,
			    Range(12, 64));
	request->appendValue("Frame rate (frames per second):",
	                     &creator()->frameRate, PositiveNumbers);
	request->appendValue("Source sample rate:", &creator()->sampleRate,
	                     PositiveIntegers);
}

//********

LPCFileCreator::LPCFileCreator()
	: FileCreator(FileName::untitledName(".lpc"), 32),
	  nPoles(LPCData::defaultNumberOfPoles()),
	  frameRate(LPCData::defaultFrameRate()),
	  sampleRate(Sound::defaultSampleRate()) {}

Requester *
LPCFileCreator::createRequester() {
	return new LPCFileCreateRequester(this);
}

void
LPCFileCreator::initialize() {
	Super::initialize();
	okIf(setData(new LPCData(dataLength(), nPoles, sampleRate, frameRate)));
}

//********

class PvocFileCreateRequester : public FileCreateRequester {
	friend PvocFileCreator;
protected:
	PvocFileCreateRequester(PvocFileCreator *);
	redefined void configureRequest(Request *);
private:
	PvocFileCreator* creator() { return (PvocFileCreator *) client; }
};

PvocFileCreateRequester::PvocFileCreateRequester(PvocFileCreator* pfc)
	: FileCreateRequester("Create New PVoc Datafile:", pfc) {}

void
PvocFileCreateRequester::configureRequest(Request* request) {
	FileCreateRequester::configureRequest(request);
	request->appendValue("Frame rate (frames per second):",
	                     &creator()->frameRate, PositiveNumbers);
	request->appendValue("Source sample rate:", &creator()->sampleRate,
	                     PositiveIntegers);
	request->appendChoice("Number of frequency bands (N/2):",
	                      "|64|128|256|512|1024|2048|4096|8192|16384|",
	                      &creator()->nBands,
	                      true);
}

//********

PvocFileCreator::PvocFileCreator()
	: FileCreator(FileName::untitledName(".pv"), 32),
	  frameRate(PvocData::defaultFrameRate()),
	  sampleRate(Sound::defaultSampleRate()),
	  nBands(PvocData::defaultNumberOfBands() >> 6) {}

Requester *
PvocFileCreator::createRequester() {
	return new PvocFileCreateRequester(this);
}

void
PvocFileCreator::initialize() {
	Super::initialize();
	int npoints = nBands << 6; // maps {1, 2, 4, ..} into {64, 128, ...}
	okIf(setData(new PvocData(dataLength(),
				  (npoints + 1) * 2,
				  sampleRate,
				  frameRate)));
}

//********

EnvelopeFileCreator::EnvelopeFileCreator()
	: FileCreator(FileName::untitledName(".evp"), 512) {}

Requester *
EnvelopeFileCreator::createRequester() {
	return new FileCreateRequester("Create New Envelope:", this);
}

void
EnvelopeFileCreator::initialize() {
	Super::initialize();
	okIf(setData(new Envelope(dataLength())));
}

//********

class FileAccessRequester : public TitledRequester {
	friend FileAccessCommand;
protected:
	FileAccessRequester(const char* title, FileAccessCommand* fac)
		: TitledRequester(title), client(fac) {}
	redefined Request* createRequest();
protected:
	FileAccessCommand* client;
};

Request *
FileAccessRequester::createRequest() {
	return new FileRequest(
		requestTitle(), &client->defaultDir,
		client->defaultSuffixes(), createDelegate()
	);
}

//********

FileAccessCommand::FileAccessCommand(const char* dir)
	: FileCommand(""), defaultDir(dir) {}

//********

class FileSaveRequester : public FileAccessRequester {
	friend FileSaver;
protected:
	FileSaveRequester(FileSaver* fs)
		: FileAccessRequester("Save to File:", fs) {}
	redefined void configureRequest(Request *);
	redefined boolean confirmValues();
private:
	FileSaver* saver() { return (FileSaver *) client; }
};

void
FileSaveRequester::configureRequest(Request* request) {
	request->appendChoice("File format:", "|Raw (no header)|Use header|",
		&saver()->headerFormat, true);
}

boolean
FileSaveRequester::confirmValues() {
	FileSaver* fs = saver();
	// directory name has file appended upon return from browser
	fs->setFileName(fs->defaultDirectory());
	fs->isNewFile = (DiskFile::exists(fs->fileName()) == false);
	boolean confirmed = true;
	if(!fs->isNewFile) {
		Response r = Application::choice(
			scat("The file ", fs->fileName(), " already exists."),
			"Do you wish to overwrite it?", nil, Cancel,
			"force new header", "keep old header", "cancel");
		switch (r) {
		case Yes:
			fs->forceHeader = true; break;
		case No:
			fs->forceHeader = false; break;
		case Cancel:
		default:
			fs->forceHeader = false;
			confirmed = false;
			break;
		};
	}
	return confirmed;
}

//********

extern const char *scat(const char *, const char *);
extern const char *scat(const char *, const char *, const char *);

FileSaver::FileSaver(const char* name, const char* defaultDir, Data* data)
		: FileAccessCommand(defaultDir),
		  dataToSave(data), forceHeader(false) {
	// dont allow saving to "untitled_*" or "tmp_*" temp names
	if(!FileName::isTempName(name)) {
		// prepend default directory to name if none
		const char* filename = (FileName::isFullPathName(name)) ?
			name : scat(defaultDir, "/", name);
		isNewFile = (DiskFile::exists(filename) == false);
		setFileName(filename);
		initialize();
	}
	else isNewFile = true;
}

FileSaver::FileSaver(const char* dir, Data* data) : FileAccessCommand(dir),
	dataToSave(data), headerFormat(WithHeader),
	isNewFile(true), forceHeader(false) {}

Requester *
FileSaver::createRequester() {
	return new FileSaveRequester(this);
}

void
FileSaver::initialize() {
	Super::initialize();
}

const char *
FileSaver::message() {
	return isNewFile ?
		"Writing new file to disk..." : "Overwriting file on disk...";
}

int
FileSaver::doApply() {
	DataFile file;
	// File truncated if new header type is to be forced
	file.open(fileName(), (isNewFile || forceHeader) ? "w" : "r+");
	int status = (file.writable() && dataToSave->write(&file));
	if(status)
		controller()->setFileName(fileName());	// rename file to new name
	else if(isNewFile)
		file.remove();		// if new file was unwritable, delete it
	return status;
}

//********

class SoundFileSaveRequester : public FileSaveRequester {
	friend SoundFileSaver;
protected:
	SoundFileSaveRequester(SoundFileSaver* s) : FileSaveRequester(s) {}
	redefined void configureRequest(Request *);
private:
	SoundFileSaver* saver() { return (SoundFileSaver *) client; }
};

void
SoundFileSaveRequester::configureRequest(Request* request) {
	request->appendChoice("Sound file header type:",
		"|Raw (No Header)|Snd/au|Hybrid|BSD/IRCAM|AIFF-C|WAVE|",
		&saver()->soundHeaderFormat,
		true
	);
}
	
//********

SoundFileSaver::SoundFileSaver(const char* dir, Data* data)
	: FileSaver(dir, data),
	  soundHeaderFormat(SoundHeader::defaultHeaderType()) {}

SoundFileSaver::SoundFileSaver(const char* name, const char* dir, Data* data)
	: FileSaver(name, dir, data),
	  soundHeaderFormat(SoundHeader::defaultHeaderType()) {}

Requester *
SoundFileSaver::createRequester() {
	return new SoundFileSaveRequester(this);
}

void
SoundFileSaver::initialize() {
	Super::initialize();
	SoundHeader::setDefaultHeaderType(SoundHeader::Type(soundHeaderFormat));
}

//********

LPCFileSaver::LPCFileSaver(const char* dir, Data* data)
	: FileSaver(dir, data) {}

LPCFileSaver::LPCFileSaver(const char* name, const char* dir, Data* data)
	: FileSaver(name, dir, data) {}

void
LPCFileSaver::initialize() {
	Super::initialize();
	LPCHeader::setDefaultHeaderType(
		(headerFormat == WithHeader) ? LPCHeader::With : LPCHeader::None
	);
}

//********

class DataDumpRequester : public FileAccessRequester {
	friend DataDumper;
protected:
	DataDumpRequester(DataDumper* d)
		: FileAccessRequester(
			"Dump selection's frame/sample data as ASCII file:", d) {}
	redefined void configureRequest(Request *) {}
};

//********

Requester *
DataDumper::createRequester() {
	return new DataDumpRequester(this);
}

const char *
DataDumper::message() {
	return "Dumping data to file...";
}

int
DataDumper::doApply() {
	int status = true;
	DataFile file;
	file.open(fileName(), "w+");
	if ((status = file.writable()) == true)
		dataToSave->print(file);
	return status;
}

//********

class FileOpenRequester : public FileAccessRequester {
	friend FileOpener;
protected:
	FileOpenRequester(FileOpener* f) : FileAccessRequester(nil, f) {}
	redefined const char* requestTitle();
	redefined void configureRequest(Request*);
	redefined boolean confirmValues();
private:
	FileOpener* opener() { return (FileOpener *) client; }
};

// this overrides TitledRequester::requestTitle()

const char*
FileOpenRequester::requestTitle() { return opener()->theTitle; }

void
FileOpenRequester::configureRequest(Request* request) {
	request->appendValue("Time to skip (secs.): ", &opener()->skipTime,
                         NonNegativeNumbers);
	request->appendValue("Duration to read (secs, 0 = all):",
	                     &opener()->duration,
                         NonNegativeNumbers);
}

boolean
FileOpenRequester::confirmValues() {
	// directory name has file appended upon return from browser
	client->setFileName(client->defaultDirectory());
	return true;
}

//********

FileOpener::FileOpener(const char* dir, const char* title)
	: FileAccessCommand(dir), theTitle(title),
	  skipTime(0), duration(0), newController(nil) {}

Requester *
FileOpener::createRequester() {
	return new FileOpenRequester(this);
}

const char *
FileOpener::message() {
	return nil;		// until other routines redone
}

int
FileOpener::doApply() {
	DataFile* file = new DataFile(fileName(), "r", skipTime, duration);
	file->ref();
	if(file->is_open()) {
		if(file->readable())
			newController = Controller::create(file);
		else
			Application::error(scat("Unable to read file ", file->name()));
	}
	if(newController)
		newController->display(controller()->world());
	Resource::unref(file);
	return (newController != nil);
}