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

frame-test.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 = new Canvas(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. var img = new Canvas.Image;
  66. img.src = f1;
  67. var bg = getColor(options.backgroundColor || "#fff"),
  68. fg = getColor(options.waveColor || options.foregroundColor || "#000");
  69. context.drawImage(img, 0, 0, options.width, options.height);
  70. test.deepEqual(getColor(context.getImageData(0, 0, 1, 1)), bg);
  71. test.deepEqual(getColor(context.getImageData(options.width - 1, options.height - 1, 1, 1)), bg);
  72. test.deepEqual(getColor(context.getImageData(0, options.height / 2 - 10, 1, 1)), bg);
  73. test.deepEqual(getColor(context.getImageData(options.width - 1, options.height / 2 + 10, 1, 1)), bg);
  74. test.deepEqual(getColor(context.getImageData(options.width / 2, options.height / 2, 1, 1)), fg);
  75. test.deepEqual(getColor(context.getImageData(options.width / 2, options.height / 2 - 10, 1, 1)), fg);
  76. test.deepEqual(getColor(context.getImageData(options.width / 2, options.height / 2 + 10, 1, 1)), fg);
  77. img.src = f2;
  78. context.drawImage(img, 10, 0, options.width, options.height);
  79. test.deepEqual(getColor(context.getImageData(options.width / 2, options.height / 2, 1, 1)), fg);
  80. test.deepEqual(getColor(context.getImageData(options.width / 2 - 10, options.height / 2 - 10, 1, 1)), bg);
  81. test.deepEqual(getColor(context.getImageData(options.width / 2 - 10, options.height / 2 + 10, 1, 1)), bg);
  82. test.end();
  83. });
  84. }
  85. function getColor(c) {
  86. if (typeof c === "string") {
  87. c = d3.color(c);
  88. return [c.r, c.g, c.b];
  89. } else {
  90. return Array.prototype.slice.call(c.data, 0, 3);
  91. }
  92. }
  93. // Cleanup
  94. tape.onFinish(function(){
  95. require("rimraf")(path.join(__dirname, "tmp"), function(err){
  96. if (err) {
  97. throw err;
  98. }
  99. });
  100. });