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

frame-test.js 3.9KB

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