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

waveform-test.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var tape = require("tape"),
  2. path = require("path");
  3. var Audiogram = require("../audiogram/"),
  4. getWaveform = require("../audiogram/waveform.js"),
  5. probe = require("../lib/probe.js");
  6. var sample = path.join(__dirname, "data/glazed-donut.mp3");
  7. tape("Waveform", function(test) {
  8. var options = {
  9. framesPerSecond: 20,
  10. samplesPerFrame: 10
  11. };
  12. probe(sample, function(e1, data){
  13. test.error(e1);
  14. options.channels = data.channels;
  15. options.numFrames = Math.floor(data.duration * options.framesPerSecond);
  16. getWaveform(sample, options, function(e2, waveform){
  17. test.error(e2);
  18. test.assert(Array.isArray(waveform) && waveform.length === options.numFrames);
  19. test.assert(waveform.every(function(frame){
  20. return frame.length === options.samplesPerFrame && frame.every(function(f){
  21. return f.length === 2 && f.every(function(d){ return typeof d === "number"; }) && f[0] >= 0 && f[0] <= 1 && f[1] >= -1 && f[1] <= 1;
  22. });
  23. }));
  24. test.end();
  25. });
  26. });
  27. });
  28. tape("Max Duration Error", function(test) {
  29. var audiogram = new Audiogram("xyz");
  30. audiogram.audioPath = sample;
  31. audiogram.settings = {
  32. theme: {
  33. maxDuration: 20
  34. }
  35. };
  36. audiogram.getWaveform(function(err, waveform){
  37. test.assert(err);
  38. test.assert(err.toString().match(/Exceeds max duration/));
  39. test.end();
  40. });
  41. });
  42. tape("Max Duration OK", function(test) {
  43. var audiogram = new Audiogram("xyz");
  44. audiogram.audioPath = sample;
  45. audiogram.settings = {
  46. theme: {
  47. samplesPerFrame: 10,
  48. framesPerSecond: 20
  49. }
  50. };
  51. probe(sample, function(err, data){
  52. test.deepEqual(Math.round(data.duration), 27);
  53. test.deepEqual(data.channels, 2);
  54. audiogram.getWaveform(function(waveformErr, waveform){
  55. test.error(waveformErr);
  56. test.assert(Array.isArray(waveform));
  57. test.deepEqual(waveform.length, Math.floor(data.duration * audiogram.settings.theme.framesPerSecond));
  58. test.end();
  59. });
  60. });
  61. });