Originally Posted by
d_m_hopper
Does anyone know how to turn on the speakers of users computers and adjust the volume via lisp?
Code:
(defun C:SpeakCfg(/ Sapi ~voice ~volume ~rate Voice VoiceMary VoiceMike
VoiceSam VoiceLst NumVoices @PlayWav @NoPath
@VoiceList @member+ @Voice&TextInput)
;**********************SubPrograms************************************************
(defun @PlayWav (Sapi WavFile / WavPath @@FindFile ObjFile)
(defun @@FindFile(WavFile / WinDir Path i DirLst)
(setq WinDir (getenv "Windir"))
(setq Path nil i 0 DirLst
(list "" "c:\\" (strcat WinDir "\\")
(strcat WinDir "\\media\\")
(strcat WinDir "\\System\\")
(strcat WinDir "\\System32\\")
(strcat WinDir "\\System64\\")
(strcat WinDir "\\System32\\drivers\\")))
(while (and (< i (length DirLst))
(= (setq Path (findfile (strcat (nth i DirLst) WavFile))) nil))
(setq i (1+ i)))
Path
)
(setq WavPath (@@FindFile WavFile))
(if (and WavPath Sapi)
(progn
(setq ObjFile (vlax-create-object "SAPI.SpFileStream.1"))
(vlax-invoke ObjFile "Open" WavPath)
(vlax-invoke Sapi "Speakstream" ObjFile 0)
(vlax-release-object ObjFile)
))
)
(defun @NoPath(s / cnt ct name)
(setq name (strcase s T))
(if (= (substr name 2 1) ":")
(setq name (substr name 3)))
(setq cnt 1 ct 0)
(repeat (strlen name)
(if (= (substr name cnt 1) "\\")
(setq ct cnt)
)
(setq cnt (1+ cnt))
)
(setq name (substr name (+ 1 ct) cnt))
)
(defun @VoiceList(Sapi / i VoiceLst Voices)
(setq Voices (vlax-invoke Sapi 'GetVoices))
(setq i 0 VoiceLst '())
(repeat (vla-get-count Voices)
(setq VoiceLst (append VoiceLst
(list (vlax-invoke (vla-item Voices i) 'GetDescription))))
(setq i (1+ i))
)
VoiceLst
)
(defun @member+ (x lst /)
(if (vl-member-if '(lambda (y) (wcmatch x y)) lst) x)
)
(defun @Voice&TextInput(voice text / SpeekContent)
(setq SpeekContent (strcat "<VOICE REQUIRED='NAME=" voice "'>" text "</VOICE>"))
)
;**********************Main Program***********************************************
(vl-load-com)
(setq Sapi (vlax-create-object "Sapi.SpVoice"))
(if (not Sapi)(progn
(load "AI_UTILS")(princ "\nWarning: For sound, first install Microsoft Speech SDK 5.1")
(ai_abort "Sapi.SpVoice" (strcat "Warning: No Speech Application Programming Interface"
" with Microsoft Speech" "\n or with the speech module in MS Office."
"\n Suggestion: Install Microsoft Speech SDK 5.1 from http://www.microsoft.com/downloads/"))
))
(setq ~voice (@NoPath (vlax-get-property (vlax-get-property Sapi 'voice) 'Id)))
(cond
((= ~voice "msmary") (setq Voice "Microsoft Mary"))
((= ~voice "msmike") (setq Voice "Microsoft Mike"))
((= ~voice "mssam") (setq Voice "Microsoft Sam"))
(t (setq Voice ~voice))
)
;The voice is normally in Microsoft Speech "Microsoft Mary",
;but in the speech module in MS Office it is "Microsoft Sam".
(setq ~volume (vlax-get-property Sapi 'Volume))
(vlax-put-property Sapi 'Volume 100)
(@playwav Sapi "notify.wav")
(vlax-invoke Sapi "Speak" (strcat "This is " Voice " speaking in AutoCAD with Sapi. "
"Sapi is short for Speech Application Programming Interface. "
"You can also use Sapi for playing Wave soundfiles in AutoCAD.") 0)
(@playwav Sapi "Ringin.wav")
(vlax-put-property Sapi 'Volume 10)
(vlax-invoke Sapi "Speak" "Do you want AutoCAD to speak with a soft volume?" 0)
(vlax-put-property Sapi 'Volume 50)
(vlax-invoke Sapi "Speak" "Do you want AutoCAD to speak with a medium volume?" 0)
(vlax-put-property Sapi 'Volume 100)
(vlax-invoke Sapi "Speak" "Do you want AutoCAD to speak with a loud volume?" 0)
;(vlax-put-property Sapi 'Volume ~volume)
(setq ~rate (vlax-get-property Sapi 'Rate))
(vlax-put-property Sapi 'Rate -8)
(vlax-invoke Sapi "Speak" "Do you want AutoCAD to speak with a slow rate?" 0)
(vlax-put-property Sapi 'Rate 5)
(vlax-invoke Sapi "Speak" "Do you want AutoCAD to speak with a fast rate?" 0)
(vlax-put-property Sapi 'Rate 0)
(vlax-invoke Sapi "Speak" "Do you want AutoCAD to speak with a normal rate?" 0)
;(vlax-put-property Sapi 'Rate ~rate)
(vlax-invoke Sapi "Speak" "Question, Can I also change the voice?" 0)
(setq VoiceLst (@VoiceList Sapi))
;Gives normally with Microsoft Speech:("Microsoft Mary" "Microsoft Mike" "Microsoft Sam" "Sample TTS Voice")
;The voice "Sample TTS Voice" is not usable in a Sapi object, it is for use in TTSapp.exe and TTSappVB.exe
;Gives normally with Microsoft Office speech module:("Microsoft Sam")
(setq NumVoices(length VoiceLst))
(if (@member+ "Sample TTS Voice" VoiceLst) (setq NumVoices (1- NumVoices)))
(if (> NumVoices 1)
(vlax-invoke Sapi "Speak" (strcat "Yes. We can do this, because there are "
(itoa NumVoices) " usable voices on this system.") 0)
(progn
(vlax-invoke Sapi "Speak" (strcat "No. We can't do this, because there is only one voice on this system. "
" Probably, only the speech module of Microsoft Office is installed.") 0)
(princ (strcat "\nWarning: For more voices, first install Microsoft Speech SDK 5.1"
"\nfrom http://www.microsoft.com/downloads/"))))
(if (@member+ "Microsoft Mary" VoiceLst) (setq VoiceMary "Microsoft Mary"))
(if (@member+ "Microsoft Mike" VoiceLst) (setq VoiceMike "Microsoft Mike"))
(if (@member+ "Microsoft Sam" VoiceLst) (setq VoiceSam "Microsoft Sam"))
(if (and VoiceMary VoiceMike VoiceSam)
(progn
(vlax-invoke Sapi "Speak" (@Voice&TextInput VoiceMike
"My name is Microsoft Mike, and I have a question for Sam.") 0)
(vlax-invoke Sapi "Speak" (@Voice&TextInput VoiceMike
"Sam, could you say something about Mary?") 0)
(vlax-invoke Sapi "Speak" (@Voice&TextInput VoiceSam
"My name is Microsoft Sam, and I can say that Mary has a little lamb.") 0)
(vlax-invoke Sapi "Speak" (@Voice&TextInput VoiceMary
"Hi, this is Microsoft Mary and I think that Sam is joking like Mister Thomas Edison.") 0)
(vlax-invoke Sapi "Speak" (@Voice&TextInput VoiceMike
"This information is very interesting, and I would like to thank you all.") 0)
)
(progn
(vlax-invoke Sapi "Speak" "Warning " 0)
(if (not VoiceMary) (vlax-invoke Sapi "Speak" "There is no Microsoft Mary." 0))
(if (not VoiceMike) (vlax-invoke Sapi "Speak" "There is no Microsoft Mike." 0))
(if (not VoiceSam) (vlax-invoke Sapi "Speak" "There is no Microsoft Sam." 0))
))
(@playwav Sapi "tada.wav")
(vlax-release-object sapi)
(princ)
)
(princ "SpeakCfg.lsp Loaded. \nRun with SPEAKCFG.")
The program SpeakCfg is Using the Speech Application Programming Interface
from Microsoft Speech SDK 5.1 (http://www.microsoft.com/downloads/") or the
speech module from Microsoft Office (network versions????).
For Sound Animations or Simulations see:
http://forums.augi.com/showthread.php?t=55713&page=4
23/09-2009 VBA version of SpeakCfg for using SAPI (Sound and Speech).
Regards,
HofCAD CSI