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

preview.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var d3 = require("d3"),
  2. audio = require("./audio.js"),
  3. video = require("./video.js"),
  4. minimap = require("./minimap.js"),
  5. sampleWave = require("./sample-wave.js"),
  6. getRenderer = require("../renderer/"),
  7. getWaveform = require("./waveform.js");
  8. var context = d3.select("canvas").node().getContext("2d");
  9. var theme,
  10. caption,
  11. label,
  12. file,
  13. selection;
  14. function _file(_) {
  15. return arguments.length ? (file = _) : file;
  16. }
  17. function _theme(_) {
  18. return arguments.length ? (theme = _, redraw()) : theme;
  19. }
  20. function _caption(_) {
  21. return arguments.length ? (caption = _, redraw()) : caption;
  22. }
  23. function _label(_) {
  24. return arguments.length ? (label = _, redraw()) : label;
  25. }
  26. function _selection(_) {
  27. return arguments.length ? (selection = _) : selection;
  28. }
  29. minimap.onBrush(function(extent){
  30. var duration = audio.duration();
  31. selection = {
  32. duration: duration * (extent[1] - extent[0]),
  33. start: extent[0] ? extent[0] * duration : null,
  34. end: extent[1] < 1 ? extent[1] * duration : null
  35. };
  36. d3.select("#duration strong").text(Math.round(10 * selection.duration) / 10)
  37. .classed("red", theme && theme.maxDuration && theme.maxDuration < selection.duration);
  38. });
  39. // Resize video and preview canvas to maintain aspect ratio
  40. function resize(width, height) {
  41. var widthFactor = 640 / width,
  42. heightFactor = 360 / height,
  43. factor = Math.min(widthFactor, heightFactor);
  44. d3.select("canvas")
  45. .attr("width", factor * width)
  46. .attr("height", factor * height);
  47. d3.select("#canvas")
  48. .style("width", (factor * width) + "px");
  49. d3.select("video")
  50. .attr("height", widthFactor * height);
  51. d3.select("#video")
  52. .attr("height", (widthFactor * height) + "px");
  53. context.setTransform(factor, 0, 0, factor, 0, 0);
  54. }
  55. function redraw() {
  56. resize(theme.width, theme.height);
  57. video.kill();
  58. var renderer = getRenderer(theme);
  59. renderer.backgroundImage(theme.backgroundImageFile || null);
  60. renderer.drawFrame(context, {
  61. caption: caption,
  62. label: label,
  63. waveform: sampleWave,
  64. frame: 0
  65. });
  66. }
  67. function loadAudio(f, cb) {
  68. d3.queue()
  69. .defer(getWaveform, f)
  70. .defer(audio.src, f)
  71. .await(function(err, data){
  72. if (err) {
  73. return cb(err);
  74. }
  75. file = f;
  76. minimap.redraw(data.peaks);
  77. cb(err);
  78. });
  79. }
  80. module.exports = {
  81. caption: _caption,
  82. label: _label,
  83. theme: _theme,
  84. file: _file,
  85. selection: _selection,
  86. loadAudio: loadAudio
  87. };