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

frame-test.js 4.6KB

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