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
|
/*
hrtferX.c:
Copyright (C) 1995, 2001 Eli Breder, David McIntyre, John ffitch
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
/***********************************************************
* FINAL PROJECT *
* *
* course: 216-605A *
* Digital Sound Synthesis & Audio Processing *
* *
* *
* hrtferxk.c *
* ---------- *
* *
* Eli Breder (9111216) and David McIntyre (9005614) *
* *
* Dec. 17, 1995 *
* *
* Code modifications John ffitch January 2001 *
* *
***********************************************************/
/***************************************************************
* This version of hrtfer loads the file HRTFcompact into memory.
* Offsets into the file are calculated in the a-rate code to
* get the requested HRTF measurements. We've implemented
* a linear crossfade to deal with the clicks which occur
* when the input audio is convolved with new HRTFs. Although this
* the clicking, it does not eliminate them.
* A better solution would be to implement interpolation between
* the old and new HRTFs (probably a project in itself).
***************************************************************/
// #include "csdl.h"
#include "csoundCore.h"
#include "interlocks.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "hrtferx.h"
/* This array transferred here so as to be declared once. Belongs to
the structure of the HRTF data really in 3Dug.h */
static const int32_t elevation_data[N_ELEV] = {56, 60, 72, 72, 72, 72, 72,
60, 56, 45, 36, 24, 12, 1 };
#define ROUND(x) ((int32_t)floor((x)+FL(0.5)))
#define GET_NFAZ(el_index) ((elevation_data[el_index] / 2) + 1)
static int32_t hrtferxkSet(CSOUND *csound, HRTFER *p)
{
// int32_t i; /* standard loop counter */
char filename[MAXNAME];
int32_t bytrev_test;
MEMFIL *mfp;
/* first check if orchestra's sampling rate is compatible with HRTF
measurement's */
if (UNLIKELY(CS_ESR != SAMP_RATE)) {
return csound->InitError(csound,
Str("Orchestra sampling rate is not compatible with HRTF.\n"
"Should be %d...exiting."), SAMP_RATE);
return NOTOK; /* not reached */
}
if (!strcmp("HRTFcompact", p->ifilno->data)) {
strNcpy(filename, p->ifilno->data, MAXNAME);
//filename[MAXNAME-1] = '\0';
}
else {
csound->Message(csound, Str("\nLast argument must be the string "
"'HRTFcompact' ...correcting.\n"));
strNcpy(filename, "HRTFcompact", MAXNAME); /* for safety */
}
if ((mfp = p->mfp) == NULL)
mfp = csound->ldmemfile2withCB(csound, filename, CSFTYPE_HRTF, NULL);
p->mfp = mfp;
p->fpbegin = (int16*) mfp->beginp;
bytrev_test = 0x1234;
if (*((unsigned char*) &bytrev_test) == (unsigned char) 0x34) {
/* Byte reverse on data set if necessary */
int16 *x = p->fpbegin;
int32 len = (mfp->length)/sizeof(int16);
while (len != 0) {
int16 v = *x;
v = ((v & 0xFF) << 8) + ((v >> 8) & 0xFF); /* Swap bytes */
*x = v;
x++; len--;
}
}
/* initialize counters and indices */
p->outcount = 0;
p->incount = 0;
p->outfront = p->outend = 0; /* working indices for circ output buffer */
/* initialize oldhrtf_data with zeros */
/* initialize input buffer */
/* initialize left result buffer */
/* initialize right result buffer */
/* initialize left output buffer */
/* initialize right output buffer */
/* for (i=0; i<BUF_LEN; i++) { */
/* p->x[i] = FL(0.0); */
/* p->yl[i] = FL(0.0); */
/* p->yr[i] = FL(0.0); */
/* p->outl[i] = FL(0.0); */
/* p->outr[i] = FL(0.0); */
/* } */
memset(p->x, 0, BUF_LEN*sizeof(MYFLT));
memset(p->yl, 0, BUF_LEN*sizeof(MYFLT));
memset(p->yr, 0, BUF_LEN*sizeof(MYFLT));
memset(p->outl, 0, BUF_LEN*sizeof(MYFLT));
memset(p->outr, 0, BUF_LEN*sizeof(MYFLT));
/* initialize left overlap buffer */
/* initialize right overlap buffer */
/* for (i=0; i<FILT_LENm1; i++) { */
/* p->bl[i] = FL(0.0); */
/* p->br[i] = FL(0.0); */
/* } */
memset(p->bl, 0, FILT_LENm1*sizeof(MYFLT));
memset(p->br, 0, FILT_LENm1*sizeof(MYFLT));
return OK;
}
/********************** a-rate code ***********************************/
static int32_t hrtferxk(CSOUND *csound, HRTFER *p)
{
MYFLT *aLeft, *aRight; /* audio output streams */
MYFLT *aIn, *kAz, *kElev; /* audio and control input streams */
int32_t azim, elev, el_index, az_index;
int32_t nsmpsi, nsmpso; /* number of samples in/out */
/* input, out-left, out-right */
MYFLT *x, *yl, *yr; /* Local copies of address */
/* overlap left, overlap right */
MYFLT *bl, *br;
/* copy of current input convolved with old HRTFs */
MYFLT *outl, *outr; /* output left/right */
int32_t outfront, outend; /* circular output indices */
int32_t incount, outcount; /* number of samples in/out */
uint32_t toread; /* number of samples to read */
int32_t i; /* standard loop counter */
HRTF_DATUM hrtf_data; /* local hrtf instances */
int32_t flip; /* flag - true if we need to flip the channels */
int16 *fpindex; /* pointer into HRTF file */
int16 numskip; /* number of shorts to skip in HRTF file */
/* short arrays into which HRTFs are stored locally */
int16 sl[FILT_LEN], sr[FILT_LEN];
/* float versions of above to be sent to FFT routines */
MYFLT xl[BUF_LEN], xr[BUF_LEN];
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t ii;
if (UNLIKELY(p->mfp==NULL)) goto err1; /* RWD fix */
/* update local variables */
kElev = p->kElev;
kAz = p->kAz;
elev = (int32_t) *kElev;
azim = (int32_t) *kAz;
//oldel_index = p->oldel_index;
fpindex = (int16 *) p->fpbegin;
flip = 0;
/* Convert elevation in degrees to elevation array index. */
el_index = ROUND((double)(elev - MIN_ELEV) / ELEV_INC);
if (el_index < 0)
el_index = 0;
else if (el_index >= N_ELEV)
el_index = N_ELEV-1;
/* Convert azimuth in degrees to azimuth array index. */
azim = (int32_t)fmod((double)azim, 360.0);
if (azim < 0)
azim += 360;
if (azim > 180) {
azim = 360 - azim;
flip = 1; /* set to true */
}
/* else */
/* flip = 0; /\* it is still false *\/ */
/* azim should be 0<=azim<=180 so calculate az_index and clip
to legal range
note - accesses global array elevation_data */
az_index = ROUND((double)azim / (360.0 / elevation_data[el_index]));
if (az_index < 0)
az_index = 0;
else if (az_index >= elevation_data[el_index])
az_index = elevation_data[el_index] - 1;
/* calculate offset into HRTFcompact file */
/* first get to the first value of the requested elevation */
if (el_index == 0)
fpindex = (int16 *) p->fpbegin;
else
for (i=0; i<=el_index; i++) {
numskip = (int16)(GET_NFAZ(i) * BUF_LEN);
fpindex += numskip;
}
/* fpindex should now point to first azimuth at requested el_index */
/* now get to first value of requested azimuth */
if (az_index == 0) {
/* csound->Message(csound, "in az_index == 0\n"); */
//numskip = 0;
}
else {
for (i=0, numskip=0; i<az_index; i++)
numskip += BUF_LEN;
fpindex += (int16) (numskip);
}
/* read in (int16) data from stereo interleave HRTF file.
Split into left and right channel data. */
for (i=0; i<FILT_LEN; i++) {
sl[i] = *fpindex++;
sr[i] = *fpindex++;
}
{
MYFLT scaleFac;
scaleFac = csound->GetInverseRealFFTScale(csound, BUF_LEN) / FL(256.0);
scaleFac /= FL(32768.0);
/* copy int16 buffers into float buffers */
for (i=0; i<FILT_LEN; i++) {
xl[i] = (MYFLT) sl[i] * scaleFac;
xr[i] = (MYFLT) sr[i] * scaleFac;
}
}
for (i=FILT_LEN; i<BUF_LEN; i++) {
xl[i] = FL(0.0); /* pad buffers with zeros to BUF_LEN */
xr[i] = FL(0.0);
}
/**************
FFT xl and xr here
***************/
csound->RealFFT(csound, xl, BUF_LEN);
csound->RealFFT(csound, xr, BUF_LEN);
/* If azimuth called for right side of head, use left side
measurements and flip output channels.
This is due to the fact that the HRTFs we are using only
include left side placements. */
if (flip) {
for (i=0; i<BUF_LEN; i++) {
hrtf_data.left[i] = xr[i];
hrtf_data.right[i] = xl[i];
}
}
else {
for (i=0; i<BUF_LEN; i++) {
hrtf_data.left[i] = xl[i];
hrtf_data.right[i] = xr[i];
}
}
/* update local counters and indices */
incount = p->incount;
outcount = p->outcount;
outfront = p->outfront;
outend = p->outend;
/* Get local pointers to structures */
outl = &p->outl[0];
outr = &p->outr[0];
bl = &p->bl[0];
br = &p->br[0];
x = &p->x[0];
yl = &p->yl[0];
yr = &p->yr[0];
aIn = p->aIn;
aLeft = p->aLeft;
aRight = p->aRight;
nsmpsi = nsmpso = CS_KSMPS;
/* main loop for a-rate code. Audio read in, processed,
and output in this loop. Loop exits when control period
(ksmps) is finished. */
while (nsmpsi > 0) {
/* determine how much audio may be read in */
if ((incount + nsmpsi) <= FILT_LEN)
toread = nsmpsi;
else
toread = FILT_LEN - incount;
/* reading in audio into x */
if (incount == 0) {
for (ii = 0; ii < toread; ii++)
x[ii] = *aIn++;
}
else {
for (ii = incount; ii<(incount + toread); ii++)
x[ii] = *aIn++;
}
/* update counters for amount of audio read */
nsmpsi -= toread;
incount += toread;
/* loop for audio processing */
if (incount == FILT_LEN) {
/* enough audio for convolution - so do it! */
incount = 0;
/* pad x to BUF_LEN with zeros for Moore FFT */
for (i = FILT_LEN; i < BUF_LEN; i++)
x[i] = FL(0.0);
csound->RealFFT(csound, x, BUF_LEN);
/* complex multiplication, y = hrtf_data * x */
csound->RealFFTMult(csound, yl, hrtf_data.left, x, BUF_LEN, FL(1.0));
csound->RealFFTMult(csound, yr, hrtf_data.right, x, BUF_LEN, FL(1.0));
/* convolution is the inverse FFT of above result (yl,yr) */
csound->InverseRealFFT(csound, yl, BUF_LEN);
csound->InverseRealFFT(csound, yr, BUF_LEN);
/* overlap-add the results */
for (i = 0; i < FILT_LENm1; i++) {
yl[i] += bl[i];
yr[i] += br[i];
bl[i] = yl[FILT_LEN+i];
br[i] = yr[FILT_LEN+i];
}
/* put convolution ouput into circular output buffer */
if (outend <= FILT_LEN) {
/* output will fit in buffer boundaries, therefore
no circular reference required */
for (i = outend; i < (outend + FILT_LEN); i++) {
outl[i] = yl[i-outend];
outr[i] = yr[i-outend];
}
outcount += FILT_LEN;
outend += FILT_LEN;
}
else {
/* circular reference required due to buffer boundaries */
for (i = outend; i < BUF_LEN; i++) {
outl[i] = yl[i-outend];
outr[i] = yr[i-outend];
}
for (i = 0; i < (-FILT_LEN + outend); i++) {
outl[i] = yl[(BUF_LEN-outend) + i];
outr[i] = yr[(BUF_LEN-outend) + i];
}
outcount += FILT_LEN;
outend -= FILT_LEN;
}
}
/* output audio to audio stream.
Can only output one control period (ksmps) worth of samples */
if (nsmpso < outcount) {
if ((outfront+nsmpso) < BUF_LEN) {
for (i = 0; i < nsmpso; i++) {
*aLeft++ = outl[outfront + i];
*aRight++ = outr[outfront + i];
}
outcount -= nsmpso;
outfront += nsmpso;
nsmpso = 0;
}
else {
uint32 j = 0;
/* account for circular reference */
for (i = outfront; i < BUF_LEN; i++, j++) {
*aLeft++ = (j<offset || j>early) ? FL(0.0) : outl[i];
*aRight++ = (j<offset || j>early) ? FL(0.0) : outr[i];
}
outcount -= nsmpso;
nsmpso -= (BUF_LEN - outfront);
for (i = 0; i < nsmpso; i++, j++) {
*aLeft++ = (j<offset || j>early) ? FL(0.0) : outl[i];
*aRight++ = (j<offset || j>early) ? FL(0.0) : outr[i];
}
outfront = nsmpso;
nsmpso = 0;
}
}
else {
uint32 j = 0;
if ((outfront+nsmpso) < BUF_LEN) {
for (i = 0; i < outcount; i++, j++) {
*aLeft++ = (j<offset || j>early) ? FL(0.0) : outl[outfront + i];
*aRight++ = (j<offset || j>early) ? FL(0.0) : outr[outfront + i];
}
nsmpso -= outcount;
outfront += outcount;
outcount = 0;
}
else {
/* account for circular reference */
for (i = outfront; i < BUF_LEN; i++, j++) {
*aLeft++ = (j<offset || j>early) ? FL(0.0) : outl[i];
*aRight++ = (j<offset || j>early) ? FL(0.0) : outr[i];
}
nsmpso -= outcount;
outcount -= (BUF_LEN - outfront);
for (i = 0; i < outcount; i++, j++) {
*aLeft++ = (j<offset || j>early) ? FL(0.0) : outl[i];
*aRight++ = (j<offset || j>early) ? FL(0.0) : outr[i];
}
outfront = outcount;
outcount = 0;
}
} /* end of audio processing loop - "if" */
} /* end of control period loop - "while" */
/* update state in p */
p->outcount = outcount;
p->incount = incount;
p->outfront = outfront;
p->outend = outend;
p->oldel_index = el_index;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("hrtfer: not initialised"));
}
static OENTRY hrtferX_localops[] =
{
{ "hrtfer", sizeof(HRTFER), _QQ, 3, "aa", "akkS",
(SUBR)hrtferxkSet, (SUBR)hrtferxk},
};
LINKAGE_BUILTIN(hrtferX_localops)
|