从WasapiLoopbackCapture捕获音频,然后转换为muLaw [英] Capture audio from WasapiLoopbackCapture, and convert to muLaw

查看:993
本文介绍了从WasapiLoopbackCapture捕获音频,然后转换为muLaw的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WasapiLoopbackCapture捕获音频

I'm capturing audio with WasapiLoopbackCapture


- format        = IeeeFloat
- SampleRate    = 48000
- BitsPerSample = 32

我需要将其转换为muLaw(8Khz,8位,单声道)-最终它将通过SIP中继发送到电话.我已经尝试了100个样本(其中大多数样本都使用NAudio)和解决方案,但仍然不知道如何执行此操作...

I need to convert this to muLaw (8Khz, 8 bit, mono) - eventually it'll be sent to a phone via SIP trunking. I've tried 100s of samples (most of them with NAudio) and solutions but still have no clue how to do this ...

推荐答案

NAudio中的Mu-Law工具受到限制,因此您可能必须自己动手.

The Mu-Law tools in NAudio are limited so you might have to roll your own.

您需要设置一个IWaveProvider过滤器链,以转换为单声道,更改比特率和更改比特深度.

You'll need to set up a chain of IWaveProvider filters to convert to mono, change bit-rate, and change bit-depth.

waveBuffer = new BufferedWaveProvider(waveIn.WaveFormat);
waveBuffer.DiscardOnBufferOverflow = true;
waveBuffer.ReadFully = false;  // leave a buffer?

sampleStream = new WaveToSampleProvider(waveBuffer);

// Stereo to mono
monoStream = new StereoToMonoSampleProvider(sampleStream)
    {
        LeftVolume = 1f,
        RightVolume = 1f
    };

// Downsample to 8000
resamplingProvider = new WdlResamplingSampleProvider(monoStream, 8000);

// Convert to 16-bit in order to use ACM or MuLaw tools.
ieeeToPcm = new SampleToWaveProvider16(resamplingProvider);

然后为下一步创建自定义IWaveProvider.

Then create a custom IWaveProvider for the next step.

// In MuLawConversionProvider
public int Read(byte[] destinationBuffer, int offset, int readingCount)
{
    // Source buffer has twice as many items as the output array.
    var sizeOfPcmBuffer = readingCount * 2;
    _sourceBuffer = BufferHelpers.Ensure(_sourceBuffer, sizeOfPcmBuffer);
    var sourceBytesRead = _sourceProvider.Read(_sourceBuffer, offset * 2, sizeOfPcmBuffer);
    var samplesRead = sourceBytesRead / 2;

    var outIndex = 0;
    for (var n = 0; n < sizeOfPcmBuffer; n += 2)
    {
        destinationBuffer[outIndex++] = MuLawEncoder.LinearToMuLawSample(BitConverter.ToInt16(_sourceBuffer, offset + n));
    }

    return samplesRead * 2;
}

新的提供程序可以直接发送到WaveOut

The new provider can be sent directly to WaveOut

outputStream = new MuLawConversionProvider(ieeeToPcm);
waveOut.Init(outputStream);
waveOut.Play();

这些过滤器保留在原处,并且BufferedWaveProvider为根".每当您调用BufferedWaveProvider.AddSamples()时,数据都会通过所有这些过滤器.

These filters remain in place with the BufferedWaveProvider as the "root". Whenever you call BufferedWaveProvider.AddSamples(), the data will go through all these filters.

这篇关于从WasapiLoopbackCapture捕获音频,然后转换为muLaw的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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