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

frame-test.js 3.3KB

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