summaryrefslogtreecommitdiffstats
path: root/www/devmaster/lesson7.html
blob: bdfe33b450947d51b90312fc92a41595d223d243 (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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JOAL OpenAL Tutorials from DevMaster.net Lesson 7: The Doppler Effect</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="general.css">
</head>
<body>
<a href="../index.html"><img src="../../../images/jogamp_symbols/website_final_blue_joal_346x70pel.png" alt="JOAL Symbol"></a><a href="http://www.openal.org"><img src="../openal_c.gif"></a>
<br>
OpenAL Tutorials from DevMaster.net. Reprinted with Permission.<br>
<br>
<table border="0" cellspacing="0" style="border-collapse: collapse" width="100%" cellpadding="0" id="AutoNumber1" height="12" bgcolor="#666699">
  <tr> 
    <td width="47%" height="12" valign="middle"><p><b><font color="#FFFFFF">OpenAL 
        Tutorials</font></b></p></td>
    <td width="53%" height="12" align="right" valign="middle"><p align="right"><a href="http://devmaster.net/"><font color="#66FF99">DevMaster.net</font></a></p></td>
  </tr>
</table>
<p class="ArticleTitle"><font size="5">The Doppler Effect<br>
  </font><font color="#000000" size="4"><strong>Lesson 7</strong></font></p>
<p align="right" class="ArticleAuthor">Author: <a href="mailto:lightonthewater@hotmail.com">Jesse 
  Maurais</a><br>
  Adapted For Java By: <a href="mailto:athomas@dev.java.net">Athomas Goldberg</a></p>
<p align="justify">This is a translation of <a href="http://devmaster.net/posts/2894/openal-lesson-7-the-doppler-effect">
OpenAL Lesson 7: The Doppler Effect</a>
tutorial from <a href="http://devmaster.net/">DevMaster.net</a> to JOAL.

</p>
<h1>A Look at Real-World Physics</h1>
<p align="justify">I know this will be boring review for anyone with a course 
  in high school physics, but lets humour ourselves. The Doppler effect can be 
  a very tricky concept for some people, but it is a logical process, and kind 
  of interesting when you get right down to it. To begin understanding the Doppler 
  effect we first must start to understand what a &quot;sound&quot; really is. 
  Basically a sound is your minds interpretation of a compression wave that is 
  traveling through the air. Whenever the air becomes disturbed it starts a wave 
  which compresses the air particles around it. This wave travels outward from 
  it's point of origin. Consider the following diagram.</p>
<p align="justify"><img src="sound_waves.jpg" width="150" height="132" hspace="5" vspace="0" border="0" align="left">In 
  this diagram (on the left) the big red &quot;S&quot; stands for the sources 
  position, and the big red &quot;L&quot; stands for (you guessed it), the Listener's 
  position. Both source and Listener are not moving. The source is emitting compression 
  waves outward, which are represented in this diagram by the blue circles. The 
  Listener is experiencing the sound exactly as it is being made in this diagram. 
  The Doppler effect is not actually present in this example since there is no 
  motion; the Doppler effect only describes the warping of sound due to motion.</p>
<p align="justify">What you should try to do is picture this diagram animated. 
  When the source emits a wave (the circles) it will look as though it is growing 
  away from it's point of origin, which is the sources position. A good example 
  of a similar effect is the ripples in a pond. When you throw a pebble into a 
  calm body of water it will emit waves which constantly move away from the point 
  of impact. Believe it or not this occurs from the exact same physical properties. 
  But what does this have to do with the Doppler effect? Check out the next diagram 
  (on the right).</p>

<p align="justify"> <img src="doppler_effect.jpg" width="150" height="132" hspace="5" border="0" align="right">Wow, 
  what's going on here? The source is now in motion, indicated by the little red 
  arrow. In fact the source is now moving towards the Listener with an implied 
  velocity. Notice particularly that the waves (circles) are being displaced inside 
  each other. The displacement follows the approximate path of the source which 
  emits them. This is the key to the Doppler effect. Essentially what has happened 
  is that the source has emitted a wave at different points in it's path of travel. 
  The waves it emits do not move with it, but continue on their own path of travel 
  from the point they were emitted.</p>
<p align="justify">So how does this effect the perceived sound by the Listener? 
  Well, notice too in the last diagram that the waves (circles) that are between 
  the source and the Listener are kind of compressed together. This will cause 
  the sound waves to run together, which in turn causes the perceived sound seem 
  like it's faster. What we are talking about here is frequency. The distances 
  between the waves effects the frequency of the sound. When the source that emits 
  the sound is in motion, it causes a change in frequency. You may notice too 
  that distance between the waves varies at different points in space. For example, 
  on the opposite side of the moving source (anywhere along the previous path 
  of travel) the distances are actually wider, so the frequency will be lower 
  (the distance and frequency have an inverse relationship). What this implies 
  is that the frequency perceived by the Listener is relative to where the Listener 
  is standing. </p>
<p align="justify">The motion of the Listener can also affect the frequency. This 
  one is a little harder to picture though. If the source is still, and the Listener 
  is moving toward the source, then the perceived frequency by the Listener will 
  be warped in the same exact manner that we described for the moving source. 
</p>
<p>If you still have trouble picturing this, consider the following two 
diagrams:</p>
<p align="center"><img border="0" src="sin_wave.jpg" width="255" height="135">&nbsp;&nbsp;
<img border="0" src="compress_sin_wave.jpg" width="255" height="135"></p>
<p align="justify">These two diagrams will represent the sound in the form of 
  a sine wave. Look at the first one. Think of the peaks as the instance of the 
  wave. The very top point of the wave will be the same as the instance of the 
  blue circle in the previous set of diagrams. The valleys will be like the spaces 
  in between the blue circles. The second diagram represents a compressed wave. 
  When you compare the two you will notice an obvious difference. The second diagram 
  simply has more wave occurrences in the same amount of space. Other ways of 
  saying this are that they occur more often, with a greater regularity, or with 
  a greater frequency. </p>
<p align="justify">For anyone who is interested in some added information: The 
  velocity of the waves is the speed of sound. If the velocity of the source is 
  greater than that of the wave, then the source is breaking the sound barrier.</p>
<h1>The Physics of OpenAL</h1>

<p align="justify">Ok, either you have understood my ramblings on the Doppler 
  effect from above, or you have skipped it because you already have full knowledge 
  of the Doppler effect and just want to know how it effects the OpenAL rendering 
  pipeline. I think the best start to his section will be to quote the OpenAL 
  spec directly:</p>
<blockquote>
  <p align="justify"><i>&quot;The Doppler Effect depends on the velocities of 
    Source and Listener relative to the medium, and the propagation speed of sound 
    in that medium.&quot; - chapter 3, subsection 7&quot;</i></p>
</blockquote>
<p align="justify">We can take this to mean that there are 3 factors which are 
  going to affect the final frequency of the sound heard by the Listener. These 
  factors are the velocity of the source, the velocity of the Listener, and a 
  predefined speed of sound. </p>
<p align="justify">When we refer to a &quot;medium&quot;, what we mean is the 
  kind of material that both the source and Listener are &quot;in&quot;. For example, 
  sounds that are heard from underwater are much different than sounds that are 
  heard in the open air. Air and water are examples of different mediums. The 
  reason that sound is so different between these mediums has to do with the particle 
  density. As we said before, sound is nothing but the motion of particles in 
  the air. In a medium with a much greater particle density the sound will be 
  much different because the particles are in closer contact. When they are in 
  closer contact it allows for the wave to travel much better. As an example of 
  the opposite, think of outer space. In outer space there is an extremely low 
  particle density. In fact there is only a few very light particles (mostly hydrogen) 
  scattered about. This is why no sound can be heard from space. </p>

<p align="justify">Ok, lets get back on topic. OpenAL calculates the Doppler effect 
  internally for us, so we need only define a few parameters that will effect 
  the calculation. We would do this in case we don't want a realistic rendering. 
  Rather if want to exaggerate or deemphasize the effect. The calculation goes 
  like this.</p>
<p><span class="codeNormal">&nbsp;&nbsp;&nbsp; shift = DOPPLER_FACTOR * freq * (DOPPLER_VELOCITY 
- l.velocity) / (DOPPLER_VELOCITY + s.velocity)</span></p>
<p align="justify">Constants are written in all caps to differentiate. The &quot;l&quot; 
  and &quot;s&quot; variables are the Listener and source respectively. &quot;freq&quot; 
  is the initial unaltered frequency of the emitting wave, and &quot;shift&quot; 
  is the altered frequency of the wave. The term &quot;shift&quot; is the proper 
  way to address the altered frequency and will be used from now on. This final 
  shifted frequency will be sampled by OpenAL for all audio streaming that is 
  affected. </p>

<p align="justify">We already know that we can define the velocity of both source 
  and Listener by using the 'AL_VELOCITY' field to 'alListenerfv' and 'alSourcefv'. 
  The 'freq' parameter comes straight from the buffer properties when it was loaded 
  from file. To set the constant values the following functions are provided for 
  us.</p>
<pre class=code><font color="#0000FF">public void </font>alDopplerFactor(<font color="#0000FF">float</font> factor);
<font color="#0000FF">public void </font>alDopplerVelocity(<font color="#0000FF">float</font> velocity);
</pre>
<p align="justify">For 'alDopplerFactor' any non-negative value will suffice. 
  Passing a negative value will raise an error of 'AL_INVALID_VALUE', and the 
  whole command will be ignored. Passing zero is a perfectly valid argument. Doing 
  this will disable the Doppler effect and may in fact help overall performance 
  (but won't be as realistic). The effect of the Doppler factor will directly 
  change the magnitude of the equation. A value of 1.0 will not change the effect 
  at all. Passing anything between 0.0 and 1.0 will minimize the Doppler effect, 
  and anything greater than 1.0 will maximize the effect. </p>
<p align="justify">For 'alDopplerVelocity' any non-negative non-zero value will 
  suffice. Passing either a negative or a zero will raise an error of 'AL_INVALID_VALUE', 
  and the whole command will be ignored. The Doppler velocity is essentially the 
  speed of sound. Setting this will be like setting how fast sound can move through 
  the medium. OpenAL has no sense of medium, but setting the velocity will give 
  the effect of a medium. OpenAL also has no sense of units (kilometer, miles, 
  parsecs), so keep that in mind when you set this value so it is consistent with 
  all other notions of units that you have defined.</p>
</p>
<table border="0" cellspacing="1" style="border-collapse: collapse" width="100%" id="AutoNumber2" bgcolor="#666699">
  <tr> 
    <td width="40%"> <p dir="ltr"><font color="#FFFFFF" size="2"> 2003 DevMaster.net. 
        All rights reserved.</font></td>
    <td width="60%"> <p align="right" dir="ltr"><font size="2"><a href="mailto:webmaster@devmaster.net"> 
        <font color="#FFFFFF">Contact us</font></a><font color="#FFFFFF"> if you 
        want to write for us or for any comments, suggestions, or feedback.</font></font></td>
  </tr>
</table>
</body>
</html>