24 #define AMBI_INVSQRT2 (1/1.414213562)
25 #define INV_SQRT2 (1/1.414213562)
26 #define AMBI_INVSQRT2 (1/1.414213562)
70 return (
unsigned)floor(sqrt((
double)channels)-1);
84 return (
unsigned)pow(torder + 1.f, 2);
102 unsigned *channelIndex = indexArray;
110 unsigned currentOrder = 1;
111 unsigned hoaChannel = 1;
112 unsigned numChannelsCurrentOrder;
115 for (
unsigned i = 0; i < numChannelsGreaterOrder; i++)
119 while (n < nnumChannels) {
123 channelIndex[hoaChannel++] = n++;
124 channelIndex[hoaChannel++] = n++;
135 while(hoaChannel < numChannelsCurrentOrder) {
136 channelIndex[hoaChannel++] = n++;
139 hoaChannel = numChannelsCurrentOrder;
151 unsigned *channelIndex = indexArray;
158 unsigned currentOrder = 1;
159 unsigned numChannelsCurrentOrder;
162 while (n < nnumChannels) {
166 channelIndex[n] = n++;
167 channelIndex[n] = n++;
176 while(n < numChannelsCurrentOrder) {
177 channelIndex[n] = n++;
180 n = numChannelsCurrentOrder;
240 logMsg(
"AmbisonicEncoder::setInput");
249 unsigned numFrames = outputBuffer.mNumFrames;
253 logMsg(
"AmbisonicEncoder::nextBuffer");
255 if (outputBuffer.mNumChannels < mNumChannels)
256 logMsg(
kLogError,
"The AmbisonicEncoder requires %d output channels, found only %d ???\n", mNumChannels, outputBuffer.mNumChannels);
261 inputBuffer = mInputPort->mBuffer;
264 mInputPort->mUGen->nextBuffer(*inputBuffer);
274 for (
unsigned i = 0; i < mNumChannels; i++) {
278 for (
unsigned j = 0; j < numFrames; j++) {
280 *outPtr++ += (*inPtr++) * mWeights[i];
296 mDecodingMethod(method), mDecoderFlavour(flavour) {
303 mDecodingMethod(method), mDecoderFlavour(flavour) {
310 mDecodingMethod(method), mDecoderFlavour(flavour) {
368 "Reducing decoding order because encoded input does not contain enough channels for specified order.\n Input channels:(%d): horizontal order = %i, vertical order = %i\n",
378 unsigned channelIndex[numChannelsGreaterOrder];
379 unsigned invChannelIndex[numChannelsGreaterOrder];
382 memset(invChannelIndex , 0, numChannelsGreaterOrder *
sizeof(
unsigned));
394 for (
unsigned s = 0; s < numSpeakers; s++)
406 logMsg(
kLogWarning,
"AmbisonicDecoder: the in-phase decoding method is only well defined for non-hybrid systems - you have asked for an in phase decoder for a hybrid ambisonic decoding.\n");
411 logMsg(
kLogWarning,
"AmbisonicDecoder: the max-rE decoding method is only well defined for non-hybrid systems - you have asked for a max rE decoder for a hybrid ambisonic decoding.\n");
417 printf(
"Decoding from %i ambisonic channels to %i speakers", mNumChannels, numSpeakers);
420 case 0: printf(
" of basic flavour\n");
break;
421 case 1: printf(
" of in-phase flavour\n");
break;
422 case 2: printf(
" of max rE flavour\n");
break;
425 printf(
"Decoding Matrix:\n");
426 for (
unsigned s=0; s< numSpeakers; s++ ) {
427 printf(
"speaker %d\t",s);
464 sample invNumSpeakers = 1.f/((float)numSpeakers);
467 for (
unsigned s = 0; s < numSpeakers; s++) {
485 for (
unsigned n = 0; n < nnumChannels; n++) {
486 squareMatrix[n] =
new sample[nnumChannels];
490 for (
unsigned n = 0; n < nnumChannels; n++) {
491 invSquareMatrix[n] =
new sample[nnumChannels];
495 sample** transposedReEncodingMatrix = (
sample**) malloc(numSpeakers *
sizeof(
sample*));
496 for (
unsigned s = 0; s < numSpeakers; s++)
497 transposedReEncodingMatrix[s] =
new sample[nnumChannels];
502 for (
unsigned n = 0; n < nnumChannels; n++) {
504 for (
unsigned j = 0; j < nnumChannels; j++ ) {
505 squareMatrix[n][j] = 0.f;
507 for (
unsigned s = 0; s < numSpeakers; s++ ) {
509 squareMatrix[n][j] += transposedReEncodingMatrix[s][n] * transposedReEncodingMatrix[s][j];
522 for (
unsigned i = 0; i < nnumChannels; i++) {
523 u[i] = &(uu[nnumChannels*i]);
524 v[i] = &(vv[nnumChannels*i]);
525 for (
unsigned j = 0; j < nnumChannels; j++)
526 u[i][j] = squareMatrix[i][j];
532 for (
unsigned j = 0; j < nnumChannels; j++) {
538 for (
unsigned k = 0; k < nnumChannels; k++)
544 for (
unsigned i = 0; i < nnumChannels; i++) {
545 for (
unsigned j = 0; j < nnumChannels; j++) {
546 invSquareMatrix[i][j] = 0.0;
547 for (
unsigned k = 0; k < nnumChannels; k++)
548 invSquareMatrix[i][j] += v[i][k]*w[k]*u[j][k];
563 for (
unsigned s = 0; s < numSpeakers; s++ ) {
564 for (
unsigned n = 0; n < nnumChannels; n++) {
566 for (
unsigned j = 0; j < nnumChannels; j++) {
568 mDecodingMatrix[s][n] += transposedReEncodingMatrix[s][j] * invSquareMatrix[j][n];
574 for (
unsigned n = 0; n < nnumChannels; n++) {
575 delete [] squareMatrix[n];
576 delete [] invSquareMatrix[n];
578 for (
unsigned s = 0; s < numSpeakers; s++)
579 delete [] transposedReEncodingMatrix[s];
582 free(invSquareMatrix);
583 free(transposedReEncodingMatrix);
594 float **transposedReEncodingMatrix = transposeMatrix;
597 for (
unsigned s = 0; s < numSpeakers; s++) {
616 sample maxREParameters[][5] = { {1.0, 1.0, 1.0, 1.0, 1.0},
618 {0.f, 0.577, 0.775, 0.861, 0.906},
620 {0.f, 0.f, 0.4, 0.612, 0.732},
622 {0.f, 0.f, 0.f, 0.305, 0.501},
624 {0.f, 0.f, 0.f, 0.f, 0.246},
632 for (
unsigned l = 0; l < numSpeakers; l++) {
646 sample inPhaseParameters[][5] = {
647 {1.0, 1.0, 1.0, 1.0, 1.0},
648 {0.f, 0.333, 0.5, 0.6, 0.667},
649 {0.f, 0.f, 0.1, 0.2, 0.286},
650 {0.f, 0.f, 0.f, 0.029, 0.071},
651 {0.f, 0.f, 0.f, 0.f, 0.008},
655 unsigned M = tgreaterOrder;
658 for (
unsigned l = 0; l < numSpeakers; l++) {
671 unsigned numChannels = mNumChannels;
672 unsigned numSpeakers = mSpeakerLayout->numSpeakers();
673 unsigned numFrames = outputBuffer.mNumFrames;
676 if (outputBuffer.mNumChannels < numSpeakers) {
677 logMsg(
kLogError,
"AmbisonicDecoder needs a buffer with %d channels, found only %d\n", numChannels, outputBuffer.mNumChannels);
678 throw RunTimeError(
"Insufficient number of channels in buffer passed.");
681 outputBuffer.zeroBuffers();
685 inputBuffer = mInputPort->mBuffer;
688 mInputPort->mUGen->nextBuffer(*inputBuffer);
693 for (
unsigned n = 0; n < numChannels; n++) {
698 for (
unsigned l = 0; l < numSpeakers; l++) {
702 for (
unsigned i = 0; i < numFrames; i++) {
704 *outPtr++ += mDecodingMatrix[l][n] * inPtr[i];