Browse Source

Waveform updates, RMS and midpoint only

Noah 8 years ago
parent
commit
ebc596313a
5 changed files with 25 additions and 27 deletions
  1. 16 18
      audiogram/waveform.js
  2. 1 1
      client/sample-wave.js
  3. 5 5
      renderer/patterns.js
  4. 2 2
      test/frame-test.js
  5. 1 1
      test/waveform-test.js

+ 16 - 18
audiogram/waveform.js View File

36
       perPoint = Math.floor(perFrame / samplesPerFrame),
36
       perPoint = Math.floor(perFrame / samplesPerFrame),
37
       range = d3.range(samplesPerFrame),
37
       range = d3.range(samplesPerFrame),
38
       maxFrame,
38
       maxFrame,
39
-      maxMedian = max = 0;
39
+      maxRms = maxMid = 0;
40
 
40
 
41
   var unadjusted = d3.range(numFrames).map(function(frame){
41
   var unadjusted = d3.range(numFrames).map(function(frame){
42
 
42
 
43
     var frameSamples = samples.slice(frame * perFrame, (frame + 1) * perFrame),
43
     var frameSamples = samples.slice(frame * perFrame, (frame + 1) * perFrame),
44
         points = range.map(function(point){
44
         points = range.map(function(point){
45
 
45
 
46
-          var pointSamples = frameSamples.slice(point * perPoint, (point + 1) * perPoint);
46
+          var pointSamples = frameSamples.slice(point * perPoint, (point + 1) * perPoint),
47
+              midpoint = pointSamples[Math.floor(pointSamples.length / 2)];
47
 
48
 
48
           var rms = Math.sqrt(d3.sum(pointSamples.map(function(d){
49
           var rms = Math.sqrt(d3.sum(pointSamples.map(function(d){
49
             return d * d;
50
             return d * d;
50
           })) / perPoint);
51
           })) / perPoint);
51
 
52
 
52
-          if (rms > max) {
53
-            max = rms;
53
+          if (rms > maxRms) {
54
+            maxRms = rms;
55
+            maxFrame = frame;
56
+          }
57
+
58
+          if (Math.abs(midpoint) > maxMid) {
59
+            maxMid = Math.abs(midpoint);
54
           }
60
           }
55
 
61
 
56
           // Min value, max value, and midpoint value
62
           // Min value, max value, and midpoint value
57
-          return [-rms, rms, pointSamples[Math.floor(pointSamples.length / 2)]];
63
+          return [rms, midpoint];
58
 
64
 
59
-        }),
60
-        median = d3.median(points.map(function(point){
61
-          return point[1];
62
-        }));
63
-
64
-    if (median > maxMedian) {
65
-      maxMedian = median;
66
-      maxFrame = frame;
67
-    }
65
+        });
68
 
66
 
69
     return points;
67
     return points;
70
 
68
 
71
   });
69
   });
72
 
70
 
73
-  // Scale up to -1 / 1
74
-  var adjustment = 1 / max;
75
-
76
   var adjusted = unadjusted.map(function(frame){
71
   var adjusted = unadjusted.map(function(frame){
77
     return frame.map(function(point){
72
     return frame.map(function(point){
78
-      return point.map(function(p){ return adjustment * p; });
73
+      return [
74
+        point[0] / maxRms,
75
+        point[1] / maxMid
76
+      ];
79
     });
77
     });
80
   });
78
   });
81
 
79
 

File diff suppressed because it is too large
+ 1 - 1
client/sample-wave.js


+ 5 - 5
renderer/patterns.js View File

36
 
36
 
37
     var top = data.map(function(d,i){
37
     var top = data.map(function(d,i){
38
 
38
 
39
-      return [x(i), baseline - height(d[1])];
39
+      return [x(i), baseline - height(d[0])];
40
 
40
 
41
     });
41
     });
42
 
42
 
43
     var bottom = data.map(function(d,i){
43
     var bottom = data.map(function(d,i){
44
 
44
 
45
-      return [x(i), baseline + height(-d[0])];
45
+      return [x(i), baseline + height(d[0])];
46
 
46
 
47
     }).reverse();
47
     }).reverse();
48
 
48
 
90
 
90
 
91
     data.forEach(function(val, i){
91
     data.forEach(function(val, i){
92
 
92
 
93
-      var h = height(-val[0]) + height(val[1]),
93
+      var h = height(val[0]) * 2,
94
           x = barX(i),
94
           x = barX(i),
95
-          y = baseline - height(val[1]);
95
+          y = baseline - height(val[0]);
96
 
96
 
97
       context.fillRect(x, y, barWidth, h);
97
       context.fillRect(x, y, barWidth, h);
98
 
98
 
133
 
133
 
134
     data.forEach(function(val, i){
134
     data.forEach(function(val, i){
135
 
135
 
136
-      var bricks = Math.max(1, Math.floor(height(val[1]) / (brickHeight + brickGap))),
136
+      var bricks = Math.max(1, Math.floor(height(val[0]) / (brickHeight + brickGap))),
137
           x = barX(i);
137
           x = barX(i);
138
 
138
 
139
       d3.range(bricks).forEach(function(b){
139
       d3.range(bricks).forEach(function(b){

+ 2 - 2
test/frame-test.js View File

12
 
12
 
13
 var waveform = [
13
 var waveform = [
14
   [
14
   [
15
-    [0, 0], [-1, 1], [0, 0]
15
+    [0, 0], [1, 1], [0, 0]
16
   ],
16
   ],
17
   [
17
   [
18
-    [-1, 1], [-0.1, 0.1], [-1, 1]
18
+    [1, 1], [0.1, 0.1], [1, 1]
19
   ]
19
   ]
20
 ];
20
 ];
21
 
21
 

+ 1 - 1
test/waveform-test.js View File

28
 
28
 
29
       test.assert(waveform.every(function(frame){
29
       test.assert(waveform.every(function(frame){
30
         return frame.length === options.samplesPerFrame && frame.every(function(f){
30
         return frame.length === options.samplesPerFrame && frame.every(function(f){
31
-          return f.length === 3 && f.every(function(d){ return typeof d === "number"; }) && f[0] <= 0 && f[0] >= -1 && f[1] >= 0 && f[1] <= 1 && f[2] >= -1 && f[2] <= 1;
31
+          return f.length === 2 && f.every(function(d){ return typeof d === "number"; }) && f[0] >= 0 && f[0] <= 1 && f[1] >= -1 && f[1] <= 1;
32
         });
32
         });
33
       }));
33
       }));
34
 
34