Rockbox open source high quality audio player as a Music Player Daemon
mpris rockbox mpd libadwaita audio rust zig deno
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Work in progress hermite resampler.

Based on http://src.gnu-darwin.org/ports/multimedia/helixplayer/work/hxplay-1.0.7/audio/resampler/hermite.c

Change-Id: Id87565a060aa2383701e7c2f3ea023c7555ad9ef

+124 -6
+124 -6
lib/rbcodec/dsp/lin_resample.c
··· 30 30 * Linear interpolation resampling that introduces a one sample delay because 31 31 * of our inability to look into the future at the end of a frame. 32 32 */ 33 + 34 + #define HERMITE 1 33 35 34 - #if 0 /* Set to '1' to enable debug messages */ 36 + #if 1 /* Set to '1' to enable debug messages */ 35 37 #include <debug.h> 36 38 #else 37 39 #undef DEBUGF ··· 46 48 /* Data for each resampler on each DSP */ 47 49 static struct resample_data 48 50 { 49 - uint32_t delta; /* 00h: Phase delta for each step */ 51 + uint32_t delta; /* 00h: Phase delta for each step in s15.16*/ 50 52 uint32_t phase; /* 04h: Current phase [pos16|frac16] */ 51 53 int32_t last_sample[2]; /* 08h: Last samples for interpolation (L+R) */ 52 54 /* 10h */ ··· 54 56 struct dsp_config *dsp; /* The DSP for this resampler */ 55 57 struct dsp_buffer resample_buf; /* Buffer descriptor for resampled data */ 56 58 int32_t *resample_buf_arr[2]; /* Actual output data pointers */ 59 + 60 + /*Hermite Resampler*/ 61 + 62 + int32_t last_samples[6]; 63 + 57 64 } resample_data[DSP_COUNT] IBSS_ATTR; 58 65 59 66 /* Actual worker function. Implemented here or in target assembly code. */ 60 67 int lin_resample_resample(struct resample_data *data, struct dsp_buffer *src, 68 + struct dsp_buffer *dst); 69 + 70 + int hermite_resample_resample(struct resample_data *data, struct dsp_buffer *src, 61 71 struct dsp_buffer *dst); 62 72 63 73 static void lin_resample_flush_data(struct resample_data *data) ··· 94 104 return true; 95 105 } 96 106 107 + 108 + 109 + int hermite_resample_resample(struct resample_data *data, struct dsp_buffer *src, 110 + struct dsp_buffer *dst) 111 + { 112 + int ch = src->format.num_channels - 1; 113 + uint32_t count = MIN(src->remcount, 0x8000); 114 + uint32_t delta = data->delta; 115 + uint32_t phase, pos; 116 + int32_t *d; 117 + int x0, x1, x2, x3, frac, acc0; 118 + //DEBUGF("hermite_resample_resample top\n"); 119 + /* restore state */ 120 + 121 + 122 + //DEBUGF("count: %d delta: %d, phase: %d (%d)\n",count, delta,phase >> 16, phase); 123 + do 124 + { 125 + const int32_t *s = src->p32[ch]; 126 + 127 + d = dst->p32[ch]; 128 + int32_t *dmax = d + dst->bufcount; 129 + 130 + phase = data->phase; 131 + pos = phase >> 16; 132 + pos = MIN(pos, count); 133 + 134 + int32_t last = pos > 0 ? s[pos - 1] : data->last_sample[ch]; 135 + 136 + if (pos < count) 137 + { 138 + while (1) 139 + { 140 + 141 + int i = pos; 142 + if (i < 3) { 143 + x3 = (i < 3 ? data->last_samples[i+0] : s[i-3]) ; 144 + x2 = (i < 2 ? data->last_samples[i+1] : s[i-2]) ; 145 + x1 = (i < 1 ? data->last_samples[i+2] : s[i-1]) ; 146 + } else { 147 + x3 = s[i-3] ; 148 + x2 = s[i-2] ; 149 + x1 = s[i-1] ; 150 + } 151 + x0 = s[i] ; 152 + //frac = f >> 1; 153 + frac=(0x0000FFFF&phase) << 15; 154 + //DEBUGF("pos: %d phase: %d frac: %d\n",pos, phase, frac); 155 + 156 + /* 4-tap Hermite, using Farrow structure */ 157 + acc0 = (3 * (x2 - x1) + x0 - x3) >> 1; 158 + acc0 = FRACMUL(acc0, frac); 159 + acc0 += 2 * x1 + x3 - ((5 * x2 + x0) >> 1); 160 + acc0 = FRACMUL(acc0, frac); 161 + acc0 += (x1 - x3) >> 1; 162 + acc0 = FRACMUL(acc0, frac); 163 + acc0 += x2; 164 + 165 + 166 + *d++ = acc0; 167 + 168 + phase += delta; 169 + pos = phase >> 16; 170 + 171 + if (pos >= count || d >= dmax) 172 + break; 173 + 174 + // if (pos > 0){ 175 + // /* save delay samples for next time (last_samples[0] = oldest, last_samples[2] = newest) */ 176 + // data->last_samples[ch*3+0] = (pos < 3 ? data->last_samples[pos+0] : s[pos-3]); 177 + // data->last_samples[ch*3+1] = (pos < 2 ? data->last_samples[pos+1] : s[pos-2]); 178 + // data->last_samples[ch*3+2] = (pos < 1 ? data->last_samples[pos+2] : s[pos-1]); 179 + // } 180 + } 181 + 182 + //if (pos > 0) 183 + //{ 184 + pos = MIN(pos, count); 185 + data->last_samples[ch*3+0] = (pos < 3 ? data->last_samples[pos+0] : s[pos-3]); 186 + data->last_samples[ch*3+1] = (pos < 2 ? data->last_samples[pos+1] : s[pos-2]); 187 + data->last_samples[ch*3+2] = (pos < 1 ? data->last_samples[pos+2] : s[pos-1]); 188 + //} 189 + } 190 + 191 + } 192 + while (--ch >= 0); 193 + 194 + 195 + /* Wrap phase accumulator back to start of next frame. */ 196 + data->phase = phase - (pos << 16); 197 + 198 + dst->remcount = d - dst->p32[0]; 199 + return pos; 200 + } 201 + 97 202 #if !defined(CPU_COLDFIRE) && !defined(CPU_ARM) 98 203 /* Where the real work is done */ 99 204 int lin_resample_resample(struct resample_data *data, struct dsp_buffer *src, ··· 104 209 uint32_t delta = data->delta; 105 210 uint32_t phase, pos; 106 211 int32_t *d; 107 - 212 + DEBUGF("count: %d delta: %d, phase: %d (%d)\n",count, delta,phase >> 16, phase); 108 213 do 109 214 { 110 215 const int32_t *s = src->p32[ch]; ··· 122 227 { 123 228 while (1) 124 229 { 230 + DEBUGF("phase: %d frac: %d\n", phase, (phase & 0xffff) << 15); 125 231 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last); 126 232 phase += delta; 127 233 pos = phase >> 16; ··· 139 245 last = s[pos - 1]; 140 246 } 141 247 } 142 - 248 + 249 + DEBUGF("pos: %d count: %d\n", pos, count); 143 250 data->last_sample[ch] = last; 144 251 } 145 252 while (--ch >= 0); 146 253 147 254 /* Wrap phase accumulator back to start of next frame. */ 148 255 data->phase = phase - (pos << 16); 149 - 256 + 150 257 dst->remcount = d - dst->p32[0]; 151 - 258 + DEBUGF("remcount: %d, pos %d\n", dst->remcount, pos); 152 259 return pos; 153 260 } 154 261 #endif /* CPU */ ··· 176 283 { 177 284 dst->bufcount = RESAMPLE_BUF_COUNT; 178 285 286 + #if HERMITE 287 + int consumed = hermite_resample_resample(data, src, dst); 288 + #else 179 289 int consumed = lin_resample_resample(data, src, dst); 290 + #endif 291 + 180 292 181 293 /* Advance src by consumed amount */ 182 294 if (consumed > 0) ··· 211 323 if (src->format.frequency != frequency) 212 324 { 213 325 DEBUGF(" DSP_PROC_RESAMPLE- new delta\n"); 326 + DEBUGF("hermite_resample_new_delta in\n"); 327 + #if 0 328 + active = hermite_resample_new_delta(data, src); 329 + #else 214 330 active = lin_resample_new_delta(data, src); 331 + #endif 332 + DEBUGF("hermite_resample_new_delta out\n"); 215 333 dsp_proc_activate(dsp, DSP_PROC_RESAMPLE, active); 216 334 } 217 335