private void _updateAudioContext() { _dataWasSent = false; _stretcher?.Dispose(); var speedRatio = (_renderer.RefreshRate / _config.TargetFps); Console.Write(speedRatio); _stretcher = new RubberBandStretcher( _config.SampleRate, 2, RubberBandStretcher.Options.ProcessRealTime, 1 / speedRatio, speedRatio ); _latencyCounter = _stretcher.GetLatency(); if (_resamplerState != IntPtr.Zero) { SampleRate.src_delete(_resamplerState); } if (_config.SampleRate != (int)_currentSystemAvInfo.Timing.SampleRate) { _resampleNeeded = true; _audioResampleRatio = (_config.SampleRate / _currentSystemAvInfo.Timing.SampleRate); _resamplerState = SampleRate.src_new(SampleRate.Quality.SRC_SINC_BEST_QUALITY, 2, out var error); if (error > 0) { Logger.Error("Error initializing resampler: '{0}'", SampleRate.src_strerror(error)); } Logger.Debug("Audio Resample Ratio: {0}", _audioResampleRatio); } else { _resampleNeeded = false; Logger.Debug("Resampling not needed"); } }
private void _resampleAudioData() { unsafe { var sampleCount = _temporaryBuffer.CurrentLength; var frames = sampleCount / 2; fixed(float *conversionPtr = &_temporaryConversionBuffer[0]) { _temporaryBuffer.CopyTo(_temporaryPreconversionAudioBuffer, sampleCount); fixed(short *shortDataPtr = &_temporaryPreconversionAudioBuffer[0]) { SampleRate.src_short_to_float_array(shortDataPtr, conversionPtr, sampleCount); } if (_resampleNeeded) { fixed(float *resamplePtr = &_temporaryResampleBuffer[0]) { var convert = new SampleRate.SRC_DATA() { data_in = conversionPtr, data_out = resamplePtr, input_frames = frames, output_frames = frames * 2, src_ratio = _audioResampleRatio }; var res = SampleRate.src_process(_resamplerState, ref convert); if (res != 0) { Logger.Error(SampleRate.src_strerror(res)); } else { for (var i = 0; i < convert.output_frames_gen; i++) { _temporaryStretcherBuffer[0][i] = _temporaryResampleBuffer[i * 2]; _temporaryStretcherBuffer[1][i] = _temporaryResampleBuffer[i * 2 + 1]; } _stretcher.Process( _temporaryStretcherBuffer, convert.output_frames_gen, false ); if (_stretcher.GetSamplesRequired() > 0) { return; } var availableFrames = _stretcher.Available(); _stretcher.Retrieve(_temporaryStretcherBuffer, availableFrames); for (var i = 0; i < availableFrames; i++) { _temporaryResampleBuffer[i * 2] = _temporaryStretcherBuffer[0][i]; _temporaryResampleBuffer[i * 2 + 1] = _temporaryStretcherBuffer[1][i]; } _audioBuffer.CopyFrom(_temporaryResampleBuffer, availableFrames * 2); } } } else { Console.WriteLine("else"); // _audioBuffer.CopyFrom(_temporaryConversionBuffer, frames * 2); } } } }