root/juggler/branches/2.2/modules/sonix/snx/SoundImplementation.h

Revision 19729, 13.3 kB (checked in by patrick, 2 years ago)

Copyright update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /****************** <SNX heading BEGIN do not edit this line> *****************
2  *
3  * sonix
4  *
5  * Original Authors:
6  *   Kevin Meinert, Carolina Cruz-Neira
7  *
8  ****************** <SNX heading END do not edit this line> ******************/
9
10 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
11  *
12  * VR Juggler is (C) Copyright 1998-2007 by Iowa State University
13  *
14  * Original Authors:
15  *   Allen Bierbaum, Christopher Just,
16  *   Patrick Hartling, Kevin Meinert,
17  *   Carolina Cruz-Neira, Albert Baker
18  *
19  * This library is free software; you can redistribute it and/or
20  * modify it under the terms of the GNU Library General Public
21  * License as published by the Free Software Foundation; either
22  * version 2 of the License, or (at your option) any later version.
23  *
24  * This library is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27  * Library General Public License for more details.
28  *
29  * You should have received a copy of the GNU Library General Public
30  * License along with this library; if not, write to the
31  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
32  * Boston, MA 02111-1307, USA.
33  *
34  *************** <auto-copyright.pl END do not edit this line> ***************/
35
36 /* Generated by Together */
37
38 #ifndef SNXSOUNDIMPLEMENTATION_H
39 #define SNXSOUNDIMPLEMENTATION_H
40
41 #include <snx/snxConfig.h>
42
43 #include <string>
44 #include <map>
45 #include <boost/concept_check.hpp>
46 #include <gmtl/Matrix.h>
47 #include <vpr/Util/Assert.h>
48
49 #include <snx/SoundInfo.h>
50 #include <snx/SoundAPIInfo.h>
51 #include <snx/ISoundImplementation.h>
52
53 namespace snx
54 {
55
56 /** \class SoundImplementation SoundImplementation.h snx/SoundImplementation.h
57  *
58  * Sound implementation.
59  */
60 class SNX_CLASS_API SoundImplementation : public ISoundImplementation
61 {
62 public:
63    /** Default constructor. */
64    SoundImplementation()
65       : ISoundImplementation()
66       , mName( "unknown" )
67       , mSoundAPIInfo()
68       , mSounds()
69       , mListenerPos()
70    {
71    }
72
73    /**
74     * Every implementation can return a new copy of itself.
75     */
76    virtual void clone( ISoundImplementation* &newCopy ) = 0;
77
78    /** Destructor. */
79    virtual ~SoundImplementation()
80    {
81       // make sure the API has gracefully exited.
82       this->shutdownAPI();
83    }
84
85    /**
86     * Copies the current state of the system from the given API into this
87     * object.  This does not perform any binding.  That must be done
88     * separately.
89     *
90     * @param si The object from which the state will be copied into this
91     *           object.
92     */
93    void copy( const SoundImplementation& si );
94
95 public:
96
97    /**
98     * Triggers a sound.
99     *
100     * @pre alias does not have to be associated with a loaded sound.
101     * @post If alias is associated with a loaded sound, then the loaded sound
102     *       is triggered.  If it isn't, then nothing happens.
103     *
104     * @param alias  Alias of the sound to trigger.
105     * @param repeat The number of times to play.  Use -1 to repeat forever.
106     */
107    virtual void trigger( const std::string & alias, const int & repeat = 1 )
108    {
109       vprASSERT(this->isStarted() == true && "must call startAPI prior to this function");
110
111       this->lookup( alias ).repeat = repeat;
112       this->lookup( alias ).repeatCountdown = repeat;
113    }
114
115    /**
116     * Is the named sound currently playing?
117     *
118     * @param alias The alias of the sound to query.
119     */
120    virtual bool isPlaying( const std::string& alias )
121    {
122       boost::ignore_unused_variable_warning(alias);
123       return false;
124    }
125
126    /**
127     * Specifies whether the named sound retriggers from beginning when
128     * triggered while playing.  In other words, when the named sound is already
129     * playing and trigger() is called, does the sound restart from the
130     * beginning?
131     *
132     * @param alias The alias of the sound to change.
133     * @param onOff A Boolean value enabling or disabling retriggering.
134     */
135    virtual void setRetriggerable( const std::string& alias, bool onOff )
136    {
137       this->lookup( alias ).retriggerable = onOff;
138    }
139
140    /**
141     * Is the named sound retriggerable?
142     *
143     * @param alias The alias of the sound to query.
144     */
145    virtual bool isRetriggerable( const std::string& alias )
146    {
147       return bool( this->lookup( alias ).retriggerable == true );
148    }
149
150    /**
151     * Stops the named sound.
152     *
153     * @param alias Alias of the sound to be stopped.
154     */
155    virtual void stop( const std::string& alias )
156    {
157       vprASSERT(this->isStarted() == true && "must call startAPI prior to this function");
158       this->lookup( alias ).repeatCountdown = 0;
159    }
160
161    /**
162     * Pauses the named sound.  Use unpause() to return playback from where the
163     * sound was paused.
164     *
165     * @param alias The alias of the sound to pause.
166     */
167    virtual void pause( const std::string& alias )
168    {
169       this->stop( alias );
170    }
171
172    /**
173     * Resumes playback of the named sound from a paused state.  This does
174     * nothing if sound was not paused.
175     *
176     * @param alias The alias of the sound to unpause.
177     */
178    virtual void unpause( const std::string& alias )
179    {
180       this->trigger( alias, this->lookup( alias ).repeat );
181    }
182
183    /**
184     * If the named sound is paused, then return true.
185     *
186     * @param alias The alias of the sound to query.
187     */
188    virtual bool isPaused( const std::string& alias )
189    {
190       boost::ignore_unused_variable_warning(alias);
191       return false;
192    }
193
194    /**
195     * Sets the named sound as either ambient or positional depending on the
196     * value of ambient.  If the sound is ambient, it is attached to the
197     * listener, and its volume does not change when the listener moves.  If
198     * the sound is positional, the volume changes when the listener moves.
199     *
200     * @param ambient A flag identifying whether this sound is ambient (true)
201     *                or positional (false).  This parameter is optional,
202     *                and it defaults to false (the sound is positional).
203     */
204    virtual void setAmbient( const std::string& alias, bool ambient = false )
205    {
206       this->lookup( alias ).ambient = ambient;
207    }
208
209    /**
210     * Is the named sound ambient?
211     *
212     * @param alias The alias of the sound to query.
213     */
214    virtual bool isAmbient( const std::string& alias )
215    {
216       return this->lookup( alias ).ambient;
217    }
218
219    /**
220     * Alters the frequency of the named sound.
221     *
222     * @param alias  The alias of the sound to change.
223     * @param amount The value that determines the pitch bend.  1.0 means
224     *               that there is no change.  A value less than 1.0 is low;
225     *               a value greather than 1.0 is high.
226     */
227    virtual void setPitchBend( const std::string& alias, float amount )
228    {
229       this->lookup( alias ).pitchbend = amount;
230    }
231
232    /**
233     * Sets the effect volume of the named sound.  The value must be in the
234     * range [0,1].
235     *
236     * @param alias  The alias of the sound to change.
237     * @param amount The value of the volume.  It must be between 0.0 and
238     *               1.0 inclusive.
239     */
240    virtual void setVolume( const std::string& alias, float amount )
241    {
242       this->lookup( alias ).volume = amount;
243    }
244
245    /**
246     * Sets the effect cutoff of the named soudn.  The value must be in the
247     * range [0,1].
248     *
249     * @param alias  The alias of the sound to change.
250     * @param amount The value of the cutoff.  1.0 is no change; 0.0 is
251     *               total cutoff.
252     */
253    virtual void setCutoff( const std::string& alias, float amount )
254    {
255       this->lookup( alias ).cutoff = amount;
256    }
257
258    /**
259     * Set the named sound's 3D position.
260     *
261     * @param alias The alias of the sound to change.
262     * @param x     The X coordinate of the sound in 3D OpenGL coordinates.
263     * @param y     The Y coordinate of the sound in 3D OpenGL coordinates.
264     * @param z     The Z coordinate of the sound in 3D OpenGL coordinates.
265     */
266    virtual void setPosition(const std::string& alias, float x, float y,
267                             float z)
268    {
269       vprASSERT(this->isStarted() == true && "must call startAPI prior to this function");
270       this->lookup( alias ).position[0] = x;
271       this->lookup( alias ).position[1] = y;
272       this->lookup( alias ).position[2] = z;
273    }
274
275    /**
276     * Gets the named sound's 3D position.
277     *
278     * @param alias A name that has been associated with some sound data.
279     * @param x     Storage for the X coordinate of the sound's position (in
280     *              OpenGL coordinates).
281     * @param y     Storage for the Y coordinate of the sound's position (in
282     *              OpenGL coordinates).
283     * @param z     Storage for the Z coordinate of the sound's position (in
284     *              OpenGL coordinates).
285     */
286    virtual void getPosition(const std::string& alias, float& x, float& y,
287                             float& z)
288    {
289       vprASSERT(this->isStarted() == true && "must call startAPI prior to this function");
290
291       x = this->lookup( alias ).position[0];
292       y = this->lookup( alias ).position[1];
293       z = this->lookup( alias ).position[2];
294    }
295
296    /**
297     * Sets the position of the listener.
298     *
299     * @param mat A transformation matrix representing the position of the
300     *            listener.
301     */
302    virtual void setListenerPosition( const gmtl::Matrix44f& mat )
303    {
304       vprASSERT(this->isStarted() == true && "must call startAPI prior to this function");
305       mListenerPos = mat;
306    }
307
308    /**
309     * Gets the position of the listener.
310     *
311     * @param mat Storage for returning the position of the listener.
312     */
313    virtual void getListenerPosition( gmtl::Matrix44f& mat )
314    {
315       vprASSERT(this->isStarted() == true && "must call startAPI prior to this function");
316       mat = mListenerPos;
317    }
318
319    /**
320     * Start the sound API, creating any contexts or other configurations at
321     * startup.  This function should be called before using the other
322     * functions in the class.
323     *
324     * @post Sound API is ready to go.
325     * @return 1 if success, 0 otherwise.
326     */
327    virtual int startAPI() = 0;
328
329    /**
330     * Queries whether the API has been started.
331     *
332     * @return true if API has been started, false otherwise.
333     */
334    virtual bool isStarted() const = 0;
335
336    /**
337     * Kills the sound API, deallocating any sounds, etc.  This function could
338     * be called any time.  The function could be called multiple times, so it
339     * should be smart.
340     *
341     * @post The sound API is no longer running or available for use.
342     */
343    virtual void shutdownAPI() {}
344
345    /**
346     * Configures/reconfigures the sound API global settings.
347     *
348     * @param sai A description of the settings for this sound API.
349     */
350    virtual void configure( const snx::SoundAPIInfo& sai )
351    {
352       mSoundAPIInfo = sai;
353    }
354
355    /**
356     * Configures/reconfigures the named sound by associating sound data with
357     * the named sound.  Afterwards, the alias can be used to operate on sound
358     * data.
359     *
360     * Configure: associate a name (alias) to the description if not already
361     * done.
362     *
363     * Reconfigure: change properties of the sound to the description
364     * provided.
365     *
366     * @pre Provide a SoundInfo which describes the sound.
367     * @post This handle will point to loaded sound data.
368     *
369     * @param description An object that describes the sound for which this
370     *                    object will be a handle.
371     */
372    virtual void configure(const std::string& alias,
373                           const snx::SoundInfo& description);
374
375    /**
376     * Removes a configured sound.  Any future reference to the alias will not
377     * cause an error, but it will not result in a rendered sound.
378     *
379     * @param alias The alias of the sound to be removed.
380     */
381    virtual void remove(const std::string& alias);
382
383    /**
384     * Call once per sound frame (which does not have to be same as the graphics
385     * frame).
386     *
387     * @param timeElapsed The time elapsed since the last sound frame.
388     */
389    virtual void step( const float& timeElapsed )
390    {
391       boost::ignore_unused_variable_warning(timeElapsed);
392    }
393
394    /**
395     * Clears all associations.
396     *
397     * @post Any existing aliases will be stubbed. Sounds will be unbound.
398     */
399    virtual void clear()
400    {
401       this->unbindAll();
402    }
403
404    /**
405     * Bind: load (or reload) all associated sounds.
406     *
407     * @post All sound associations are buffered by the sound API.
408     */
409    virtual void bindAll();
410
411    /**
412     * Unbind: unload/deallocate all associated sounds.
413     *
414     * @post All sound associations are unbuffered by the sound API.
415     */
416    virtual void unbindAll();
417
418    /**
419     * Loads/allocates the sound data this alias refers to the sound API.
420     *
421     * @post The sound API has the sound buffered.
422     */
423    virtual void bind( const std::string& alias ) = 0;
424
425    /**
426     * Unloads/deallocates the sound data this alias refers to in the sound API.
427     *
428     * @post The sound API no longer has the sound buffered.
429     */
430    virtual void unbind( const std::string& alias ) = 0;
431
432    snx::SoundInfo& lookup( const std::string& alias )
433    {
434       return mSounds[alias];
435    }
436
437    void setName( const std::string& name )
438    {
439       mName = name;
440    }
441
442    std::string& name()
443    {
444       return mName;
445    }
446
447 protected:
448    /*
449     * The name of this implementation.
450     */
451    std::string mName;
452
453    snx::SoundAPIInfo mSoundAPIInfo;
454    std::map<std::string, snx::SoundInfo> mSounds;
455
456    gmtl::Matrix44f mListenerPos; /**< Position of the observer/listener. */
457 };
458
459 } // end namespace
460
461 #endif //SNXSOUNDIMPLEMENTATION_H
Note: See TracBrowser for help on using the browser.