public unsafe PaNonInterleavedBuffer(IntPtr pointer, int channels, int frames) : base(pointer, channels, frames) { channelBuffers = new PaBuffer <T> [Channels]; IntPtr *ptr = (IntPtr *)Pointer.ToPointer(); for (int channelIndex = 0; channelIndex < Channels; channelIndex++) { channelBuffers[channelIndex] = new PaBuffer <T>(ptr[channelIndex], channels, frames); } }
public PaNonInterleavedBuffer(int channels, int frames) : base(channels * Marshal.SizeOf(typeof(IntPtr)), channels, frames) { channelBuffers = new PaBuffer <T> [Channels]; unsafe { IntPtr *ptr = (IntPtr *)Pointer.ToPointer(); for (int channelIndex = 0; channelIndex < Channels; channelIndex++) { var channelBuffer = new PaBuffer <T>(1, Frames); channelBuffers[channelIndex] = channelBuffer; ptr[channelIndex] = channelBuffer.Pointer; } } }
private void WriteStreamPrivate(PaBuffer buffer, bool isUnsafe = false, bool isNonInterleaved = false) { if (numOutputChannels == 0) { throw new InvalidOperationException("Cannot write to a stream with no output channels."); } if (numOutputChannels != buffer.Channels) { throw new ArgumentException( $"Expected buffer with {numOutputChannels} channels but got {buffer.Channels}.", nameof(buffer)); } if (!isUnsafe) { var expectedType = SampleFormatToType(outputSampleFormat); if (expectedType == typeof(PaBuffer)) { throw new InvalidOperationException( "To ensure memory safety, this overload of WriteStream is not supported for streams with " + "custom sample formats. Use WriteStream(PaBuffer) instead."); } var formatNonInterleaved = (outputSampleFormat & PaSampleFormat.paNonInterleaved) != 0; if (isNonInterleaved && !formatNonInterleaved) { throw new InvalidOperationException( "Only streams with non-interleaved sample formats support this overload of WriteStream, " + "Use WriteStream(PaBuffer<T>) instead."); } if (!isNonInterleaved && formatNonInterleaved) { throw new InvalidOperationException( "Only streams with interleaved sample formats suppoort this overload of WriteStream. " + "Use WriteStream(PaNonInterleavedBuffer<T>) instead."); } if (expectedType != buffer.GetType()) { throw new ArgumentException( $"Expected a buffer of type {expectedType} for the sample format, but got {buffer.GetType()}.", nameof(buffer)); } } PaBindings.Pa_ReadStream(stream, buffer.Pointer, (unsigned_long_t)buffer.Frames); }
public PaNonInterleavedBuffer(T[][] array, int channels, int frames) : base(channels * Marshal.SizeOf(typeof(IntPtr)), channels, frames) { try { if (array.Length != channels) { throw new ArgumentException( "The length of the jagged array is expected to match the number of channels, " + "i.e. array.Length == channels.", nameof(array)); } for (int channelIndex = 0; channelIndex < Channels; channelIndex++) { if (array[channelIndex].Length != frames) { throw new ArgumentException( "The length of each array in the jagged array is expected to match the number of frames, " + "i.e. array[channel].Length == frames, for all channel = 1 ... channel = Channels - 1.", nameof(array)); } } } catch { // unfortunately the base constructor will already have allocated memory, which must be freed Dispose(); } channelBuffers = new PaBuffer <T> [Channels]; unsafe { IntPtr *ptr = (IntPtr *)Pointer.ToPointer(); for (int channelIndex = 0; channelIndex < Channels; channelIndex++) { var channelBuffer = new PaBuffer <T>(array[channelIndex], 1, frames); channelBuffers[channelIndex] = channelBuffer; ptr[channelIndex] = channelBuffer.Pointer; } } }
public void WriteStream <T>(PaBuffer <T> buffer) where T : unmanaged => WriteStreamPrivate(buffer);
public unsafe void WriteStream(PaBuffer buffer) => WriteStreamPrivate(buffer, isUnsafe: true);
public void ReadStream <T>(PaBuffer <T> buffer) where T : unmanaged => ReadStreamPrivate(buffer);
public unsafe void ReadStream(PaBuffer buffer) => ReadStreamPrivate(buffer, isUnsafe: true);