|
@@ -1,5 +1,6 @@
|
1
|
1
|
var probe = require("../lib/probe.js"),
|
2
|
|
- processWaveform = require("../lib/waveform.js");
|
|
2
|
+ d3 = require("d3"),
|
|
3
|
+ pcm = require("../lib/pcm.js");
|
3
|
4
|
|
4
|
5
|
function getWaveform(filename, options, cb) {
|
5
|
6
|
|
|
@@ -13,12 +14,68 @@ function getWaveform(filename, options, cb) {
|
13
|
14
|
return cb("Exceeds max duration of " + options.maxDuration + "s");
|
14
|
15
|
}
|
15
|
16
|
|
16
|
|
- processWaveform(filename, {
|
17
|
|
- numFrames: Math.floor(data.duration * options.samplesPerFrame),
|
18
|
|
- samplesPerFrame: options.samplesPerFrame,
|
19
|
|
- channels: data.channels
|
20
|
|
- }, cb);
|
|
17
|
+ var stream = pcm(filename, {
|
|
18
|
+ channels: options.channels
|
|
19
|
+ }),
|
|
20
|
+ samples = [];
|
21
|
21
|
|
|
22
|
+ stream.on("data",function(sample, channel){
|
|
23
|
+
|
|
24
|
+ // Average multiple channels
|
|
25
|
+ if (channel > 0) {
|
|
26
|
+ samples[samples.length - 1] = ((samples[samples.length - 1] * channel) + sample) / (channel + 1);
|
|
27
|
+ } else {
|
|
28
|
+ samples.push(sample);
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ });
|
|
32
|
+
|
|
33
|
+ stream.on("error", cb);
|
|
34
|
+
|
|
35
|
+ stream.on("end", function(output){
|
|
36
|
+ var processed = processSamples(samples, options.numFrames, options.samplesPerFrame);
|
|
37
|
+ return cb(null, processed);
|
|
38
|
+ });
|
|
39
|
+
|
|
40
|
+ });
|
|
41
|
+
|
|
42
|
+}
|
|
43
|
+
|
|
44
|
+function processSamples(samples, numFrames, samplesPerFrame) {
|
|
45
|
+
|
|
46
|
+ // TODO spread out slop across frames
|
|
47
|
+ var perFrame = Math.floor(samples.length / numFrames),
|
|
48
|
+ perPoint = Math.floor(perFrame / samplesPerFrame),
|
|
49
|
+ range = d3.range(samplesPerFrame),
|
|
50
|
+ min = max = 0;
|
|
51
|
+
|
|
52
|
+ var unadjusted = d3.range(numFrames).map(function(frame){
|
|
53
|
+
|
|
54
|
+ var frameSamples = samples.slice(frame * perFrame, (frame + 1) * perFrame);
|
|
55
|
+
|
|
56
|
+ return range.map(function(point){
|
|
57
|
+
|
|
58
|
+ var pointSamples = frameSamples.slice(point * perPoint, (point + 1) * perPoint),
|
|
59
|
+ localMin = localMax = 0;
|
|
60
|
+
|
|
61
|
+ for (var i = 0, l = pointSamples.length; i < l; i++) {
|
|
62
|
+ localMin = Math.min(localMin, pointSamples[i]);
|
|
63
|
+ localMax = Math.min(localMax, pointSamples[i]);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ min = Math.min(min, localMin);
|
|
67
|
+ max = Math.min(max, localMax);
|
|
68
|
+
|
|
69
|
+ return [localMin, localMax];
|
|
70
|
+
|
|
71
|
+ });
|
|
72
|
+
|
|
73
|
+ });
|
|
74
|
+
|
|
75
|
+ return unadjusted.map(function(frame){
|
|
76
|
+ return frame.map(function(point){
|
|
77
|
+ return [point[0] / min, point[1] / max];
|
|
78
|
+ });
|
22
|
79
|
});
|
23
|
80
|
|
24
|
81
|
}
|