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

probe-test.js 3.1KB

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