如何更改麦克风实时音频的播放速度(使用缓冲区)? [英] How to change playback speed of live audio from microphone (using a buffer)?

查看:163
本文介绍了如何更改麦克风实时音频的播放速度(使用缓冲区)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说,通过更改播放速度,我们可以修改音频的频率.我在这里测试过: https://teropa.info/blog/2016/08/10/frequency-and-pitch.html

I have heard that by changing playback speed we can modify the frequency of the audio. I have tested it here : https://teropa.info/blog/2016/08/10/frequency-and-pitch.html

但是问题是我需要一个录制的音频文件才能做到这一点.根据我发现,网络音频无法更改实时音频的播放速度.我一直在想,如果将音频保存在缓冲区中,我们可以更改其播放速度,从而更改频率.

But the problem is that I need a recorded audio file to do that. From what I have found web audio can't change playback speed of live audio. I have been thinking that if we save the audio in buffer we can change its playback speed thus changing the frequency.

我是网络音频API的新手.我发现了一篇通过将实时音频保存到缓冲区来记录实时音频的文章. https://docs.sumerian.amazonaws.com/articles/webaudio-1/

I am new to web-audio API. I have found an article that records live audio by saving it to the buffer. https://docs.sumerian.amazonaws.com/articles/webaudio-1/

我想要的是:-

  1. 从麦克风获取音频.
  2. 将其保存到缓冲区中.
  3. 更改播放速度.
  4. 在扬声器上播放.

演示了如何更改缓冲节点的播放速度的演示 https://mdn.github.io/webaudio-examples/decode-audio-data/

Demo explaining how to change playback speed of a buffernode https://mdn.github.io/webaudio-examples/decode-audio-data/

但是我希望使用现场麦克风音频代替录制的声音.

But I want live mic audio to be used in place of recorded sound.

这是我的小提琴

https://jsfiddle.net/5dza62b8/13/

var audioContext = new(window.AudioContext || window.webkitAudioContext)();
var streamSource, scriptNode, bufferSource, audioBuffer;
var playbackControl = document.querySelector('#playback-rate-control');
var playbackValue = document.querySelector('#playback-rate-value');

// define variables

window.start_audio = function() {
  navigator.mediaDevices.getUserMedia({
    audio: true
  }).then((stream) => {

    alert("Got audio stream from microphone!");
    audioContext = new AudioContext();
    // Create an AudioNode from the stream.
    streamSource = audioContext.createMediaStreamSource(stream);
    scriptNode = audioContext.createScriptProcessor(2048, 1, 1);
    bufferSource = audioContext.createBufferSource();


    // Whenever onaudioprocess event is dispatched it creates a buffer array with the length bufferLength
    scriptNode.onaudioprocess = (audioProcessingEvent) => {
      realtimeBuffer = audioProcessingEvent.inputBuffer.getChannelData(0);
      // Create an array of buffer array
      audioBuffer.push(realtimeBuffer);
    }

    bufferSource.buffer = audioBuffer;
    bufferSource.playbackRate.value = 0.8;

    streamSource.connect(scriptNode);
    bufferSource.connect(audioContext.destination);
    bufferSource.start();

  }).catch((e) => {

    alert(e.name + ". " + e.message);

  });
}


// wire up buttons to stop and play audio, and range slider control
playbackControl.addEventListener('input', function() {
  bufferSource.playbackRate.value = playbackControl.value;
  playbackValue.innerHTML = playbackControl.value;
});

推荐答案

这可能是比您想象的要难的问题-如果播放速度大于1,您将尝试播放尚未发生的声音!

This might be a harder problem than you think – if the playback speed is greater than 1, you'll be trying to play sounds that haven't happened yet!

不过,通常,您可以使用网络音频API向现场麦克风输入添加效果-这是MDN文档中的示例,该示例向输入添加了过滤器:

In general though, you can use the web audio API to add effects to live microphone input – here's an example from the MDN documentation which adds a filter to the input: https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createMediaStreamSource

如果您想放慢现场音频的播放速度,则有可能 -需要注意的是.允许您更改播放速率的Web音频节点是BufferSourceNode,它依赖于您先前已加载缓冲区.但是,您可能可以通过使用自定义AudioWorklet将数据递增地放入缓冲区并使用BufferSourceNode进行播放来解决此问题.要考虑的一件事是,您允许这种情况持续多长时间–随着时间的流逝,缓冲区只会越来越大,而计算机迟早会耗尽内存!

If you want to slow the live audio down, it is possible – with some caveats. The Web Audio node which allows you to change the playback rate is a BufferSourceNode, which relies on you having loaded the buffer previously. However, you could probably work around this by using a custom AudioWorklet to incrementally put the data into a buffer and play it back using a BufferSourceNode. One thing to consider is how long you'd let this go on for – the buffer would just keep getting bigger and bigger as time went on, and sooner or later your computer would run out of memory!

这涉及很多,对于第一次尝试使用Web Audio来说可能不是理想的选择,但是了解音频工作集的最佳起点是:

This is quite involved, and might not be ideal for a first foray into Web Audio, but the best jumping-off point to learn about audio worklets is here: https://developers.google.com/web/updates/2017/12/audio-worklet

使用音频工作集,您还可以研究一些更复杂的算法,这些算法将使您可以更改声音的音高而不改变其长度. https://en.wikipedia.org/wiki/Audio_time_stretching_and_pitch_scaling

Using an audio worklet, you could also investigate some more sophisticated algorithms which would allow you to change the pitch of a sound without changing its length. https://en.wikipedia.org/wiki/Audio_time_stretching_and_pitch_scaling

如果您刚开始使用网络音频,我的建议是编写一些可以录制声音的内容,然后然后更改其播放速率.

If you're just getting started with web audio, my recommendation would be that you write something that lets you record a sound, then change its playback rate.

这篇关于如何更改麦克风实时音频的播放速度(使用缓冲区)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆