gpt4 book ai didi

audio - MediaDevices.getUserMedia() 如何设置音频约束(采样率/位深度)?

转载 作者:行者123 更新时间:2023-12-02 22:36:08 49 4
gpt4 key购买 nike

使用浏览器 Web API,我想设置 MediaDevices.getUserMedia 约束属性,适合记录音频语音(语音消息),例如设置这些参数:

  • 单声道
  • 16 位
  • 16KHz

  • 这是我的代码:

       const mediaStreamConstraints = {
    audio: {
    channelCount: 1,
    sampleRate: 16000,
    sampleSize: 16,
    volume: 1
    },

    video: false
    }

    navigator.mediaDevices.getUserMedia(mediaStreamConstraints)
    .catch( err => serverlog(`ERROR mediaDevices.getUserMedia: ${err}`) )
    .then( stream => {

    // 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)

    } )


    录制的剪辑被抓取并存储(使用 MediaRecorder API),最终发送到 nodejs 服务器,在那里 blob 被保存为文件并进行处理(应用程序是一个语音机器人)。

    出了点问题,WebM 保存的文件没有所需的参数:
    $ 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

    例如。
    Sampling rate                            : 48.0 kHz
    Bit depth : 32 bits

    但约束将意味着不同的值:
    Sampling rate                            : 16 kHz
    Bit depth : 16 bits

    还有 blob,重新玩了 Audio(audioUrl(blob)).play() , 不玩 .奇怪的。但如果约束只是:
    const mediaStreamConstraints = { audio: true }

    我检查了浏览器控制台,没有看到任何错误 navigator.mediaDevices.getUserMedia(mediaStreamConstraints) API 调用。

    顺便说一句,我在这里遵循了指导方针:
  • https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
  • https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints

  • 请注意,我的用户代理是: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 (我使用的是最新的 Brave 浏览器版本)。

    在我看来,浏览器不允许的任何音频限制设置:
  • 打破音频 blob,
  • 没有引发错误异常(我同时捕获了 navigator.mediaDevices.getUserMedia()new MediaRecorder(...) 。这至少不是最后一个错误吗?


  • 我的问题是:

    有什么方法可以按要求获得采样率/位深度?

    或者音频格式是“硬编码”/由浏览器实现决定的?

    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)

    最佳答案

    首先检查浏览器的功能:

    let stream = await navigator.mediaDevices.getUserMedia({audio: true});
    let track = stream.getAudioTracks()[0];
    console.log(track.getCapabilities());
    演示输出:
    autoGainControl: (2) [true, false]
    channelCount: {max: 2, min: 1}
    deviceId: "default"
    echoCancellation: (2) [true, false]
    groupId: "1e76386ad54f9ad3548f6f6c14c08e7eff6753f9362d93d8620cc48f546604f5"
    latency: {max: 0.01, min: 0.01}
    noiseSuppression: (2) [true, false]
    sampleRate: {max: 48000, min: 48000}
    sampleSize: {max: 16, min: 16}

    关于audio - MediaDevices.getUserMedia() 如何设置音频约束(采样率/位深度)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62392352/

    49 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com