File: interpolater.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 (270 lines) | stat: -rw-r--r-- 7,784 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
// interpolater.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 "editor.h"
#include "ellipfilt.h"
#include "interpolater.h"
#include "localdefs.h"
#include "data.h"
#include "request.h"
#include "pitchconv.h"
#include "valuerequester.h"

class InterpolateRequester : public ValueRequester<double> {
	friend Interpolater;
protected:
	InterpolateRequester(Interpolater* i)
		: ValueRequester<double>("Interpolate Selected Region to New Length:",
		                 "Gain Factor:", i->gain),
		  client(i) {}
	redefined void configureRequest(Request *);
	redefined boolean confirmValues();
protected:
	Interpolater* client;
};

void
InterpolateRequester::configureRequest(Request* request) {
	request->appendLabel("(Use either a Factor or Enter Desired Length)");
	request->appendValue(
		"Interpolatation Factor:",
		&client->theInterpFactor,
		PositiveNumbers
	);
	request->appendValue("New Length:", &client->theNewLength,
	                     PositiveIntegers);
	ValueRequester<double>::configureRequest(request);
}

boolean
InterpolateRequester::confirmValues() {
	double ifactor = client->interpFactor();
	if(ifactor == 0.0) {
		Application::alert(
			"Zero is an illegal value for interpolation factor.");
		return false;
	}
	int oldLength = client->getInput()->length();
	ifactor = (ifactor != 1.0) ?
		1.0/ifactor : double(oldLength)/client->theNewLength;
	client->setInterpFactor(ifactor);
	return true;
}

//********

class TransposeRequester : public ValueRequester<double> {
	friend Interpolater;
protected:
	TransposeRequester(Interpolater* i)
		: ValueRequester<double>("Transpose Selected Region:", "Gain Factor:", i->gain),
		  client(i), theTransposeMode(OctavePoint) {}
	redefined void configureRequest(Request *);
	redefined boolean confirmValues();
private:
	enum TransposeMode { Ratio = 1, OctavePoint = 2, LinearOctave = 4 };
	Interpolater* client;
	ChoiceValue theTransposeMode;
};

void
TransposeRequester::configureRequest(Request* request) {
	request->appendValue("Transposition Interval/Ratio:",
	                     &client->theInterpFactor);
	request->appendChoice(
		"Transposition mode:",
		"|Ratio|Octave Pt. P.C.|Linear Octave P.C.|",
		&theTransposeMode,
		true
	);
	request->appendChoice(
		"Apply Tracking Filter:",
		"|Use Filter|",
		&client->useFilter,
		false);
	ValueRequester<double>::configureRequest(request);
}

boolean
TransposeRequester::confirmValues() {
	double factor = client->interpFactor();
	double ifactor = 0;
	switch(theTransposeMode) {
	case Ratio:
		ifactor = factor;
		break;
	case OctavePoint:
		factor = Frequency::octaveFromPitch(factor);
		ifactor = Frequency::cpsFromOctave(10.0+factor) / Frequency::cpsFromOctave(10.0);  
		break;
	case LinearOctave:
		ifactor = Frequency::cpsFromOctave(10.0+factor) / Frequency::cpsFromOctave(10.0);  
		break;
	default:
		Application::die("TransposeRequester::confirmValues: invalid choice");
		break;
	}
	if(ifactor <= 0.0) {
		Application::alert("Ratio mode value must be a positive number.");
		return false;
	}
	client->setInterpFactor(ifactor);
	return true;
}

//********

Interpolater::Interpolater(Data* input, Data* output, double factor,
	boolean filt)
	: InputOutputFunction(input, output, InputQLen, OutputQLen),
		processMode(None), theNewLength(input->length()),
	  	theInterpFactor(factor), gain(1.0), useFilter(filt),
		filter(nil) {
	initialize();
}

Interpolater::Interpolater(Data* input, Data* output, int newlen, boolean filt)
	: InputOutputFunction(input, output, InputQLen, OutputQLen),
		processMode(None),
		theNewLength(newlen),
		theInterpFactor(double(input->length())/newlen),
		gain(1.0), useFilter(filt), filter(nil) {
	initialize();
}

Interpolater::Interpolater(Data* input, Data* output, boolean transposing)
	: InputOutputFunction(input, output, InputQLen, OutputQLen),
		processMode(None),
		theNewLength(input->length()),
		theInterpFactor(1.0),
		gain(1.0), 
		useFilter(false), transpose(transposing), filter(nil) {}

Interpolater::~Interpolater() { delete filter; }

Requester *
Interpolater::createRequester() {
	if(transposing())
		return new TransposeRequester(this);
	else
		return new InterpolateRequester(this);
}

const char *
Interpolater::message() {
	return transposing() ? 
		(filtering() ? "Transposing and filtering..." : "Transposing...")
		: "Interpolating...";
}

Modifier *
Interpolater::create(DataEditor *ed) {
//	return new Interpolater(ed->currentSelection(), ed->copyBuffer());
	return nil;
}

boolean
Interpolater::areCompatibleLengths(int outlen, int newlen) {
	return outlen == newlen;
}

double
Interpolater::interpolate(double y_0, double y_1, double y_2, double fraction) {
	register double hy_2, hy_0, a, b, c;
	a = y_0;
	hy_0 = y_0/2.0;
	hy_2 =  y_2/2.0;
	b = (-3.0 * hy_0) + (2.0 * y_1) - hy_2;
	c = hy_0 - y_1 + hy_2;
	return(a + b * fraction + c * fraction * fraction);
}

void
Interpolater::restoreState() {
	Super::restoreState();
	clear();
	if(filter)
		filter->restoreState();
}

inline double
Interpolater::preProcess(double input) {
	return (processMode == Pre) ? (*filter)(input) : input;
}

inline double
Interpolater::postProcess(double input) {
	return (processMode == Post) ? (*filter)(input) : input;
}

int
Interpolater::processInput(boolean flushing) {
	BUG("Interpolater::processInput()");
	// if interpolating counter is approaching input counter, get new input
	if((fractionCounter - double(incount)) >= -0.5) {
		voldsig = oldsig;
		oldsig = sig;
		sig = preProcess(flushing ? 0.0 : takeFromInQueue() * gain);
		increment();
	}
	// while fractioncounter is lagging behind, do interp and increment
	while((double(incount) - fractionCounter) > 0.5) {
		double fraction = fractionCounter - incount + 2.0;
		double output = interpolate(voldsig, oldsig, sig, fraction);
		fractionCounter += interpFactor();
		if(addToOutQueue(postProcess(output)) == false)
			return false;	// not done writing to output queue
	}
	return true;			// done writing to output queue
}

void
Interpolater::initialize() {
	clear();
	Super::initialize();
	// to avoid overflow in short int samples due to 2nd order interp
	double ifactor = interpFactor();
	gain *= (ifactor >= 1.0) ? 1.0 : .9996;
	if(useFilter) {
		double cutoff = 0.0;
		if(ifactor > 1.0) {		// transposing upwards
			cutoff = sampRate() / (2.0 * ifactor);
			processMode = Pre;	// do processing before interpolation
		}
		else if(ifactor < 1.0) {	// transposing downwards
			cutoff = sampRate()  * ifactor / 2.0;
			processMode = Post;	// do processing after interpolation
		}
		if(processMode != None)
			filter = new EllipticalFilter(
				target(), .95 * cutoff, cutoff, 0.0, 1.0, 90.0
			);
	}
}