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

probe-test.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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("Mono probe", function(test) {
  18. probe(path.join(__dirname, "data/glazed-donut-mono.mp3"), function(err, data){
  19. test.error(err);
  20. test.equal(typeof data.duration, "number");
  21. test.equal(data.channels, 1);
  22. test.assert(Math.abs(data.duration - 26.67) < 0.1);
  23. test.end();
  24. });
  25. });
  26. tape("WAV probe", function(test) {
  27. probe(path.join(__dirname, "data/glazed-donut.wav"), function(err, data){
  28. test.error(err);
  29. test.equal(typeof data.duration, "number");
  30. test.equal(data.channels, 2);
  31. test.assert(Math.abs(data.duration - 1.83) < 0.1);
  32. test.end();
  33. });
  34. });
  35. tape("Mono probe", function(test) {
  36. probe(path.join(__dirname, "data/short.wav"), function(err, data){
  37. test.error(err);
  38. test.equal(typeof data.duration, "number");
  39. test.equal(data.channels, 1);
  40. test.assert(Math.abs(data.duration - 0.01) < 0.01);
  41. test.end();
  42. });
  43. });
  44. tape("Probe error", function(test) {
  45. probe(path.join(__dirname, "..", "README.md"), function(err){
  46. test.ok(err);
  47. test.end();
  48. });
  49. });
  50. tape("Trim start", function(test) {
  51. var options = {
  52. origin: path.join(__dirname, "data/glazed-donut.mp3"),
  53. destination: path.join(__dirname, "tmp/trim-start.mp3"),
  54. startTime: 6.67
  55. };
  56. queue(1)
  57. .defer(trimAudio, options)
  58. .defer(probe, options.destination)
  59. .await(function(err, _, data){
  60. test.error(err);
  61. test.equal(typeof data.duration, "number");
  62. test.assert(Math.abs(data.duration - 20) < 0.1);
  63. test.end();
  64. });
  65. });
  66. tape("Trim end", function(test) {
  67. var options = {
  68. origin: path.join(__dirname, "data/glazed-donut.mp3"),
  69. destination: path.join(__dirname, "tmp/trim-end.mp3"),
  70. startTime: 6.67
  71. };
  72. queue(1)
  73. .defer(trimAudio, options)
  74. .defer(probe, options.destination)
  75. .await(function(err, _, data){
  76. test.error(err);
  77. test.equal(typeof data.duration, "number");
  78. test.assert(Math.abs(data.duration - 20) < 0.1);
  79. test.end();
  80. });
  81. });
  82. tape("Trim start & end", function(test) {
  83. var options = {
  84. origin: path.join(__dirname, "data/glazed-donut.mp3"),
  85. destination: path.join(__dirname, "tmp/trim-start-end.mp3"),
  86. startTime: 5,
  87. endTime: 10
  88. };
  89. queue(1)
  90. .defer(trimAudio, options)
  91. .defer(probe, options.destination)
  92. .await(function(err, _, data){
  93. test.error(err);
  94. test.equal(typeof data.duration, "number");
  95. test.assert(Math.abs(data.duration - 5) < 0.1);
  96. test.end();
  97. });
  98. });
  99. tape("Trim invalid", function(test) {
  100. var options = {
  101. origin: path.join(__dirname, "data/glazed-donut.mp3"),
  102. destination: path.join(__dirname, "tmp/trim-invalid.mp3"),
  103. startTime: 5,
  104. endTime: 4
  105. };
  106. trimAudio(options, function(err){
  107. test.ok(err);
  108. test.end();
  109. });
  110. });
  111. // Cleanup
  112. tape.onFinish(function(){
  113. require("rimraf")(path.join(__dirname, "tmp"), function(err){
  114. if (err) {
  115. throw err;
  116. }
  117. });
  118. });