Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

c# - WaveMixerStream32 and IWaveProvider

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...