从“ISampleSource”读取样本数组后如何播放它们

所以,我有一段代码可以读取ISampleSourcea 的输入float[][],第一个数组层用于通道数,第二层用于通道内的样本数据。我将获取这些数据并尝试对其应用信号处理,但是出于调试目的,我可能想要操作样本数组,然后播放它,以便我可以“听到”代码在做什么。有没有一种简单的方法来获取返回的数据ISampleSource.Read并将其重新粘贴到新 的数据中,ISampleSource然后可以将其转换为IWaveSource并使用 播放WasapiOut?


这是我到目前为止尝试制作的课程,您将其传递给 a 中的float[][]基本上所有数据,WaveFormat以便从中制作一个……但它实际上并没有做任何事情。不会出错,不会播放.. 什么也不做。我究竟做错了什么?


private class SampleSource : ISampleSource

{

    public long Position { get; set; }

    public WaveFormat WaveFormat { get; private set; }

    public bool CanSeek => true;

    public long Length  => _data.Length;


    private float[] _data;

    private long readPoint = 0;


    public SampleSource(float[][] samples, int sampleRate, int bits, int channels)

    {

        WaveFormat = new WaveFormat(sampleRate, bits, channels);

        if (samples.Length <= 0) return;


        _data = new float[samples[0].Length * samples.Length];

        int cchannels = samples.Length;

        int sampleLength = samples[0].Length;


        for (var i = 0; i < sampleLength; i += cchannels)

            for (var n = 0; n < cchannels; n++)

                _data[i + n] = samples[n][i / cchannels];

    }


    public int Read(float[] buffer, int offset, int count)

    {

        if (_data.Length < Position + count)

            count = (int) (_data.Length - Position);


        float[] outFloats = new float[count];

        for (var i = 0; i < count; i++)

            outFloats[i] = _data[i + Position + offset];


        buffer = outFloats;

        Position += count;

        return count;

    }


    public void Dispose() =>_data = null;

}


慕侠2389804
浏览 202回答 1
1回答

慕工程0101907

buffer我需要直接写入buffer数组元素,而不是尝试设置为新数组(这毫无意义),以便它们可以在函数调用之外使用。我真的不喜欢这样做,也许是为了解决我看不到的问题,但显然我正在使用的库就是这样做的。private class SampleSource : ISampleSource{&nbsp; &nbsp; public long Position { get; set; }&nbsp; &nbsp; public WaveFormat WaveFormat { get; private set; }&nbsp; &nbsp; public bool CanSeek => true;&nbsp; &nbsp; public long Length&nbsp; => _data.Length;&nbsp; &nbsp; private float[] _data;&nbsp; &nbsp; private long readPoint = 0;&nbsp; &nbsp; public SampleSource(float[][] samples, int sampleRate, int bits, int channels)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; WaveFormat = new WaveFormat(sampleRate, bits, channels);&nbsp; &nbsp; &nbsp; &nbsp; if (samples.Length <= 0) return;&nbsp; &nbsp; &nbsp; &nbsp; _data = new float[samples[0].Length * samples.Length];&nbsp; &nbsp; &nbsp; &nbsp; int cchannels = samples.Length;&nbsp; &nbsp; &nbsp; &nbsp; int sampleLength = samples[0].Length;&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < sampleLength; i += cchannels)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var n = 0; n < cchannels; n++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _data[i + n] = samples[n][i / cchannels];&nbsp; &nbsp; }&nbsp; &nbsp; public int Read(float[] buffer, int offset, int count)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; /*THIS IS THE CHANGED FUNCTION*/&nbsp; &nbsp; &nbsp; &nbsp; if (_data.Length < Position + count)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = (int) (_data.Length - Position);&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < count; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[i] = _data[i + Position + offset];&nbsp; &nbsp; &nbsp; &nbsp; Position += count;&nbsp; &nbsp; &nbsp; &nbsp; return count;&nbsp; &nbsp; }&nbsp; &nbsp; public void Dispose() =>_data = null;}
打开App,查看更多内容
随时随地看视频慕课网APP