app.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Create an instance
  2. var wavesurfer;
  3. window.onload = function () {
  4. wavesurfer = WaveSurfer.create({
  5. container: document.querySelector('#waveform'),
  6. splitChannels: true
  7. });
  8. // Load audio from URL
  9. wavesurfer.load('stereo.mp3');
  10. // Play/pause on button press
  11. document.querySelector('[data-action="play"]').addEventListener(
  12. 'click', wavesurfer.playPause.bind(wavesurfer)
  13. );
  14. // Drag'n'drop
  15. var toggleActive = function (e, toggle) {
  16. e.stopPropagation();
  17. e.preventDefault();
  18. toggle ? e.target.classList.add('wavesurfer-dragover') :
  19. e.target.classList.remove('wavesurfer-dragover');
  20. };
  21. var handlers = {
  22. // Drop event
  23. drop: function (e) {
  24. toggleActive(e, false);
  25. // Load the file into wavesurfer
  26. if (e.dataTransfer.files.length) {
  27. wavesurfer.loadBlob(e.dataTransfer.files[0]);
  28. } else {
  29. wavesurfer.fireEvent('error', 'Not a file');
  30. }
  31. },
  32. // Drag-over event
  33. dragover: function (e) {
  34. toggleActive(e, true);
  35. },
  36. // Drag-leave event
  37. dragleave: function (e) {
  38. toggleActive(e, false);
  39. }
  40. };
  41. var dropTarget = document.querySelector('#drop');
  42. Object.keys(handlers).forEach(function (event) {
  43. dropTarget.addEventListener(event, handlers[event]);
  44. });
  45. };