How usergetmedia to use the microphone in chrome and then stream to get raw audio? I need need to get the audio in linear 16.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- Can we recover audio from MFCC coefficients?
- void before promise syntax
- Keeping track of variable instances
Unfortunately, the MediaRecorder doesn't support raw PCM capture. (A sad oversight, in my opinion.) Therefore, you'll need to get the raw samples and buffer/save them yourself.
You can do this with the ScriptProcessorNode. Normally, this Node is used to modify the audio data programmatically, for custom effects and what not. But, there's no reason you can't just use it as a capture point. Untested, but try something like this code:
(You can find a more complete example on MDN.)
Those floating point samples are centered on zero
0
and will ideally be bound to-1
and1
. When converting to an integer range, you'll want to clamp values to this range, clipping anything beyond it. (The values can sometimes exceed-1
and1
in the event loud sounds are mixed together in-browser. In theory, the browser can also record float32 samples from an external sound device which may also exceed that range, but I don't know of any browser/platform that does this.)When converting to integer, it matters if the values are signed or unsigned. If signed, for 16-bit, the range is
-32768
to32767
. For unsigned, it's0
to65535
. Figure out what format you want to use and scale the-1
to1
values up to that range.One final note on this conversion... endianness can matter. See also: https://stackoverflow.com/a/7870190/362536
The only two examples I've found that are clear and make sense are the following:
AWS Labs: https://github.com/awslabs/aws-lex-browser-audio-capture/blob/master/lib/worker.js
The AWS resource is very good. It shows you how to export your recorded audio to "WAV format encoded as PCM". Amazon Lex, which is a transcription service offered by AWS requires the audio to be PCM encoded and wrapped in a WAV container. You can merely adapt some of the code to make it work for you! AWS has some additional features such as "downsampling" which allows you to change the sample rate without affecting the recording.
RecordRTC: https://github.com/muaz-khan/RecordRTC/blob/master/simple-demos/raw-pcm.html
RecordRTC is a complete library. You can, once again, adapt their code or find the snippet of code that encodes the audio to raw PCM. You could also implement their library and use the code as-is. Using the "desiredSampleRate" option for audio config with this library negatively affects the recording.
They are both excellent resources and you'll definitely be able to solve your question.
here is some Web Audio API where it uses the microphone to capture and playback raw audio (turn down your volume before running this page) ... to see snippets of raw audio in PCM format view the browser console ... for kicks it also sends this PCM into a call to FFT to obtain the frequency domain as well as the time domain of the audio curve
You should look into MediaTrackConstraints.sampleSize property for the MediaDevices.getUserMedia() API. Using the
sampleSize
constraint, if your audio hardware permits you can set the sample size to 16 bits.As far as the implementation goes, well that's what the links are and google are for...