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

frame-test.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. var tape = require("tape"),
  2. Canvas = require("canvas"),
  3. d3 = require("d3"),
  4. path = require("path"),
  5. fs = require("fs"),
  6. initializeCanvas = require("../audiogram/initialize-canvas.js"),
  7. drawFrames = require("../audiogram/draw-frames.js");
  8. require("mkdirp").sync(path.join(__dirname, "tmp", "frames"));
  9. var frameDir = path.join(__dirname, "tmp", "frames");
  10. tape.test("Draw frame", function(test){
  11. var options = {
  12. width: 1280,
  13. height: 720,
  14. backgroundColor: "#f00",
  15. foregroundColor: "#fff",
  16. waveTop: 340,
  17. waveBottom: 380,
  18. waveform: [[0, 1, 0], [1, 0.1, 1]]
  19. };
  20. initializeCanvas(options, function(err, renderer){
  21. test.error(err);
  22. test.assert(renderer.context.canvas instanceof Canvas);
  23. test.assert(renderer.context.canvas.width === options.width);
  24. test.assert(renderer.context.canvas.height === options.height);
  25. drawFrames(renderer, {
  26. numFrames: 2,
  27. frameDir: frameDir
  28. }, function(err){
  29. test.error(err);
  30. checkFrame(test, options);
  31. });
  32. });
  33. });
  34. tape.test("Default colors", function(test){
  35. var options = {
  36. width: 1280,
  37. height: 720,
  38. waveTop: 340,
  39. waveBottom: 380,
  40. waveform: [[0, 1, 0], [1, 0.1, 1]]
  41. };
  42. initializeCanvas(options, function(err, renderer){
  43. test.error(err);
  44. test.assert(renderer.context.canvas instanceof Canvas);
  45. test.assert(renderer.context.canvas.width === options.width);
  46. test.assert(renderer.context.canvas.height === options.height);
  47. drawFrames(renderer, {
  48. numFrames: 2,
  49. frameDir: frameDir
  50. }, function(err){
  51. test.error(err);
  52. checkFrame(test, options);
  53. });
  54. });
  55. });
  56. tape.test("Square frame", function(test){
  57. var options = {
  58. width: 720,
  59. height: 720,
  60. backgroundColor: "#fc0",
  61. foregroundColor: "#fff",
  62. waveTop: 340,
  63. waveBottom: 380,
  64. waveform: [[0, 1, 0], [1, 0.1, 1]]
  65. };
  66. initializeCanvas(options, function(err, renderer){
  67. test.error(err);
  68. test.assert(renderer.context.canvas instanceof Canvas);
  69. test.assert(renderer.context.canvas.width === options.width);
  70. test.assert(renderer.context.canvas.height === options.height);
  71. drawFrames(renderer, {
  72. numFrames: 2,
  73. frameDir: frameDir
  74. }, function(err){
  75. test.error(err);
  76. checkFrame(test, options);
  77. });
  78. });
  79. });
  80. function checkFrame(test, options) {
  81. var testCanvas = new Canvas(options.width, options.height),
  82. context = testCanvas.getContext("2d");
  83. d3.queue()
  84. .defer(fs.readFile, path.join(frameDir, "000001.png"))
  85. .defer(fs.readFile, path.join(frameDir, "000002.png"))
  86. .await(function(e, f1, f2){
  87. test.error(e);
  88. var img = new Canvas.Image;
  89. img.src = f1;
  90. var bg = getColor(options.backgroundColor || "#fff"),
  91. fg = getColor(options.waveColor || options.foregroundColor || "#000");
  92. context.drawImage(img, 0, 0, options.width, options.height);
  93. test.deepEqual(getColor(context.getImageData(0, 0, 1, 1)), bg);
  94. test.deepEqual(getColor(context.getImageData(options.width - 1, options.height - 1, 1, 1)), bg);
  95. test.deepEqual(getColor(context.getImageData(0, options.height / 2 - 10, 1, 1)), bg);
  96. test.deepEqual(getColor(context.getImageData(options.width - 1, options.height / 2 + 10, 1, 1)), bg);
  97. test.deepEqual(getColor(context.getImageData(options.width / 2, options.height / 2, 1, 1)), fg);
  98. test.deepEqual(getColor(context.getImageData(options.width / 2, options.height / 2 - 10, 1, 1)), fg);
  99. test.deepEqual(getColor(context.getImageData(options.width / 2, options.height / 2 + 10, 1, 1)), fg);
  100. img.src = f2;
  101. context.drawImage(img, 10, 0, options.width, options.height);
  102. test.deepEqual(getColor(context.getImageData(options.width / 2, options.height / 2, 1, 1)), fg);
  103. test.deepEqual(getColor(context.getImageData(options.width / 2 - 10, options.height / 2 - 10, 1, 1)), bg);
  104. test.deepEqual(getColor(context.getImageData(options.width / 2 - 10, options.height / 2 + 10, 1, 1)), bg);
  105. test.end();
  106. });
  107. }
  108. function getColor(c) {
  109. if (typeof c === "string") {
  110. c = d3.color(c);
  111. return [c.r, c.g, c.b];
  112. } else {
  113. return Array.prototype.slice.call(c.data, 0, 3);
  114. }
  115. }
  116. // Cleanup
  117. tape.onFinish(function(){
  118. require("rimraf")(path.join(__dirname, "tmp"), function(err){
  119. if (err) {
  120. throw err;
  121. }
  122. });
  123. });