开发者

WaveMixerStream32 and IWaveProvider

开发者 https://www.devze.com 2023-03-19 06:21 出处:网络
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 do开发者_StackOverflo

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 do开发者_StackOverflowesn't seem to be an easy way to convert it into a WaveStream.

Cheers!

Luke


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号