' Draft of Scripting class 26 (9/4/2011)

' examples involve use of the new audio kit shared object, created by Steve
of GW Micro.
' It's documentation is in an included text file named "GWAudioKit
Methods.txt", which is found in the same directory as the scripts.


' Below is a short summary of the methods available, arranged together by
their use:

' Methods for initializing a steady tone or noise:
' createTone(), createSquare(),  createPink(), createWhite()

' Methods for use with a sound file:
' load(), invalidate()

' Methods for altering the sound of the played noise or sound file:
' setPan(), setPitShift(), setVolume(), 
' getPan(), getPitchShift(),  getVolume()

' Methods for causing a noise or sound file to play (or not), or to repeat
(only sound files), or for the state of these:
' isPlaying(), play(), stop(), pause(), setRepeating(), 
'   getRepeating()

' Methods related to time (of play) for a sound file:
' setPosition(), reset(), 
' getLength(), getPosition(), 

' Methods related to samples (of play) for a sound file: 
'  setPositionSamples(), 
' getLengthSamples(), getPositionSamples(),   getSampleRate()

' Miscellaneous methods:
' getVersion()





' Example 1:
' shows how to play a sound file, and have it unload from memory when done.

dim lastSoundFile: lastSoundFile = 0
dim objAudioKit
set objAudioKit = sharedObjects.get("com.GWMicro.AudioKit")

sub playFile(filename)

' below loads the audio file into memory (it can be any of a long list of
types of audio files)
lastSoundFile = objAudioKit.load(filename)
' below plays it one time and then unloads it from memory, making this
routine similar to the playSound() method of the WE object model
objAudioKit.play (lastSoundFile, false, objAudioKit.getLength(n), true)

' the above commands starts the file playing and then returns immediately
before it has finished playing.
' unlike the playSound method, if you played a second file before the first
one is finished, it will not stop the first one from playing (they will play
together).
' if you did want to stop the first file from playing, then you could first
call the sub below (stopLastSoundFile).

end sub


sub stopLastSoundFile()
' stops the previous sound file from playing

if objAudioKit.isPlaying(lastSoundFile) then objAudioKit.stop(lastSoundFile)

end sub

' end of example 1



' example 2:
' plays sound files similar to example 1, but queues them so they won't play
together.

dim lastSoundFile: lastSoundFile = 0
dim objAudioKit
set objAudioKit = sharedObjects.get("com.GWMicro.AudioKit")

sub playFile(filename)

if objAudioKit.isPlaying(lastSoundFile) then
' need to wait until it's finished before playing the next sound, check
again in 100 milliseconds
  startTimer 100, "playFile", filename
  exit sub
end if

' ready to play; below loads the audio file into memory (it can be any of a
long list of types of audio files)
lastSoundFile = objAudioKit.load(filename)
' below plays it one time and then unloads it from memory, making this
routine similar to the playSound() method of the WE object model
objAudioKit.play (lastSoundFile, false, objAudioKit.getLength(n), true)

end sub

' end of example 2




' example 3:
' this example simulates a 3D effect with a circling beep sound.
' the beep happens in a clock-face pattern around the user (with a beep at
each hour), and gives an audio effect of going "behind" the user.
' (this might be used in a real-time position indicator, or a game
positioning indicator)

                        Set AudioKit = SharedObjects("com.GWMicro.AudioKit")
                        tone = AudioKit.CreateTone(880)
                        AudioKit.SetVolume tone, 0.5 ' Don't make the sound
too loud.
toneTimer = 0

' make a beeping point circle around the listener and beep at each hour of
the clock relative to him
for i = 1 to 12
  beepAt i
next
' and now one more beep
beepAt 1

sub beepAt(clockHour)
' simulate a beep at the specified hour position

angle = (clockHour-1)*30 + 15

if angle >= 270 or angle <= 90 then
' it's ahead
  AudioKit.SetVolume tone, 0.6 ' make it a little louder when ahead
  freq = 1.01 ' and increase the frequency slightly
  AudioKit.SetPitchShift tone, freq

else
' it's behind
  AudioKit.SetVolume tone, 0.3 ' and make it a little softer when behind
  freq = 0.99 ' and lower the frequency slightly when behind
  AudioKit.SetPitchShift tone, freq
end if

if angle <= 180 then
' it's to the right
  panPos = 1-(abs(90-angle)/90)
  AudioKit.SetPan tone, panPos

else '  angle <= 180
' it's to the left
  panPos = -1 + (abs(270-angle)/90)
  AudioKit.SetPan tone, panPos
end if

AudioKit.Play tone

' now use a timer to stop the tone after it's played long enough
toneTimer = StartTimer(200, "StopTone")
' now pause between beeps
sleep 1000

end sub

Sub StopTone()

toneTimer = 0
AudioKit.Stop tone

End Sub


' end of example 3

' example 4:
' the Audible Mouse routine included with the audioKit


Dim mouseTimer : mouseTimer = 0
Dim tone
Dim mouseEvent
Dim AudioKit

ConnectEvent SharedObjects, "OnStateChange", "OnStateChange"


Sub OnStateChange(objName, objState)
        If objName = "com.GWMicro.AudioKit" Then
                If objState Then
                        Set AudioKit = SharedObjects("com.GWMicro.AudioKit")
                        tone = AudioKit.CreateTone(880)
                        AudioKit.SetVolume tone, 0.5 ' Don't make the sound
too loud.
                        mouseEvent = ConnectEvent(Mouse, "OnMove", "OnMove")

                Else
                        Disconnect mouseEvent
                        Set AudioKit = Nothing
                End If
        End If

End Sub


Sub OnMove()

Queue "MouseNoise"

End Sub


Sub MouseNoise()
' called whenever the mouse moves (by it's event handler)

dim perCentage
dim baseValue
dim range
dim x, y, h, w

' get info on the mouse position and the screen width and height
x = Mouse.Position.X
y = Mouse.Position.Y
h = Screen.Height
w = Screen.Width

' calculate the left offset of the mouse as a percentage value of how wide
the screen is, then multiply this percentage by the range of input values
used to specify left to right positioning.  
' Finally, add this result to the base value of the position input to arrive
at the valid parameter.

perCentage = x/w ' mouse horizontal position, expressed as a percentage of
screen width
range = 2 ' input parameter may range from -1 (only left channel) to 1 (only
right channel)
baseValue = -1 ' minimum valid value for this parameter

panPos = baseValue + (perCentage * range)
' the variable above is now the left/right indication which will be passed
as the parameter to the .SetPan method
' (which can cause the sound to issue anywhere from only the left channel
(with a value of -1), all the way to only the right channel (with a value of
1))

AudioKit.SetPan tone, panPos
' note that the sound identifier has to be passed in as a parameter, because
this method only effects the particular sound, not all sounds playing.

freq = y/h

perCentage = freq  ' indicates the vertical mouse position as a percentage
of the total height of the screen
range = 1.5 ' range of the input parameter which may change the tone by an
octive up or down
baseValue = 2.0 ' maximum value of the input parameter for an octive change
(upward)

AudioKit.SetPitchShift tone, baseValue - (range * perCentage)
' this is how much of an octive to change the frequency of the initial tone
of 880 hz by
' (that is, the initial tone might go up as much as an octive, or it might
go down as much as an octive, depending upon the current mouse position)

if mouseTimer = 0 Then ' if nothing is playing for a mouse sound
AudioKit.Play tone ' start the tone playing
mouseTimer = StartTimer(30, "StopMouse") ' needed to stop the tone from
playing
End If

End Sub


Sub StopMouse()
' called by the timer used to stop the tone from playing; there is no other
way to stop a noise from playing

mouseTimer = 0
AudioKit.Stop tone

End Sub

' end of example 4





' archives of these classes can be found at:
' https://www.gwmicro.com/App_Central/Developers/Interactive_Classes/


Reply via email to