WaveMixerStream32和IWaveProvider [英] WaveMixerStream32 and IWaveProvider

查看:128
本文介绍了WaveMixerStream32和IWaveProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NAudio是否可以将WaveMixerStream32与WaveProviders而不是WaveStreams链接起来?我正在使用BufferedWaveProvider传输多个网络流.似乎没有简单的方法可以将其转换为WaveStream.

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.

干杯!

卢克

推荐答案

将IWaveProvider转换为WaveStream非常简单. IWaveProvider只是简化的WaveStream,不支持重新定位,并且长度未知.您可以创建一个这样的适配器:

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.

此外,没有什么阻止您创建可在IWaveProvider上运行的自己的WaveMixerStream32版本.您可以简化很多事情,因为无需在混合器中实现任何重定位逻辑,因为您无法重定位任何输入.

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.

这篇关于WaveMixerStream32和IWaveProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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