1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
|
/*
* (c) Copyright 2002, 2004, 2006 -- Anders Torger
*
* This program is open source. For license terms, see the LICENSE file.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include "defs.h"
#include "pinfo.h"
#include "delay.h"
#include "bfrun.h"
#include "emalloc.h"
#include "firwindow.h"
#include "convolver.h"
#include "timestamp.h"
static int realsize;
static int subdelay_filter_length;
static td_conv_t **subdelay_filter = NULL;
static int subdelay_step_count;
static int subdelay_filterblock_size;
static int subdelay_fragment_size;
struct _delaybuffer_t_ {
int fragsize; /* fragment size */
int maxdelay; /* maximum allowable delay, or negative if delay cannot be
changed in runtime */
int curdelay; /* current delay */
int curbuf; /* index of current full-sized buffer */
int n_fbufs; /* number of full-sized buffers currently used */
int n_fbufs_cap; /* number of full-sized buffers allocated */
void **fbufs; /* full-sized buffers */
int n_rest; /* samples in rest buffer */
void *rbuf; /* rest buffer */
void *shortbuf[2]; /* pointers to buffers which fit the whole delay, only
used when delay is <= fragment size */
};
static double
sinc(double x)
{
if (x == 0.0) {
return 1.0;
}
return sin(x) / x;
}
static void *
sample_sinc(int half_length,
double offset,
double kaiser_beta)
{
int n, filter_length;
void *filter;
double x;
filter_length = 2 * half_length + 1;
filter = emallocaligned(filter_length * realsize);
for (n = 0; n < filter_length; n++) {
x = M_PI * ((double)(n - half_length) - offset);
if (realsize == 4) {
((float *)filter)[n] = (float)sinc(x);
} else {
((double *)filter)[n] = sinc(x);
}
}
firwindow_kaiser(filter, filter_length, offset, 9, realsize);
return filter;
}
static void
copy_to_delaybuf(void *dbuf,
void *buf,
int sample_size,
int sample_spacing,
int n_samples)
{
int n, i;
if (sample_spacing == 1) {
memcpy(dbuf, buf, n_samples * sample_size);
return;
}
switch (sample_size) {
case 1:
for (n = i = 0; n < n_samples; n++, i += sample_spacing) {
((uint8_t *)dbuf)[n] = ((uint8_t *)buf)[i];
}
break;
case 2:
for (n = i = 0; n < n_samples; n++, i += sample_spacing) {
((uint16_t *)dbuf)[n] = ((uint16_t *)buf)[i];
}
break;
case 3:
n_samples *= 3;
sample_spacing = sample_spacing * 3 - 3;
for (n = i = 0; n < n_samples; i += sample_spacing) {
((uint8_t *)dbuf)[n++] = ((uint8_t *)buf)[i++];
((uint8_t *)dbuf)[n++] = ((uint8_t *)buf)[i++];
((uint8_t *)dbuf)[n++] = ((uint8_t *)buf)[i++];
}
break;
case 4:
for (n = i = 0; n < n_samples; n++, i += sample_spacing) {
((uint32_t *)dbuf)[n] = ((uint32_t *)buf)[i];
}
break;
case 8:
for (n = i = 0; n < n_samples; n++, i += sample_spacing) {
((uint64_t *)dbuf)[n] = ((uint64_t *)buf)[i];
}
break;
default:
fprintf(stderr, "Sample byte size %d not supported.\n", sample_size);
bf_exit(BF_EXIT_OTHER);
break;
}
}
static void
copy_from_delaybuf(void *buf,
void *dbuf,
int sample_size,
int sample_spacing,
int n_samples)
{
int n, i;
if (sample_spacing == 1) {
memcpy(buf, dbuf, n_samples * sample_size);
return;
}
switch (sample_size) {
case 1:
for (n = i = 0; n < n_samples; n++, i += sample_spacing) {
((uint8_t *)buf)[i] = ((uint8_t *)dbuf)[n];
}
break;
case 2:
for (n = i = 0; n < n_samples; n++, i += sample_spacing) {
((uint16_t *)buf)[i] = ((uint16_t *)dbuf)[n];
}
break;
case 3:
n_samples *= 3;
sample_spacing = sample_spacing * 3 - 3;
for (n = i = 0; n < n_samples; i += sample_spacing) {
((uint8_t *)buf)[i++] = ((uint8_t *)dbuf)[n++];
((uint8_t *)buf)[i++] = ((uint8_t *)dbuf)[n++];
((uint8_t *)buf)[i++] = ((uint8_t *)dbuf)[n++];
}
break;
case 4:
for (n = i = 0; n < n_samples; n++, i += sample_spacing) {
((uint32_t *)buf)[i] = ((uint32_t *)dbuf)[n];
}
break;
case 8:
for (n = i = 0; n < n_samples; n++, i += sample_spacing) {
((uint64_t *)buf)[i] = ((uint64_t *)dbuf)[n];
}
break;
default:
fprintf(stderr, "Sample byte size %d not supported.\n", sample_size);
bf_exit(BF_EXIT_OTHER);
break;
}
}
static void
shift_samples(void *buf,
int sample_size,
int sample_spacing,
int n_samples,
int distance)
{
int n, i;
n = (n_samples - 1) * sample_spacing;
i = (n_samples + distance - 1) * sample_spacing;
switch (sample_size) {
case 1:
for (; n >= 0; n -= sample_spacing, i -= sample_spacing) {
((uint8_t *)buf)[i] = ((uint8_t *)buf)[n];
}
break;
case 2:
for (; n >= 0; n -= sample_spacing, i -= sample_spacing) {
((uint16_t *)buf)[i] = ((uint16_t *)buf)[n];
}
break;
case 3:
n *= 3;
i *= 3;
sample_spacing *= 3;
for (; n >= 0; n -= sample_spacing, i -= sample_spacing) {
((uint8_t *)buf)[i++] = ((uint8_t *)buf)[n++];
((uint8_t *)buf)[i++] = ((uint8_t *)buf)[n++];
((uint8_t *)buf)[i++] = ((uint8_t *)buf)[n++];
}
break;
case 4:
for (; n >= 0; n -= sample_spacing, i -= sample_spacing) {
((uint32_t *)buf)[i] = ((uint32_t *)buf)[n];
}
break;
case 8:
for (; n >= 0; n -= sample_spacing, i -= sample_spacing) {
((uint64_t *)buf)[i] = ((uint64_t *)buf)[n];
}
break;
default:
fprintf(stderr, "Sample byte size %d not supported.\n", sample_size);
bf_exit(BF_EXIT_OTHER);
break;
}
}
static void
update_delay_buffer(delaybuffer_t *db,
int sample_size,
int sample_spacing,
uint8_t *buf)
{
uint8_t *lastbuf;
lastbuf = (db->curbuf == db->n_fbufs - 1) ? db->fbufs[0] :
db->fbufs[db->curbuf + 1];
/* 1. copy buffer to current full-sized delay buffer */
copy_to_delaybuf(db->fbufs[db->curbuf], buf, sample_size,
sample_spacing, db->fragsize);
if (db->n_rest != 0) {
/* 2. copy from delay rest buffer to the start of buffer */
copy_from_delaybuf(buf, db->rbuf, sample_size, sample_spacing,
db->n_rest);
/* 3. copy from end of last full-sized delay buffer to rest buffer */
memcpy(db->rbuf, lastbuf + (db->fragsize - db->n_rest) * sample_size,
db->n_rest * sample_size);
}
/* 4. copy from start of last full-sized buffer to end of buffer */
copy_from_delaybuf(buf + db->n_rest * sample_size * sample_spacing,
lastbuf, sample_size, sample_spacing,
db->fragsize - db->n_rest);
if (++db->curbuf == db->n_fbufs) {
db->curbuf = 0;
}
}
static void
update_delay_short_buffer(delaybuffer_t *db,
int sample_size,
int sample_spacing,
uint8_t *buf)
{
copy_to_delaybuf(db->shortbuf[db->curbuf],
buf + (db->fragsize - db->n_rest) * sample_size *
sample_spacing, sample_size, sample_spacing,
db->n_rest);
shift_samples(buf, sample_size, sample_spacing,
db->fragsize - db->n_rest, db->n_rest);
db->curbuf = !db->curbuf;
copy_from_delaybuf(buf, db->shortbuf[db->curbuf], sample_size,
sample_spacing, db->n_rest);
}
static void
change_delay(delaybuffer_t *db,
int sample_size,
int newdelay)
{
int size, i;
if (newdelay == db->curdelay || newdelay > db->maxdelay) {
return;
}
if (newdelay <= db->fragsize) {
db->n_rest = newdelay;
size = newdelay * sample_size;
if (db->curdelay > db->fragsize || db->curdelay < newdelay) {
memset(db->shortbuf[0], 0, size);
memset(db->shortbuf[1], 0, size);
}
db->n_fbufs = 0;
db->curbuf = 0;
db->curdelay = newdelay;
return;
}
db->n_rest = newdelay % db->fragsize;
db->n_fbufs = newdelay / db->fragsize + 1;
size = db->fragsize * sample_size;
if (db->curdelay < newdelay) {
for (i = 0; i < db->n_fbufs; i++) {
memset(db->fbufs[i], 0, size);
}
if (db->n_rest != 0) {
memset(db->rbuf, 0, db->n_rest * sample_size);
}
}
db->curbuf = 0;
db->curdelay = newdelay;
}
void
delay_update(delaybuffer_t *db,
void *buf,
int sample_size,
int sample_spacing,
int delay,
void *optional_target_buf)
{
change_delay(db, sample_size, delay);
if (optional_target_buf != NULL) {
copy_to_delaybuf(optional_target_buf, buf, sample_size, sample_spacing,
db->fragsize);
buf = optional_target_buf;
sample_spacing = 1;
}
if (db->n_fbufs > 0) {
update_delay_buffer(db, sample_size, sample_spacing, buf);
} else if (db->n_rest > 0) {
update_delay_short_buffer(db, sample_size, sample_spacing, buf);
}
}
delaybuffer_t *
delay_allocate_buffer(int fragment_size,
int initdelay,
int maxdelay,
int sample_size)
{
delaybuffer_t *db;
int n, delay;
int size;
/* if maxdelay is negative, no delay changing will be allowed, thus
memory need only to be allocated for the current delay */
db = emalloc(sizeof(delaybuffer_t));
memset(db, 0, sizeof(delaybuffer_t));
db->fragsize = fragment_size;
delay = (maxdelay <= 0) ? initdelay : maxdelay;
if (maxdelay >= 0 && delay > maxdelay) {
delay = initdelay = maxdelay;
}
db->curdelay = initdelay;
db->maxdelay = maxdelay;
if (delay == 0) {
return db;
}
if (delay <= fragment_size) {
/* optimise for short delay */
db->n_rest = initdelay; /* current value */
size = delay * sample_size;
db->shortbuf[0] = emallocaligned(size);
db->shortbuf[1] = emallocaligned(size);
memset(db->shortbuf[0], 0, size);
memset(db->shortbuf[1], 0, size);
return db;
}
if (maxdelay > 0) {
/* allocate full-length short buffers to keep this option if the
delay is reduced in run-time */
size = fragment_size * sample_size;
db->shortbuf[0] = emallocaligned(size);
db->shortbuf[1] = emallocaligned(size);
memset(db->shortbuf[0], 0, size);
memset(db->shortbuf[1], 0, size);
}
db->n_rest = initdelay % fragment_size;
db->n_fbufs = initdelay / fragment_size + 1;
if (db->n_fbufs == 1) {
db->n_fbufs = 0;
}
db->n_fbufs_cap = delay / fragment_size + 1;
db->fbufs = emalloc(db->n_fbufs_cap * sizeof(void *));
size = fragment_size * sample_size;
for (n = 0; n < db->n_fbufs_cap; n++) {
db->fbufs[n] = emallocaligned(size);
memset(db->fbufs[n], 0, size);
}
if (maxdelay > 0) {
db->rbuf = emallocaligned(size);
memset(db->rbuf, 0, size);
} else if (db->n_rest != 0) {
size = db->n_rest * sample_size;
db->rbuf = emallocaligned(size);
memset(db->rbuf, 0, size);
}
return db;
}
int
delay_subsample_filterblocksize(void)
{
return subdelay_filterblock_size;
}
void
delay_subsample_update(void *buf,
void *rest,
int subdelay)
{
void *cbuf_low, *cbuf_high, *cbuffer;
int i, blocksize;
uint64_t t1, t2;
if (subdelay <= -subdelay_step_count || subdelay >= subdelay_step_count) {
return;
}
timestamp(&t1);
blocksize = subdelay_filterblock_size * realsize;
cbuffer = alloca(blocksize << 1);
cbuf_low = cbuffer;
cbuf_high = &((uint8_t *)cbuf_low)[blocksize];
for (i = 0; i < subdelay_fragment_size * realsize; i += blocksize) {
memcpy(cbuf_low, rest, blocksize);
memcpy(cbuf_high, &((uint8_t *)buf)[i], blocksize);
memcpy(rest, cbuf_high, blocksize);
convolver_td_convolve(subdelay_filter[subdelay], cbuffer);
memcpy(&((uint8_t *)buf)[i], cbuf_low, blocksize);
}
timestamp(&t2);
t2 -= t1;
/*fprintf(stderr, "%" PRIu64 "\n", t2 / (uint64_t)bfconf->cpu_mhz);*/
}
bool_t
delay_subsample_init(int step_count,
int half_filter_length,
double kaiser_beta,
int fragment_size,
int _realsize)
{
void *filter;
int n;
realsize = _realsize;
subdelay_filter_length = 2 * half_filter_length + 1;
subdelay_filterblock_size =
convolver_td_block_length(subdelay_filter_length);
if (step_count < 2) {
fprintf(stderr, "Invalid step_count %d.\n", step_count);
return false;
}
if (half_filter_length < 1) {
fprintf(stderr, "Invalid half filter length %d.\n", half_filter_length);
return false;
}
if (fragment_size % subdelay_filterblock_size != 0) {
fprintf(stderr, "Incompatible fragment/filter sizes (%d/%d).\n",
fragment_size, subdelay_filter_length);
return false;
}
if (realsize != 4 && realsize != 8) {
fprintf(stderr, "Invalid real size %d.\n", realsize);
return false;
}
subdelay_fragment_size = fragment_size;
subdelay_step_count = step_count;
subdelay_filter = emalloc((2 * step_count + 1) * sizeof(td_conv_t *));
subdelay_filter = &subdelay_filter[step_count];
filter = emalloc(subdelay_filter_length * realsize);
memset(filter, 0, subdelay_filter_length * realsize);
if (realsize == 4) {
((float *)filter)[subdelay_filter_length >> 1] = 1.0;
} else {
((double *)filter)[subdelay_filter_length >> 1] = 1.0;
}
subdelay_filter[0] = convolver_td_new(filter, subdelay_filter_length);
efree(filter);
for (n = 1; n < step_count; n++) {
filter = sample_sinc(subdelay_filter_length >> 1,
(double)n / step_count,
kaiser_beta);
subdelay_filter[n] = convolver_td_new(filter, subdelay_filter_length);
efree(filter);
}
for (n = -1; n > -step_count; n--) {
filter = sample_sinc(subdelay_filter_length >> 1,
(double)n / step_count,
kaiser_beta);
subdelay_filter[n] = convolver_td_new(filter, subdelay_filter_length);
efree(filter);
}
pinfo("Created %d subsample delay filters with %d taps "
"and kaiser beta %g.\n",
2 * step_count - 1, subdelay_filter_length, kaiser_beta);
return true;
}
|