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

duration-test.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. var tape = require("tape"),
  2. path = require("path"),
  3. fs = require("fs"),
  4. queue = require("d3").queue;
  5. require("mkdirp").sync(path.join(__dirname, "tmp"));
  6. var getDuration = require("../audiogram/duration.js"),
  7. trimAudio = require("../audiogram/trim.js");
  8. tape("MP3 duration", function(test) {
  9. getDuration(path.join(__dirname, "data/glazed-donut.mp3"), function(err, duration){
  10. test.error(err);
  11. test.equal(typeof duration, "number");
  12. test.assert(Math.abs(duration - 26.67) < 0.1);
  13. test.end();
  14. });
  15. });
  16. tape("WAV duration", function(test) {
  17. getDuration(path.join(__dirname, "data/glazed-donut.wav"), function(err, duration){
  18. test.error(err);
  19. test.equal(typeof duration, "number");
  20. test.assert(Math.abs(duration - 1.83) < 0.1);
  21. test.end();
  22. });
  23. });
  24. tape("Duration error", function(test) {
  25. getDuration(path.join(__dirname, "..", "README.md"), function(err){
  26. test.ok(err);
  27. test.end();
  28. });
  29. });
  30. tape("Trim start", function(test) {
  31. var options = {
  32. origin: path.join(__dirname, "data/glazed-donut.mp3"),
  33. destination: path.join(__dirname, "tmp/trim-start.mp3"),
  34. startTime: 6.67
  35. };
  36. queue(1)
  37. .defer(trimAudio, options)
  38. .defer(getDuration, options.destination)
  39. .await(function(err, _, duration){
  40. test.error(err);
  41. test.equal(typeof duration, "number");
  42. test.assert(Math.abs(duration - 20) < 0.1);
  43. test.end();
  44. });
  45. });
  46. tape("Trim end", function(test) {
  47. var options = {
  48. origin: path.join(__dirname, "data/glazed-donut.mp3"),
  49. destination: path.join(__dirname, "tmp/trim-end.mp3"),
  50. startTime: 6.67
  51. };
  52. queue(1)
  53. .defer(trimAudio, options)
  54. .defer(getDuration, options.destination)
  55. .await(function(err, _, duration){
  56. test.error(err);
  57. test.equal(typeof duration, "number");
  58. test.assert(Math.abs(duration - 20) < 0.1);
  59. test.end();
  60. });
  61. });
  62. tape("Trim start & end", function(test) {
  63. var options = {
  64. origin: path.join(__dirname, "data/glazed-donut.mp3"),
  65. destination: path.join(__dirname, "tmp/trim-start-end.mp3"),
  66. startTime: 5,
  67. endTime: 10
  68. };
  69. queue(1)
  70. .defer(trimAudio, options)
  71. .defer(getDuration, options.destination)
  72. .await(function(err, _, duration){
  73. test.error(err);
  74. test.equal(typeof duration, "number");
  75. test.assert(Math.abs(duration - 5) < 0.1);
  76. test.end();
  77. });
  78. });
  79. tape("Trim invalid", function(test) {
  80. var options = {
  81. origin: path.join(__dirname, "data/glazed-donut.mp3"),
  82. destination: path.join(__dirname, "tmp/trim-invalid.mp3"),
  83. startTime: 5,
  84. endTime: 4
  85. };
  86. queue(1)
  87. .defer(trimAudio, options)
  88. .defer(getDuration, options.destination)
  89. .await(function(err, _, duration){
  90. test.ok(err);
  91. test.end();
  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. });
  102. // Cleanup
  103. tape.onFinish(function(){
  104. require("rimraf")(path.join(__dirname, "tmp"), function(err){
  105. if (err) {
  106. throw err;
  107. }
  108. });
  109. });