H3D API  2.4.1
X3DSoundSourceNode.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 #ifndef __X3DSOUNDSOURCENODE_H__
30 #define __X3DSOUNDSOURCENODE_H__
31 
33 #include <H3D/X3DSoundNode.h>
34 #include <H3D/PeriodicUpdate.h>
35 #include <H3D/H3DSoundStreamNode.h>
36 #include <list>
37 #include <fstream>
38 
39 #ifdef HAVE_OPENAL
40 #if defined( MACOSX )
41 #include <OpenAL/al.h>
42 #include <OpenAL/alc.h>
43 #else
44 #include <AL/al.h>
45 #include <AL/alc.h>
46 #endif
47 #endif
48 
49 #define NR_STREAM_BUFFERS 3
50 #define STREAM_BUFFER_SIZE (4096*8)
51 
52 namespace H3D {
53 
61  class H3DAPI_API X3DSoundSourceNode : public X3DTimeDependentNode {
62  public:
66  public:
69  protected:
70  virtual void update();
71  };
72 #ifdef __BORLANDC__
73  friend class TimeHandler;
74 #endif
75 
78  class H3DAPI_API ALSoundBuffer:
79  public PeriodicUpdate< Field > {
80  protected:
81  virtual void update() {
82  X3DSoundSourceNode *s =
83  static_cast< X3DSoundSourceNode * >( getOwner());
84  s->ALrender();
85  event_fields.clear();
86  }
87 
89  virtual void propagateEvent( Event e ) {
91  event_fields.insert( e.ptr );
92  }
93  public:
94 
97  inline bool hasCausedEvent( Field *f ) {
98  return event_fields.find( f ) != event_fields.end();
99  }
100 
103  template< class FieldType >
104  inline bool hasCausedEvent( H3DUniquePtr< FieldType > &f ) {
105  return hasCausedEvent( f.get() );
106  }
107 
108  protected:
111  set< Field * > event_fields;
112  };
113 
114 
116  X3DSoundSourceNode( Inst< SFNode > _metadata = 0,
117  Inst< SFString > _description = 0,
118  Inst< SFBool > _loop = 0,
119  Inst< SFTime > _pauseTime = 0,
120  Inst< SFFloat > _pitch = 0,
121  Inst< SFTime > _resumeTime = 0,
122  Inst< StartTime > _startTime = 0,
123  Inst< StopTime > _stopTime = 0,
124  Inst< SFTime > _duration_changed = 0,
125  Inst< SFTime > _elapsedTime = 0,
126  Inst< SFBool > _isActive = 0,
127  Inst< SFBool > _isPaused = 0,
128  Inst< TimeHandler > _timeHandler = 0 );
129 
130  virtual ~X3DSoundSourceNode();
131 
135  virtual bool initALBuffers( bool stream );
136 
138  virtual void ALrender();
139 
142  virtual string defaultXMLContainerField() {
143  return "source";
144  }
145 
149  virtual void registerSoundNode( X3DSoundNode *n );
150 
152  virtual void unregisterSoundNode( X3DSoundNode *n );
153 
155  virtual void onPause();
156 
158  virtual void onResume();
159 
161  virtual void onStart();
162 
164  virtual void onStop();
165 
172  H3DUniquePtr< SFString > description;
173 
182  H3DUniquePtr< SFFloat > pitch;
183 
190  H3DUniquePtr< SFTime > duration_changed;
191 
194  protected:
196  list< X3DSoundNode * > parent_sound_nodes;
197 
198 #ifdef HAVE_OPENAL
200  ALuint al_buffers[NR_STREAM_BUFFERS];
201 
203  ALenum al_format;
204 #endif
205 
208 
211 
214  H3DUniquePtr< ALSoundBuffer > soundBuffer;
215  };
216 }
217 
218 #endif
Header file for H3DSoundStreamNode, X3D scene-graph node.
Header file for PeriodicUpdate, template field modifier.
Header file for X3DSoundNode, X3D scene-graph node.
Header file for X3DTimeDependentNode, X3D scene-graph node.
The Field class.
Definition: Field.h:46
virtual void propagateEvent(Event e)
Propagates an event to all Fields we are routed to.
Definition: Field.cpp:367
The PeriodicUpdate is a template modifier that changes the way the field is updated.
Definition: PeriodicUpdate.h:74
This abstract node type is the base for all Sound nodes.
Definition: X3DSoundNode.h:48
Field that calls ALrender() when a field routed to it has generated an event.
Definition: X3DSoundSourceNode.h:79
bool hasCausedEvent(H3DUniquePtr< FieldType > &f)
Returns true if the Field given has generated an event to this field since the last call to the updat...
Definition: X3DSoundSourceNode.h:104
set< Field * > event_fields
The fields that has generated an event since the last call to update()
Definition: X3DSoundSourceNode.h:111
virtual void propagateEvent(Event e)
When the event is propagated the display list is invalidated.
Definition: X3DSoundSourceNode.h:89
bool hasCausedEvent(Field *f)
Returns true if the Field given has generated an event to this field since the last call to the updat...
Definition: X3DSoundSourceNode.h:97
virtual void update()
This function will be called to update the field if it has a pending event when the upToDate() functi...
Definition: X3DSoundSourceNode.h:81
TimeHandler is extended to stream audio data to OpenAL buffers if the sound source is a streaming sou...
Definition: X3DSoundSourceNode.h:65
TimeHandler()
Constructor.
Definition: X3DSoundSourceNode.h:68
This abstract node type is used to derive node types that can emit audio data.
Definition: X3DSoundSourceNode.h:61
H3DUniquePtr< ALSoundBuffer > soundBuffer
Field that calls ALrender() when a field routed to it has generated an event.
Definition: X3DSoundSourceNode.h:214
AutoRef< H3DSoundStreamNode > reader
The H3DSoundStreamNode used to read sound data.
Definition: X3DSoundSourceNode.h:207
virtual void ALrender()
Perform the OpenAL calls to render the sound.
Definition: X3DSoundSourceNode.cpp:302
bool sound_as_stream
Indicates if the current sounds source is being streamed or not.
Definition: X3DSoundSourceNode.h:210
list< X3DSoundNode * > parent_sound_nodes
The X3DSoundNodes that use this node as sound source.
Definition: X3DSoundSourceNode.h:196
H3DUniquePtr< SFTime > duration_changed
The duration field is the length of time in seconds for one cycle of the audio for a pitch set to 1....
Definition: X3DSoundSourceNode.h:190
ALenum al_format
The OpenAL format of the current sound source.
Definition: X3DSoundSourceNode.h:203
static H3DNodeDatabase database
The H3DNodeDatabase for this node.
Definition: X3DSoundSourceNode.h:193
H3DUniquePtr< SFString > description
The description field specifies a textual description of the audio source.
Definition: X3DSoundSourceNode.h:172
virtual string defaultXMLContainerField()
Returns the default xml containerField attribute value.
Definition: X3DSoundSourceNode.h:142
H3DUniquePtr< SFFloat > pitch
The pitch field specifies a multiplier for the rate at which sampled sound is played.
Definition: X3DSoundSourceNode.h:182
The TimeHandler field updates once per scene-graph loop to monitor the current time and updates the f...
Definition: X3DTimeDependentNode.h:50
This abstract node type is the base node type from which all time-dependent nodes are derived.
Definition: X3DTimeDependentNode.h:41
H3D API namespace.
Definition: Anchor.h:38
The Event struct encapsulates an event in the field network.
Definition: Field.h:75
Field * ptr
The Field that caused the event.
Definition: Field.h:84
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194