main.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. // Create an instance
  3. var wavesurfer;
  4. // Init & load audio file
  5. document.addEventListener('DOMContentLoaded', function () {
  6. // Init
  7. wavesurfer = WaveSurfer.create({
  8. container: document.querySelector('#waveform'),
  9. minPxPerSec: 30,
  10. scrollParent: true,
  11. waveColor: '#A8DBA8',
  12. progressColor: '#3B8686'
  13. });
  14. // Load audio from URL
  15. wavesurfer.load('media.wav');
  16. // Panner
  17. (function () {
  18. // Add panner
  19. wavesurfer.panner = wavesurfer.backend.ac.createPanner();
  20. wavesurfer.backend.setFilter(wavesurfer.panner);
  21. // Bind panner slider
  22. // @see http://stackoverflow.com/a/14412601/352796
  23. var onChange = function () {
  24. var xDeg = parseInt(slider.value);
  25. var x = Math.sin(xDeg * (Math.PI / 180));
  26. wavesurfer.panner.setPosition(x, 0, 0);
  27. };
  28. var slider = document.querySelector('[data-action="pan"]');
  29. slider.addEventListener('input', onChange);
  30. slider.addEventListener('change', onChange);
  31. onChange();
  32. }());
  33. // Log errors
  34. wavesurfer.on('error', function (msg) {
  35. console.log(msg);
  36. });
  37. // Bind play/pause button
  38. document.querySelector(
  39. '[data-action="play"]'
  40. ).addEventListener('click', wavesurfer.playPause.bind(wavesurfer));
  41. // Progress bar
  42. (function () {
  43. var progressDiv = document.querySelector('#progress-bar');
  44. var progressBar = progressDiv.querySelector('.progress-bar');
  45. var showProgress = function (percent) {
  46. progressDiv.style.display = 'block';
  47. progressBar.style.width = percent + '%';
  48. };
  49. var hideProgress = function () {
  50. progressDiv.style.display = 'none';
  51. };
  52. wavesurfer.on('loading', showProgress);
  53. wavesurfer.on('ready', hideProgress);
  54. wavesurfer.on('destroy', hideProgress);
  55. wavesurfer.on('error', hideProgress);
  56. }());
  57. });