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

waveform.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var extractPeaks = require("webaudio-peaks"),
  2. d3 = require("d3");
  3. var width = 640;
  4. function decoded(cb) {
  5. return function(decodedData) {
  6. var duration = decodedData.duration;
  7. var samplesPerPixel = Math.floor(decodedData.length / width);
  8. var peaks = extractPeaks(decodedData, samplesPerPixel, true);
  9. // FF and Chrome support Int8Array.filter, Safari doesn't, that's fun
  10. var positive = Array.prototype.filter.call(peaks.data[0], function(d,i){
  11. return i % 2;
  12. });
  13. var scale = d3.scaleLinear()
  14. .domain([0, getMax(positive)])
  15. .range([0, 1])
  16. .clamp(true);
  17. positive = Array.prototype.slice.call(positive).map(scale);
  18. cb(null,{ duration: duration, peaks: positive });
  19. };
  20. }
  21. module.exports = function(file, cb) {
  22. var ctx = new (window.AudioContext || window.webkitAudioContext)();
  23. var fileReader = new FileReader();
  24. var close = function(err, data) {
  25. console.warn(err);
  26. ctx.close();
  27. cb(err, data);
  28. };
  29. fileReader.onerror = cb;
  30. fileReader.onload = function(){
  31. ctx.decodeAudioData(this.result, decoded(close), function(err){ close(err || "Error decoding audio."); });
  32. };
  33. fileReader.readAsArrayBuffer(file);
  34. }
  35. // Faster
  36. function getMax(arr) {
  37. var max = -Infinity;
  38. for (var i = 0, l = arr.length; i < l; i++) {
  39. if (arr[i] > max) {
  40. max = arr[i];
  41. }
  42. }
  43. return max;
  44. }