@@ -483,27 +483,6 @@ namespace Binaural {
483483 {
484484 environmentABIR.Setup (ownerCore->GetAudioState ().bufferSize , environmentBRIR->GetBRIRLength ());
485485
486-
487- // 1. Get BRIR values for each channel
488- TImpulseResponse_Partitioned northLeft = environmentBRIR->GetBRIR_Partitioned (VirtualSpeakerPosition::NORTH, Common::T_ear::LEFT);
489- TImpulseResponse_Partitioned southLeft = environmentBRIR->GetBRIR_Partitioned (VirtualSpeakerPosition::SOUTH, Common::T_ear::LEFT);
490- TImpulseResponse_Partitioned eastLeft = environmentBRIR->GetBRIR_Partitioned (VirtualSpeakerPosition::EAST, Common::T_ear::LEFT);
491- TImpulseResponse_Partitioned westLeft = environmentBRIR->GetBRIR_Partitioned (VirtualSpeakerPosition::WEST, Common::T_ear::LEFT);
492- TImpulseResponse_Partitioned zenitLeft = environmentBRIR->GetBRIR_Partitioned (VirtualSpeakerPosition::ZENIT, Common::T_ear::LEFT);
493- TImpulseResponse_Partitioned nadirLeft = environmentBRIR->GetBRIR_Partitioned (VirtualSpeakerPosition::NADIR, Common::T_ear::LEFT);
494-
495- TImpulseResponse_Partitioned northRight = environmentBRIR->GetBRIR_Partitioned (VirtualSpeakerPosition::NORTH, Common::T_ear::RIGHT);
496- TImpulseResponse_Partitioned southRight = environmentBRIR->GetBRIR_Partitioned (VirtualSpeakerPosition::SOUTH, Common::T_ear::RIGHT);
497- TImpulseResponse_Partitioned eastRight = environmentBRIR->GetBRIR_Partitioned (VirtualSpeakerPosition::EAST, Common::T_ear::RIGHT);
498- TImpulseResponse_Partitioned westRight = environmentBRIR->GetBRIR_Partitioned (VirtualSpeakerPosition::WEST, Common::T_ear::RIGHT);
499- TImpulseResponse_Partitioned zenitRight = environmentBRIR->GetBRIR_Partitioned (VirtualSpeakerPosition::ZENIT, Common::T_ear::RIGHT);
500- TImpulseResponse_Partitioned nadirRight = environmentBRIR->GetBRIR_Partitioned (VirtualSpeakerPosition::NADIR, Common::T_ear::RIGHT);
501-
502- long s = northLeft.size ();
503-
504- TImpulseResponse_Partitioned newAIR_W_left, newAIR_X_left, newAIR_Y_left, newAIR_Z_left;
505- TImpulseResponse_Partitioned newAIR_W_right, newAIR_X_right, newAIR_Y_right, newAIR_Z_right;
506-
507486 switch (reverberationOrder) {
508487 case TReverberationOrder::BIDIMENSIONAL:
509488 return CalculateABIRPartitionedBidimensional ();
@@ -626,19 +605,19 @@ namespace Binaural {
626605 continue ;
627606 }
628607
608+ // Check if the source is in the same position as the listener head. If yes, do not apply spatialization to this source
609+ if (eachSource->distanceToListener < ownerCore->GetListener ()->GetHeadRadius ())
610+ {
611+ continue ;
612+ }
613+
629614 // Get azimuth, elevation and distance from listener to each source
630615 // We precompute everything, to minimize per-sample computations.
631616 Common::CTransform sourceTransform = eachSource->GetSourceTransform ();
632617 Common::CVector3 vectorToSource = ownerCore->GetListener ()->GetListenerTransform ().GetVectorTo (sourceTransform);
633- float sourceAzimuth = vectorToSource.GetAzimuthRadians ();
634- float sourceElevation = vectorToSource.GetElevationRadians ();
618+
635619 float sourceDistance = vectorToSource.GetDistance ();
636- float cosAzimuth = std::cos (sourceAzimuth);
637- float sinAzimuth = std::sin (sourceAzimuth);
638- float sinElevation = std::sin (sourceElevation);
639- float cosElevation = std::cos (sourceElevation);
640- float cosAcosE = cosAzimuth * cosElevation;
641- float sinAcosE = sinAzimuth * cosElevation;
620+
642621 CMonoBuffer<float > sourceBuffer = eachSource->GetBuffer ();
643622 // ASSERT(sourceBuffer.size() > 0, RESULT_ERROR_NOTSET, "Attempt to process virtual ambisonics reverb without previously feeding audio source buffers", "");
644623
@@ -746,7 +725,7 @@ namespace Binaural {
746725 CMonoBuffer<float > temp_OutputBlockLeft (ouputBuffer_temp.begin () + halfsize, ouputBuffer_temp.end ());
747726 mixerOutput_left = std::move (temp_OutputBlockLeft); // To use in C++11
748727
749- // Right channel
728+ // Right channel
750729 ouputBuffer_temp.clear ();
751730 Common::CFprocessor::CalculateIFFT (mixerOutput_right_FFT, ouputBuffer_temp);
752731 // We are left only with the final half of the result
@@ -755,16 +734,11 @@ namespace Binaural {
755734 mixerOutput_right = std::move (temp_OutputBlockRight); // To use in C++11
756735#endif
757736
758- // ////////////////////////////////////////////
759- // Mix of chabbels decoded after convolution
760- // ////////////////////////////////////////////
761-
762- // Interlace TODO Use the method in bufferClass??
763- for (int i = 0 ; i < mixerOutput_left.size (); i++) {
764- outBufferLeft.push_back (mixerOutput_left[i]);
765- outBufferRight.push_back (mixerOutput_right[i]);
766- }
767-
737+ // ////////////////////////////////////////////
738+ // Move channels to output buffers
739+ // ////////////////////////////////////////////
740+ outBufferLeft = std::move (mixerOutput_left); // To use in C++11
741+ outBufferRight = std::move (mixerOutput_right); // To use in C++11
768742
769743#ifdef USE_PROFILER_Environment
770744 PROFILER3DTI.RelativeSampleEnd (dsEnvInvFFT);
@@ -787,6 +761,7 @@ namespace Binaural {
787761 WATCH (WV_ENVIRONMENT_OUTPUT_LEFT, outBufferLeft, CMonoBuffer<float >);
788762 WATCH (WV_ENVIRONMENT_OUTPUT_RIGHT, outBufferRight, CMonoBuffer<float >);
789763 }
764+
790765 void CEnvironment::ProcessVirtualAmbisonicReverbBidimensional (CMonoBuffer<float > & outBufferLeft, CMonoBuffer<float > & outBufferRight)
791766 {
792767 CMonoBuffer<float > w, x, y; // B-Format data
@@ -844,15 +819,23 @@ namespace Binaural {
844819 // We precompute everything, to minimize per-sample computations.
845820 Common::CTransform sourceTransform = eachSource->GetSourceTransform ();
846821 Common::CVector3 vectorToSource = ownerCore->GetListener ()->GetListenerTransform ().GetVectorTo (sourceTransform);
847- float sourceAzimuth = vectorToSource.GetAzimuthRadians ();
848822 float sourceElevation = vectorToSource.GetElevationRadians ();
849- float sourceDistance = vectorToSource.GetDistance ();
850- float cosAzimuth = std::cos (sourceAzimuth);
851- float sinAzimuth = std::sin (sourceAzimuth);
852823 float sinElevationAbs = std::fabs (std::sin (sourceElevation)); // TEST: adding power to W channel to compensate for the lack of Z channel
853824 float cosElevation = std::cos (sourceElevation);
854- float cosAcosE = cosAzimuth * cosElevation;
855- float sinAcosE = sinAzimuth * cosElevation;
825+
826+ float cosAcosE = 0 .0f ;
827+ float sinAcosE = 0 .0f ;
828+ if (!Common::CMagnitudes::AreSame (0 .0f , cosElevation, EPSILON))
829+ {
830+ float sourceAzimuth = vectorToSource.GetAzimuthRadians ();
831+ float cosAzimuth = std::cos (sourceAzimuth);
832+ float sinAzimuth = std::sin (sourceAzimuth);
833+ cosAcosE = cosAzimuth * cosElevation;
834+ sinAcosE = sinAzimuth * cosElevation;
835+ }
836+
837+ float sourceDistance = vectorToSource.GetDistance ();
838+
856839 CMonoBuffer<float > sourceBuffer = eachSource->GetBuffer ();
857840 // ASSERT(sourceBuffer.size() > 0, RESULT_ERROR_NOTSET, "Attempt to process virtual ambisonics reverb without previously feeding audio source buffers", "");
858841
@@ -975,16 +958,11 @@ namespace Binaural {
975958 mixerOutput_right = std::move (temp_OutputBlockRight); // To use in C++11
976959#endif
977960
978- // ////////////////////////////////////////////
979- // Mix of channels decoded after convolution
980- // ////////////////////////////////////////////
981-
982- // Interlace TODO Use the method in bufferClass??
983- for (int i = 0 ; i < mixerOutput_left.size (); i++) {
984- outBufferLeft.push_back (mixerOutput_left[i]);
985- outBufferRight.push_back (mixerOutput_right[i]);
986- }
987-
961+ // ////////////////////////////////////////////
962+ // Move channels to output buffers
963+ // ////////////////////////////////////////////
964+ outBufferLeft = std::move (mixerOutput_left); // To use in C++11
965+ outBufferRight = std::move (mixerOutput_right); // To use in C++11
988966
989967#ifdef USE_PROFILER_Environment
990968 PROFILER3DTI.RelativeSampleEnd (dsEnvInvFFT);
@@ -1007,6 +985,7 @@ namespace Binaural {
1007985 WATCH (WV_ENVIRONMENT_OUTPUT_LEFT, outBufferLeft, CMonoBuffer<float >);
1008986 WATCH (WV_ENVIRONMENT_OUTPUT_RIGHT, outBufferRight, CMonoBuffer<float >);
1009987 }
988+
1010989 void CEnvironment::ProcessVirtualAmbisonicReverbThreedimensional (CMonoBuffer<float > & outBufferLeft, CMonoBuffer<float > & outBufferRight)
1011990 {
1012991 CMonoBuffer<float > w, x, y, z; // B-Format data
@@ -1054,21 +1033,36 @@ namespace Binaural {
10541033 {
10551034 SET_RESULT (RESULT_WARNING, " Attempt to do reverb process without updating source buffer; please call to SetBuffer before ProcessVirtualAmbisonicReverb." );
10561035 continue ;
1057- }
1036+ }
1037+
1038+ // Check if the source is in the same position as the listener head. If yes, do not apply spatialization to this source
1039+ if (eachSource->distanceToListener < ownerCore->GetListener ()->GetHeadRadius ())
1040+ {
1041+ continue ;
1042+ }
10581043
10591044 // Get azimuth, elevation and distance from listener to each source
10601045 // We precompute everything, to minimize per-sample computations.
10611046 Common::CTransform sourceTransform = eachSource->GetSourceTransform ();
10621047 Common::CVector3 vectorToSource = ownerCore->GetListener ()->GetListenerTransform ().GetVectorTo (sourceTransform);
1063- float sourceAzimuth = vectorToSource. GetAzimuthRadians ();
1048+
10641049 float sourceElevation = vectorToSource.GetElevationRadians ();
1065- float sourceDistance = vectorToSource.GetDistance ();
1066- float cosAzimuth = std::cos (sourceAzimuth);
1067- float sinAzimuth = std::sin (sourceAzimuth);
10681050 float sinElevation = std::sin (sourceElevation);
10691051 float cosElevation = std::cos (sourceElevation);
1070- float cosAcosE = cosAzimuth * cosElevation;
1071- float sinAcosE = sinAzimuth * cosElevation;
1052+
1053+ float cosAcosE = 0 .0f ;
1054+ float sinAcosE = 0 .0f ;
1055+ if (!Common::CMagnitudes::AreSame (0 .0f , cosElevation, EPSILON))
1056+ {
1057+ float sourceAzimuth = vectorToSource.GetAzimuthRadians ();
1058+ float cosAzimuth = std::cos (sourceAzimuth);
1059+ float sinAzimuth = std::sin (sourceAzimuth);
1060+ cosAcosE = cosAzimuth * cosElevation;
1061+ sinAcosE = sinAzimuth * cosElevation;
1062+ }
1063+
1064+ float sourceDistance = vectorToSource.GetDistance ();
1065+
10721066 CMonoBuffer<float > sourceBuffer = eachSource->GetBuffer ();
10731067 // ASSERT(sourceBuffer.size() > 0, RESULT_ERROR_NOTSET, "Attempt to process virtual ambisonics reverb without previously feeding audio source buffers", "");
10741068
@@ -1194,16 +1188,11 @@ namespace Binaural {
11941188 mixerOutput_right = std::move (temp_OutputBlockRight); // To use in C++11
11951189#endif
11961190
1197- // ////////////////////////////////////////////
1198- // Mix of chabbels decoded after convolution
1199- // ////////////////////////////////////////////
1200-
1201- // Interlace TODO Use the method in bufferClass??
1202- for (int i = 0 ; i < mixerOutput_left.size (); i++) {
1203- outBufferLeft.push_back (mixerOutput_left[i]);
1204- outBufferRight.push_back (mixerOutput_right[i]);
1205- }
1206-
1191+ // ////////////////////////////////////////////
1192+ // Move channels to output buffers
1193+ // ////////////////////////////////////////////
1194+ outBufferLeft = std::move (mixerOutput_left); // To use in C++11
1195+ outBufferRight = std::move (mixerOutput_right); // To use in C++11
12071196
12081197#ifdef USE_PROFILER_Environment
12091198 PROFILER3DTI.RelativeSampleEnd (dsEnvInvFFT);
@@ -1235,6 +1224,13 @@ namespace Binaural {
12351224 SET_RESULT (RESULT_ERROR_NOTINITIALIZED, " Data is not ready to be processed" );
12361225 return ;
12371226 }
1227+
1228+ // Check outbuffers size
1229+ if (outBufferLeft.size () != 0 || outBufferRight.size () != 0 ) {
1230+ outBufferLeft.clear ();
1231+ outBufferRight.clear ();
1232+ SET_RESULT (RESULT_ERROR_BADSIZE, " outBufferLeft and outBufferRight were expected to be empty, they will be cleared. CEnvironment::ProcessVirtualAmbisonicReverb" );
1233+ }
12381234
12391235 // This would crash if there are no sources created. Rather than reporting error, do nothing
12401236 if (ownerCore->audioSources .size () == 0 )
0 commit comments