Allolib  1.0
C++ Components For Interactive Multimedia
al_Vbap.hpp
1 #ifndef INCLUDE_AL_PANNING_VBAP
2 #define INCLUDE_AL_PANNING_VBAP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39 
40  File description:
41  Vector-based amplitude panner (VBAP)
42 
43  File author(s):
44  Ryan McGee, 2012, ryanmichaelmcgee@gmail.com
45 */
46 
47 #include <map>
48 
49 #include "al/math/al_Vec.hpp"
50 #include "al/sound/al_Spatializer.hpp"
51 #include "al/sound/al_Speaker.hpp"
52 #include "al/spatial/al_DistAtten.hpp"
53 #include "al/spatial/al_Pose.hpp"
54 
55 #define MAX_NUM_VBAP_TRIPLETS 512
56 //#define MIN_VOLUME_TO_LENGTH_RATIO 0.01
57 
58 #define MIN_VOLUME_TO_LENGTH_RATIO 0.000001
59 #define MIN_LENGTH 0.00001
60 
61 namespace al {
62 
64 struct SpeakerTriple {
65  int s1;
66  Vec3d s1Vec;
67  int s2;
68  Vec3d s2Vec;
69  int s3;
70  Vec3d s3Vec;
71  Vec3d vec[3];
72  Mat3d mat;
73  int speakerIdx[3];
74 
75  // Speaker speakers[3];
76  unsigned int speakerChan[3];
77  unsigned int s1Chan;
78  unsigned int s2Chan;
79  unsigned int s3Chan;
80 
81  bool loadVectors(const std::vector<Speaker>& spkrs);
82 };
83 
87 class Vbap : public Spatializer {
88  public:
89  typedef enum {
90  KEEP_SAME_ELEVATION =
91  0x1, // Don't discard triplets that have the same elevation
92  } VbapOptions;
93 
95  Vbap(const Speakers& sl, bool is3D = false);
96 
97  void setOptions(VbapOptions options) { mOptions = options; }
98 
99  virtual void compile() override;
100 
112  void makePhantomChannel(int channelIndex,
113  std::vector<unsigned int> assignedOutputs);
114 
118  void set3D(bool is3D) { mIs3D = is3D; }
119 
120  virtual void renderSample(AudioIOData& io, const Pose& reldir,
121  const float& sample,
122  const unsigned int& frameIndex) override;
123  virtual void renderBuffer(AudioIOData& io, const Pose& reldir,
124  const float* samples,
125  const unsigned int& numFrames) override;
126 
127  virtual void print(std::ostream& stream = std::cout) override;
128 
130  void makeTriple(int s1, int s2, int s3 = -1);
131 
132  // Returns vector of triplets
133  std::vector<SpeakerTriple> triplets() const;
134 
135  private:
136  std::vector<SpeakerTriple> mTriplets;
137  std::map<unsigned int, std::vector<unsigned int> > mPhantomChannels;
138  // Listener* mListener;
139  bool mIs3D;
140  VbapOptions mOptions;
141 
142  Vec3d computeGains(const Vec3d& vecA, const SpeakerTriple& speak);
143 
145  void findSpeakerPairs(const Speakers& spkrs);
146 
148  void findSpeakerTriplets(const Speakers& spkrs);
149 
150  bool isCrossing(Vec3d c, Vec3d li, Vec3d lj, Vec3d ln, Vec3d lm);
151 
153  void addTriple(const SpeakerTriple& st);
154 };
155 
156 } // namespace al
157 #endif
A local coordinate frame.
Definition: al_Pose.hpp:63
virtual void numFrames(unsigned int v)
Set number of frames.
void makeTriple(int s1, int s2, int s3=-1)
Manually add a triple from indeces to speakers.
void makePhantomChannel(int channelIndex, std::vector< unsigned int > assignedOutputs)
Make an existing channel a phantom channel.
virtual void print(std::ostream &stream=std::cout) override
Print out information about spatializer.
virtual void renderSample(AudioIOData &io, const Pose &reldir, const float &sample, const unsigned int &frameIndex) override
Render audio sample in position.
virtual void renderBuffer(AudioIOData &io, const Pose &reldir, const float *samples, const unsigned int &numFrames) override
Render audio buffer in position.
virtual void compile() override
Vbap(const Speakers &sl, bool is3D=false)
void set3D(bool is3D)
Definition: al_Vbap.hpp:118
Definition: al_App.hpp:23
std::vector< Speaker > Speakers
A set of speakers.
Definition: al_Speaker.hpp:101
A triplet of speakers.
Definition: al_Vbap.hpp:64