从PCM样本创建WaveStream

时间:2010-11-24 20:11:19

标签: c# audio naudio

我检查了NAudio及其WaveStream相关类,但我找不到基于我提供的PCM样本创建WaveStream的内置方法。

理想情况下,我想做这样的事情:

byte[] samples = ...
WaveFormat waveFormat = new WaveFormat(audioSampleRate, 
                                       audioBitsPerSample,
                                       audioChannels);
WaveStream waveStream = CreateWaveStreamfromSamples(waveFormat,
                                                    samples);

有没有办法使用NAudio做到这一点?

在Hans,Mark澄清之后

编辑(感谢您的反馈):

我需要一个可以传递给SoundPlayer.Play的流,这与WaveStream不太匹配。在我的项目中,我最终实现了以下课程,该课程取样WaveFormat并可以SoundPlayer播放。

public class MemoryWaveStream : Stream
{
    public override bool CanSeek { get { return false; } }
    public override bool CanWrite { get { return false; } }
    public override bool CanRead { get { return true; } }
    public override long Length { get { return _waveStream.Length; } }
    public override long Position { get { return _waveStream.Position; } set { _waveStream.Position = value; } }

    private MemoryStream _waveStream;

    public MemoryWaveStream(byte[] sampleData, WaveFormat waveFormat)
    {
        _waveStream = new MemoryStream();
        BinaryWriter bw = new BinaryWriter(_waveStream);
        bw.Write(new char[4] { 'R', 'I', 'F', 'F' });

        int length = 36 + sampleData.Length;
        bw.Write(length);

        bw.Write(new char[8] { 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ' });

        waveFormat.Serialize(bw);
        bw.Write(new char[4] { 'd', 'a', 't', 'a' });
        bw.Write(sampleData.Length);
        bw.Write(sampleData, 0, sampleData.Length);
        _waveStream.Position = 0;
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return _waveStream.Read(buffer, offset, count);
    }

    public override void Flush()
    {
        _waveStream.Flush();
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return _waveStream.Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        throw new NotImplementedException();
    }
    public override void Write(byte[] buffer, int offset, int count)
    {
        throw new NotImplementedException();
    }
}

2 个答案:

答案 0 :(得分:5)

这是一个类,如果需要,可以根据字节数组传递MemoryStream。我可能会在NAudio 1.4中包含类似的内容,因为我得到了很多关于如何执行此操作的支持请求。

public class RawSourceWaveStream : WaveStream
{
    private Stream sourceStream;
    private WaveFormat waveFormat;

    public RawSourceWaveStream(Stream sourceStream, WaveFormat waveFormat)
    {
        this.sourceStream = sourceStream;
        this.waveFormat = waveFormat;
    }

    public override WaveFormat WaveFormat
    {
        get { return this.waveFormat; }
    }

    public override long Length
    {
        get { return this.sourceStream.Length; }
    }

    public override long Position
    {
        get
        {
            return this.sourceStream.Position;
        }
        set
        {
            this.sourceStream.Position = value;
        }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return sourceStream.Read(buffer, offset, count);
    }
}

答案 1 :(得分:2)

不,不喜欢它。但是,您可以创建自己的。 wave \ wavestream \ wavefilereader.cs源代码文件可能是最好的开始。你可以从中剥离一堆东西,用正确初始化的MemoryStream替换waveStream。