Library Speak_lib

Share your advanced PureBasic knowledge/code with the community.
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Library Speak_lib

Post by dobro »

Here, the complete source of my library Speak
that works with any SAPI5 voices and also Espeak

to appoint (after passing through Tailbite) "lib_Speak"
in the folder "\ PureLibraries \ UserLibraries"

Code: Select all


; Lire une chaine dans le registre
Procedure.s ReadRegKey(OpenKey.l,SubKey.s,ValueName.s)  ; ====================================== ReadRegKey 
	; 
	hKey.l=0
	keyvalue.s=Space(255)
	DataSize.l=255
	
	If RegOpenKeyEx_(OpenKey,SubKey,0,#KEY_READ,@hKey)
		keyvalue=""
		Else
		If RegQueryValueEx_(hKey,ValueName,0,0,@keyvalue,@DataSize)
			keyvalue=""
			Else 
			keyvalue=Left(keyvalue,DataSize-1)
		EndIf
		RegCloseKey_(hKey)
	EndIf
	
	ProcedureReturn keyvalue
EndProcedure 



ProcedureDLL.s getvoix( )
	
	; retourne une chaine qui commence par le nombre de voix, suivi des noms des voix,separé par des virgules
	; on peux tout recuperer avec stringfield ;o)
	idx = 0 
	nom.s=""
	NetBTKeyName.s = "SOFTWARE\Microsoft\Speech\Voices\Tokens" 
	NetBTKeyHandle.l = 0 
	RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, @NetBTKeyName, 0, #KEY_READ, @NetBTKeyHandle)  
	;Debug NetBTKeyHandle 
	keyLength = 255 
	keyname.s = Space(keyLength) 
	idx = 0 
	While RegEnumKeyEx_(NetBTKeyHandle, idx, @keyname, @keyLength, 0, 0, 0, 0) = 0 
		NetBTKeyName.s = "SOFTWARE\Microsoft\Speech\Voices\Tokens\"+keyname.s+"\Attributes\" 
		keyn.s= ReadRegKey(#HKEY_LOCAL_MACHINE,  NetBTKeyName.s,"Name")
		
		nom.s=nom.s+keyn+"," 
		idx + 1 
		keyLength = 255  
		keyn=""
	Wend 
	nom.s=Str(idx)+","+nom.s
	nom.s=Left(nom.s,Len(nom.s)-1)
	ProcedureReturn nom.s
EndProcedure

ProcedureDLL Speak(voix$,Text$,volume=100,vitesse=-3,pitch=0)
	If voix$<>""
		vox.s="<voice required="+Chr(34)+"Name="+ voix$+Chr(34)+"/>"+"<pitch absmiddle="+Chr(34)+Str(pitch)+Chr(34)+"/>" 
		Else
		vox.s=""
	EndIf
	
	Interface ISpNotifySource Extends IUnknown
		SetNotifySink(a)
		SetNotifyWindowMessage(a,b,c,d)
		SetNotifyCallbackFunction(a,b,c)
		SetNotifyCallbackInterface(a,b,c)
		SetNotifyWin32Event()
		WaitForNotifyEvent(a)
		GetNotifyEventHandle()
	EndInterface
	
	Interface ISpEventSource Extends ISpNotifySource
		SetInterest(a,b)
		GetEvents(a,b,c)
		GetInfo(a)
	EndInterface
	
	Interface ISpVoice Extends ISpEventSource
		SetOutput(a,b)
		GetOutputObjectToken(a)
		GetOutputStream(a)
		Pause()
		Resume()
		SetVoice(a)
		GetVoice(a)
		Speak(a,b,c)
		SpeakStream(a,b,c)
		GetStatus(a,b)
		Skip(a,b,c)
		SetPriority(a)
		GetPriority(a)
		SetAlertBoundary(a)
		GetAlertBoundary(a)
		SetRate(a)
		GetRate(a)
		SetVolume(a)
		GetVolume(a)
		WaitUntilDone(a)
		SetSyncSpeakTimeout(a)
		GetSyncSpeakTimeout(a)
		SpeakCompleteEvent()
		IsUISupported(a,b,c,d)
		DisplayUI(a,b,c,d,e)
	EndInterface
	Global VoiceObject.ISpVoice 
	#CLSCTX_INPROC_SERVER  = $1
	#CLSCTX_INPROC_HANDLER = $2
	#CLSCTX_LOCAL_SERVER   = $4
	#CLSCTX_REMOTE_SERVER  = $10
	#CLSCTX_ALL = (#CLSCTX_INPROC_SERVER|#CLSCTX_INPROC_HANDLER|#CLSCTX_LOCAL_SERVER|#CLSCTX_REMOTE_SERVER)
	
	CoInitialize_(0)
	If CoCreateInstance_(?CLSID_SpVoice, 0, #CLSCTX_ALL, ?IID_ISpVoice, @VoiceObject.ISpVoice) = 0  
		VoiceObject\SetVolume(volume)
		VoiceObject\SetRate(vitesse)
		;VoiceObject\SetVoice({0879A4E0-A92C-11d1-B17B-0020AFED142E}) ; voix 0 
		length = Len(vox.s+Text$)*2+10
		*mem = AllocateMemory(length)
		Text$=vox.s+Text$
		MultiByteToWideChar_(#CP_ACP ,0,Text$,-1,*mem, length)
		VoiceObject\Speak(*mem,0,0)
		FreeMemory(*mem)
		Else
		MessageRequester("ERROR","MS SPEECH API not installed",0)
	EndIf
	
	
	DataSection
		CLSID_SpVoice:
		;96749377-3391-11D2-9EE3-00C04F797396
		Data.l $96749377
		Data.w $3391,$11D2
		Data.b $9E,$E3,$00,$C0,$4F,$79,$73,$96
		IID_ISpVoice:
		;6C44DF74-72B9-4992-A1EC-EF996E0422D4
		Data.l $6C44DF74
		Data.w $72B9,$4992
		Data.b $A1,$EC,$EF,$99,$6E,$04,$22,$D4
	EndDataSection 
	
EndProcedure
; 
; EPB 




Use:
Speak(the name of the voice$,say the sentence$,Volume ,Speed Diction,Pitch)
Speed Diction and Pitch , depends on the voice used


Examples :



; **********************************************************************************
; in French
Speak( "ScanSoft Virginie_Dri40_16kHz" , "bonjour" , 100, 2, 150) ; if the voice Virginie installed
Speak( "eSpeak-FR" , "bonjour" , 100, 2, 150) ; if the voice Espeak installed
Speak( "eSpeak-FR+F2" , "bonjour" , 100, 2, 150) ; if the voice Espeak installed

; **********************************************************************************
; in english
Speak( "eSpeak-en" , "Hello" , 100, 2, 150) ; if the voice Espeak installed

Speak( "Microsoft Anna" , "hello" , 100, 2, 150) ; if the voice Microsoft Anna installed
; ***********************************************************************************
   
; EPB




the list of installed voices:
just With
voix$=getvoix( )





voix$=getvoix( ) ; on recupere le nombre et le nom des voix installé
; getvoix() retourne une chaine "nbr_de_voix,voix1$,voix2$,voix3$........ etc "

nombre = Val(StringField(voix$, 1, "," )) ; recupere le nombre de voix installé (premier element de la chaine)
voix$=Right(voix$,Len(voix$)- 2) ; on retire le nombre de voix, et sa virgule de la liste des noms de voix


For i= 1 To nombre ;
             Debug StringField(voix$,i, "," ) ; ressort tout les noms des voix installées
Next i


These are the names that will be used with fonction Speak ()

:)
Last edited by dobro on Wed Aug 22, 2012 12:25 pm, edited 2 times in total.
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: Library Speak_lib

Post by kvitaliy »

dobro wrote:SetNotifyWin 32Event()
PB v4.61, winXP SP3:
Bad name
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Library Speak_lib

Post by Fangbeast »

kvitaliy wrote:
dobro wrote:SetNotifyWin 32Event()
PB v4.61, winXP SP3:
Bad name

Remove the space in the name I.e: change "SetNotifyWin 32Event()" to "SetNotifyWin32Event()"
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Library Speak_lib

Post by dobro »

sorry, my code coloring, added a space :lol:

fixed ;)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
hichem
User
User
Posts: 26
Joined: Sun Sep 04, 2005 4:18 pm

Re: Library Speak_lib

Post by hichem »

nice lib thank's dobro
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Library Speak_lib

Post by dobro »

thank's to you ( 113 view , 1 Thank ) ... :? :lol:
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: Library Speak_lib

Post by VB6_to_PBx »

dobro wrote:thank's to you ( 113 view , 1 Thank ) ... :? :lol:

dobro , Thank you very much for your Code !

i tested it with Microsoft Anna :
Speak( "Microsoft Anna" , "hello , i am Microsoft Anna , what shall i speak for you ?" , 100, 1, 150)

i'm currently using TextSpeaker v3.19 by DeskShare

and i have following Voices installed :

Microsoft Anna

LH Michelle
LH Michael

AT&T Mike 16
AT&T Crystal 16

IVONA Brian - British voice
IVONA Amy - British voice

my favorites are IVONA's Brian and Amy

is there a way to use IVONA's voices in your Code ?
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Library Speak_lib

Post by dobro »

only if the voices are standards SAPI4 or SAPI5 ... ;)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Library Speak_lib

Post by IdeasVacuum »

Strange, it doesn't want to work on my XP PC - correct MS voice (tested via Control Panel), no error messages................

Debug NetBTKeyHandle does nothing?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Library Speak_lib

Post by dobro »

for certain voices, you must:

Ordinateur > c:\ Windows > SysWOW64 > Speech >SpeechUX
et double click on the file "sapi.cpl" (This is to be done only once)

the lib in my system work with XP Sp3 and Seven 64bit (build 32) ...

correct MS voice
what voice?
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Library Speak_lib

Post by Kwai chang caine »

Thanks for sharing 8)

Actual score = 181 view , 3 Thanks :mrgreen:
ImageThe happiness is a road...
Not a destination
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: Library Speak_lib

Post by kvitaliy »

It is possible to implement library similar on functionality with PureTTS library?
Old library for PureBasic 4.00 doesn't work with the modern PureBasic versions :(
I will remind the main possibilities:
TTSEnd
TTSEngCount
TTSEngLang
TTSEngMfg
TTSEngName
TTSGetPitch
TTSGetSpeed
TTSGetVolume
TTSInit
TTSPause
TTSResume
TTSSAPIVer
TTSSelect
TTSSetPitch
TTSSetSpeed
TTSSetVolume
TTSSpeak
TTSStop

And as a recording capability in the file with the increased speed.
In advance many thanks!
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Library Speak_lib

Post by IdeasVacuum »

"Microsoft Sam", which is delivered with XP.

Must add to the score and say thank you for sharing this code 8)

Edit: the WOW path does not exist on my PC, sapi.cpl is in C:\WINDOWS\ServicePackFiles\i386
double-clicking it pops-up a message: "The requested task cannot be carried out because the necessary engine could not be created. Please select a different engine and/or a different audio device." Clicking on OK in the message box launches the Control Panel Speech Properties dialog, from which the "Microsoft Sam" voice tests OK.

The registry path does exist and MS Sam is there, along with LH Michael and LH Michelle - all three do not want to work with the lib though :|
Last edited by IdeasVacuum on Thu Aug 23, 2012 11:12 pm, edited 1 time in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Library Speak_lib

Post by dobro »

Just be careful not to compile mode Unicod :)

Unicod mode does not work ...


This library is the result of code of many people (including myself), the French forum
(Nico,)
and here (code of ts-soft)

http://www.purebasic.fr/french/viewtopi ... e&start=15


otherwise here:
http://www.purebasic.fr/french/viewtopi ... ese+vocale
Coolman was an adaptation of the library PureTTS
to write to a file

for information, Nico began work on a large Speech Recognition
here:
http://www.purebasic.fr/french/viewtopi ... lit=vocale

currently works for the French ....

Nico, I did not want to talk about here ...
but after all ; the codes are available on the French Forum, for several months , you are free
to go ...
Last edited by dobro on Thu Aug 23, 2012 11:23 pm, edited 1 time in total.
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Library Speak_lib

Post by dobro »

IdeasVacuum wrote:]"The requested task cannot be carried out because the necessary engine could not be created.
he'll need to install the engine Sapi 4 and 5 Sapi

http://www.audiochannel.net/software/SPCHAPI.EXE < -------
http://www.audiochannel.net/software/mstts.exe

http://www.nch.com.au/speech/fr/index.html
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
Post Reply