private void ReadAndUseAudioProcessor(int requestedBytes, double speedRatio) { if (AudioProcessorBuffer == null || AudioProcessorBuffer.Length < Convert.ToInt32(requestedBytes * Constants.Controller.MaxSpeedRatio)) { AudioProcessorBuffer = new short[Convert.ToInt32(requestedBytes * Constants.Controller.MaxSpeedRatio / Constants.Audio.BytesPerSample)]; } var bytesToRead = Convert.ToInt32((requestedBytes * speedRatio).ToMultipleOf(SampleBlockSize)); var samplesToRequest = requestedBytes / SampleBlockSize; // Set the new tempo (without changing the pitch) according to the speed ratio AudioProcessor.SetTempo(Convert.ToSingle(speedRatio)); // Sending Samples to the processor while (AudioProcessor.AvailableSampleCount < samplesToRequest && AudioBuffer != null) { var realBytesToRead = Math.Min(AudioBuffer.ReadableCount, bytesToRead); if (realBytesToRead == 0) { break; } realBytesToRead = Convert.ToInt32((realBytesToRead * 1.0).ToMultipleOf(SampleBlockSize)); AudioBuffer.Read(realBytesToRead, ReadBuffer, 0); Buffer.BlockCopy(ReadBuffer, 0, AudioProcessorBuffer, 0, realBytesToRead); AudioProcessor.PutSamplesI16(AudioProcessorBuffer, Convert.ToUInt32(realBytesToRead / SampleBlockSize)); } // Receiving samples from the processor var numSamples = AudioProcessor.ReceiveSamplesI16(AudioProcessorBuffer, Convert.ToUInt32(samplesToRequest)); Array.Clear(ReadBuffer, 0, ReadBuffer.Length); Buffer.BlockCopy(AudioProcessorBuffer, 0, ReadBuffer, 0, Convert.ToInt32(numSamples * SampleBlockSize)); }
private void ReadAndUseAudioProcessor(int requestedBytes) { if (AudioProcessorBuffer == null || AudioProcessorBuffer.Length < (int)(requestedBytes * Constants.MaxSpeedRatio)) { AudioProcessorBuffer = new short[(int)(requestedBytes * Constants.MaxSpeedRatio / BytesPerSample)]; } var speedRatio = SpeedRatio; var bytesToRead = (int)(requestedBytes * speedRatio).ToMultipleOf(SampleBlockSize); var samplesToRead = bytesToRead / SampleBlockSize; var samplesToRequest = requestedBytes / SampleBlockSize; AudioProcessor.Tempo = Convert.ToSingle(speedRatio); while (AudioProcessor.AvailableSampleCount < samplesToRequest && AudioBuffer != null) { var realBytesToRead = Math.Min(AudioBuffer.ReadableCount, bytesToRead); realBytesToRead = (int)(realBytesToRead * 1.0).ToMultipleOf(SampleBlockSize); AudioBuffer.Read(realBytesToRead, ReadBuffer, 0); Buffer.BlockCopy(ReadBuffer, 0, AudioProcessorBuffer, 0, realBytesToRead); AudioProcessor.PutSamplesI16(AudioProcessorBuffer, (uint)(realBytesToRead / SampleBlockSize)); } uint numSamples = AudioProcessor.ReceiveSamplesI16(AudioProcessorBuffer, (uint)samplesToRequest); Array.Clear(ReadBuffer, 0, ReadBuffer.Length); Buffer.BlockCopy(AudioProcessorBuffer, 0, ReadBuffer, 0, (int)(numSamples * SampleBlockSize)); }