Noah преди 7 години
родител
ревизия
b713f69e31
променени са 2 файла, в които са добавени 44 реда и са изтрити 27 реда
  1. 15 5
      audiogram/waveform.js
  2. 29 22
      test/waveform-test.js

+ 15 - 5
audiogram/waveform.js Целия файл

@@ -34,7 +34,6 @@ function getWaveform(filename, options, cb) {
34 34
 
35 35
     stream.on("end", function(output){
36 36
       var processed = processSamples(samples, Math.floor(data.duration * options.framesPerSecond), options.samplesPerFrame);
37
-      console.log(processed[0]);
38 37
       return cb(null, processed);
39 38
     });
40 39
 
@@ -48,6 +47,7 @@ function processSamples(samples, numFrames, samplesPerFrame) {
48 47
   var perFrame = Math.floor(samples.length / numFrames),
49 48
       perPoint = Math.floor(perFrame / samplesPerFrame),
50 49
       range = d3.range(samplesPerFrame),
50
+      maxFrame,
51 51
       min = max = 0;
52 52
 
53 53
   var unadjusted = d3.range(numFrames).map(function(frame){
@@ -65,7 +65,11 @@ function processSamples(samples, numFrames, samplesPerFrame) {
65 65
       }
66 66
 
67 67
       min = Math.min(min, localMin);
68
-      max = Math.max(max, localMax);
68
+
69
+      if (localMax > max) {
70
+        max = localMax;
71
+        maxFrame = frame;
72
+      }
69 73
 
70 74
       return [localMin, localMax];
71 75
 
@@ -73,14 +77,20 @@ function processSamples(samples, numFrames, samplesPerFrame) {
73 77
 
74 78
   });
75 79
 
76
-  console.log(unadjusted[0]);
80
+  // Scale up to -1 or 1
81
+  var adjustment = 1 / Math.max(Math.abs(min), Math.abs(max));
77 82
 
78
-  return unadjusted.map(function(frame){
83
+  var adjusted = unadjusted.map(function(frame){
79 84
     return frame.map(function(point){
80
-      return [-point[0] / min, point[1] / max];
85
+      return [adjustment * point[0], adjustment * point[1]];
81 86
     });
82 87
   });
83 88
 
89
+  // Make first and last frame peaky
90
+  adjusted[0] = adjusted[numFrames - 1] = adjusted[maxFrame];
91
+
92
+  return adjusted;
93
+
84 94
 }
85 95
 
86 96
 module.exports = getWaveform;

+ 29 - 22
test/waveform-test.js Целия файл

@@ -1,66 +1,73 @@
1 1
 var tape = require("tape"),
2 2
     path = require("path");
3 3
 
4
-var getWaveform = require("../audiogram/waveform.js");
4
+var getWaveform = require("../audiogram/waveform.js"),
5
+    probe = require("../lib/probe.js");
5 6
 
6 7
 var sample = path.join(__dirname, "data/glazed-donut.mp3");
7 8
 
8 9
 tape("Waveform", function(test) {
9 10
 
10 11
   var options = {
11
-    numFrames: 500,
12
+    framesPerSecond: 20,
12 13
     samplesPerFrame: 10
13 14
   };
14 15
 
15
-  getWaveform(sample, options, function(err, waveform){
16
+  probe(sample, function(e1, data){
16 17
 
17
-    test.error(err);
18
-    test.assert(Array.isArray(waveform) && waveform.length === options.numFrames);
18
+    test.error(e1);
19 19
 
20
-    var firstMax = Math.max.apply(null, waveform[0]);
20
+    getWaveform(sample, options, function(e2, waveform){
21 21
 
22
-    test.assert(firstMax <= 1);
22
+      test.error(e2);
23
+      test.assert(Array.isArray(waveform) && waveform.length === Math.floor(data.duration * options.framesPerSecond));
23 24
 
24
-    test.assert(waveform.every(function(frame){
25
-      return frame.length === options.samplesPerFrame;
26
-    }));
25
+      var firstMax = Math.max.apply(null, waveform[0].map(function(d){ return d[1]; }));
27 26
 
28
-    test.assert(waveform.every(function(frame){
29
-      return frame.every(function(val){
30
-        return typeof val === "number" && val >= 0 && val <= firstMax;
31
-      });
32
-    }));
27
+      test.assert(firstMax <= 1);
33 28
 
34
-    test.end();
29
+      test.assert(waveform.every(function(frame){
30
+        return frame.length === options.samplesPerFrame && frame.every(function(f){
31
+          return f.length === 2 && typeof f[0] === "number" && typeof f[1] === "number" && f[0] <= 0 && f[0] >= -1 && f[1] >= 0 && f[1] <= firstMax;
32
+        });
33
+      }));
34
+
35
+      test.end();
36
+
37
+    });
35 38
 
36 39
   });
37 40
 
38 41
 });
39 42
 
40
-tape("Waveform missing numFrames", function(test) {
43
+tape("Max Duration Error", function(test) {
41 44
 
42 45
   var options = {
43
-    samplesPerFrame: 10
46
+    framesPerSecond: 20,
47
+    samplesPerFrame: 10,
48
+    maxDuration: 20
44 49
   };
45 50
 
46 51
   getWaveform(sample, options, function(err, waveform){
47 52
 
48
-    test.ok(err);
53
+    test.assert(err);
49 54
     test.end();
50 55
 
51 56
   });
52 57
 
53 58
 });
54 59
 
55
-tape("Waveform missing samplesPerFrame", function(test) {
60
+tape("Max Duration OK", function(test) {
56 61
 
57 62
   var options = {
58
-    numFrames: 500,
63
+    framesPerSecond: 20,
64
+    samplesPerFrame: 10,
65
+    maxDuration: 30
59 66
   };
60 67
 
61 68
   getWaveform(sample, options, function(err, waveform){
62 69
 
63
-    test.ok(err);
70
+    test.error(err);
64 71
     test.end();
65 72
 
66 73
   });