00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00027
00029 #ifndef __X3DSOUNDSOURCENODE_H__
00030 #define __X3DSOUNDSOURCENODE_H__
00031
00032 #include "X3DTimeDependentNode.h"
00033 #include "X3DSoundNode.h"
00034 #include "PeriodicUpdate.h"
00035 #include "H3DSoundStreamNode.h"
00036 #include <list>
00037 #include <fstream>
00038
00039 #ifdef HAVE_OPENAL
00040 #ifdef WIN32
00041 #include <al.h>
00042 #include <alc.h>
00043 #ifdef _MSC_VER
00044 #pragma comment( lib, "OpenAL32.lib" )
00045 #endif
00046 #elif defined( MACOSX )
00047 #include <OpenAL/al.h>
00048 #include <OpenAL/alc.h>
00049 #else
00050 #include <AL/al.h>
00051 #include <AL/alc.h>
00052 #endif
00053 #endif
00054
00055 #define NR_STREAM_BUFFERS 3
00056 #define STREAM_BUFFER_SIZE (4096*8)
00057
00058 namespace H3D {
00059
00067 class H3DAPI_API X3DSoundSourceNode : public X3DTimeDependentNode {
00068 public:
00071 class H3DAPI_API TimeHandler: public X3DTimeDependentNode::TimeHandler {
00072 public:
00074 TimeHandler() {}
00075 protected:
00076 virtual void update();
00077 };
00078
00081 class H3DAPI_API ALSoundBuffer:
00082 public PeriodicUpdate< Field > {
00083 protected:
00084 virtual void update() {
00085 X3DSoundSourceNode *s =
00086 static_cast< X3DSoundSourceNode * >( getOwner());
00087 s->ALrender();
00088 event_fields.clear();
00089 }
00090
00092 virtual void propagateEvent( Event e ) {
00093 Field::propagateEvent( e );
00094 event_fields.insert( e.ptr );
00095 }
00096 public:
00097
00100 inline bool hasCausedEvent( Field *f ) {
00101 return event_fields.find( f ) != event_fields.end();
00102 }
00103
00106 template< class FieldType >
00107 inline bool hasCausedEvent( auto_ptr< FieldType > &f ) {
00108 return hasCausedEvent( f.get() );
00109 }
00110
00111 protected:
00114 set< Field * > event_fields;
00115 };
00116
00117
00119 X3DSoundSourceNode( Inst< SFNode > _metadata = 0,
00120 Inst< SFString > _description = 0,
00121 Inst< SFBool > _loop = 0,
00122 Inst< SFTime > _pauseTime = 0,
00123 Inst< SFFloat > _pitch = 0,
00124 Inst< SFTime > _resumeTime = 0,
00125 Inst< StartTime > _startTime = 0,
00126 Inst< StopTime > _stopTime = 0,
00127 Inst< SFTime > _duration_changed = 0,
00128 Inst< SFTime > _elapsedTime = 0,
00129 Inst< SFBool > _isActive = 0,
00130 Inst< SFBool > _isPaused = 0,
00131 Inst< TimeHandler > _timeHandler = 0 );
00132
00136 virtual void initALBuffers( bool stream );
00137
00139 virtual void ALrender();
00140
00143 virtual string defaultXMLContainerField() {
00144 return "source";
00145 }
00146
00150 virtual void registerSoundNode( X3DSoundNode *n );
00151
00153 virtual void unregisterSoundNode( X3DSoundNode *n );
00154
00156 virtual void onPause();
00157
00159 virtual void onResume();
00160
00162 virtual void onStart();
00163
00165 virtual void onStop();
00166
00173 auto_ptr< SFString > description;
00174
00183 auto_ptr< SFFloat > pitch;
00184
00191 auto_ptr< SFTime > duration_changed;
00192
00194 static H3DNodeDatabase database;
00195 protected:
00197 list< X3DSoundNode * > parent_sound_nodes;
00198
00199 #ifdef HAVE_OPENAL
00200
00201 ALuint al_buffers[NR_STREAM_BUFFERS];
00202
00204 ALenum al_format;
00205 #endif
00206
00208 AutoRef< H3DSoundStreamNode > reader;
00209
00211 bool sound_as_stream;
00212
00215 auto_ptr< ALSoundBuffer > sound_buffer;
00216 };
00217 }
00218
00219 #endif