简体   繁体   English

使用NAudio获取RTP的Ulaw样本

[英]Use NAudio to get Ulaw samples for RTP

I've been looking over the NAudio examples trying to work out how I can get ulaw samples suitable for packaging up as an RTP payload. 我一直在查看NAudio示例,试图弄清楚如何获得适用于打包为RTP有效负载的ulaw样本。 I'm attempting to generate the samples from an mp3 file using the code below. 我正在尝试使用以下代码从mp3文件生成示例。 Not surprisingly, since I don't really have a clue what I'm doing with NAudio, when I transmit the samples across the network to a softphone all I get is static. 毫不奇怪,由于我对NAudio的使用一无所知,因此当我将样本通过网络传输到软件电话时,我得到的都是静态的。

Can anyone provide any direction on how I should be getting 160 bytes (8Khz @ 20ms) ULAW samples from an MP3 file using NAudio? 谁能提供任何关于我应该如何使用NAudio从MP3文件中获取160字节(8Khz @ 20ms)ULAW样本的指示?

private void GetAudioSamples()
{
    var pcmStream = WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader("whitelight.mp3"));
    byte[] buffer = new byte[2];
    byte[] sampleBuffer = new byte[160];
    int sampleIndex = 0;
    int bytesRead = pcmStream.Read(buffer, 0, 2);

    while (bytesRead > 0)
    {
        var ulawByte = MuLawEncoder.LinearToMuLawSample(BitConverter.ToInt16(buffer, 0));
        sampleBuffer[sampleIndex++] = ulawByte;

        if (sampleIndex == 160)
        {
            m_rtpChannel.AddSample(sampleBuffer);
            sampleBuffer = new byte[160];
            sampleIndex = 0;
        }

        bytesRead = pcmStream.Read(buffer, 0, 2);
    }

    logger.Debug("Finished adding audio samples.");
}

Below is the way I eventually got it working. 下面是我最终使它工作的方式。 I do lose one of the channels from the mp3, and I guess there's some way to combine the channels as part of a conversion, but that doesn't matter for my situation. 我确实从mp3中丢失了一个频道,我想有某种方法可以将这些频道合并为转换的一部分,但这对我的情况无关紧要。

The 160 byte buffer size gives me 20ms ulaw samples which work perfectly with the SIP softphone I'm testing with. 160字节的缓冲区大小为我提供了20ms的ulaw采样,可与我正在测试的SIP软电话完美配合。

var pcmFormat = new WaveFormat(8000, 16, 1);
var ulawFormat = WaveFormat.CreateMuLawFormat(8000, 1);

using (WaveFormatConversionStream pcmStm = new WaveFormatConversionStream(pcmFormat, new Mp3FileReader("whitelight.mp3")))
{
    using (WaveFormatConversionStream ulawStm = new WaveFormatConversionStream(ulawFormat, pcmStm))
    {
        byte[] buffer = new byte[160];
        int bytesRead = ulawStm.Read(buffer, 0, 160);

        while (bytesRead > 0)
        {
            byte[] sample = new byte[bytesRead];
            Array.Copy(buffer, sample, bytesRead);
            m_rtpChannel.AddSample(sample);

            bytesRead = ulawStm.Read(buffer, 0, 160);
        }
    }
}

Here's a few pointers. 这里有一些提示。 First of all, as long as you are using NAudio 1.5, no need for the additional WaveFormatConversionStream - Mp3FileReader's Read method returns PCM. 首先,只要您使用NAudio 1.5,就不需要附加的WaveFormatConversionStream-Mp3FileReader的Read方法返回PCM。

However, you will not be getting 8kHz out, so you need to resample it first. 但是,您不会得到8kHz的信号,因此需要首先对其进行重新采样。 WaveFormatConversionStream can do this, although it uses the built-in Windows ACM sample rate conversion, which doesn't seem to filter the incoming audio well, so there could be aliasing artefacts. WaveFormatConversionStream可以执行此操作,尽管它使用内置的Windows ACM采样率转换,该转换似乎不能很好地过滤传入的音频,因此可能会出现混叠伪像。

Also, you usually read bigger blocks than just two bytes at a time as the MP3 decoder needs to decode frames one at a time (the resampler also will want to deal with bigger block sizes). 另外,由于MP3解码器需要一次解码一个帧,因此通常一次读取的块不止两个字节(重采样器也将要处理更大的块大小)。 I would try reading at least 20ms worth of bytes at a time. 我会尝试一次读取至少20ms的字节。

Your use of BitConverter.ToInt16 is correct for getting the 16 bit sample value, but bear in mind that an MP3 is likely stereo, with left, right samples. 您对BitConverter.ToInt16的使用对于获取16位采样值是正确的,但请记住,MP3可能是立体声的,带有左右采样。 Are you sure your phone expects stereo. 您确定手机需要立体声吗?

Finally, I recommend making a mu-law WAV file as a first step, using WaveFileWriter. 最后,我建议首先使用WaveFileWriter制作一个mu-law WAV文件。 Then you can easily listen to it in Windows Media Player and check that what you are sending to your softphone is what you intended. 然后,您可以轻松地在Windows Media Player中收听它,并检查发送到软件电话的内容是否符合您的意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM