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

frame-test.js 3.4KB

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