main.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. // Create an instance
  3. var wavesurfer;
  4. // Init & load audio file
  5. document.addEventListener('DOMContentLoaded', function () {
  6. var options = {
  7. container : document.querySelector('#waveform'),
  8. waveColor : 'violet',
  9. progressColor : 'purple',
  10. cursorColor : 'navy'
  11. };
  12. if (location.search.match('scroll')) {
  13. options.minPxPerSec = 100;
  14. options.scrollParent = true;
  15. }
  16. // Init
  17. wavesurfer = WaveSurfer.create(options);
  18. // Load audio from URL
  19. wavesurfer.load('example/media/demo.wav');
  20. // Regions
  21. if (wavesurfer.enableDragSelection) {
  22. wavesurfer.enableDragSelection({
  23. color: 'rgba(0, 255, 0, 0.1)'
  24. });
  25. }
  26. });
  27. // Play at once when ready
  28. // Won't work on iOS until you touch the page
  29. wavesurfer.on('ready', function () {
  30. //wavesurfer.play();
  31. });
  32. // Report errors
  33. wavesurfer.on('error', function (err) {
  34. console.error(err);
  35. });
  36. // Do something when the clip is over
  37. wavesurfer.on('finish', function () {
  38. console.log('Finished playing');
  39. });
  40. /* Progress bar */
  41. document.addEventListener('DOMContentLoaded', function () {
  42. var progressDiv = document.querySelector('#progress-bar');
  43. var progressBar = progressDiv.querySelector('.progress-bar');
  44. var showProgress = function (percent) {
  45. progressDiv.style.display = 'block';
  46. progressBar.style.width = percent + '%';
  47. };
  48. var hideProgress = function () {
  49. progressDiv.style.display = 'none';
  50. };
  51. wavesurfer.on('loading', showProgress);
  52. wavesurfer.on('ready', hideProgress);
  53. wavesurfer.on('destroy', hideProgress);
  54. wavesurfer.on('error', hideProgress);
  55. });
  56. // Drag'n'drop
  57. document.addEventListener('DOMContentLoaded', function () {
  58. var toggleActive = function (e, toggle) {
  59. e.stopPropagation();
  60. e.preventDefault();
  61. toggle ? e.target.classList.add('wavesurfer-dragover') :
  62. e.target.classList.remove('wavesurfer-dragover');
  63. };
  64. var handlers = {
  65. // Drop event
  66. drop: function (e) {
  67. toggleActive(e, false);
  68. // Load the file into wavesurfer
  69. if (e.dataTransfer.files.length) {
  70. wavesurfer.loadBlob(e.dataTransfer.files[0]);
  71. } else {
  72. wavesurfer.fireEvent('error', 'Not a file');
  73. }
  74. },
  75. // Drag-over event
  76. dragover: function (e) {
  77. toggleActive(e, true);
  78. },
  79. // Drag-leave event
  80. dragleave: function (e) {
  81. toggleActive(e, false);
  82. }
  83. };
  84. var dropTarget = document.querySelector('#drop');
  85. Object.keys(handlers).forEach(function (event) {
  86. dropTarget.addEventListener(event, handlers[event]);
  87. });
  88. });