Turn audio into a shareable video. forked from nypublicradio/audiogram

patterns.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. var d3 = require("d3");
  2. module.exports = {
  3. wave: filledPath(d3.curveCardinal.tension(0.1)),
  4. pixel: filledPath(d3.curveStep),
  5. roundBars: bars(true),
  6. bars: bars(),
  7. halfbars: halfbars(),
  8. bricks: bricks(),
  9. equalizer: bricks(true),
  10. line: strokedPath(),
  11. curve: strokedPath(d3.curveCardinal.tension(0.1))
  12. };
  13. function filledPath(interpolator) {
  14. return function drawCurve(context, data, options) {
  15. context.fillStyle = options.waveColor;
  16. context.strokeStyle = options.waveColor;
  17. context.lineWidth = 3;
  18. var line = d3.line()
  19. .context(context);
  20. if (interpolator) {
  21. line.curve(interpolator);
  22. }
  23. var waveHeight = options.waveBottom - options.waveTop;
  24. var baseline = options.waveTop + waveHeight / 2;
  25. var x = d3.scalePoint()
  26. .padding(0.1)
  27. .domain(d3.range(data.length))
  28. .rangeRound([options.waveLeft, options.waveRight]);
  29. var height = d3.scaleLinear()
  30. .domain([0, 1])
  31. .range([0, waveHeight / 2]);
  32. var top = data.map(function(d,i){
  33. return [x(i), baseline - height(d[0])];
  34. });
  35. var bottom = data.map(function(d,i){
  36. return [x(i), baseline + height(d[0])];
  37. }).reverse();
  38. top.unshift([options.waveLeft, baseline]);
  39. top.push([options.waveRight, baseline]);
  40. // Fill waveform
  41. context.beginPath();
  42. line(top.concat(bottom));
  43. context.fill();
  44. // Stroke waveform edges / ensure baseline
  45. [top, bottom].forEach(function(path){
  46. context.beginPath();
  47. line(path);
  48. context.stroke();
  49. });
  50. }
  51. }
  52. function halfbars () {
  53. return function(context, data, options) {
  54. context.fillStyle = options.waveColor;
  55. var waveHeight = options.waveBottom - options.waveTop;
  56. var baseline = options.waveTop + waveHeight / 2;
  57. var barX = d3.scaleBand()
  58. .paddingInner(0.25)
  59. .paddingOuter(0.01)
  60. .domain(d3.range(data.length))
  61. .rangeRound([options.waveLeft, options.waveRight]);
  62. var height = d3.scaleLinear()
  63. .domain([0, 1])
  64. .range([0, waveHeight * .45]);
  65. var barWidth = barX.bandwidth();
  66. data.forEach(function(val, i){
  67. var h = height(val[0]),
  68. x = barX(i),
  69. y = waveHeight - h;
  70. context.fillRect(x, y, barWidth, h);
  71. });
  72. }
  73. } // end halfbars function
  74. function bars(round) {
  75. return function(context, data, options) {
  76. context.fillStyle = options.waveColor;
  77. var waveHeight = options.waveBottom - options.waveTop;
  78. var baseline = options.waveTop + waveHeight / 2;
  79. var barX = d3.scaleBand()
  80. .paddingInner(0.5)
  81. .paddingOuter(0.01)
  82. .domain(d3.range(data.length))
  83. .rangeRound([options.waveLeft, options.waveRight]);
  84. var height = d3.scaleLinear()
  85. .domain([0, 1])
  86. .range([0, waveHeight / 2]);
  87. var barWidth = barX.bandwidth();
  88. data.forEach(function(val, i){
  89. var h = height(val[0]) * 2,
  90. x = barX(i),
  91. y = baseline - height(val[0]);
  92. context.fillRect(x, y, barWidth, h);
  93. if (round) {
  94. context.beginPath();
  95. context.arc(x + barWidth / 2, y, barWidth / 2, 0, 2 * Math.PI);
  96. context.moveTo(x + barWidth / 2, y + h);
  97. context.arc(x + barWidth / 2, y + h, barWidth / 2, 0, 2 * Math.PI);
  98. context.fill();
  99. }
  100. });
  101. }
  102. }
  103. function bricks(rainbow) {
  104. return function(context, data, options) {
  105. context.fillStyle = options.waveColor;
  106. var waveHeight = options.waveBottom - options.waveTop;
  107. var barX = d3.scaleBand()
  108. .paddingInner(0.1)
  109. .paddingOuter(0.01)
  110. .domain(d3.range(data.length))
  111. .rangeRound([options.waveLeft, options.waveRight]);
  112. var height = d3.scaleLinear()
  113. .domain([0, 1])
  114. .range([0, waveHeight]);
  115. var barWidth = barX.bandwidth(),
  116. brickHeight = 10,
  117. brickGap = 3,
  118. maxBricks = Math.max(1, Math.floor(waveHeight / (brickHeight + brickGap)));
  119. data.forEach(function(val, i){
  120. var bricks = Math.max(1, Math.floor(height(val[0]) / (brickHeight + brickGap))),
  121. x = barX(i);
  122. d3.range(bricks).forEach(function(b){
  123. if (rainbow) {
  124. context.fillStyle = d3.interpolateWarm(1 - (b + 1) / maxBricks);
  125. }
  126. context.fillRect(x, options.waveBottom - (brickHeight * (b+1)) - brickGap * b, barWidth, brickHeight);
  127. });
  128. });
  129. };
  130. }
  131. function strokedPath(interpolator) {
  132. return function(context, data, options) {
  133. context.fillStyle = options.waveColor;
  134. context.strokeStyle = options.waveColor;
  135. context.lineWidth = 5;
  136. var line = d3.line()
  137. .context(context);
  138. if (interpolator) {
  139. line.curve(interpolator);
  140. }
  141. var x = d3.scalePoint()
  142. .padding(0.1)
  143. .domain(d3.range(data.length))
  144. .range([options.waveLeft, options.waveRight]);
  145. var y = d3.scaleLinear()
  146. .domain([-1, 1])
  147. .range([options.waveBottom, options.waveTop]);
  148. var points = data.map(function(d, i){
  149. return [x(i), y(d[1])];
  150. });
  151. // Fill waveform
  152. context.beginPath();
  153. line(points);
  154. context.stroke();
  155. }
  156. }