; stereo localization of points in 2-D space ; simulation of distance and motion ; by specifying the sound source as a moving object ; from one point to another in the 2-D space ; the distance from the listener will determine the ; reduction in amplitude and the delay sr = 44100 kr = 4410 ksmps = 10 nchnls = 2 gipi = 3.1415926535898 ; pi, for cacluating angles in radians gimach = 344 ; speed of sound, in meters per second gispeakerx = 3 ; speaker x coordinate, in meters gispeakery = 3 ; speaker y coordinate, in meters ; the number for "gispeakery" should always be at least 1 ; listener is at coordinates 0, 0 ; from the above information we can calculate much other information gispeakerd = sqrt((gispeakerx*gispeakerx)+(gispeakery*gispeakery)) ; distance of the speaker from the listener gispeakerRx = gispeakerx ; right speaker x coordinate gispeakerLx = -1*(gispeakerx) ; left speaker x coordinate gispeakerRy = gispeakery ; right speaker y coordinate gispeakerLy = gispeakery ; left speaker y coordinate gispeakerRa taninv2 gispeakerRx, gispeakerRy ; right speaker angle (in radians) gispeakerLa taninv2 gispeakerLx, gispeakerLy ; left speaker angle (in radians) instr 1 ; move linearly from one point to another gkx line p4, p3, p6 ; x coordinate of sound source gky line p5, p3, p7 ; y coordinate of sound source endin instr 2 ; simulate the movement of a sound ; based on the location specified in global variables "gkx, gky" from instrument 1 ; using the sound object's distance from the listener ; to calculate its amplitude and its delay (thus its Doppler shift) kx = gkx ; x coordinate of sound source ky = gky ; y coordinate of sound source kd = sqrt((kx*kx)+(ky*ky)) ; distance of sound source from listener kD = kd / gispeakerd ; factor by which to divide the overall amplitude ka taninv2 kx, abs(ky) ; angle of sound source (in radians) ; use absolute value of y coordinate for source angle because ; for purposes of this instrument, negative y coordinates are not meaningful ; since there are only two speakers, placed in FRONT of the listener on either side kAL = ka-gispeakerLa ; angle of source relative to left speaker kAL limit kAL, -0.5*gipi, 0.5*gipi ; if it exceeds 90 degrees, set it to 90 degrees kAR = ka-gispeakerRa ; angle of source relative to right speaker kAR limit kAR, -0.5*gipi, 0.5*gipi ; if it exceeds 90 degrees, set it to 90 degrees kampL = cos(kAL) / kD ; calculate amplitude factor for left speaker kampL limit kampL, -1., 1. ; limit it to avoid clipping kampR = cos(kAR) / kD ; calculate amplitude factor for right speaker kampR limit kampR, -1., 1. ; limit it to avoid clipping ; at this point kampL and kampR have the correct amplitude factor for each speaker ; the next part can be any kind of sound generator ; next, we'll generate a sound ; in this case we're using a cheezy imitation of a European police siren imaxamp = 32767 ; set the maximum amplitude of the source kenv linen p4*imaxamp, 5, p3, 5 ; taper the beginning and ending of the sound kfreq oscil 1, 1, 2 ; read through a table of two alternating frequencies awave oscili kenv, kfreq, 3 ; play those frequencies with a fixed waveform ; then this part delays the sound by the appropriate amount ; based on its distance from the listener adelayline delayr 1 ; create a 1-second delay line adelwave deltap kd/gimach ; look a certain time into the delay line, ; based on distance of the sound source delayw awave ; this is what is written into the delay line outs kampL*adelwave, kampR*adelwave ; apply this delay to each speaker endin