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

patterns.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. var d3 = require("d3");
  2. module.exports = {
  3. wave: curve(d3.curveCardinal.tension(0.1)),
  4. pixel: curve(d3.curveStep),
  5. roundBars: bars(true),
  6. bars: bars(),
  7. bricks: bricks(),
  8. equalizer: bricks(true)
  9. };
  10. function curve(interpolator) {
  11. return function drawCurve(context, data, options) {
  12. context.fillStyle = options.waveColor;
  13. context.strokeStyle = options.waveColor;
  14. context.lineWidth = 3;
  15. var line = d3.line()
  16. .curve(interpolator)
  17. .context(context);
  18. var waveHeight = options.waveBottom - options.waveTop;
  19. var baseline = options.waveTop + waveHeight / 2;
  20. var x = d3.scalePoint()
  21. .padding(0.1)
  22. .domain(d3.range(data.length))
  23. .rangeRound([options.waveLeft, options.waveRight]);
  24. var height = d3.scaleLinear()
  25. .domain([0, 1])
  26. .range([0, waveHeight / 2]);
  27. var top = data.map(function(d,i){
  28. return [x(i), baseline - height(d[0])];
  29. });
  30. var bottom = data.map(function(d,i){
  31. return [x(i), baseline + height(d[0])];
  32. }).reverse();
  33. top.unshift([options.waveLeft, baseline]);
  34. top.push([options.waveRight, baseline]);
  35. // Fill waveform
  36. context.beginPath();
  37. line(top.concat(bottom));
  38. context.fill();
  39. // Stroke waveform edges / ensure baseline
  40. [top, bottom].forEach(function(path){
  41. context.beginPath();
  42. line(path);
  43. context.stroke();
  44. });
  45. }
  46. }
  47. function bars(round) {
  48. return function(context, data, options) {
  49. context.fillStyle = options.waveColor;
  50. var waveHeight = options.waveBottom - options.waveTop;
  51. var baseline = options.waveTop + waveHeight / 2;
  52. var barX = d3.scaleBand()
  53. .paddingInner(0.5)
  54. .paddingOuter(0.01)
  55. .domain(d3.range(data.length))
  56. .rangeRound([options.waveLeft, options.waveRight]);
  57. var height = d3.scaleLinear()
  58. .domain([0, 1])
  59. .range([0, waveHeight / 2]);
  60. var barWidth = barX.bandwidth();
  61. data.forEach(function(val, i){
  62. var h = height(val[0]) * 2,
  63. x = barX(i),
  64. y = baseline - height(val[0]);
  65. context.fillRect(x, y, barWidth, h);
  66. if (round) {
  67. context.beginPath();
  68. context.arc(x + barWidth / 2, y, barWidth / 2, 0, 2 * Math.PI);
  69. context.moveTo(x + barWidth / 2, y + h);
  70. context.arc(x + barWidth / 2, y + h, barWidth / 2, 0, 2 * Math.PI);
  71. context.fill();
  72. }
  73. });
  74. }
  75. }
  76. function bricks(rainbow) {
  77. return function (context, data, options) {
  78. context.fillStyle = options.waveColor;
  79. var waveHeight = options.waveBottom - options.waveTop;
  80. var barX = d3.scaleBand()
  81. .paddingInner(0.1)
  82. .paddingOuter(0.01)
  83. .domain(d3.range(data.length))
  84. .rangeRound([options.waveLeft, options.waveRight]);
  85. var height = d3.scaleLinear()
  86. .domain([0, 1])
  87. .range([0, waveHeight]);
  88. var barWidth = barX.bandwidth(),
  89. brickHeight = 10,
  90. brickGap = 3,
  91. maxBricks = Math.max(1, Math.floor(waveHeight / (brickHeight + brickGap)));
  92. data.forEach(function(val, i){
  93. var bricks = Math.max(1, Math.floor(height(val[0]) / (brickHeight + brickGap))),
  94. x = barX(i);
  95. d3.range(bricks).forEach(function(b){
  96. if (rainbow) {
  97. context.fillStyle = d3.interpolateWarm(1 - (b + 1) / maxBricks);
  98. }
  99. context.fillRect(x, options.waveBottom - (brickHeight * (b+1)) - brickGap * b, barWidth, brickHeight);
  100. });
  101. });
  102. };
  103. }