Guest User

Untitled

a guest
Jan 16th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /* to run:
  2. budo index.js --live
  3.  
  4. uncomment the `source.connect(context.destination)` to hear tracks
  5. */
  6.  
  7. 'use strict'
  8.  
  9. const Meyda = require('../../path/to/meyda') // use your local meyda installation
  10.  
  11. const context = new window.AudioContext()
  12.  
  13. const tune1 = new window.Audio('./tune1.mp3')
  14. tune1.preload = true
  15. tune1.loop = true
  16. tune1.controls = true
  17. document.body.appendChild(tune1)
  18. const source1 = context.createMediaElementSource(tune1)
  19. // source1.connect(context.destination)
  20.  
  21. const tune2 = new window.Audio('./tune2.mp3')
  22. tune2.preload = true
  23. tune2.loop = true
  24. tune2.controls = true
  25. document.body.appendChild(tune2)
  26. const source2 = context.createMediaElementSource(tune2)
  27. // source2.connect(context.destination)
  28.  
  29. const tunestereo = new window.Audio('./tune-stereo.mp3')
  30. tunestereo.preload = true
  31. tunestereo.loop = true
  32. tunestereo.controls = true
  33. document.body.appendChild(tunestereo)
  34. const stereo = context.createMediaElementSource(tunestereo)
  35. // stereo.connect(context.destination)
  36.  
  37. const controls = document.createElement('div')
  38. document.body.appendChild(controls)
  39.  
  40. const setSRC1toAnalyzer1 = document.createElement('button')
  41. setSRC1toAnalyzer1.textContent = 'source 1 on analyzer 1'
  42. setSRC1toAnalyzer1.addEventListener('click', () => {
  43. meydaAnalyzer1.setSource(source1)
  44. })
  45. controls.appendChild(setSRC1toAnalyzer1)
  46.  
  47. const setSRC2toAnalyzer1 = document.createElement('button')
  48. setSRC2toAnalyzer1.textContent = 'source 2 on analyzer 1'
  49. setSRC2toAnalyzer1.addEventListener('click', () => {
  50. meydaAnalyzer1.setSource(source2)
  51. })
  52. controls.appendChild(setSRC2toAnalyzer1)
  53.  
  54. const setSRC1toAnalyzer2 = document.createElement('button')
  55. setSRC1toAnalyzer2.textContent = 'source 1 on analyzer 2'
  56. setSRC1toAnalyzer2.addEventListener('click', () => {
  57. meydaAnalyzer2.setSource(source1)
  58. })
  59. controls.appendChild(setSRC1toAnalyzer2)
  60.  
  61. const setSRC2toAnalyzer2 = document.createElement('button')
  62. setSRC2toAnalyzer2.textContent = 'source 2 on analyzer 2'
  63. setSRC2toAnalyzer2.addEventListener('click', () => {
  64. meydaAnalyzer2.setSource(source2)
  65. })
  66. controls.appendChild(setSRC2toAnalyzer2)
  67.  
  68. const debug1 = document.createElement('pre')
  69. document.body.appendChild(debug1)
  70.  
  71. const debug2 = document.createElement('pre')
  72. document.body.appendChild(debug2)
  73.  
  74. const debugStereo1 = document.createElement('div')
  75. debugStereo1.style.width = debugStereo1.style.height = '20px'
  76. debugStereo1.style.backgroundColor = '#AEF0AE'
  77. document.body.appendChild(debugStereo1)
  78.  
  79. const debugStereo2 = document.createElement('div')
  80. debugStereo2.style.width = debugStereo2.style.height = '20px'
  81. debugStereo2.style.backgroundColor = '#FEA0FE'
  82. document.body.appendChild(debugStereo2)
  83.  
  84. const meydaAnalyzer1 = Meyda.createMeydaAnalyzer({
  85. audioContext: context,
  86. source: source1,
  87. bufferSize: 512,
  88. featureExtractors: ['rms'],
  89. callback: (datas) => {
  90. debug1.innerHTML = JSON.stringify(datas, null, ' ')
  91. }
  92. })
  93. meydaAnalyzer1.start()
  94.  
  95. const meydaAnalyzer2 = Meyda.createMeydaAnalyzer({
  96. audioContext: context,
  97. source: source2,
  98. bufferSize: 512,
  99. featureExtractors: ['rms'],
  100. callback: (datas) => {
  101. debug2.innerHTML = JSON.stringify(datas, null, ' ')
  102. }
  103. })
  104. meydaAnalyzer2.start()
  105.  
  106. const meydaAnalyzerStereo1 = Meyda.createMeydaAnalyzer({
  107. audioContext: context,
  108. source: stereo,
  109. inputs: 2,
  110. channel: 0,
  111. bufferSize: 512,
  112. featureExtractors: ['rms'],
  113. callback: (datas) => {
  114. debugStereo1.style.width = `${datas.rms * 2000}px`
  115. }
  116. })
  117. meydaAnalyzerStereo1.start()
  118.  
  119. const meydaAnalyzerStereo2 = Meyda.createMeydaAnalyzer({
  120. audioContext: context,
  121. source: stereo,
  122. inputs: 2,
  123. channel: 1,
  124. bufferSize: 512,
  125. featureExtractors: ['rms'],
  126. callback: (datas) => {
  127. debugStereo2.style.width = `${datas.rms * 2000}px`
  128. }
  129. })
  130. meydaAnalyzerStereo2.start()
Add Comment
Please, Sign In to add comment