WaveMixerStream32 and IWaveProvider

我怕爱的太早我们不能终老 提交于 2019-12-18 09:09:26

问题


Is there any way with NAudio to link a WaveMixerStream32 with WaveProviders, rather than WaveStreams? I am streaming multiple network streams, using a BufferedWaveProvider. There doesn't seem to be an easy way to convert it into a WaveStream.

Cheers!

Luke


回答1:


It's a fairly simple to convert an IWaveProvider to a WaveStream. An IWaveProvider is just a simplified WaveStream that doesn't support repositioning and has an unknown length. You can create an adapter like this:

public class WaveProviderToWaveStream : WaveStream
{
    private readonly IWaveProvider source;
    private long position;

    public WaveProviderToWaveStream(IWaveProvider source)
    {
        this.source = source;
    }

    public override WaveFormat WaveFormat
    {
        get { return source.WaveFormat;  }
    }

    /// <summary>
    /// Don't know the real length of the source, just return a big number
    /// </summary>
    public override long Length
    {
        get { return Int32.MaxValue; } 
    }

    public override long Position
    {
        get
        {
            // we'll just return the number of bytes read so far
            return position;
        }
        set
        {
            // can't set position on the source
            // n.b. could alternatively ignore this
            throw new NotImplementedException();
        }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        int read = source.Read(buffer, offset, count);
        position += read;
        return read;
    }
}

I've put some comments in about the Length and Position properties. What you need to do with them depends on whether the class you are passing this into attempts to make use of those properties or not.

Also, there is nothing stopping you creating your own version of WaveMixerStream32 that works on IWaveProvider. You could simplify things a lot since there would be no need to implement any repositioning logic in the mixer since you can't reposition any of your inputs.



来源:https://stackoverflow.com/questions/6636142/wavemixerstream32-and-iwaveprovider

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!