Noah 8 anni fa
parent
commit
8d7a0dba94
3 ha cambiato i file con 65 aggiunte e 81 eliminazioni
  1. 2 5
      audiogram/index.js
  2. 63 6
      audiogram/waveform.js
  3. 0 70
      lib/waveform.js

+ 2 - 5
audiogram/index.js Vedi File

@@ -35,12 +35,9 @@ Audiogram.prototype.getWaveform = function(cb) {
35 35
 
36 36
   this.status("waveform");
37 37
 
38
-  getWaveform(this.audioPath, {
39
-    samplesPerFrame: this.settings.samplesPerFrame,
40
-    maxDuration: this.settings.maxDuration
41
-  }, function(err, waveform){
38
+  getWaveform(this.audioPath, this.settings, function(err, waveform){
42 39
 
43
-    self.set("numFrames", self.numFrames = self.settings.waveform.length);
40
+    self.set("numFrames", self.numFrames = waveform.length);
44 41
 
45 42
     return cb(err, self.settings.waveform = waveform);
46 43
 

+ 63 - 6
audiogram/waveform.js Vedi File

@@ -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
 }

+ 0 - 70
lib/waveform.js Vedi File

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