|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "AudioTools/CoreAudio/AudioBasic/KalmanFilter.h" |
| 4 | +#include "AudioTools/CoreAudio/AudioBasic/PIDController.h" |
| 5 | +#include "AudioTools/CoreAudio/AudioStreams.h" |
| 6 | +#include "AudioTools/CoreAudio/ResampleStream.h" |
| 7 | + |
| 8 | +namespace audio_tools { |
| 9 | + |
| 10 | +/** |
| 11 | + * @brief An Audio Stream backed by a buffer (queue) which tries to correct |
| 12 | + * jitter and automatically adjusts for the slightly different clock rates |
| 13 | + * between an audio source and audio target. Use separate tasks to write and |
| 14 | + * read the data. Also make sure that you protect the access with a mutex or |
| 15 | + * provide a thread-safe buffer! |
| 16 | + * |
| 17 | + * The resamping step size is calculated with the help of a PID controller. |
| 18 | + * The fill level is smoothed using a Kalman filter. |
| 19 | + * |
| 20 | + * @ingroup buffers |
| 21 | + * @author Phil Schatzmann |
| 22 | + */ |
| 23 | +class AdaptiveResamplingStream : public AudioStream { |
| 24 | + public: |
| 25 | + /** |
| 26 | + * @brief Construct a new AdaptiveResamplingStream object |
| 27 | + * |
| 28 | + * @param buffer Reference to the buffer used for audio data |
| 29 | + * @param stepRangePercent Allowed resampling range in percent (default: 0.05) |
| 30 | + */ |
| 31 | + AdaptiveResamplingStream(BaseBuffer<uint8_t>& buffer, |
| 32 | + float stepRangePercent = 0.05) { |
| 33 | + p_buffer = &buffer; |
| 34 | + setStepRangePercent(stepRangePercent); |
| 35 | + addNotifyAudioChange(resample_stream); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @brief Initialize the stream and internal components. |
| 40 | + * |
| 41 | + * @return true if initialization was successful, false otherwise |
| 42 | + */ |
| 43 | + bool begin() { |
| 44 | + if (p_buffer == nullptr) return false; |
| 45 | + queue_stream.setBuffer(*p_buffer); |
| 46 | + queue_stream.begin(); |
| 47 | + resample_stream.setAudioInfo(audioInfo()); |
| 48 | + resample_stream.setStream(queue_stream); |
| 49 | + resample_stream.begin(audioInfo()); |
| 50 | + float from_step = 1.0 - resample_range; |
| 51 | + float to_step = 1.0 + resample_range; |
| 52 | + return pid.begin(1.0, from_step, to_step, p, i, d); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @brief Write audio data to the buffer. |
| 57 | + * |
| 58 | + * @param data Pointer to the data to write |
| 59 | + * @param len Number of bytes to write |
| 60 | + * @return size_t Number of bytes actually written |
| 61 | + */ |
| 62 | + size_t write(const uint8_t* data, size_t len) override { |
| 63 | + if (p_buffer == 0) return 0; |
| 64 | + size_t result = p_buffer->writeArray(data, len); |
| 65 | + recalculate(); |
| 66 | + return result; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @brief End the stream and release resources. |
| 71 | + */ |
| 72 | + void end() { |
| 73 | + queue_stream.end(); |
| 74 | + resample_stream.end(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @brief Read resampled audio data from the buffer. |
| 79 | + * |
| 80 | + * @param data Pointer to the buffer to fill |
| 81 | + * @param len Number of bytes to read |
| 82 | + * @return size_t Number of bytes actually read |
| 83 | + */ |
| 84 | + size_t readBytes(uint8_t* data, size_t len) override { |
| 85 | + if (p_buffer->available() == 0) return 0; |
| 86 | + |
| 87 | + return resample_stream.readBytes(data, len); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @brief Recalculate the resampling step size based on buffer fill level. |
| 92 | + * |
| 93 | + * @return float The new step size |
| 94 | + */ |
| 95 | + float recalculate() { |
| 96 | + if (p_buffer == nullptr) return step_size; |
| 97 | + |
| 98 | + // calculate new resampling step size |
| 99 | + level_percent = p_buffer->levelPercent(); |
| 100 | + kalman_filter.addMeasurement(level_percent); |
| 101 | + step_size = pid.calculate(50.0, kalman_filter.calculate()); |
| 102 | + |
| 103 | + // log step size every 10th read |
| 104 | + if (read_count++ % 10 == 0) { |
| 105 | + LOGI("step_size: %f", step_size); |
| 106 | + } |
| 107 | + |
| 108 | + // return resampled result |
| 109 | + resample_stream.setStepSize(step_size); |
| 110 | + return step_size; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @brief Set the allowed resampling range as a percent. |
| 115 | + * |
| 116 | + * @param rangePercent Allowed range in percent (e.g., 0.05 for ±0.05%) |
| 117 | + */ |
| 118 | + void setStepRangePercent(float rangePercent) { |
| 119 | + resample_range = rangePercent / 100.0; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @brief Get the current actual buffer fill level in percent. |
| 124 | + * |
| 125 | + * @return float Current fill level (0-100) |
| 126 | + */ |
| 127 | + float levelPercentActual() { |
| 128 | + if (p_buffer == nullptr) return 0.0f; |
| 129 | + return p_buffer->levelPercent(); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @brief Get the fill level at the last calculation in percent. |
| 134 | + * |
| 135 | + * @return float Last calculated fill level (0-100) |
| 136 | + */ |
| 137 | + float levelPercent() { return level_percent; } |
| 138 | + |
| 139 | + /** |
| 140 | + * @brief Set the Kalman filter parameters. |
| 141 | + * |
| 142 | + * @param process_noise Process noise covariance (Q) |
| 143 | + * @param measurement_noise Measurement noise covariance (R) |
| 144 | + */ |
| 145 | + void setKalmanParameters(float process_noise, float measurement_noise) { |
| 146 | + kalman_filter.begin(process_noise, measurement_noise); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @brief Set the PID controller parameters. |
| 151 | + * |
| 152 | + * @param p_value Proportional gain |
| 153 | + * @param i_value Integral gain |
| 154 | + * @param d_value Derivative gain |
| 155 | + */ |
| 156 | + void setPIDParameters(float p_value, float i_value, float d_value) { |
| 157 | + p = p_value; |
| 158 | + i = i_value; |
| 159 | + d = d_value; |
| 160 | + } |
| 161 | + |
| 162 | +protected: |
| 163 | + PIDController pid; // PID controller for adaptive resampling step size (p=0.005, i=0.00005, d=0.0001) |
| 164 | + QueueStream<uint8_t> queue_stream; // Internal queue stream for buffering audio data |
| 165 | + BaseBuffer<uint8_t>* p_buffer = nullptr; // Pointer to the user-provided buffer |
| 166 | + ResampleStream resample_stream; // Resample stream for adjusting playback rate |
| 167 | + KalmanFilter kalman_filter{0.01f, 0.1f}; // Kalman filter for smoothing buffer fill level |
| 168 | + float step_size = 1.0; // Current resampling step size |
| 169 | + float resample_range = 0; // Allowed resampling range (fraction) |
| 170 | + float p = 0.005; // PID proportional gain |
| 171 | + float i = 0.00005; // PID integral gain |
| 172 | + float d = 0.0001; // PID derivative gain |
| 173 | + float level_percent = 0.0; // Last calculated buffer fill level (percent) |
| 174 | + uint32_t read_count = 0; // Read operation counter |
| 175 | +}; |
| 176 | + |
| 177 | +} // namespace audio_tools |
0 commit comments