Skip to content

Instantly share code, notes, and snippets.

@r01010010
Last active March 20, 2020 00:01
Show Gist options
  • Save r01010010/5328b7c4e9a853e9e5374f227711f946 to your computer and use it in GitHub Desktop.
Save r01010010/5328b7c4e9a853e9e5374f227711f946 to your computer and use it in GitHub Desktop.
Create RxJS Observable from getUserMedia, createMediaStreamSource and analyserNode (Only Chrome)
import { Observable, of } from 'rxjs'
import { skipWhile } from 'rxjs/operators
// ...
navigator.getUserMedia({
"audio": true
}, (stream) => {
const buf = new Float32Array(1024)
const mediaStreamSource = window.audioContext.createMediaStreamSource(stream)
const analyser = window.audioContext.createAnalyser()
mediaStreamSource.connect(this.analyser)
const observable$ = Observable.create(observer => {
const updatePitch = () => {
this.analyser.getFloatTimeDomainData(buf)
observer.next(buf)
window.requestAnimationFrame(updatePitch)
}
updatePitch()
})
observable$.subscribe({ next: (buf) => console.log(buf) })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment