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

frame-test.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. const { Image } = require("canvas");
  9. require("mkdirp").sync(path.join(__dirname, "tmp", "frames"));
  10. var frameDir = path.join(__dirname, "tmp", "frames");
  11. var waveform = [
  12. [
  13. [0, 0], [1, 1], [0, 0]
  14. ],
  15. [
  16. [1, 1], [0.1, 0.1], [1, 1]
  17. ]
  18. ];
  19. function tester(options) {
  20. return function(test) {
  21. initializeCanvas(options, function(err, renderer){
  22. test.error(err);
  23. drawFrames(renderer, {
  24. numFrames: waveform.length,
  25. frameDir: frameDir,
  26. width: options.width,
  27. height: options.height,
  28. waveform: waveform
  29. }, function(err){
  30. test.error(err);
  31. checkFrame(test, options);
  32. });
  33. });
  34. };
  35. }
  36. tape.test("Draw frame", tester({
  37. width: 1280,
  38. height: 720,
  39. backgroundColor: "#f00",
  40. foregroundColor: "#fff",
  41. waveTop: 340,
  42. waveBottom: 380
  43. }));
  44. tape.test("Default colors", tester({
  45. width: 1280,
  46. height: 720,
  47. waveTop: 340,
  48. waveBottom: 380
  49. }));
  50. tape.test("Square frame", tester({
  51. width: 720,
  52. height: 720,
  53. backgroundColor: "#f00",
  54. foregroundColor: "#fff",
  55. waveTop: 340,
  56. waveBottom: 380
  57. }));
  58. function checkFrame(test, options) {
  59. var testCanvas = Canvas.createCanvas(options.width, options.height),
  60. context = testCanvas.getContext("2d");
  61. d3.queue()
  62. .defer(fs.readFile, path.join(frameDir, "000001.png"))
  63. .defer(fs.readFile, path.join(frameDir, "000002.png"))
  64. .await(function(e, f1, f2){
  65. test.error(e);
  66. const img = new 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. });