title: MediaDevices.getUserMedia() How can I set audio constraints (sampling rate/bit depth)?
body: <p>With browser Web API, I'd like to set <code>MediaDevices.getUserMedia</code> <strong>constraints</strong> attributes, suitable to record audio speech (voice messages), e.g. setting these parameters:</p>
  
  <ul>
  <li>mono</li>
  <li>16bit</li>
  <li>16KHz</li>
  </ul>
  
  <p>Here my code:</p>
  
  <pre class="lang-js prettyprint-override"><code>   const mediaStreamConstraints = {
         audio: {
           channelCount: 1,
           sampleRate: 16000,
           sampleSize: 16,
           volume: 1
         },
  
         video: false
     }
  
     navigator.mediaDevices.getUserMedia(mediaStreamConstraints)
       .catch( err =&gt; serverlog(`ERROR mediaDevices.getUserMedia: ${err}`) )
       .then( stream =&gt; {
  
          // audio recorded as Blob 
          // and the binary data are sent via socketio to a nodejs server
          // that store blob as a file (e.g. audio/inp/audiofile.webm)
  
        } )
  
  </code></pre>
  
  <p>The recorded clip is grabbed and stored (using <code>MediaRecorder</code> API), eventually sent to a nodejs server where the blob is saved as a file and processed (the application is a voicebot).</p>
  
  <p>Something goes wrong and the WebM saved file hasn't the required parameters:</p>
  
  <pre><code>$ mediainfo audio/inp/audiofile.webm
  General
  Complete name                            : audio/inp/audiofile.webm
  Format                                   : WebM
  Format version                           : Version 4 / Version 2
  File size                                : 2.04 KiB
  Writing application                      : Chrome
  Writing library                          : Chrome
  IsTruncated                              : Yes
  
  Audio
  ID                                       : 1
  Format                                   : Opus
  Codec ID                                 : A_OPUS
  Channel(s)                               : 1 channel
  Channel positions                        : Front: C
  Sampling rate                            : 48.0 kHz
  Bit depth                                : 32 bits
  Compression mode                         : Lossy
  Language                                 : English
  Default                                  : Yes
  Forced                                   : No
  </code></pre>
  
  <p>E.g. </p>
  
  <pre><code>Sampling rate                            : 48.0 kHz
  Bit depth                                : 32 bits
  </code></pre>
  
  <p>But constraints would imply different values: </p>
  
  <pre><code>Sampling rate                            : 16 kHz
  Bit depth                                : 16 bits
  </code></pre>
  
  <p>Also the blob, played with anew <code>Audio(audioUrl(blob)).play()</code>, <strong>doesn't play</strong>. Weird. But all works if constraints are just:</p>
  
  <pre><code>const mediaStreamConstraints = { audio: true }
  </code></pre>
  
  <p>I checked the browser console and I didn't see any error of <code>navigator.mediaDevices.getUserMedia(mediaStreamConstraints)</code> API call.</p>
  
  <p>BTW, I followed guidelines here: </p>
  
  <ul>
  <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia" rel="noreferrer">https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia</a></li>
  <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints" rel="noreferrer">https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints</a></li>
  </ul>
  
  <p>Note that my user agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36</code> (I'm using a last Brave browser version).</p>
  
  <hr>
  
  <p>Seems to me that any audio constraints settings not allowed by the browser:</p>
  
  <ul>
  <li>brokes the audio blob, </li>
  <li>without raising an error exception (I catched both <code>navigator.mediaDevices.getUserMedia()</code> and <code>new MediaRecorder(...)</code>. Isn't this last at least a bug?</li>
  </ul>
  
  <hr>
  
  <p>My question is:</p>
  
  <p><strong>There is any way to have sampling rate/bit depth as requested?</strong></p>
  
  <p>Or the audio format is "hardcoded"/decided by browser implementation?</p>
  
  <hr>
  
  <blockquote>
    <p>BTW, The reason of audio parameters formats is because I want to minimize audio blob size to minimize bandwidth in websocket communications between the browser client and the server, optimizing audio blob exchanges for speech (voice messages) </p>
  </blockquote>