osgAL



  • Kann jemand mir mal sagen wo der Fehler ist? Das Programm läuft manchmal rechtig, manchmal falsch... Beim Laden des 2. WAV files tritt ab und zu eine Fehler meldung "SoundManager::~SoundManager(): SoundManager::shutdown() should be called for the SoundManager before the deconstructor is called"

    #include "osgAL/SoundNode"
    #include "osgAL/SoundRoot"
    #include "osgAL/SoundManager"
    #include "osgAL/SoundState"
    #include "osgAL/OccludeCallback"
    
    #include <osg/Notify>
    #include <osg/MatrixTransform>
    #include <osg/PositionAttitudeTransform>
    #include <osg/Geometry>
    #include <osg/Geode>
    
    #include <osgUtil/Optimizer>
    
    #include <osgDB/Registry>
    #include <osgDB/ReadFile>
    
    #include <osgGA/TrackballManipulator>
    #include <osgGA/FlightManipulator>
    #include <osgGA/DriveManipulator>
    
    #include <osg/ShapeDrawable>
    
    #include <osgProducer/Viewer>
    #include "osgAL/Version"
    
    osg::PositionAttitudeTransform *createSoundNode(const std::string& file, bool occlude, osg::Node *root, bool is_stream,osgProducer::Viewer* viewer);
    
    class KeyboardHandler: public osgGA::GUIEventHandler {
    	public:
    		KeyboardHandler(osgAL::SoundState *state): m_sound_state(state) {}
    
    		bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &)
    		{
    			if (ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN) {
    
    				if (ea.getKey() == 'e') {
    
    				/*	// Now push the soundstate to the queue so it will be played.
    					// Set any of the parameters that will be activated when it 
    					// is head of the queue when the sound manager processes the soundstates later on.
    					// Get the current position of the listener
    					float x,y,z;
    					osgAL::SoundManager::instance()->getListener()->getPosition(x,y,z);
    					// Place this sound state at the position of the listener
    					m_sound_state->setPosition(osg::Vec3(x,y,z));*/
    
    					m_sound_state->setPlay(true); // It will play!		
    					m_sound_state->setLooping(true);
    					/*
    					for( unsigned int i = 0; i < 20; ++i ){
    						m_sound_state->setPitch( 1.0 / i );
    						Sleep( 300 );}
    					*/
    					// Now push the event to the sound manager so it can play it whenever this
    					// event reaches the top of the queue. A higher priority will move it to the top
    					// faster!
    					//	int priority = 10;
    					//	osgAL::SoundManager::instance()->pushSoundEvent(m_sound_state.get(), priority);
    					return true;
    				}
    				if (ea.getKey() == 'a') {					
    					m_sound_state->setPlay(false); // It will play!
    					m_sound_state->setLooping(true);
    					return true;
    				}
    			}
    			return false;
    		}
    
    	private:
       osg::ref_ptr<osgAL::SoundState> m_sound_state;
    	};
    
    osg::ref_ptr<osgAL::SoundNode> createSound(const std::string& file, const osg::Vec3 v,osgProducer::Viewer *viewer,float n)
    {
    	osg::ref_ptr<osgAL::SoundNode> sound = new osgAL::SoundNode;
    
    	bool add_to_cache = false;
    	//osgAL::SoundManager::instance()->
    	openalpp::ref_ptr<openalpp::Sample> sample = osgAL::SoundManager::instance()->getSample(file.c_str(), add_to_cache);
    	std::cerr << "Loading sample: " << file << std::endl;
    	// Create a sample, load a .wav file.
    	//openalpp::Sample *sample = new openalpp::Sample(file.c_str());
    
    	std::cerr << "0"<< std::endl;
    	// Create a named sound state.
    	osg::ref_ptr<osgAL::SoundState> sound_state = new osgAL::SoundState(file);
    
    	std::cerr << "1"<< std::endl;
    
    	// Create a KeyboardHandler
    	viewer->getEventHandlerList().push_front(new KeyboardHandler(sound_state.get()));
    
    		std::cerr << "2" << std::endl;
    
    	// Let the soundstate use the sample we just created
    		sound_state->setSample(sample.get());
    
    		std::cerr << "3"<< std::endl;
    
        sound_state->setReferenceDistance(60);
        sound_state->setRolloffFactor(3);
    	// Set its gain (volume) to 0.9
    	sound_state->setGain(0.9f);
    	// Set its pitch to 1 (normal speed)
    	sound_state->setPitch(1);
    	// Make it play
    	sound_state->setPlay(true);
    	// The sound should loop over and over again
    	sound_state->setLooping(true);
    
    	// Allocate a hardware soundsource to this soundstate (priority 10)
    	//
    	sound_state->allocateSource(10, false);
    	// set the Position for every model.
    	if(n==0){
    		sound_state->setPosition(v);
    	}else{
    		sound_state->setPosition(v);
    	}
    	// At 40 the gain will be half of full!
    	sound_state->setReferenceDistance(100);
    	sound_state->setRolloffFactor(4);
    	sound_state->apply();
    
    	// Add the soundstate to the sound manager, so we can find it later on if we want to
    	osgAL::SoundManager::instance()->addSoundState(sound_state.get());
    
    	sound->setSoundState(sound_state.get());
      return sound;
    }
    
    osg::MatrixTransform* createModel(const std::string& file, float x,float y, float z)
    {
    //	osg::Group* model = new osg::Group;
    	osg::MatrixTransform* positioned = new osg::MatrixTransform;
    	osg::Node* flugzeug = osgDB::readNodeFile(file);
    	    if (flugzeug){
    
    			positioned->setDataVariance(osg::Object::STATIC);
    			positioned->setMatrix(osg::Matrix::translate(x,y,z));
    			positioned->addChild(flugzeug);
    //			model->addChild(positioned);
    		}
        return positioned;
    }
    
    int main( int argc, char **argv )
    {
    	osg::Group* rootnode = new osg::Group;
    	openalpp::ref_ptr<openalpp::Sample> sample;  
    	std::cerr << "\n\n" << osgAL::getLibraryName() << " demo" << std::endl;
    	std::cerr << "Version: " << osgAL::getVersion() << "\n\n" << std::endl;
    
    	std::cerr << "Demonstrates occluders" << std::endl;
    
    	try {
    		// use an ArgumentParser object to manage the program arguments.
    		osg::ArgumentParser arguments(&argc,argv);
    
    		// set up the usage document, in case we need to print out how to use this program.
    		arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" demonstrates the use of the OsgAL toolkit for spatial sound.");
    		arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
    		arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
    
    		// initialize the viewer.
    		osgProducer::Viewer viewer(arguments);
    		// set up the value with sensible default event handlers.
    		viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
    		// get details on keyboard and mouse bindings used by the viewer.
    		viewer.getUsage(*arguments.getApplicationUsage());
    		// if user request help write it out to cout.
    		if (arguments.read("-h") || arguments.read("--help"))
    		{
    			arguments.getApplicationUsage()->write(std::cout);
    			return 1;
    		}
    			// any option left unread are converted into errors to write out later.
    			arguments.reportRemainingOptionsAsUnrecognized();
    			arguments.getApplicationUsage()->addKeyboardMouseBinding("RETURN", "Play a sound");
    		// report any errors if they have occured when parsing the program aguments.
    		if (arguments.errors())
    		{
    			arguments.writeErrorMessages(std::cout);
    			return 1;
    		}
    
    		osg::MatrixTransform* model_1 = createModel("A310_DLH.ive",500.0f,0.0f,0.0f);
    		if (!model_1)
    		{
    		osg::notify(osg::FATAL) << "Error loading model_1 from commandline" << std::endl;
    		return 1;
    		}
    		osg::MatrixTransform* model_2 = createModel("A310_DLH.ive",0.0f,0.0f,0.0f);
    			if (!model_2)
    		{
    		osg::notify(osg::FATAL) << "Error loading model_2 from commandline" << std::endl;
    		return 1;
    		}
    		rootnode->addChild(model_1);
    		rootnode->addChild(model_2);
    
    	/*SoundManager
    	The SoundManager is responsible for handling queued SoundStates (for sound events described below)
    	and to store all SoundStates to make it possible to find them later on.
    	*/
    		osgAL::SoundManager::instance()->init(16);
    		osgAL::SoundManager::instance()->getEnvironment()->setDistanceModel(openalpp::InverseDistance);
    		osgAL::SoundManager::instance()->getEnvironment()->setDopplerFactor(1);
    
    	/*SoundRoot
    	A node that during cull traversal updates the SoundManager and updates the transformation 
    	for the current listener from the current modelView matrix. This could be done manually, 
    	but this node makes it all happen automatically during the cull traversal, 
    	which reduces the need of inserting code in the viewer run loop.
    	*/
    
    		osg::ref_ptr<osgAL::SoundRoot> sound_root = new osgAL::SoundRoot;   
    		rootnode->addChild(sound_root.get());
    		// Create a sound node and attach the soundstate to it.
    		osg::ref_ptr<osgAL::SoundNode> sound = new osgAL::SoundNode;
    
    		std::vector<std::string> wave_vector;
    		wave_vector.push_back("737LP.wav");
    		wave_vector.push_back("bee.wav");
    		std::string file = wave_vector[0];
    		const osg::Vec3 v1 = osg::Vec3(500.0f,0.0f,0.0f);
    		//create a soundnode
    		model_1->addChild( createSound(file,v1,&viewer,0).get() );
    
    		std::string file2 = wave_vector[1];
    		const osg::Vec3 v2 = osg::Vec3(0.0f,0.0f,0.0f);
    		//create a soundnode
    		model_2->addChild( createSound(file2,v2,&viewer,0).get() );
    
    		osg::Timer_t curr, start = osg::Timer::instance()->tick();
    		double interval = 2; // 2 seconds interval
    		unsigned int n = 0;
    
    		// run optimization over the scene graph
    		osgUtil::Optimizer optimizer;
    		optimizer.optimize(rootnode);
    
    		// set the scene to render
    		viewer.setSceneData(rootnode);
    
    		// create the windows and run the threads.
    		viewer.realize();
    
    	while( !viewer.done() )
        {
    	   // wait for all cull and draw threads to complete.
    	    viewer.sync();
    
    	   // update the scene by traversing it with the the update visitor which will
    	   // call all node update callbacks and animations.
    		viewer.update();
    
    		viewer.frame();
    
    		viewer.sync();
    
    	}
    }
    	catch (std::exception& e) {
    		std::cerr << "Caught: " << e.what() << std::endl;
    	}
    
    	osgAL::SoundManager::instance()->shutdown();
    		return 0;
    }
    

Log in to reply