From: Erik Hofman Date: Sat, 6 Aug 2016 09:24:58 +0000 (+0200) Subject: Clean up the code a bit and combine soundmgr_openal.hxx and soundmgr_aeonwave.hxx... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d3b15dc6dc1002919dcedd160cc6d20dd5e95231;p=simgear.git Clean up the code a bit and combine soundmgr_openal.hxx and soundmgr_aeonwave.hxx into a single file --- diff --git a/simgear/sound/CMakeLists.txt b/simgear/sound/CMakeLists.txt index 556244dc..4c4408e5 100644 --- a/simgear/sound/CMakeLists.txt +++ b/simgear/sound/CMakeLists.txt @@ -5,7 +5,7 @@ set(HEADERS sample_group.hxx xmlsound.hxx readwav.hxx - ${CMAKE_CURRENT_BINARY_DIR}/soundmgr.hxx + soundmgr.hxx ) set(SOURCES @@ -17,16 +17,10 @@ set(SOURCES ) if (USE_AEONWAVE) - CONFIGURE_FILE( - "soundmgr_aeonwave.hxx" - "soundmgr.hxx") set(SOURCES ${SOURCES} soundmgr_aeonwave.cxx ) else() - CONFIGURE_FILE( - "soundmgr_openal.hxx" - "soundmgr.hxx") set(SOURCES ${SOURCES} soundmgr_openal.cxx ) @@ -55,7 +49,6 @@ if(ENABLE_TESTS AND ENABLE_SOUND) endif() create_test(aeonwave_test1) - create_test(aeonwave_test2) else () if (SIMGEAR_SHARED) @@ -66,7 +59,8 @@ if(ENABLE_TESTS AND ENABLE_SOUND) endif() create_test(openal_test1) - create_test(openal_test2) - create_test(openal_test3) endif() + + create_test(soundmgr_test) + create_test(soundmgr_test2) endif() diff --git a/simgear/sound/openal_test2.cxx b/simgear/sound/openal_test2.cxx deleted file mode 100644 index 97680e58..00000000 --- a/simgear/sound/openal_test2.cxx +++ /dev/null @@ -1,105 +0,0 @@ -#include -#ifdef _WIN32 -#include -#define sleep(x) Sleep(x*1000) -#else -#include -#endif - -#include -#include -#include - -#include "soundmgr_openal.hxx" -#include "sample_group.hxx" -#include "sample.hxx" - - -int main( int argc, char *argv[] ) { - SGSampleGroup *sgr; - SGSoundMgr *smgr; - - smgr = new SGSoundMgr; - - smgr->bind(); - smgr->init(); - sgr = smgr->find("default", true); - smgr->set_volume(0.9); - smgr->activate(); - - SGPath srcDir(SRC_DIR); - - SGSoundSample *sample1 = new SGSoundSample("jet.wav", srcDir); - sample1->set_volume(1.0); - sample1->set_pitch(1.0); - sample1->play_looped(); - sgr->add(sample1, "sound1"); - smgr->update(1.0); - printf("playing sample1\n"); - sleep(1); - - SGSoundSample *sample2 = new SGSoundSample("jet_ulaw.wav", srcDir); - sample2->set_volume(0.5); - sample2->set_pitch(0.4); - sample2->play_looped(); - sgr->add(sample2, "sound2"); - smgr->update(1.0); - printf("playing sample2\n"); - sleep(1); - - SGSoundSample *sample3 = new SGSoundSample("jet_ima4.wav", srcDir); - sample3->set_volume(0.5); - sample3->set_pitch(0.8); - sample3->play_looped(); - sgr->add(sample3, "sound3"); - smgr->update(1.0); - printf("playing sample3\n"); - sleep(1); - - SGSoundSample *sample4 = new SGSoundSample("jet.wav", srcDir); - sample4->set_volume(0.5); - sample4->set_pitch(1.2); - sample4->play_looped(); - sgr->add(sample4, "sound4"); - smgr->update(1.0); - printf("playing sample4\n"); - sleep(1); - - SGSoundSample *sample5 = new SGSoundSample("jet.wav", srcDir); - sample5->set_volume(0.5); - sample5->set_pitch(1.6); - sample5->play_looped(); - sgr->add(sample5, "sound5"); - smgr->update(1.0); - printf("playing sample5\n"); - sleep(1); - - SGSoundSample *sample6 = new SGSoundSample("jet.wav", srcDir); - sample6->set_volume(0.5); - sample6->set_pitch(2.0); - sample6->play_looped(); - sgr->add(sample6, "sound6"); - smgr->update(1.0); - printf("playing sample6\n"); - sleep(1); - - for (int i=0; i<10; i++) { - sleep(1); - smgr->update(1); - } - - sgr->stop("sound1"); - sgr->stop("sound2"); - sgr->stop("sound3"); - SGTimeStamp::sleepForMSec(500); - smgr->update(0.5); - sgr->stop("sound4"); - sgr->stop("sound5"); - sgr->stop("sound6"); - smgr->update(1); - sleep(1); - - smgr->unbind(); - sleep(2); - delete smgr; -} diff --git a/simgear/sound/openal_test3.cxx b/simgear/sound/openal_test3.cxx deleted file mode 100644 index e93aa272..00000000 --- a/simgear/sound/openal_test3.cxx +++ /dev/null @@ -1,60 +0,0 @@ -#include -#ifdef _WIN32 -#include -#define sleep(x) Sleep(x*1000) -#else -#include -#endif - -#include -#include - -#include "soundmgr_openal.hxx" -#include "sample_group.hxx" -#include "sample.hxx" - -int main( int argc, char *argv[] ) { - SGSampleGroup *sgr; - SGSoundMgr *smgr; - SGGeod pos; - - smgr = new SGSoundMgr; - - smgr->bind(); - smgr->init(); - sgr = smgr->find("default", true); - smgr->set_volume(0.9); - smgr->activate(); - - SGPath srcDir(SRC_DIR); - - printf("default position and orientation\n"); - SGSoundSample *sample1 = new SGSoundSample("jet.wav", srcDir); - sample1->set_volume(1.0); - sample1->set_pitch(1.0); - sample1->play_looped(); - sgr->add(sample1, "sound1"); - smgr->update(1.0); - printf("playing sample\n"); - sleep(3); - sample1->stop(); - smgr->update(3.0); - sleep(1); - - printf("source at lat,lon = (10,-10), listener at (9.99,-9.99)\n"); - pos = SGGeod::fromDeg(9.99,-9.99); - sgr->set_position_geod( SGGeod::fromDeg(10,-10) ); - smgr->set_position( SGVec3d::fromGeod(pos), pos ); - sample1->play_looped(); - smgr->update(1.0); - printf("playing sample\n"); - sleep(3); - sample1->stop(); - smgr->update(3.0); - sleep(1); - - sgr->remove("sound1"); - smgr->unbind(); - sleep(2); - delete smgr; -} diff --git a/simgear/sound/sample.cxx b/simgear/sound/sample.cxx index a2734efa..f9a5b63a 100644 --- a/simgear/sound/sample.cxx +++ b/simgear/sound/sample.cxx @@ -35,8 +35,7 @@ #include #include -#include "soundmgr_openal_private.hxx" -#include "soundmgr_openal.hxx" +#include "soundmgr.hxx" #include "sample.hxx" #define AL_FALSE 0 diff --git a/simgear/sound/sample_group.cxx b/simgear/sound/sample_group.cxx index e45165a3..863cac1f 100644 --- a/simgear/sound/sample_group.cxx +++ b/simgear/sound/sample_group.cxx @@ -28,8 +28,7 @@ #include #include -#include "soundmgr_openal.hxx" -#include "soundmgr_openal_private.hxx" +#include "soundmgr.hxx" #include "sample_group.hxx" SGSampleGroup::SGSampleGroup () : diff --git a/simgear/sound/soundmgr.hxx b/simgear/sound/soundmgr.hxx new file mode 100644 index 00000000..18844564 --- /dev/null +++ b/simgear/sound/soundmgr.hxx @@ -0,0 +1,356 @@ +///@file +/// Sound effect management class +/// +/// Provides a sound manager class to keep track of multiple sounds and manage +/// playing them with different effects and timings. +// +// Sound manager initially written by David Findlay +// 2001 +// +// C++-ified by Curtis Olson, started March 2001. +// Modified for the new SoundSystem by Erik Hofman, October 2009 +// +// Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt +// Copyright (C) 2009 Erik Hofman +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. + +#ifndef _SG_SOUNDMGR_HXX +#define _SG_SOUNDMGR_HXX 1 + +#include +#include +#include +#include // for std::auto_ptr + +#include +#include +#include + +// forward decls +class SGSampleGroup; +class SGSoundSample; + +/** + * Manage a collection of SGSampleGroup instances + */ +class SGSoundMgr : public SGSubsystem +{ +public: + + SGSoundMgr(); + ~SGSoundMgr(); + + void init(); + void update(double dt); + + void suspend(); + void resume(); + void stop(); + + void reinit(); + + /** + * Select a specific sound device. + * Requires a init/reinit call before sound is actually switched. + */ + inline void select_device(const char* devname) {_device_name = devname;} + + /** + * Test is the sound manager is in a working condition. + * @return true is the sound manager is working + */ + bool is_working() const; + + /** + * Set the sound manager to a working condition. + */ + void activate(); + + /** + * Test is the sound manager is in an active and working condition. + * @return true is the sound manager is active + */ + inline bool is_active() const { return _active; } + + /** + * Register a sample group to the sound manager. + * @param sgrp Pointer to a sample group to add + * @param refname Reference name of the sample group + * @return true if successful, false otherwise + */ + bool add( SGSampleGroup *sgrp, const std::string& refname ); + + /** + * Remove a sample group from the sound manager. + * @param refname Reference name of the sample group to remove + * @return true if successful, false otherwise + */ + bool remove( const std::string& refname ); + + /** + * Test if a specified sample group is registered at the sound manager + * @param refname Reference name of the sample group test for + * @return true if the specified sample group exists + */ + bool exists( const std::string& refname ); + + /** + * Find a specified sample group in the sound manager + * + * @param refname Reference name of the sample group to find + * @param create If the group should be create if it does not exist + * @return A pointer to the SGSampleGroup + */ + SGSampleGroup *find( const std::string& refname, bool create = false ); + + /** + * Set the Cartesian position of the sound manager. + * + * @param pos OpenAL listener position + */ + void set_position( const SGVec3d& pos, const SGGeod& pos_geod ); + + /** + * Get the position of the sound manager. + * This is in the same coordinate system as OpenGL; y=up, z=back, x=right + * + * @return OpenAL listener position + */ + const SGVec3d& get_position() const; + + /** + * Set the velocity vector (in meters per second) of the sound manager + * This is the horizontal local frame; x=north, y=east, z=down + * + * @param vel Velocity vector + */ + void set_velocity( const SGVec3d& vel ) { + _velocity = vel; _changed = true; + } + + /** + * Get the velocity vector of the sound manager + * This is in the same coordinate system as OpenGL; y=up, z=back, x=right. + * + * @return Velocity vector of the OpenAL listener + */ + inline SGVec3f get_velocity() { return toVec3f(_velocity); } + + /** + * Set the orientation of the sound manager + * + * @param ori Quaternation containing the orientation information + */ + void set_orientation( const SGQuatd& ori ); + + /** + * Get the orientation of the sound manager + * + * @return Quaternation containing the orientation information + */ + const SGQuatd& get_orientation() const; + + /** + * Get the direction vector of the sound manager + * This is in the same coordinate system as OpenGL; y=up, z=back, x=right. + * + * @return Look-at direction of the OpenAL listener + */ + SGVec3f get_direction() const; + + enum { + NO_SOURCE = (unsigned int)-1, + NO_BUFFER = (unsigned int)-1, + FAILED_BUFFER = (unsigned int)-2 + }; + + /** + * Set the master volume. + * + * @param vol Volume (must be between 0.0 and 1.0) + */ + void set_volume( float vol ); + + /** + * Get the master volume. + * + * @return Volume (must be between 0.0 and 1.0) + */ + inline float get_volume() { return _volume; } + + /** + * Get a free OpenAL source-id + * + * @return NO_SOURCE if no source is available + */ + unsigned int request_source(); + + /** + * Free an OpenAL source-id for future use + * + * @param source OpenAL source-id to free + */ + void release_source( unsigned int source ); + + /** + * Get a free OpenAL buffer-id + * The buffer-id will be assigned to the sample by calling this function. + * + * @param sample Pointer to an audio sample to assign the buffer-id to + * @return NO_BUFFER if loading of the buffer failed. + */ + unsigned int request_buffer(SGSoundSample *sample); + + /** + * Free an OpenAL buffer-id for this sample + * + * @param sample Pointer to an audio sample for which to free the buffer + */ + void release_buffer( SGSoundSample *sample ); + + /** + * Initialize sample for playback. + * + * @param sample Pointer to an audio sample to initialize. + */ + void sample_init( SGSoundSample *sample ); + + /** + * Stop and destroy a sample + * + * @param sample Pointer to an audio sample to destroy. + */ + void sample_destroy( SGSoundSample *sample ); + + /** + * Start playback of a sample + * + * @param sample Pointer to an audio sample to start playing. + */ + void sample_play( SGSoundSample *sample ); + + /** + * Stop a sample + * + * @param sample Pointer to an audio sample to stop. + */ + void sample_stop( SGSoundSample *sample ); + + /** + * Suspend playback of a sample + * + * @param sample Pointer to an audio sample to suspend. + */ + void sample_suspend( SGSoundSample *sample ); + + /** + * Resume playback of a sample + * + * @param sample Pointer to an audio sample to resume. + */ + void sample_resume( SGSoundSample *sample ); + + /** + * Check if a sample is stopped, or still playing + * + * @param sample Pointer to an audio sample to test. + * @return true if the sample is stopped. + */ + bool is_sample_stopped( SGSoundSample *sample ); + + /** + * Update all status and 3d parameters of a sample. + * + * @param sample Pointer to an audio sample to update. + */ + void update_sample_config( SGSoundSample *sample, SGVec3d& position, SGVec3f& orientation, SGVec3f& velocity ); + + /** + * Test if the position of the sound manager has changed. + * The value will be set to false upon the next call to update_late() + * + * @return true if the position has changed + */ + inline bool has_changed() { return _changed; } + + /** + * Some implementations seem to need the velocity multiplied by a + * factor of 100 to make them distinct. I've not found if this is + * a problem in the implementation or in out code. Until then + * this function is used to detect the problematic implementations. + */ + inline bool bad_doppler_effect() { return _bad_doppler; } + + /** + * Load a sample file and return it's configuration and data. + * + * @param samplepath Path to the file to load + * @param data Pointer to a variable that points to the allocated data + * @param format Pointer to a vairable that gets the OpenAL format + * @param size Pointer to a vairable that gets the sample size in bytes + * @param freq Pointer to a vairable that gets the sample frequency in Herz + * @return true if succesful, false on error + */ + virtual bool load( const std::string &samplepath, + void **data, + int *format, + size_t *size, + int *freq, + int *block ); + + /** + * Get a list of available playback devices. + */ + std::vector get_available_devices(); + + /** + * Get the current OpenAL vendor or rendering backend. + */ + const std::string& get_vendor() { return _vendor; } + const std::string& get_renderer() { return _renderer; } + + bool testForError(std::string s, std::string name = "sound manager"); + + static const char* subsystemName() { return "sound"; }; +private: + class SoundManagerPrivate; + /// private implementation object + std::auto_ptr d; + + bool _block_support; + bool _active; + bool _changed; + float _volume; + + // Position of the listener. + SGGeod _geod_pos; + + // Velocity of the listener. + SGVec3d _velocity; + + bool _bad_doppler; + std::string _renderer; + std::string _vendor; + std::string _device_name; + + bool testForALCError(std::string s); + bool testForError(void *p, std::string s); + + void update_sample_config( SGSampleGroup *sound ); +}; + + +#endif // _SG_SOUNDMGR_HXX diff --git a/simgear/sound/soundmgr_aeonwave.cxx b/simgear/sound/soundmgr_aeonwave.cxx index 0ba5c54d..1ae801a5 100644 --- a/simgear/sound/soundmgr_aeonwave.cxx +++ b/simgear/sound/soundmgr_aeonwave.cxx @@ -1,4 +1,5 @@ -// soundmgr.cxx -- Sound effect management class +// soundmgr_aeonwave.cxx -- Sound effect management class +// for the AeonWave 3D and 4D audio rendering engine. // // Sound manager initially written by David Findlay // 2001 @@ -36,8 +37,9 @@ #include #include +#include -#include "soundmgr_aeonwave.hxx" +#include "soundmgr.hxx" #include "sample_group.hxx" #include @@ -60,7 +62,7 @@ typedef std::map < std::string, SGSharedPtr > sample_group_map; typedef sample_group_map::iterator sample_group_map_iterator; typedef sample_group_map::const_iterator const_sample_group_map_iterator; -#if 1 +#ifndef NDEBUG # define TRY(a) if ((a) == 0) printf("%i: %s\n", __LINE__, d->_aax.strerror()) #else # define TRY(a) (a) @@ -143,12 +145,12 @@ public: // constructor SGSoundMgr::SGSoundMgr() : - _renderer("unknown"), - _vendor("unknown"), _active(false), _changed(true), _volume(0.0), - _velocity(SGVec3d::zeros()) + _velocity(SGVec3d::zeros()), + _renderer("unknown"), + _vendor("unknown") { d.reset(new SoundManagerPrivate); d->_base_pos = SGVec3d::fromGeod(_geod_pos); @@ -765,3 +767,13 @@ SGVec3f SGSoundMgr::get_direction() const d->_mtx.get(pos, at, up); return SGVec3f( at ); } + +bool SGSoundMgr::load( const std::string &samplepath, + void **dbuf, + int *fmt, + size_t *sz, + int *frq, + int *block ) +{ + return true; +} diff --git a/simgear/sound/soundmgr_aeonwave.hxx b/simgear/sound/soundmgr_aeonwave.hxx deleted file mode 100644 index d9e70a0d..00000000 --- a/simgear/sound/soundmgr_aeonwave.hxx +++ /dev/null @@ -1,339 +0,0 @@ -///@file -/// Sound effect management class -/// -/// Provides a sound manager class to keep track of multiple sounds and manage -/// playing them with different effects and timings. -// -// Sound manager initially written by David Findlay -// 2001 -// -// C++-ified by Curtis Olson, started March 2001. -// Modified for the new SoundSystem by Erik Hofman, October 2009 -// -// Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt -// Copyright (C) 2016 Erik Hofman -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library General Public -// License as published by the Free Software Foundation; either -// version 2 of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Library General Public License for more details. -// -// You should have received a copy of the GNU Library General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. - -#ifndef _SG_SOUNDMGR_AEONWAVE_HXX -#define _SG_SOUNDMGR_AEONWAVE_HXX 1 - -#include -#include -#include -#include // for std::auto_ptr - -#include -#include - -#include -#include -#include - -// forward decls -class SGSampleGroup; -class SGSoundSample; - -/** - * Manage a collection of SGSampleGroup instances - */ -class SGSoundMgr : public SGSubsystem -{ -public: - - SGSoundMgr(); - ~SGSoundMgr(); - - void init(); - void update(double dt); - - void suspend(); - void resume(); - void stop(); - - void reinit(); - - /** - * Select a specific sound device. - * Requires a init/reinit call before sound is actually switched. - */ - inline void select_device(const char* devname) {_device_name = devname;} - - /** - * Test is the sound manager is in a working condition. - * @return true is the sound manager is working - */ - bool is_working() const; - - /** - * Set the sound manager to a working condition. - */ - void activate(); - - /** - * Test is the sound manager is in an active and working condition. - * @return true is the sound manager is active - */ - inline bool is_active() const { return _active; } - - /** - * Register a sample group to the sound manager. - * @param sgrp Pointer to a sample group to add - * @param refname Reference name of the sample group - * @return true if successful, false otherwise - */ - bool add( SGSampleGroup *sgrp, const std::string& refname ); - - /** - * Remove a sample group from the sound manager. - * @param refname Reference name of the sample group to remove - * @return true if successful, false otherwise - */ - bool remove( const std::string& refname ); - - /** - * Test if a specified sample group is registered at the sound manager - * @param refname Reference name of the sample group test for - * @return true if the specified sample group exists - */ - bool exists( const std::string& refname ); - - /** - * Find a specified sample group in the sound manager - * - * @param refname Reference name of the sample group to find - * @param create If the group should be create if it does not exist - * @return A pointer to the SGSampleGroup - */ - SGSampleGroup *find( const std::string& refname, bool create = false ); - - /** - * Set the Cartesian position of the sound manager. - * - * @param pos OpenAL listener position - */ - void set_position( const SGVec3d& pos, const SGGeod& pos_geod ); - - /** - * Get the position of the sound manager. - * This is in the same coordinate system as OpenGL; y=up, z=back, x=right - * - * @return OpenAL listener position - */ - const SGVec3d& get_position() const; - - /** - * Set the velocity vector (in meters per second) of the sound manager - * This is the horizontal local frame; x=north, y=east, z=down - * - * @param vel Velocity vector - */ - void set_velocity( const SGVec3d& vel ) { - _velocity = vel; _changed = true; - } - - /** - * Get the velocity vector of the sound manager - * This is in the same coordinate system as OpenGL; y=up, z=back, x=right. - * - * @return Velocity vector of the OpenAL listener - */ - inline SGVec3f get_velocity() { return toVec3f(_velocity); } - - /** - * Set the orientation of the sound manager - * - * @param ori Quaternation containing the orientation information - */ - void set_orientation( const SGQuatd& ori ); - - /** - * Get the orientation of the sound manager - * - * @return Quaternation containing the orientation information - */ - const SGQuatd& get_orientation() const; - - /** - * Get the direction vector of the sound manager - * This is in the same coordinate system as OpenGL; y=up, z=back, x=right. - * - * @return Look-at direction of the OpenAL listener - */ - SGVec3f get_direction() const; - - enum { - NO_SOURCE = (unsigned int)-1, - NO_BUFFER = (unsigned int)-1, - FAILED_BUFFER = (unsigned int)-2 - }; - - /** - * Set the master volume. - * - * @param vol Volume (must be between 0.0 and 1.0) - */ - void set_volume( float vol ); - - /** - * Get the master volume. - * - * @return Volume (must be between 0.0 and 1.0) - */ - inline float get_volume() { return _volume; } - - /** - * Get a free OpenAL source-id - * - * @return NO_SOURCE if no source is available - */ - unsigned int request_source(); - - /** - * Free an OpenAL source-id for future use - * - * @param source OpenAL source-id to free - */ - void release_source( unsigned int source ); - - /** - * Get a free OpenAL buffer-id - * The buffer-id will be assigned to the sample by calling this function. - * - * @param sample Pointer to an audio sample to assign the buffer-id to - * @return NO_BUFFER if loading of the buffer failed. - */ - unsigned int request_buffer(SGSoundSample *sample); - - /** - * Free an OpenAL buffer-id for this sample - * - * @param sample Pointer to an audio sample for which to free the buffer - */ - void release_buffer( SGSoundSample *sample ); - - /** - * Initialize sample for playback. - * - * @param sample Pointer to an audio sample to initialize. - */ - void sample_init( SGSoundSample *sample ); - - /** - * Stop and destroy a sample - * - * @param sample Pointer to an audio sample to destroy. - */ - void sample_destroy( SGSoundSample *sample ); - - /** - * Start playback of a sample - * - * @param sample Pointer to an audio sample to start playing. - */ - void sample_play( SGSoundSample *sample ); - - /** - * Stop a sample - * - * @param sample Pointer to an audio sample to stop. - */ - void sample_stop( SGSoundSample *sample ); - - /** - * Suspend playback of a sample - * - * @param sample Pointer to an audio sample to suspend. - */ - void sample_suspend( SGSoundSample *sample ); - - /** - * Resume playback of a sample - * - * @param sample Pointer to an audio sample to resume. - */ - void sample_resume( SGSoundSample *sample ); - - /** - * Check if a sample is stopped, or still playing - * - * @param sample Pointer to an audio sample to test. - * @return true if the sample is stopped. - */ - bool is_sample_stopped( SGSoundSample *sample ); - - /** - * Update all status and 3d parameters of a sample. - * - * @param sample Pointer to an audio sample to update. - */ - void update_sample_config( SGSoundSample *sample, SGVec3d& position, SGVec3f& orientation, SGVec3f& velocity ); - - /** - * Test if the position of the sound manager has changed. - * The value will be set to false upon the next call to update_late() - * - * @return true if the position has changed - */ - inline bool has_changed() { return _changed; } - - /** - * Some implementations seem to need the velocity multiplied by a - * factor of 100 to make them distinct. I've not found if this is - * a problem in the implementation or in out code. Until then - * this function is used to detect the problematic implementations. - */ - inline bool bad_doppler_effect() { return false; } - - /** - * Get a list of available playback devices. - */ - std::vector get_available_devices(); - - /** - * Get the current OpenAL vendor or rendering backend. - */ - const std::string& get_vendor() { return _vendor; } - const std::string& get_renderer() { return _renderer; } - - bool testForError(std::string s, std::string name = "sound manager"); - - static const char* subsystemName() { return "sound"; }; -private: - class SoundManagerPrivate; - /// private implementation object - std::auto_ptr d; - - std::string _renderer; - std::string _vendor; - std::string _device_name; - - bool _active; - bool _changed; - float _volume; - - // Position of the listener. - SGGeod _geod_pos; - - // Velocity of the listener. - SGVec3d _velocity; - - bool testForError(void *p, std::string s); - - void update_sample_config( SGSampleGroup *sound ); -}; - - -#endif // _SG_SOUNDMGR_AEONWAVE_HXX diff --git a/simgear/sound/soundmgr_openal.cxx b/simgear/sound/soundmgr_openal.cxx index 1db1cbc7..247d11a8 100644 --- a/simgear/sound/soundmgr_openal.cxx +++ b/simgear/sound/soundmgr_openal.cxx @@ -1,4 +1,4 @@ -// soundmgr.cxx -- Sound effect management class +// soundmgr_openal.cxx -- Sound effect management class for OpenAL // // Sound manager initially written by David Findlay // 2001 @@ -38,7 +38,7 @@ #include -#include "soundmgr_openal.hxx" +#include "soundmgr.hxx" #include "readwav.hxx" #include "soundmgr_openal_private.hxx" #include "sample_group.hxx" diff --git a/simgear/sound/soundmgr_openal.hxx b/simgear/sound/soundmgr_openal.hxx deleted file mode 100644 index afd2a854..00000000 --- a/simgear/sound/soundmgr_openal.hxx +++ /dev/null @@ -1,356 +0,0 @@ -///@file -/// Sound effect management class -/// -/// Provides a sound manager class to keep track of multiple sounds and manage -/// playing them with different effects and timings. -// -// Sound manager initially written by David Findlay -// 2001 -// -// C++-ified by Curtis Olson, started March 2001. -// Modified for the new SoundSystem by Erik Hofman, October 2009 -// -// Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt -// Copyright (C) 2009 Erik Hofman -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Library General Public -// License as published by the Free Software Foundation; either -// version 2 of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Library General Public License for more details. -// -// You should have received a copy of the GNU Library General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. - -#ifndef _SG_SOUNDMGR_OPENAL_HXX -#define _SG_SOUNDMGR_OPENAL_HXX 1 - -#include -#include -#include -#include // for std::auto_ptr - -#include -#include -#include - -// forward decls -class SGSampleGroup; -class SGSoundSample; - -/** - * Manage a collection of SGSampleGroup instances - */ -class SGSoundMgr : public SGSubsystem -{ -public: - - SGSoundMgr(); - ~SGSoundMgr(); - - void init(); - void update(double dt); - - void suspend(); - void resume(); - void stop(); - - void reinit(); - - /** - * Select a specific sound device. - * Requires a init/reinit call before sound is actually switched. - */ - inline void select_device(const char* devname) {_device_name = devname;} - - /** - * Test is the sound manager is in a working condition. - * @return true is the sound manager is working - */ - bool is_working() const; - - /** - * Set the sound manager to a working condition. - */ - void activate(); - - /** - * Test is the sound manager is in an active and working condition. - * @return true is the sound manager is active - */ - inline bool is_active() const { return _active; } - - /** - * Register a sample group to the sound manager. - * @param sgrp Pointer to a sample group to add - * @param refname Reference name of the sample group - * @return true if successful, false otherwise - */ - bool add( SGSampleGroup *sgrp, const std::string& refname ); - - /** - * Remove a sample group from the sound manager. - * @param refname Reference name of the sample group to remove - * @return true if successful, false otherwise - */ - bool remove( const std::string& refname ); - - /** - * Test if a specified sample group is registered at the sound manager - * @param refname Reference name of the sample group test for - * @return true if the specified sample group exists - */ - bool exists( const std::string& refname ); - - /** - * Find a specified sample group in the sound manager - * - * @param refname Reference name of the sample group to find - * @param create If the group should be create if it does not exist - * @return A pointer to the SGSampleGroup - */ - SGSampleGroup *find( const std::string& refname, bool create = false ); - - /** - * Set the Cartesian position of the sound manager. - * - * @param pos OpenAL listener position - */ - void set_position( const SGVec3d& pos, const SGGeod& pos_geod ); - - /** - * Get the position of the sound manager. - * This is in the same coordinate system as OpenGL; y=up, z=back, x=right - * - * @return OpenAL listener position - */ - const SGVec3d& get_position() const; - - /** - * Set the velocity vector (in meters per second) of the sound manager - * This is the horizontal local frame; x=north, y=east, z=down - * - * @param vel Velocity vector - */ - void set_velocity( const SGVec3d& vel ) { - _velocity = vel; _changed = true; - } - - /** - * Get the velocity vector of the sound manager - * This is in the same coordinate system as OpenGL; y=up, z=back, x=right. - * - * @return Velocity vector of the OpenAL listener - */ - inline SGVec3f get_velocity() { return toVec3f(_velocity); } - - /** - * Set the orientation of the sound manager - * - * @param ori Quaternation containing the orientation information - */ - void set_orientation( const SGQuatd& ori ); - - /** - * Get the orientation of the sound manager - * - * @return Quaternation containing the orientation information - */ - const SGQuatd& get_orientation() const; - - /** - * Get the direction vector of the sound manager - * This is in the same coordinate system as OpenGL; y=up, z=back, x=right. - * - * @return Look-at direction of the OpenAL listener - */ - SGVec3f get_direction() const; - - enum { - NO_SOURCE = (unsigned int)-1, - NO_BUFFER = (unsigned int)-1, - FAILED_BUFFER = (unsigned int)-2 - }; - - /** - * Set the master volume. - * - * @param vol Volume (must be between 0.0 and 1.0) - */ - void set_volume( float vol ); - - /** - * Get the master volume. - * - * @return Volume (must be between 0.0 and 1.0) - */ - inline float get_volume() { return _volume; } - - /** - * Get a free OpenAL source-id - * - * @return NO_SOURCE if no source is available - */ - unsigned int request_source(); - - /** - * Free an OpenAL source-id for future use - * - * @param source OpenAL source-id to free - */ - void release_source( unsigned int source ); - - /** - * Get a free OpenAL buffer-id - * The buffer-id will be assigned to the sample by calling this function. - * - * @param sample Pointer to an audio sample to assign the buffer-id to - * @return NO_BUFFER if loading of the buffer failed. - */ - unsigned int request_buffer(SGSoundSample *sample); - - /** - * Free an OpenAL buffer-id for this sample - * - * @param sample Pointer to an audio sample for which to free the buffer - */ - void release_buffer( SGSoundSample *sample ); - - /** - * Initialize sample for playback. - * - * @param sample Pointer to an audio sample to initialize. - */ - void sample_init( SGSoundSample *sample ); - - /** - * Stop and destroy a sample - * - * @param sample Pointer to an audio sample to destroy. - */ - void sample_destroy( SGSoundSample *sample ); - - /** - * Start playback of a sample - * - * @param sample Pointer to an audio sample to start playing. - */ - void sample_play( SGSoundSample *sample ); - - /** - * Stop a sample - * - * @param sample Pointer to an audio sample to stop. - */ - void sample_stop( SGSoundSample *sample ); - - /** - * Suspend playback of a sample - * - * @param sample Pointer to an audio sample to suspend. - */ - void sample_suspend( SGSoundSample *sample ); - - /** - * Resume playback of a sample - * - * @param sample Pointer to an audio sample to resume. - */ - void sample_resume( SGSoundSample *sample ); - - /** - * Check if a sample is stopped, or still playing - * - * @param sample Pointer to an audio sample to test. - * @return true if the sample is stopped. - */ - bool is_sample_stopped( SGSoundSample *sample ); - - /** - * Update all status and 3d parameters of a sample. - * - * @param sample Pointer to an audio sample to update. - */ - void update_sample_config( SGSoundSample *sample, SGVec3d& position, SGVec3f& orientation, SGVec3f& velocity ); - - /** - * Test if the position of the sound manager has changed. - * The value will be set to false upon the next call to update_late() - * - * @return true if the position has changed - */ - inline bool has_changed() { return _changed; } - - /** - * Some implementations seem to need the velocity multiplied by a - * factor of 100 to make them distinct. I've not found if this is - * a problem in the implementation or in out code. Until then - * this function is used to detect the problematic implementations. - */ - inline bool bad_doppler_effect() { return _bad_doppler; } - - /** - * Load a sample file and return it's configuration and data. - * - * @param samplepath Path to the file to load - * @param data Pointer to a variable that points to the allocated data - * @param format Pointer to a vairable that gets the OpenAL format - * @param size Pointer to a vairable that gets the sample size in bytes - * @param freq Pointer to a vairable that gets the sample frequency in Herz - * @return true if succesful, false on error - */ - bool load( const std::string &samplepath, - void **data, - int *format, - size_t *size, - int *freq, - int *block ); - - /** - * Get a list of available playback devices. - */ - std::vector get_available_devices(); - - /** - * Get the current OpenAL vendor or rendering backend. - */ - const std::string& get_vendor() { return _vendor; } - const std::string& get_renderer() { return _renderer; } - - bool testForError(std::string s, std::string name = "sound manager"); - - static const char* subsystemName() { return "sound"; }; -private: - class SoundManagerPrivate; - /// private implementation object - std::auto_ptr d; - - bool _block_support; - bool _active; - bool _changed; - float _volume; - - // Position of the listener. - SGGeod _geod_pos; - - // Velocity of the listener. - SGVec3d _velocity; - - bool _bad_doppler; - std::string _renderer; - std::string _vendor; - std::string _device_name; - - bool testForALCError(std::string s); - bool testForError(void *p, std::string s); - - void update_sample_config( SGSampleGroup *sound ); -}; - - -#endif // _SG_SOUNDMGR_OPENAL_HXX diff --git a/simgear/sound/soundmgr_test.cxx b/simgear/sound/soundmgr_test.cxx new file mode 100644 index 00000000..f0234142 --- /dev/null +++ b/simgear/sound/soundmgr_test.cxx @@ -0,0 +1,105 @@ +#include +#ifdef _WIN32 +#include +#define sleep(x) Sleep(x*1000) +#else +#include +#endif + +#include +#include +#include + +#include "soundmgr.hxx" +#include "sample_group.hxx" +#include "sample.hxx" + + +int main( int argc, char *argv[] ) { + SGSampleGroup *sgr; + SGSoundMgr *smgr; + + smgr = new SGSoundMgr; + + smgr->bind(); + smgr->init(); + sgr = smgr->find("default", true); + smgr->set_volume(0.9); + smgr->activate(); + + SGPath srcDir(SRC_DIR); + + SGSoundSample *sample1 = new SGSoundSample("jet.wav", srcDir); + sample1->set_volume(1.0); + sample1->set_pitch(1.0); + sample1->play_looped(); + sgr->add(sample1, "sound1"); + smgr->update(1.0); + printf("playing sample1\n"); + sleep(1); + + SGSoundSample *sample2 = new SGSoundSample("jet_ulaw.wav", srcDir); + sample2->set_volume(0.5); + sample2->set_pitch(0.4); + sample2->play_looped(); + sgr->add(sample2, "sound2"); + smgr->update(1.0); + printf("playing sample2\n"); + sleep(1); + + SGSoundSample *sample3 = new SGSoundSample("jet_ima4.wav", srcDir); + sample3->set_volume(0.5); + sample3->set_pitch(0.8); + sample3->play_looped(); + sgr->add(sample3, "sound3"); + smgr->update(1.0); + printf("playing sample3\n"); + sleep(1); + + SGSoundSample *sample4 = new SGSoundSample("jet.wav", srcDir); + sample4->set_volume(0.5); + sample4->set_pitch(1.2); + sample4->play_looped(); + sgr->add(sample4, "sound4"); + smgr->update(1.0); + printf("playing sample4\n"); + sleep(1); + + SGSoundSample *sample5 = new SGSoundSample("jet.wav", srcDir); + sample5->set_volume(0.5); + sample5->set_pitch(1.6); + sample5->play_looped(); + sgr->add(sample5, "sound5"); + smgr->update(1.0); + printf("playing sample5\n"); + sleep(1); + + SGSoundSample *sample6 = new SGSoundSample("jet.wav", srcDir); + sample6->set_volume(0.5); + sample6->set_pitch(2.0); + sample6->play_looped(); + sgr->add(sample6, "sound6"); + smgr->update(1.0); + printf("playing sample6\n"); + sleep(1); + + for (int i=0; i<10; i++) { + sleep(1); + smgr->update(1); + } + + sgr->stop("sound1"); + sgr->stop("sound2"); + sgr->stop("sound3"); + SGTimeStamp::sleepForMSec(500); + smgr->update(0.5); + sgr->stop("sound4"); + sgr->stop("sound5"); + sgr->stop("sound6"); + smgr->update(1); + sleep(1); + + smgr->unbind(); + sleep(2); + delete smgr; +} diff --git a/simgear/sound/soundmgr_test2.cxx b/simgear/sound/soundmgr_test2.cxx new file mode 100644 index 00000000..09cec705 --- /dev/null +++ b/simgear/sound/soundmgr_test2.cxx @@ -0,0 +1,60 @@ +#include +#ifdef _WIN32 +#include +#define sleep(x) Sleep(x*1000) +#else +#include +#endif + +#include +#include + +#include "soundmgr.hxx" +#include "sample_group.hxx" +#include "sample.hxx" + +int main( int argc, char *argv[] ) { + SGSampleGroup *sgr; + SGSoundMgr *smgr; + SGGeod pos; + + smgr = new SGSoundMgr; + + smgr->bind(); + smgr->init(); + sgr = smgr->find("default", true); + smgr->set_volume(0.9); + smgr->activate(); + + SGPath srcDir(SRC_DIR); + + printf("default position and orientation\n"); + SGSoundSample *sample1 = new SGSoundSample("jet.wav", srcDir); + sample1->set_volume(1.0); + sample1->set_pitch(1.0); + sample1->play_looped(); + sgr->add(sample1, "sound1"); + smgr->update(1.0); + printf("playing sample\n"); + sleep(3); + sample1->stop(); + smgr->update(3.0); + sleep(1); + + printf("source at lat,lon = (10,-10), listener at (9.99,-9.99)\n"); + pos = SGGeod::fromDeg(9.99,-9.99); + sgr->set_position_geod( SGGeod::fromDeg(10,-10) ); + smgr->set_position( SGVec3d::fromGeod(pos), pos ); + sample1->play_looped(); + smgr->update(1.0); + printf("playing sample\n"); + sleep(3); + sample1->stop(); + smgr->update(3.0); + sleep(1); + + sgr->remove("sound1"); + smgr->unbind(); + sleep(2); + delete smgr; +}