| 1 | /* |
| 2 | Copyright 2018 Google Inc. All Rights Reserved. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS-IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef RESONANCE_AUDIO_DSP_RESAMPLER_H_ |
| 18 | #define RESONANCE_AUDIO_DSP_RESAMPLER_H_ |
| 19 | |
| 20 | #include "base/audio_buffer.h" |
| 21 | |
| 22 | namespace vraudio { |
| 23 | |
| 24 | // Class that provides rational resampling of audio data. |
| 25 | class Resampler { |
| 26 | public: |
| 27 | Resampler(); |
| 28 | |
| 29 | // Resamples an |AudioBuffer| of input data sampled at |source_frequency| to |
| 30 | // |destination_frequency|. |
| 31 | // |
| 32 | // @param input Input data to be resampled. |
| 33 | // @param output Resampled output data. |
| 34 | void Process(const AudioBuffer& input, AudioBuffer* output); |
| 35 | |
| 36 | // Returns the maximum length which the output buffer will be, given the |
| 37 | // current source and destination frequencies and input length. The actual |
| 38 | // output length will either be this or one less. |
| 39 | // |
| 40 | // @param input_length Length of the input. |
| 41 | // @return Maximum length of the output. |
| 42 | size_t GetMaxOutputLength(size_t input_length) const; |
| 43 | |
| 44 | // Returns the next length which the output buffer will be, given the |
| 45 | // current source and destination frequencies and input length. |
| 46 | // |
| 47 | // @param input_length Length of the input. |
| 48 | // @return Next length of the output. |
| 49 | size_t GetNextOutputLength(size_t input_length) const; |
| 50 | |
| 51 | // Sets the source and destination sampling rate as well as the number of |
| 52 | // channels. Note this method only resets the filter state number of channel |
| 53 | // changes. |
| 54 | // |
| 55 | // @param source_frequency Sampling rate of input data. |
| 56 | // @param destination_frequency Desired output sampling rate. |
| 57 | // @param num_channels Number of channels to process. |
| 58 | void SetRateAndNumChannels(int source_frequency, int destination_frequency, |
| 59 | size_t num_channels); |
| 60 | |
| 61 | // Returns whether the sampling rates provided are supported by the resampler. |
| 62 | // |
| 63 | // @param source Source sampling rate. |
| 64 | // @param destination Destination sampling rate. |
| 65 | // @return True if the sampling rate pair are supported. |
| 66 | static bool AreSampleRatesSupported(int source, int destination); |
| 67 | |
| 68 | // Resets the inner state of the |Resampler| allowing its use repeatedly on |
| 69 | // different data streams. |
| 70 | void ResetState(); |
| 71 | |
| 72 | private: |
| 73 | friend class PolyphaseFilterTest; |
| 74 | // Initializes the |state_| buffer. Called when sampling rate is changed or |
| 75 | // the state is reset. |
| 76 | // |
| 77 | // @param size_t old_state_num_frames Number of frames in the |state_| buffer |
| 78 | // previous to the most recent call to |GenerateInterpolatingFilter|. |
| 79 | void InitializeStateBuffer(size_t old_state_num_frames); |
| 80 | |
| 81 | // Generates a windowed sinc to act as the interpolating/anti-aliasing filter. |
| 82 | // |
| 83 | // @param sample_rate The system sampling rate. |
| 84 | void GenerateInterpolatingFilter(int sample_rate); |
| 85 | |
| 86 | // Arranges the anti aliasing filter coefficients in polyphase filter format. |
| 87 | // |
| 88 | // @param filter_length Number of frames in |filter| containing filter |
| 89 | // coefficients. |
| 90 | // @param filter Vector of filter coefficients. |
| 91 | void ArrangeFilterAsPolyphase(size_t filter_length, |
| 92 | const AudioBuffer::Channel& filter); |
| 93 | |
| 94 | // Generates Hann windowed sinc function anti aliasing filters. |
| 95 | // |
| 96 | // @param cutoff_frequency Transition band (-3dB) frequency of the filter. |
| 97 | // @param sample_rate The system sampling rate. |
| 98 | // @param filter_length Number of frames in |buffer| containing filter |
| 99 | // coefficients. |
| 100 | // @param buffer |AudioBuffer::Channel| to contain the filter coefficients. |
| 101 | void GenerateSincFilter(float cutoff_frequency, float sample_rate, |
| 102 | size_t filter_length, AudioBuffer::Channel* buffer); |
| 103 | |
| 104 | // Rate of the interpolator section of the rational sampling rate converter. |
| 105 | size_t up_rate_; |
| 106 | |
| 107 | // Rate of the decimator section of the rational sampling rate convereter. |
| 108 | size_t down_rate_; |
| 109 | |
| 110 | // Time variable for the polyphase filter. |
| 111 | size_t time_modulo_up_rate_; |
| 112 | |
| 113 | // Marks the last processed sample of the input. |
| 114 | size_t last_processed_sample_; |
| 115 | |
| 116 | // Number of channels in the |AudioBuffer|s processed. |
| 117 | size_t num_channels_; |
| 118 | |
| 119 | // Number of filter coefficients in each phase of the polyphase filter. |
| 120 | size_t coeffs_per_phase_; |
| 121 | |
| 122 | // Filter coefficients stored in polyphase form. |
| 123 | AudioBuffer transposed_filter_coeffs_; |
| 124 | |
| 125 | // Filter coefficients in planar form, used for calculating the transposed |
| 126 | // filter. |
| 127 | AudioBuffer temporary_filter_coeffs_; |
| 128 | |
| 129 | // Buffer holding the samples of input required between input buffers. |
| 130 | AudioBuffer state_; |
| 131 | }; |
| 132 | |
| 133 | } // namespace vraudio |
| 134 | |
| 135 | #endif // RESONANCE_AUDIO_DSP_RESAMPLER_H_ |
| 136 | |