forked from katspaugh/wavesurfer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawer.split-wave-point-plot.js
371 lines (320 loc) · 14.1 KB
/
drawer.split-wave-point-plot.js
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
/**
* This is a drawer which extends the canvas drawer but adds a separate point plot graph
* so two separate forms are drawn, one the wave and the other the plot.
* The plot information needs to be sent as an array of objects containing the time and value
* e.g. plotArray: [{time: 1.01, value: 0.12},{time: 1.03}, value: 0.14}]
* or via the url of a file which contains the plot data
* e.g
* 1.01 0.12
* 1.03 0.14
*
*
* Params which can be sent in as initialization values are the following
* - plotArray: array of objects with time and plot (required unless plotFileUrl is set)
* - plotFileUrl: url of the file that contains the plot information (required unless plotArray is set)
* - plotNormalizeTo: [whole/segment/none/values] - what value to normalize the plot to (defaults to "whole")
* - plotMin: the minimum value to normalize points to. Any value below this will be ignored (defaults to 0)
* - plotMax: the maximum value to normalize points to. Any value above this will be ignored (defaults to 1)
* - plotTimeStart: the time included in the plot file which corresponds with the start of the displayed wave (defaults to 0)
* - plotTimeEnd: the time included in the plot file which corresponds with the end of the displayed wave (defaults maximum plot time)
* - plotColor: the color of the plot (defaults to #f63)
* - plotProgressColor : the color of the progress plot (defaults to '#F00')
* - plotFileDelimiter: the delimiter which separates the time from the value in the plot file (defaults to tab characater = "\t")
* - plotPointHeight: the canvas height of each plot point (defaults to 2)
* - plotPointWidth: the canvas width of each plot point (defaults to 2)
* - plotSeparator: boolean indicating a separator should be included between the wave and point plot
* - plotRangeDisplay: boolean indicating if the min and max range should be displayeda (defaults to false)
* - plotRangePrecision: integer determining the precision of the displayed plot range
* - plotRangeUnits: units appended to the range
* - plotRangeFontSize: the font for displaying the range - defaults to 20
* - plotRangeFontType: the font type for displaying range - defaults to Ariel
* - plotRangeIgnoreOutliers: boolean indicating if values outside of range should be ignored or plotted at min/max
*/
'use strict';
WaveSurfer.Drawer.SplitWavePointPlot = Object.create(WaveSurfer.Drawer.Canvas);
WaveSurfer.util.extend(WaveSurfer.Drawer.SplitWavePointPlot, {
defaultPlotParams: {
plotNormalizeTo: 'whole',
plotTimeStart: 0,
plotMin: 0,
plotMax: 1,
plotColor : '#f63',
plotProgressColor : '#F00',
plotPointHeight: 2,
plotPointWidth: 2,
plotSeparator: true,
plotSeparatorColor: 'black',
plotRangeDisplay: false,
plotRangeUnits: '',
plotRangePrecision: 4,
plotRangeIgnoreOutliers: false,
plotRangeFontSize: 12,
plotRangeFontType: 'Ariel',
waveDrawMedianLine: true,
plotFileDelimiter: '\t'
},
//object variables that get manipulated by various object functions
plotTimeStart: 0, //the start time of our wave according to plot data
plotTimeEnd: -1, //the end of our wave according to plot data
plotArrayLoaded: false,
plotArray: [], //array of plot data objects containing time and plot value
plotPoints: [], //calculated average plot points corresponding to value of our wave
plotMin: 0,
plotMax: 1,
/**
* Initializes the plot array. If params.plotFileUrl is provided an ajax call will be
* executed and drawing of the wave is delayed until plot info is retrieved
* @param params
*/
initDrawer: function (params) {
var my = this;
//set defaults if not passed in
for(var paramName in this.defaultPlotParams) {
if(this.params[paramName] === undefined) {
this.params[paramName] = this.defaultPlotParams[paramName];
}
}
//set the plotTimeStart
this.plotTimeStart = this.params.plotTimeStart;
//check to see if plotTimeEnd
if(this.params.plotTimeEnd !== undefined) {
this.plotTimeEnd = this.params.plotTimeEnd;
}
//set the plot array
if (Array.isArray(params.plotArray)) {
this.plotArray = params.plotArray;
this.plotArrayLoaded = true;
}
//Need to load the plot array from ajax with our callback
else {
var onPlotArrayLoaded = function (plotArray) {
my.plotArray = plotArray;
my.plotArrayLoaded = true;
my.fireEvent('plot_array_loaded');
};
this.loadPlotArrayFromFile(params.plotFileUrl, onPlotArrayLoaded, this.params.plotFileDelimiter);
}
},
/**
* Draw the peaks - this overrides the drawer.js function and does the following additional steps
* - ensures that the plotArray has already been loaded, if not it loads via ajax
* - moves the wave form to where channel 1 would normally be
* @param peaks
* @param length
* @param start
* @param end
*/
drawPeaks: function (peaks, length, start, end) {
//make sure that the plot array is already loaded
if (this.plotArrayLoaded == true) {
this.setWidth(length);
//fake that we are splitting channels
this.splitChannels = true;
this.params.height = this.params.height/2;
if (peaks[0] instanceof Array) {
peaks = peaks[0];
}
this.params.barWidth ?
this.drawBars(peaks, 1, start, end) :
this.drawWave(peaks, 1, start, end);
//set the height back to the original
this.params.height = this.params.height*2;
this.calculatePlots();
this.drawPlots();
}
//otherwise wait for the plot array to be loaded and then draw again
else {
var my = this;
my.on('plot-array-loaded', function () {
my.drawPeaks(peaks, length, start, end);
});
}
},
/**
* Loop through the calculated plot values and actually draw them
*/
drawPlots: function() {
var height = this.params.height * this.params.pixelRatio / 2;
var $ = 0.5 / this.params.pixelRatio;
this.waveCc.fillStyle = this.params.plotColor;
if(this.progressCc) {
this.progressCc.fillStyle = this.params.plotProgressColor;
}
for(var i in this.plotPoints) {
var x = parseInt(i);
var y = height - this.params.plotPointHeight - (this.plotPoints[i] * (height - this.params.plotPointHeight));
var pointHeight = this.params.plotPointHeight;
this.waveCc.fillRect(x, y, this.params.plotPointWidth, pointHeight);
if(this.progressCc) {
this.progressCc.fillRect(x, y, this.params.plotPointWidth, pointHeight);
}
}
//draw line to separate the two waves
if(this.params.plotSeparator) {
this.waveCc.fillStyle = this.params.plotSeparatorColor;
this.waveCc.fillRect(0, height, this.width, $);
}
if(this.params.plotRangeDisplay) {
this.displayPlotRange();
}
},
/**
* Display the range for the plot graph
*/
displayPlotRange: function()
{
var fontSize = this.params.plotRangeFontSize * this.params.pixelRatio;
var maxRange = this.plotMax.toPrecision(this.params.plotRangePrecision) + ' ' + this.params.plotRangeUnits;
var minRange = this.plotMin.toPrecision(this.params.plotRangePrecision) + ' ' + this.params.plotRangeUnits;
this.waveCc.font = fontSize.toString() + 'px ' + this.params.plotRangeFontType;
this.waveCc.fillText(maxRange, 3, fontSize);
this.waveCc.fillText(minRange, 3, this.height/2);
},
/**
* This function loops through the plotArray and converts it to the plot points
* to be drawn on the canvas keyed by their position
*/
calculatePlots: function() {
//reset plots array
this.plotPoints = {};
//make sure we have our plotTimeEnd
this.calculatePlotTimeEnd();
var pointsForAverage = [];
var previousWaveIndex = -1;
var maxPlot = 0;
var minPlot = 99999999999999;
var maxSegmentPlot = 0;
var minSegmentPlot = 99999999999999;
var duration = this.plotTimeEnd - this.plotTimeStart;
//loop through our plotArray and map values to wave indexes and take the average values for each wave index
for(var i = 0; i < this.plotArray.length; i++) {
var dataPoint = this.plotArray[i];
if(dataPoint.value > maxPlot) {maxPlot = dataPoint.value;}
if(dataPoint.value < minPlot) {minPlot = dataPoint.value;}
//make sure we are in the specified range
if(dataPoint.time >= this.plotTimeStart && dataPoint.time <= this.plotTimeEnd) {
//get the wave index corresponding to the data point
var waveIndex = Math.round(this.width * (dataPoint.time - this.plotTimeStart) / duration);
pointsForAverage.push(dataPoint.value);
//if we have moved on to a new position in our wave record average and reset previousWaveIndex
if(waveIndex !== previousWaveIndex) {
if(pointsForAverage.length > 0) {
//get the average plot for this point
var avgPlot = this.avg(pointsForAverage);
//check for min max
if(avgPlot > maxSegmentPlot) {maxSegmentPlot = avgPlot;}
if(avgPlot < minSegmentPlot) {minSegmentPlot = avgPlot;}
//add plot to the position
this.plotPoints[previousWaveIndex] = avgPlot;
pointsForAverage = [];
}
}
previousWaveIndex = waveIndex;
}
}
//normalize the plots points
if(this.params.plotNormalizeTo == 'whole') {
this.plotMin = minPlot;
this.plotMax = maxPlot;
}
else if(this.params.plotNormalizeTo == 'values') {
this.plotMin = this.params.plotMin;
this.plotMax = this.params.plotMax;
}
else {
this.plotMin = minSegmentPlot;
this.plotMax = maxSegmentPlot;
}
this.normalizeValues();
},
/**
* Function to take all of the plots in this.plots and normalize them from 0 to one
* depending on this.plotMin and this.plotMax values
*/
normalizeValues: function() {
var normalizedValues = {};
//check to make sure we should be normalizing
if(this.params.plotNormalizeTo === 'none') {return;}
for(var i in this.plotPoints) {
//get the normalized value between 0 and 1
var normalizedValue = (this.plotPoints[i] - this.plotMin) / (this.plotMax - this.plotMin);
//check if the value is above our specified range max
if(normalizedValue > 1) {
if(!this.params.plotRangeIgnoreOutliers) {
normalizedValues[i] = 1;
}
}
//check if hte value is below our specified rant
else if(normalizedValue < 0) {
if(!this.params.plotRangeIgnoreOutliers) {
normalizedValues[i] = 0;
}
}
//in our range add the normalized value
else {
normalizedValues[i] = normalizedValue;
}
}
this.plotPoints = normalizedValues;
},
/**
*
*/
/**
* Function to load the plot array from a external file
*
* The text file should contain a series of lines.
* Each line should contain [audio time] [delimiter character] [plot value]
* e.g. "1.2355 [tab] 124.2321"
*
* @param plotFileUrl url of the file containing time and value information
* @param onSuccess function to run on success
* @param delimiter the delimiter that separates the time and values on each line
*/
loadPlotArrayFromFile: function(plotFileUrl, onSuccess, delimiter) {
//default delimiter to tab character
if (delimiter === undefined) {delimiter = '\t';}
var plotArray = [];
var options = {
url: plotFileUrl,
responseType: 'text'
};
var fileAjax = WaveSurfer.util.ajax(options);
fileAjax.on('load', function (data) {
if (data.currentTarget.status == 200) {
//split the file by line endings
var plotLines = data.currentTarget.responseText.split('\n');
//loop through each line and find the time and plot values (delimited by tab)
for (var i = 0; i < plotLines.length; i++) {
var plotParts = plotLines[i].split(delimiter);
if(plotParts.length == 2) {
plotArray.push({time: parseFloat(plotParts[0]), value: parseFloat(plotParts[1])});
}
}
//run success function
onSuccess(plotArray);
}
});
},
/***
* Calculate the end time of the plot
*/
calculatePlotTimeEnd: function() {
if(this.params.plotTimeEnd !== undefined) {
this.plotTimeEnd = this.params.plotTimeEnd;
}
else {
this.plotTimeEnd = this.plotArray[this.plotArray.length -1].time;
}
},
/**
* Quick convenience function to average numbers in an array
* @param array of values
* @returns {number}
*/
avg: function(values) {
var sum = values.reduce(function(a, b) {return a+b;});
return sum/values.length;
}
});
WaveSurfer.util.extend(WaveSurfer.Drawer.SplitWavePointPlot, WaveSurfer.Observer);