' Draft of Scripting class 20 (7/17/2011) examples ' this class covers advanced VBScript features not covered before
' example 1: ' a very simple class definition Class speechParameters ' this class holds properties for speech parameters Public Description' used for purpose of these collection of settings Public synthesizerDescription' tracks synthesizer/engine these parameters are to be applied to Public pitch Public Tone Public rate Public volume end class ' and you can then use it like any other object, once you have defined a variable and created the object dim voice set voice = new speechParameters ' you use the "new" keyword instead of createObject() ' end of example 1 ' example 2: ' a more complex class definition Class speechParameters Public Description' used for purpose of these collection of settings Public synthesizerDescription' tracks synthesizer/engine these parameters are to be applied to Private m_pitch Private m_rate Private m_tone Private m_volume Private Sub Class_Initialize() ' copy all of the currently in use parameters into our private variables for the class m_pitch = ActiveSettings.Screen.Pitch m_rate = ActiveSettings.Screen.Rate m_tone = ActiveSettings.Screen.Tone m_volume = ActiveSettings.Screen.Volume synthesizerDescription = synthesizers.Active.Description End Sub Public Sub Init(ByVal thePitch, ByVal theRate, ByVal theTone, ByVal theVolume, ByVal theDescription) ' used to set all parameters with one call Description = theDescription If thePitch >= 0 And thePitch <= 9 Then m_pitch = thePitch Else thePitch = 3 End If If theRate >= 1 And theRate <= 100 Then m_rate = theRate Else theRate = 30 End If theTone = LCase(theTone) If theTone >= "a" And theTone <= "z" Then m_tone = theTone Else theTone = "i" End If If theVolume >= 0 And theVolume <= 9 Then m_volume = theVolume Else theVolume = 9 End If End Sub Public Function SetSpeechParameters() ' causes the speech parameters in this object to be applied as the screen voice SetSpeechParameters = False If synthesizerDescription <> synthesizers.Active.Description Then Exit Function ' prevents the parameters from being applied to an unexpected synthesizer On Error Resume Next ActiveSettings.Screen.Rate = m_rate ActiveSettings.Screen.Pitch = m_pitch ActiveSettings.Screen.Tone = m_tone ActiveSettings.Screen.Volume = m_volume If Err.Number = 0 Then SetSpeechParameters = True On Error GoTo 0 End Function '------- ' Pitch Property Public Property Get Pitch() Pitch = m_pitch End Property Public Property Let Pitch(thePitch) If thePitch >= 0 And thePitch <= 9 Then m_pitch = thePitch End If End Property '------- ' Rate Property Public Property Get Rate() Rate = m_rate End Property Public Property Let Rate(theRate) If theRate >= 1 And theRate <= 100 Then m_rate = theRate End If End Property '------- ' Tone Property Public Property Get Tone() Tone = m_tone End Property Public Property Let Tone(ByVal theTone) theTone = LCase(theTone) If theTone >= "a" And theTone <= "z" Then m_tone = theTone End If End Property '------- ' Volume Property Public Property Get Volume() Volume = m_volume End Property Public Property Let Volume(theVolume) If theVolume >= 0 And theVolume <= 9 Then m_volume = theVolume End If End Property end Class 'speechParameters ' end of example 2 ' example 3: ' showing how I use the speechParameters class ' in this example I have a primary voice for most speech from my app, and an alternate speech voice (which should sound different in some way), ' for spoken items I wish to sound different from most ' main body code for initializing the two voices I use dim curVoice, alternateVoice Set curVoice = New SpeechParameters Set alternateVoice = New SpeechParameters curVoice.Description = "Primary" With alternateVoice .Description = "Alternate" .Pitch = curVoice.Pitch .Tone = curVoice.Tone ' now create an alternate voice which is a variation of the current voice settings Select Case synthesizers.Active.Description Case "Eloquence": ' use an alternate sounding voice for Eloquence by using a different tone ' range to provide different sounds is I -- M If UCase(.Tone) >= "I" _ And UCase(.Tone) <= "L" Then .Tone = "M" Else .Tone = "I" End If Case "DECtalk Access32 (Window-Eyes)" ' use an alternate sounding voice for DT by using a different tone ' range to provide different sounds is H -- P If UCase(.Tone) >= "I" _ And UCase(.Tone) <= "P" Then .Tone = "H" Else .Tone = "I" End If Case Else ' change the pitch slightly for this unknown synth to make it different If .Pitch > 2 Then .Pitch = .Pitch - 2 Else .Pitch = .Pitch + 2 End If End Select ' end of main body sub speakFootnote(footnoteNumber, footnoteText) ' speak a footnote number and optionally it's text. ' if the user is using a synth, and has chosen an option to speak this type of info in an alternate voice, ' then change voices when speaking it If useAlternateVoice And usingSynth() Then switchToVoice alternateVoice Speak footnoteNumber ' always speak the footnote number If speakFootnoteText Then speak footnoteText ' optionally speak the footnote text If useAlternateVoice And usingSynth() Then switchToVoice curVoice end sub usingSynth = True If ActiveSettings.Screen.Voice = False Or _ synthesizers.Active.Description = "None" Then usingSynth = False End Function Sub switchToVoice(ByVal newVoice) ' speak text after whatever has already been queued, in the specified voice If Not usingSynth Then Exit Sub If Not newVoice.SetSpeechParameters() Then useAlternateVoice = False Speak "Unable to use alternate voices." End If End Sub ' end of example 3 ' example 4: ' showing another advantage of using classes, you can define methods differently depending upon the type of the class ' and your main program doesn't have to bother with, or even know about, the details class child public name public dateOfBirth public coveredByInsurance public gender function info() dim s s = "child: " & name & " (" & dateOfBirth & ")" if coveredByInsurance then if gender = "M" then s= s & ", he" if gender = "F" then s= s & ", she" s = s & " has insurance" end if ' coveredByInsurance info = s end function end class class pet public name public dateOfBirth public bread public gender public hadShots function info() dim s s = "pet: " & name & " (" & dateOfBirth & ")" if hadShots then if gender = "M" then s= s & ", he" if gender = "F" then s= s & ", she" s = s & " has had all shots" end if ' hadShots info = s end function end class dim dependents dim bob, jane, spot set dependents = createObject("scripting.dictionary") set bob = new child with bob .name = "Bob" .dateOfBirth = "3/1/2001" .gender = "M" .coveredByInsurance = true end with set jane = new child with jane .name = "Jane" .dateOfBirth = "4/20/2003" .gender = "F" .coveredByInsurance = true end with set spot = new pet with spot .name = "Spot" .dateOfBirth = "2/12/2006" .gender = "M" .hadShots = true end with with dependents .add 1, bob .add 2, jane .add 3, spot end with ' later on it's time to list this employee's dependents for each dep in dependents.items print dep.info() next ' this would show you that each object would take care of printing it's own info correctly, according to the properties it had to work with ' end of example 4 ' archives of these classes can be found at: ' https://www.gwmicro.com/App_Central/Developers/Interactive_Classes/