| 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 | 1×
1×
2×
 
 
1×
2×
 
 
1×
2×
2×
2×
 
2×
 
 
1×
2×
 
 
1×
2×
 
 
1×
9×
 
 
1×
2×
 
 
1×
 
 
 
1×
1×
4×
2×
2×
2×
 
 
 
 
 
1×
4×
 
 
1×
4×
4×
4×
 
4×
 
 
1×
4×
 
 
1×
4×
 
 
1×
18×
 
 
1×
4×
 
 
1×
 
 
 
1×
1×
3×
3×
1×
 
 
 
1×
2×
 
 
1×
2×
2×
2×
2×
 
2×
 
 
1×
4×
4×
4×
18×
 
4×
 
 
1×
2×
 
 
1×
2×
 
 
1×
9×
 
 
1×
2×
 
 
1×
 
 
 
1×
1×
19×
19×
19×
3×
 
 
 
1×
 
1×
 
 
 
1×
1×
11×
11×
11×
10×
1×
 
9×
 
 
1×
1×
 
1×
26×
26×
25×
 
1×
 
26×
 
 
1×
 
 
 
1×
1×
4×
 
 
1×
1×
 
1×
 
 
 
1×
1×
6×
6×
 
 
1×
1×
1×
 
 
1×
2×
2×
2×
 
 
2×
 
 
1×
2×
 
 
1×
 
 
 
1×
1×
52×
52×
52×
52×
24×
 
28×
 
52×
 
 
1×
44×
44×
 
44×
44×
24×
 
44×
44×
2244×
2244×
1224×
 
2244×
 
44×
 
 
1×
1224×
1224×
1224×
 
 
1224×
 
 
1×
 
 
 
1×
1×
3×
3×
2×
2×
1×
 
 
2×
 
1×
 
 
 
1×
1×
 
1×
 
1×
3×
 
3×
 
 
3×
 
 
 
1×
3×
 
 
1×
 
 
 
1×
1×
1×
 
1×
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1×
6×
 
 
 
 
 
6×
 
2×
2×
2×
1×
 
 
2×
 
1×
1×
1×
 
2×
2×
2×
1×
 
 
 
 
2×
 
1×
 
 
5×
 
 
1×
 
 
 
1×
4×
4×
 
 
 
 
 
 
 
 
 
1×
3×
3×
3×
12×
12×
 
3×
3× | /* The common DataWrapper 'interface' is validated via the tests
   Note: it is not done as a superclass (as PitchMapper is below) because
         there's really nothing in common implementation-wise; only the
         interface is shared.
var DataWrapper = (function() {
	function DataWrapper(data) {
		throw Error('Please use a derived object.');
	}
 
	DataWrapper.prototype.num_series = function() {};
	DataWrapper.prototype.series_names = function() {};
	DataWrapper.prototype.series_min = function(series) {};
	DataWrapper.prototype.series_max = function(series) {};
	DataWrapper.prototype.series_value = function(series, index) {};
	DataWrapper.prototype.series_length = function(series) {};
 
	return DataWrapper;
})();*/
 
 
var GoogleDataWrapper = (function() {
	function GoogleDataWrapper(data) {
		this.data = data;
	}
 
	GoogleDataWrapper.prototype.num_series = function() {
		return this.data.getNumberOfColumns() - 1;
	};
 
	GoogleDataWrapper.prototype.series_names = function() {
		var results = [];
		for (var i = 0; i < this.data.getNumberOfColumns() - 1; i++) {
			results.push(this.data.getColumnLabel(i));
		}
		return results;
	};
 
	GoogleDataWrapper.prototype.series_min = function(series) {
		return this.data.getColumnRange(series + 1).min;
	};
 
	GoogleDataWrapper.prototype.series_max = function(series) {
		return this.data.getColumnRange(series + 1).max;
	};
 
	GoogleDataWrapper.prototype.series_value = function(series, index) {
		return this.data.getValue(index, series + 1);
	};
 
	GoogleDataWrapper.prototype.series_length = function(series) {
		return this.data.getNumberOfRows();
	};
 
	return GoogleDataWrapper;
})();
 
 
var JSONDataWrapper = (function() {
	function JSONDataWrapper(json) {
		if (typeof json === 'string') {
			this.object = JSON.parse(json);
		} else Eif (typeof json === 'object') {
			this.object = json;
		} else {
			throw Error("Please provide a JSON string or derived object.");
		}
	}
 
	JSONDataWrapper.prototype.num_series = function() {
		return this.object.data.length;
	};
 
	JSONDataWrapper.prototype.series_names = function() {
		var results = [];
		for (var i = 0; i < this.object.data.length; i++) {
			results.push(this.object.data[i].series);
		}
		return results;
	};
 
	JSONDataWrapper.prototype.series_min = function(series) {
		return Math.min.apply(this, this.object.data[series].values);
	};
 
	JSONDataWrapper.prototype.series_max = function(series) {
		return Math.max.apply(this, this.object.data[series].values);
	};
 
	JSONDataWrapper.prototype.series_value = function(series, index) {
		return this.object.data[series].values[index];
	};
 
	JSONDataWrapper.prototype.series_length = function(series) {
		return this.object.data[series].values.length;
	};
 
	return JSONDataWrapper;
})();
 
 
var HTMLTableDataWrapper = (function() {
	function HTMLTableDataWrapper(table) {
		this.table = table;
		if (!this.table) {
			throw Error("No table given.");
		}
	}
 
	HTMLTableDataWrapper.prototype.num_series = function() {
		return this.table.getElementsByTagName('tr')[0].children.length;
	};
 
	HTMLTableDataWrapper.prototype.series_names = function() {
		var header_cells = this.table.getElementsByTagName('th');
		var results = [];
		for (var i = 0; i < header_cells.length; i++) {
			results.push(header_cells[i].textContent);
		}
		return results;
	};
 
	HTMLTableDataWrapper.prototype._series_floats = function(series) {
		var data_cells = this.table.getElementsByTagName('td');
		var results = [];
		for (var i = 0; i < data_cells.length; i++) {
			results.push(parseFloat(data_cells[i].textContent));
		}
		return results;
	};
 
	HTMLTableDataWrapper.prototype.series_min = function(series) {
		return Math.min.apply(this, this._series_floats(series));
	};
 
	HTMLTableDataWrapper.prototype.series_max = function(series) {
		return Math.max.apply(this, this._series_floats(series));
	};
 
	HTMLTableDataWrapper.prototype.series_value = function(series, index) {
		return parseFloat(this.table.getElementsByTagName('tr')[index + 1].children[series].textContent);
	};
 
	HTMLTableDataWrapper.prototype.series_length = function(series) {
		return this.table.getElementsByTagName('tr').length - 1;
	};
 
	return HTMLTableDataWrapper;
})();
 
 
var PitchMapper = (function() {
	function PitchMapper(minimum_datum, maximum_datum) {
		this.minimum_datum = minimum_datum;
		this.maximum_datum = maximum_datum;
		if (this.minimum_datum > this.maximum_datum) {
			throw Error('minimum datum should be <= maximum datum');
		}
	}
 
	PitchMapper.prototype.map = function(datum) {};  // FIXME naming conflict?
 
	return PitchMapper;
})();
 
 
var FrequencyPitchMapper = (function() {
	function FrequencyPitchMapper(minimum_datum, maximum_datum, minimum_frequency, maximum_frequency) {
		this.minimum_frequency = minimum_frequency;
		this.maximum_frequency = maximum_frequency;
		PitchMapper.call(this, minimum_datum, maximum_datum);
		if (this.minimum_frequency > this.maximum_frequency) {
			throw Error('minimum frequency should be <= maximum frequency');
		}
		this.data_range = this.maximum_datum - this.minimum_datum;
	}
 
	FrequencyPitchMapper.prototype = Object.create(PitchMapper.prototype);
	FrequencyPitchMapper.prototype.constructor = FrequencyPitchMapper;
 
	FrequencyPitchMapper.prototype.map = function(datum) {
		var ratio;
		if (this.data_range) {
			ratio = (datum - this.minimum_datum) / this.data_range;
		} else {
			ratio = 0.5;
		}
		return this.minimum_frequency + ratio * (this.maximum_frequency - this.minimum_frequency);
	};
 
	return FrequencyPitchMapper;
})();
 
 
var NotePitchMapper = (function() {
	function NotePitchMapper() {
		return PitchMapper.apply(this, arguments);
	}
 
	NotePitchMapper.prototype = Object.create(PitchMapper.prototype);
	NotePitchMapper.prototype.constructor = NotePitchMapper;
 
	return NotePitchMapper;
})();
 
 
var WebAudioSounder = (function() {
	function WebAudioSounder(context) {
		this.context = context;
		this.oscillator = this.context.createOscillator();
	}
 
	WebAudioSounder.prototype.start = function() {
		this.oscillator.connect(this.context.destination);
		this.oscillator.start(0);
	};
 
	WebAudioSounder.prototype.frequency = function(frequency, offset) {
		var callback = (function(that) {
			return function() {
				that.oscillator.frequency.value = frequency;
			};
		})(this);
		setTimeout(callback, offset);
	};
 
	WebAudioSounder.prototype.stop = function(offset) {
		this.oscillator.stop(this.context.currentTime + offset);
	};
 
	return WebAudioSounder;
})();
 
 
var Player = (function() {
	function Player(duration, data, pitch_mapper, sounder, visual_callback) {
		this.data = data;
		this.pitch_mapper = pitch_mapper;
		this.sounder = sounder;
		if (arguments.length < 5) {
			this.visual_callback = null;
		} else {
			this.visual_callback = visual_callback;
		}
		this.interval = duration / this.data.series_length(0);
	}
 
	Player.prototype.play = function() {
		var series_length = this.data.series_length(0);
		var series_max_index = series_length - 1;
 
		this.sounder.start(0);
		if (this.visual_callback !== null) {
			this.visual_callback(0, 0);
		}
		this.sounder.frequency(this.pitch_mapper.map(this.data.series_value(0, 0)));
		for (var i = 1; i <= series_max_index; i++) {
			var offset = this.interval * i;
			if (this.visual_callback !== null) {
				this._highlight_enqueue(0, i, offset);
			}
			this.sounder.frequency(this.pitch_mapper.map(this.data.series_value(0, i)), offset);
		}
		this.sounder.stop((series_length * this.interval) / 1000);
	};
 
	Player.prototype._highlight_enqueue = function(series, row, offset) {
		var callback = (function(that) {
			return function() {
				that.visual_callback(series, row);
			};
		})(this);
		setTimeout(callback, offset);
	};
 
	return Player;
})();
 
 
var AudioChart = (function() {
	function AudioChart(options, context) {
		var fail = "Sorry, your browser doesn't support the Web Audio API.";
		if (arguments.length < 2) {
			context = AudioContextGetter.get();
			if (context === null) {
				throw Error(fail);
			}
		}
		return _AudioChart(options, context);
	}
	return AudioChart;
})();
 
 
var AudioContextGetter = (function() {
	function AudioContextGetter() {}
 
	var audio_context = null;
 
	var _get_audio_context = function() {
		Iif (window.AudioContext !== undefined) {
			return new window.AudioContext();
		} else Iif (window.webkitAudioContext !== undefined) {
			return new window.webkitAudioContext();
		} else {
			return null;
		}
	};
 
	AudioContextGetter.get = function() {
		return audio_context !== null ? audio_context : audio_context = _get_audio_context();
	};
 
	return AudioContextGetter;
})();
 
 
var _AudioChart = (function() {
	function _AudioChart(options, context) {
		var data_wrapper, callback, frequency_pitch_mapper, sounder, player;
 
		var result = _AudioChart._assign_wrapper_callback(options);
		data_wrapper = new result.wrapper(result.parameter);
		callback = result.callback;
 
		frequency_pitch_mapper = new FrequencyPitchMapper(
			data_wrapper.series_min(0),
			data_wrapper.series_max(0),
			options.frequency_low,
			options.frequency_high
		);
 
		sounder = new WebAudioSounder(context);
 
		player = new Player(
			options.duration,
			data_wrapper,
			frequency_pitch_mapper,
			sounder,
			callback
		);
 
		player.play();
	}
 
	// This is being done as a sort of 'class/static method' because
	// it doesn't need 'this'.
	// http://stackoverflow.com/a/1635143
	_AudioChart._assign_wrapper_callback = function(options) {
		var result = {
			'wrapper': null,
			'parameter': null,
			'callback': null
		};
 
		switch (options.type) {
			case 'google':
				result.wrapper = GoogleDataWrapper;
				result.parameter = options.data;
				if (options.hasOwnProperty('chart')) {
					result.callback =
						google_visual_callback_maker(options.chart);
				}
				break;
			case 'json':
				result.wrapper = JSONDataWrapper;
				result.parameter = options.data;
				break;
			case 'html_table':
				result.wrapper = HTMLTableDataWrapper;
				result.parameter = options.table;
				if (options.hasOwnProperty('highlight_class')) {
					result.callback = html_table_visual_callback_maker(
						options.table,
						options.highlight_class
					);
				}
				break;
			default:
				throw Error("Invalid data type '" + options.type + "' given.");
		}
 
		return result;
	};
 
	return _AudioChart;
})();
 
 
var google_visual_callback_maker = function(chart) {
	return function(series, row) {
		chart.setSelection([
				{
					'row': row,
					'column': series + 1
				}
		]);
	};
};
 
 
var html_table_visual_callback_maker = function(table, class_name) {
	return function(series, row) {
		var tds = table.getElementsByTagName('td');
		for (var i = 0; i < tds.length; i++) {
			cell = tds[i];
			cell.className = '';
		}
		cell = table.getElementsByTagName('td')[row];
		cell.className = class_name;
	};
};
  |