首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在流之前使用webrtc放大MediaStreamTrack (音频)

在流之前使用webrtc放大MediaStreamTrack (音频)
EN

Stack Overflow用户
提问于 2017-01-30 18:11:10
回答 1查看 511关注 0票数 1

我使用getAudioTracks()从视频元素中获取音频。然后,在使用addTrack()将其添加到画布之前,我需要放大(增大音量)此audioTrack,并使用webrtc将其传输出去。

有没有办法在客户端用javascript做到这一点呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-05 16:13:56

我想出了一个解决方案。对于任何需要相同东西的人:

代码语言:javascript
复制
            // supposing we have the getUserMedia stream and a canvas
            // we want to stream the canvas content and the
            // amplified audio from user's microphone

            var s = canvas.captureStream();

            var context = new AudioContext(); 

            var gainNode = context.createGain();
            gainNode.gain.value = 1;

            // compress to avoid clipping
            var compressor = context.createDynamicsCompressor();
            compressor.threshold.value = -30;
            compressor.knee.value = 40;
            compressor.ratio.value = 4;
            compressor.reduction.value = -10;
            compressor.attack.value = 0;
            compressor.release.value = 0.25;

            var destination = context.createMediaStreamDestination();

            var input = context.createMediaStreamSource(stream); 

            input.connect(compressor); 
            compressor.connect(gainNode); 
            gainNode.connect( destination); 

            var audioTracks = destination.stream.getAudioTracks();

            // use a slider to alter the value of amplification dynamically
            var rangeElement = document.getElementById("amplifierSlider"); 
            rangeElement .addEventListener("input", function() {
                gainNode.gain.value = parseFloat(rangeElement .value); 
            }, false); 

            for (var i=0; i < audioTracks.length; i++) {  
                s.addTrack(audioTracks[i]);   
            } 

            // stream the canvas with the added audio tracks

https://jsfiddle.net/Thanos_Sar/Lt00nr8g/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41933313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档