aboutsummaryrefslogtreecommitdiff
path: root/cvs/cvsweb.cgi/code/nel/samples/sound_sources/main.cpp?rev=1.6&content-type=text/x-cvsweb-markup&sortby=rev/index.html
blob: c42bc1bcd009c9356d2f2249eb8b2b20f2442c4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>
<HEAD><style>		A { color:black }</style>
<!-- hennerik CVSweb $Revision: 1.93 $ -->
<TITLE>code/nel/samples/sound_sources/main.cpp - view - 1.6</TITLE></HEAD>
<BODY BGCOLOR="#eeeeee">
<table width="100%" border=0 cellspacing=0 cellpadding=1 bgcolor="#aaaaaa"><tr valign=bottom><td><a href="main.cpp?sortby=rev"><IMG SRC="/inc/img/picto-up.gif" ALT="[BACK]" BORDER="0" WIDTH="14" HEIGHT="13"></a> <b>Return to <A HREF="main.cpp?sortby=rev">main.cpp</A>
 CVS log</b> <IMG SRC="/inc/img/picto-news.gif" ALT="[TXT]" BORDER="0" WIDTH="13" HEIGHT="15"></td><td align=right><IMG SRC="/inc/img/picto-dir.gif" ALT="[DIR]" BORDER="0" WIDTH="15" HEIGHT="13"> <b>Up to  <a href="/cvs/cvsweb.cgi/?sortby=rev">Nevrax</a> / <a href="/cvs/cvsweb.cgi/code/?sortby=rev">code</a> / <a href="/cvs/cvsweb.cgi/code/nel/?sortby=rev">nel</a> / <a href="/cvs/cvsweb.cgi/code/nel/samples/?sortby=rev">samples</a> / <a href="/cvs/cvsweb.cgi/code/nel/samples/sound_sources/?sortby=rev">sound_sources</a></b></td></tr></table><HR noshade><table width="100%"><tr><td bgcolor="#ffffff">File:  <a href="/cvs/cvsweb.cgi/?sortby=rev">Nevrax</a> / <a href="/cvs/cvsweb.cgi/code/?sortby=rev">code</a> / <a href="/cvs/cvsweb.cgi/code/nel/?sortby=rev">nel</a> / <a href="/cvs/cvsweb.cgi/code/nel/samples/?sortby=rev">samples</a> / <a href="/cvs/cvsweb.cgi/code/nel/samples/sound_sources/?sortby=rev">sound_sources</a> / <a href="/cvs/cvsweb.cgi/code/nel/samples/sound_sources/main.cpp?sortby=rev">main.cpp</a>&nbsp;(<A HREF="/cvs/cvsweb.cgi/~checkout~/code/nel/samples/sound_sources/main.cpp?rev=1.6&amp;sortby=rev" target="cvs_checkout" onClick="window.open('/cvs/cvsweb.cgi/~checkout~/code/nel/samples/sound_sources/main.cpp?rev=1.6','cvs_checkout','resizeable,scrollbars');"><b>download</b></A>)<BR>
Revision <B>1.6</B>, <i>Wed Feb 20 18:07:22 2002 UTC</i> (5 months ago) by <i>lecroart</i>
<BR>Branch: <b>MAIN</b>
<BR>CVS Tags: <b>georges_v2, HEAD</b><BR>Changes since <b>1.5: +2 -2
 lines</b><PRE>
#FIXED: doxygen warning
</PRE>
</td></tr></table><HR noshade><PRE>/** \file sound_sources/main.cpp
 * Simple example of NeL sound engine usage
 *
 * $Id: main.cpp,v 1.6 2002/02/20 18:07:22 lecroart Exp $
 */

/* Copyright, 2001 Nevrax Ltd.
 *
 * This file is part of NEVRAX NEL.
 * NEVRAX NEL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.

 * NEVRAX NEL 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
 * General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with NEVRAX NEL; see the file COPYING. If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */


/*
 * This sample shows how to initialize the audio mixer (UAudioMixer),
 * how to create a source (USource), and how to move the listener
 * (UListener).
 */


#include &quot;nel/misc/debug.h&quot;
#include &quot;nel/misc/time_nl.h&quot;
#include &quot;nel/misc/vector.h&quot;
using namespace NLMISC;

#include &quot;nel/sound/u_audio_mixer.h&quot;
#include &quot;nel/sound/u_listener.h&quot;
using namespace NLSOUND;

#include &lt;stdio.h&gt;


// Pointer to the audio mixer object
UAudioMixer &nbsp; &nbsp; *AudioMixer = NULL;


/*
 * Initialization
 */
void Init()
{
 &nbsp; &nbsp; &nbsp; &nbsp;try
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * 1. Create the audio mixer object and init it.
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * If the sound driver cannot be loaded, an exception is thrown.
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AudioMixer = UAudioMixer::createAudioMixer();
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AudioMixer-&gt;init();

 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * 2. Load a &quot;sources sounds file&quot; (.nss), its sound properties and
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * its attached wave data files (.wav)
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AudioMixer-&gt;loadSoundBuffers( &quot;sample_sounds.nss&quot; );

 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * In this small example, we don't have any environmental effects or
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * environment sounds. If so we would load an &quot;environmental effects
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * file&quot; (.nef) and an &quot;environment sounds file&quot; (.nes):
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * AudioMixer-&gt;loadEnvEffects( &quot;effects.nef&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * UEnvSound *root;
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * AudioMixer-&gt;loadEnvSounds( &quot;envsounds.nes&quot;, &amp;root );
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */

 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * 3. Initialize listener's position and orientation (in NeL coordinate system).
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CVector initpos ( 0.0f, 0.0f, 0.0f );
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CVector frontvec( 0.0f, 1.0f, 0.0f );
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CVector upvec( 0.0f, 0.0f, 1.0f );
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AudioMixer-&gt;getListener()-&gt;setPos( initpos );
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AudioMixer-&gt;getListener()-&gt;setOrientation( frontvec, upvec );

 &nbsp; &nbsp; &nbsp; &nbsp;}
 &nbsp; &nbsp; &nbsp; &nbsp;catch( Exception&amp; e )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nlerror( &quot;Error: %s&quot;, e.what() );
 &nbsp; &nbsp; &nbsp; &nbsp;}
}


/*
 * Adding a source
 */
USource *OnAddSource( const char *name, float x, float y, float z )
{
 &nbsp; &nbsp; &nbsp; &nbsp;/*
 &nbsp; &nbsp; &nbsp; &nbsp; * Create a source with sound 'name', and set some of its initial properties, if successful
 &nbsp; &nbsp; &nbsp; &nbsp; */
 &nbsp; &nbsp; &nbsp; &nbsp;USource *source = AudioMixer-&gt;createSource( name );
 &nbsp; &nbsp; &nbsp; &nbsp;if ( source != NULL )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;source-&gt;setPos( CVector(x,y,z) );

 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* The initial gain, pitch and looping state are stored
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * in the &quot;source sounds file&quot;.
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */

 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;source-&gt;play(); // start playing immediately
 &nbsp; &nbsp; &nbsp; &nbsp;}
 &nbsp; &nbsp; &nbsp; &nbsp;else
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nlwarning( &quot;Sound '%s' not found&quot;, name );
 &nbsp; &nbsp; &nbsp; &nbsp;}
 &nbsp; &nbsp; &nbsp; &nbsp;return source;
}


/*
 * When moving the listener, wait for a short delay
 */
void OnMove( const CVector&amp; listenerpos )
{
 &nbsp; &nbsp; &nbsp; &nbsp;// Move forward
 &nbsp; &nbsp; &nbsp; &nbsp;AudioMixer-&gt;getListener()-&gt;setPos( listenerpos );

 &nbsp; &nbsp; &nbsp; &nbsp;// Wait 20 ms
 &nbsp; &nbsp; &nbsp; &nbsp;TTime time = CTime::getLocalTime();
 &nbsp; &nbsp; &nbsp; &nbsp;while ( CTime::getLocalTime() &lt; time+20 );

 &nbsp; &nbsp; &nbsp; &nbsp;/* If we used spawned sources or &quot;envsounds&quot; or if we had
 &nbsp; &nbsp; &nbsp; &nbsp; * a large number of sources, we should call:
 &nbsp; &nbsp; &nbsp; &nbsp; * AudioMixer-&gt;update();
 &nbsp; &nbsp; &nbsp; &nbsp; */
}


/*
 * main
 *
 * Note: The NeL vector coordinate system is described as follows:
 * \verbatim
 * &nbsp; &nbsp; (top)
 * &nbsp; &nbsp; &nbsp; z &nbsp; &nbsp;
 * &nbsp; &nbsp; &nbsp; | &nbsp;y (front)
 * &nbsp; &nbsp; &nbsp; | /
 * &nbsp; &nbsp; &nbsp; -----x (right)
 * \endverbatim
 */
void main()
{
 &nbsp; &nbsp; &nbsp; &nbsp;// Initialization
 &nbsp; &nbsp; &nbsp; &nbsp;Init();

 &nbsp; &nbsp; &nbsp; &nbsp;// First step: we create two sources
 &nbsp; &nbsp; &nbsp; &nbsp;printf( &quot;Press ENTER to start playing the two sources\n&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp;printf( &quot;One is 20 meters ahead, on the right\n&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp;printf( &quot;The other is 5 meters ahead, on the left\n&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp;getchar();
 &nbsp; &nbsp; &nbsp; &nbsp;USource *src1 = OnAddSource( &quot;Beep&quot;, 1.0f, 20.0f, 0.0f ); &nbsp;// Beep on the right, 20 meters ahead
 &nbsp; &nbsp; &nbsp; &nbsp;USource *src2 = OnAddSource( &quot;Tuut&quot;, -2.0f, 5.0f, 0.0f ); // Tuut on the left, 5 meters ahead

 &nbsp; &nbsp; &nbsp; &nbsp;// Second step: we will move the listener ahead
 &nbsp; &nbsp; &nbsp; &nbsp;printf( &quot;Press ENTER again to start moving the listener\n&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp;getchar();

 &nbsp; &nbsp; &nbsp; &nbsp;// Listener orientation is constant in this example (see initialization)

 &nbsp; &nbsp; &nbsp; &nbsp;// Move forward, then backward, twice
 &nbsp; &nbsp; &nbsp; &nbsp;CVector listenervel;
 &nbsp; &nbsp; &nbsp; &nbsp;CVector listenerpos ( 0.0f, 0.0f, 0.0f );
 &nbsp; &nbsp; &nbsp; &nbsp;for ( uint i=0; i!=2; i++ )
 &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf( &quot;%u of 2\n&quot;, i+1 );

 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Forward velocity
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listenervel.set( 0.0f, 0.5f, 0.0f );
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AudioMixer-&gt;getListener()-&gt;setVelocity( listenervel );

 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Move forward: set position every frame
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf( &quot;Moving forward, going past the sources...\n&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ( listenerpos.y=0.0f; listenerpos.y&lt;30.0f; listenerpos.y+=0.1f )
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OnMove( listenerpos );
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Backward velocity
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listenervel.set( 0.0f, -0.5f, 0.0f );
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AudioMixer-&gt;getListener()-&gt;setVelocity( listenervel );

 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Move backward: set position every frame
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf( &quot;Moving backward, going back to the start position...\n&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ( listenerpos.y=30.0f; listenerpos.y&gt;0.0f; listenerpos.y-=0.1f )
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OnMove( listenerpos );
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
 &nbsp; &nbsp; &nbsp; &nbsp;}

 &nbsp; &nbsp; &nbsp; &nbsp;// Finalization
 &nbsp; &nbsp; &nbsp; &nbsp;printf( &quot;Press ENTER again to exit\n&quot; );
 &nbsp; &nbsp; &nbsp; &nbsp;getchar();
 &nbsp; &nbsp; &nbsp; &nbsp;delete AudioMixer;
}</PRE>