1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
5 <meta http-equiv=
"Content-Style-Type" content=
"text/css">
7 <meta name=
"Generator" content=
"Cocoa HTML Writer">
8 <meta name=
"CocoaVersion" content=
"824.42">
9 <style type=
"text/css">
10 p
.p1
{margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica
}
11 p
.p2
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
; min-height: 14.0px}
12 p
.p3
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
}
13 p
.p4
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco
; min-height: 16.0px}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
}
15 p
.p6
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #a71e12}
16 p
.p7
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; min-height: 12.0px}
17 span
.s1
{color: #0019b7}
18 span
.s2
{color: #000000}
19 span
.s3
{color: #a71e12}
20 span
.Apple-tab-span
{white-space:pre
}
24 <p class=
"p1"><b>atan2
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>arctangent
</b></p>
25 <p class=
"p2"><br></p>
26 <p class=
"p3"><b>BinaryOperator
</b></p>
27 <p class=
"p2"><br></p>
28 <p class=
"p3"><b>atan2(y, x)
</b></p>
29 <p class=
"p3"><b>y atan2: x
</b></p>
30 <p class=
"p3"><b>y.atan2(x)
</b></p>
31 <p class=
"p4"><br></p>
32 <p class=
"p3">Returns the arctangent of y/x.
</p>
33 <p class=
"p2"><br></p>
34 <p class=
"p3">See also
<b>hypot
</b>.
</p>
35 <p class=
"p2"><br></p>
36 <p class=
"p3">OK, now we can add a pan to the
<b>hypot
</b> doppler examples by using atan2 to find the azimuth,
<span class=
"Apple-converted-space"> </span></p>
37 <p class=
"p3">or direction angle, of the sound source.
</p>
38 <p class=
"p3">Assume speakers at +/-
45 degrees and clip the direction to between those.
</p>
39 <p class=
"p4"><br></p>
42 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s1">var
</span> x, y, distance, velocity, pitchRatio, amplitude, azimuth, panValue;
</p>
43 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>// object travels
200 meters in
6 secs (=
120kph) passing
10 meters
<span class=
"Apple-converted-space"> </span></p>
44 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>// from the listener
</p>
45 <p class=
"p5"><span class=
"Apple-tab-span"> </span>x =
10;
</p>
46 <p class=
"p5"><span class=
"Apple-tab-span"> </span>y =
<span class=
"s1">LFSaw
</span>.kr(
1/
6,
0,
100);
</p>
47 <p class=
"p5"><span class=
"Apple-tab-span"> </span>distance = hypot(x, y);
</p>
48 <p class=
"p5"><span class=
"Apple-tab-span"> </span>velocity =
<span class=
"s1">Slope
</span>.kr(distance);
</p>
49 <p class=
"p5"><span class=
"Apple-tab-span"> </span>pitchRatio = (
344 - velocity) /
344;
<span class=
"Apple-converted-space"> </span><span class=
"s3">// speed of sound is
344 meters/sec
</span></p>
50 <p class=
"p5"><span class=
"Apple-tab-span"> </span>amplitude =
10 / distance.squared;
</p>
51 <p class=
"p5"><span class=
"Apple-tab-span"> </span>azimuth = atan2(y, x);
<span class=
"s3">// azimuth in radians
</span></p>
52 <p class=
"p5"><span class=
"Apple-tab-span"> </span>panValue = (azimuth /
0.5pi).clip2(
1);
</p>
53 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s1">Pan2
</span>.ar(
<span class=
"s1">FSinOsc
</span>.ar(
1000 * pitchRatio), panValue, amplitude)
</p>
54 <p class=
"p5">}.play)
</p>
55 <p class=
"p7"><br></p>
58 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s1">var
</span> x, y, distance, velocity, pitchRatio, amplitude, motorSound,
<span class=
"Apple-converted-space"> </span></p>
59 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>azimuth, panValue;
</p>
60 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>// object travels
200 meters in
6 secs (=
120kph) passing
10 meters
<span class=
"Apple-converted-space"> </span></p>
61 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>// from the listener
</p>
62 <p class=
"p5"><span class=
"Apple-tab-span"> </span>x =
10;
</p>
63 <p class=
"p5"><span class=
"Apple-tab-span"> </span>y =
<span class=
"s1">LFSaw
</span>.kr(
1/
6,
0,
100);
</p>
64 <p class=
"p5"><span class=
"Apple-tab-span"> </span>distance = hypot(x, y);
</p>
65 <p class=
"p5"><span class=
"Apple-tab-span"> </span>amplitude =
40 / distance.squared;
</p>
66 <p class=
"p5"><span class=
"Apple-tab-span"> </span>motorSound =
<span class=
"s1">RLPF
</span>.ar(
<span class=
"s1">FSinOsc
</span>.ar(
200,
0,
<span class=
"s1">LFPulse
</span>.ar(
31.3,
0,
0.4)),
400,
0.3);
</p>
67 <p class=
"p5"><span class=
"Apple-tab-span"> </span>azimuth = atan2(y, x);
<span class=
"s3">// azimuth in radians
</span></p>
68 <p class=
"p5"><span class=
"Apple-tab-span"> </span>panValue = (azimuth /
0.5pi).clip2(
1);
<span class=
"s3">// make a value for Pan2 from azimuth
</span></p>
69 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s1">Pan2
</span>.ar(
<span class=
"s1">DelayL
</span>.ar(motorSound,
110/
344, distance/
344), panValue, amplitude)
</p>
70 <p class=
"p5">}.play)
</p>