Beispiel #1
0
        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);
                    }
                }
            }
        }