CSL  6.0
SpatialAudio.cpp
Go to the documentation of this file.
1 //
2 // SpatialAudio.cpp -- Spatializer
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 // Created by Jorge Castellanos on 7/23/06. Hacked 8/09 by STP.
5 //
6 /// @todo Observe the layout for changes, just in case it's deleted, to grab the default, avoiding dangling pointers.
7 
8 #include "SpatialAudio.h"
9 #include "SimplePanner.h"
10 
11 #ifdef USE_VBAP
12  #include "VBAP.h"
13 #endif
14 #ifdef USE_AMBISONIC
15  #include "AmbisonicPanner.h"
16 #endif
17 #ifdef USE_HRTF
18  #include "Binaural.h"
19 #endif
20 #ifdef USE_WFS
21  #include "WFS.h"
22 #endif
23 
24 using namespace csl;
25 
27  : UnitGenerator(), mPanner(NULL), mSpeakerLayout(speakerLayout) {
28  setCopyPolicy(kIgnore); // This is needed so the default kCopy doesn't overide
29  // the multiple channel panning done here.
30  setNumChannels(2);
31  setPanningMode(panMode);
32 }
33 
35  delete mPanner;
36 }
37 
39 // logMsg("Spatializer::addSource");
40  if (mType == kSimple) {
41  mPanner->addSource(soundSource); // Add the source to the panner.
42  } else { // create a new distance simulator passing it the source.
43  DistanceSimulator *distanceProcessor = new DistanceSimulator(soundSource);
44  mPanner->addSource(*distanceProcessor); // Add the source to the panner.
45  mInputsHashMap[&soundSource] = distanceProcessor; // Add the pointers to the map.
46  }
47 }
48 
49 // delete from the list, shifting if necessary
50 
52 #ifdef CSL_DEBUG
53  logMsg("Panner::removeSource");
54 #endif
55  SpatialSource *tempSourcePtr = mInputsHashMap[&soundSource];
56  if (tempSourcePtr) { // If not null then remove it form the panner.
57  mPanner->removeSource(*tempSourcePtr);
58  // now remove it from the map.
59  mInputsHashMap.erase(&soundSource);
60  } else {
61  mPanner->removeSource(soundSource);
62  }
63 }
64 
65 void Spatializer::update(void * arg) {
66 
67 }
68 
70  SpatialPanner *tempPanner; // Placeholder for the panner until everything is setup.
71  mType = panType;
72  if (mType == kAutomatic) {
73  // Call the panner expert system, so that it decides which panner to instantiate.
74  mType = kVBAP;
75  } else {
76  switch(mType) {
77  case kSimple:
78  tempPanner = new SimplePanner();
79  break;
80 #ifdef USE_HRTF
81  case kBinaural:
82  tempPanner = new BinauralPanner();
83  break;
84 #endif
85 #ifdef USE_VBAP
86  case kVBAP:
87  tempPanner = new VBAP(kPeriphonic, mSpeakerLayout);
88  break;
89 #endif
90 #ifdef USE_AMBISONIC
91  case kAmbisonic:
92  tempPanner = new AmbisonicPanner(1, mSpeakerLayout);
93  break;
94 #endif
95 #ifdef USE_WFS
96  case kWFS:
97  tempPanner = new WFSPanner(mSpeakerLayout);
98  break;
99 #endif
100  default:
101  logMsg(kLogWarning, "Unknown PannerMode Specified.");
102  setPanningMode(kAutomatic); // I call myself setting the mode to Automatic.
103  break;
104  }
105  }
106  // create an iterator to go thru all the sources.
107  map <SpatialSource *, DistanceSimulator *>::iterator idx;
108  // Add all the sources to the new panner
109  for (idx = mInputsHashMap.begin(); idx != mInputsHashMap.end(); ++idx)
110  tempPanner->addSource(*(idx->second));
111 
112  // NOTE: This is not thread safe. In multithreaded apps, this could make it crash!
113  /// @todo make all spat framework thread safe.
114  // get rid of the old panner.
115  if (mPanner != NULL)
116  delete mPanner;
117  mPanner = tempPanner; // Assign the new panner.
118 }
119 
120 // the method Spatializer::nextBuffer calls the panner
121 
122 void Spatializer::nextBuffer(Buffer &outputBuffer /*, unsigned outBufNum */) throw(CException) {
123 #ifdef CSL_DEBUG
124  logMsg("Spatializer::nextBuffer");
125 #endif // Ask the decoder to fill the buffer with the data to be processed
126  outputBuffer.zeroBuffers();
127  mPanner->nextBuffer(outputBuffer);
128 }
129 
131  unsigned numSpeakers = layout->numSpeakers();
132  PannerType type = kVBAP; // Just default to something that always works.
133  switch(numSpeakers) {
134  case 0:
135  case 1:
136  logMsg("Not enough speakers to be spatialized");// throw an exception
137  break;
138  case 2:
139  // if headphones, then Binaural.
140  // otherwise for now use VBAP. At some point it could be used transaural.
141  type = kVBAP;
142  break;
143  case 3:
144  default:
145  type = kVBAP;
146  break;
147  }
148  return type;
149 }
void logMsg(const char *format,...)
These are the public logging messages.
Definition: CGestalt.cpp:292
Simple panning/filtering spatializer.
Definition: SpatialAudio.h:23
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
forward declaration
Definition: Binaural.h:49
void addSource(SpatialSource &s)
Add a sound souce to the list of inputs to be processed.
void setPanningMode(PannerType panType)
number of active inputs.
static PannerType findPannerFromLayout(SpeakerLayout *layout=SpeakerLayout::defaultSpeakerLayout())
Only handles single sound sources because objects have different positions. Two objects can't ocuppy ...
Wave Field Synthesis.
Definition: SpatialAudio.h:24
Vector Base Amplitude Panning.
Definition: SpatialAudio.h:21
Full 3D VBAP.
Definition: VBAP.h:49
PannerType mType
Definition: SpatialAudio.h:55
Base class for all panners. Handles source management and holds a speaker layout. ...
Definition: SpatialPanner.h:22
PannerType
Panner types.
Definition: SpatialAudio.h:18
Temp Spatial Sound Source.
Definition: SpatialSource.h:29
void removeSource(SpatialSource &s)
Remove a Sound Source.
virtual void removeSource(SpatialSource &s)
Remove a Sound Source.
Spatializer(PannerType panMode=kAutomatic, SpeakerLayout *speakerLayout=SpeakerLayout::defaultSpeakerLayout())
virtual void addSource(SpatialSource &s)
number of active inputs.
SpeakerLayout * mSpeakerLayout
If null, it will use the default layout by calling SpeakerLayout::defaultSpeakerLayout();.
Definition: SpatialAudio.h:54
Ambisonic Panner Wraps around the different ambisonic classes, providing a single interface for encod...
map< SpatialSource *, DistanceSimulator * > mInputsHashMap
a map between a source passed/key and a the corresponding distance simulator (used for removing sourc...
Definition: SpatialAudio.h:51
void setCopyPolicy(BufferCopyPolicy ch)
get/set the receiver's buffer copy policy
Definition: CSL_Core.h:256
Vector Base Amplitude Panning.
Definition: VBAP.h:61
Simple Panner.
Definition: SimplePanner.h:16
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
unsigned numSpeakers() const
Definition: SpeakerLayout.h:58
virtual void nextBuffer(Buffer &outputBuffer)
fill the buffer with data :-)
ignore extra buffer channels
Definition: CSL_Core.h:213
forward declaration
Definition: CSL_Core.h:241
void setNumChannels(unsigned ch)
get/set the receiver's number of outputs
Definition: CSL_Core.h:253
SpatialPanner * mPanner
Definition: SpatialAudio.h:48
Full 3D Ambisonics.
Definition: SpatialAudio.h:22
virtual void update(void *arg)
called when the speaker layout changes, so panners update precalculated data.
Base class of CSL exceptions (written upper-case). Has a string message.
HRTF 2-channel panning.
Definition: SpatialAudio.h:20