public abstract Read ( Array samples, long count ) : long | ||
samples | Array | The sample array to fill. |
count | long | The maximum number of samples to read. |
return | long |
/// <summary> /// Reads a block of audio samples from the sound and writes the data to given array of samples. /// </summary> /// <param name="samples">Sample array to fill.</param> /// <param name="count">Maximum number of samples to read.</param> /// <returns>The number of samples actually read.</returns> public long Decode(short[] samples, long count) { long read = reader?.Read(samples, count) ?? 0L; SampleOffset += read; return(read); }
/// <summary> /// Request a new chunk of audio samples from the stream source. /// </summary> /// <param name="samples">The audio chunk that contains audio samples.</param> /// <returns><code>true</code> if reach the end of stream, otherwise false.</returns> protected override bool OnGetData(out short[] samples) { lock (_mutex) { // Fill the chunk parameters samples = new short[_sampleCount]; long count = _reader.Read(samples, samples.Length); // Remove the gap when processing last buffer Array.Resize(ref samples, (int)count); // Check if we have reached the end of the audio file return(count == _sampleCount); } }
private void Initialize(SoundReader reader, SampleInfo info) { // Retrieve the sound parameters long sampleCount = info.SampleCount; int channelCount = info.ChannelCount; int sampleRate = info.SampleRate; // Read the samples from the provided file using (reader) { _samples = new short[sampleCount]; if (reader.Read(_samples, sampleCount) == sampleCount) { // Update the internal buffer with the new samples Update(channelCount, sampleRate); } else { throw new Exception("Failed to decode the sound data."); } } }