Skip to content

Instantly share code, notes, and snippets.

@hugohil
Last active January 8, 2018 14:06
Show Gist options
  • Save hugohil/027cce877c5dd5a534324e2abdca861f to your computer and use it in GitHub Desktop.
Save hugohil/027cce877c5dd5a534324e2abdca861f to your computer and use it in GitHub Desktop.
meyda multiple source and analyzers
/* to run:
budo index.js --live
uncomment the `source.connect(context.destination)` to hear tracks
*/
'use strict'
const Meyda = require('../../path/to/meyda') // use your local meyda installation
const context = new window.AudioContext()
const tune1 = new window.Audio('./tune1.mp3')
tune1.preload = true
tune1.loop = true
tune1.controls = true
document.body.appendChild(tune1)
const source1 = context.createMediaElementSource(tune1)
// source1.connect(context.destination)
const tune2 = new window.Audio('./tune2.mp3')
tune2.preload = true
tune2.loop = true
tune2.controls = true
document.body.appendChild(tune2)
const source2 = context.createMediaElementSource(tune2)
// source2.connect(context.destination)
const tunestereo = new window.Audio('./tune-stereo.mp3')
tunestereo.preload = true
tunestereo.loop = true
tunestereo.controls = true
document.body.appendChild(tunestereo)
const stereo = context.createMediaElementSource(tunestereo)
// stereo.connect(context.destination)
const controls = document.createElement('div')
document.body.appendChild(controls)
const setSRC1toAnalyzer1 = document.createElement('button')
setSRC1toAnalyzer1.textContent = 'source 1 on analyzer 1'
setSRC1toAnalyzer1.addEventListener('click', () => {
meydaAnalyzer1.setSource(source1)
})
controls.appendChild(setSRC1toAnalyzer1)
const setSRC2toAnalyzer1 = document.createElement('button')
setSRC2toAnalyzer1.textContent = 'source 2 on analyzer 1'
setSRC2toAnalyzer1.addEventListener('click', () => {
meydaAnalyzer1.setSource(source2)
})
controls.appendChild(setSRC2toAnalyzer1)
const setSRC1toAnalyzer2 = document.createElement('button')
setSRC1toAnalyzer2.textContent = 'source 1 on analyzer 2'
setSRC1toAnalyzer2.addEventListener('click', () => {
meydaAnalyzer2.setSource(source1)
})
controls.appendChild(setSRC1toAnalyzer2)
const setSRC2toAnalyzer2 = document.createElement('button')
setSRC2toAnalyzer2.textContent = 'source 2 on analyzer 2'
setSRC2toAnalyzer2.addEventListener('click', () => {
meydaAnalyzer2.setSource(source2)
})
controls.appendChild(setSRC2toAnalyzer2)
const debug1 = document.createElement('pre')
document.body.appendChild(debug1)
const debug2 = document.createElement('pre')
document.body.appendChild(debug2)
const debugStereo1 = document.createElement('div')
debugStereo1.style.width = debugStereo1.style.height = '20px'
debugStereo1.style.backgroundColor = '#AEF0AE'
document.body.appendChild(debugStereo1)
const debugStereo2 = document.createElement('div')
debugStereo2.style.width = debugStereo2.style.height = '20px'
debugStereo2.style.backgroundColor = '#FEA0FE'
document.body.appendChild(debugStereo2)
const meydaAnalyzer1 = Meyda.createMeydaAnalyzer({
audioContext: context,
source: source1,
bufferSize: 512,
featureExtractors: ['rms'],
callback: (datas) => {
debug1.innerHTML = JSON.stringify(datas, null, ' ')
}
})
meydaAnalyzer1.start()
const meydaAnalyzer2 = Meyda.createMeydaAnalyzer({
audioContext: context,
source: source2,
bufferSize: 512,
featureExtractors: ['rms'],
callback: (datas) => {
debug2.innerHTML = JSON.stringify(datas, null, ' ')
}
})
meydaAnalyzer2.start()
const meydaAnalyzerStereo1 = Meyda.createMeydaAnalyzer({
audioContext: context,
source: stereo,
inputs: 2,
channel: 0,
bufferSize: 512,
featureExtractors: ['rms'],
callback: (datas) => {
debugStereo1.style.width = `${datas.rms * 2000}px`
}
})
meydaAnalyzerStereo1.start()
const meydaAnalyzerStereo2 = Meyda.createMeydaAnalyzer({
audioContext: context,
source: stereo,
inputs: 2,
channel: 1,
bufferSize: 512,
featureExtractors: ['rms'],
callback: (datas) => {
debugStereo2.style.width = `${datas.rms * 2000}px`
}
})
meydaAnalyzerStereo2.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment