Last active
March 20, 2020 00:01
-
-
Save r01010010/5328b7c4e9a853e9e5374f227711f946 to your computer and use it in GitHub Desktop.
Create RxJS Observable from getUserMedia, createMediaStreamSource and analyserNode (Only Chrome)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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