I'm currently trying to implement a speech module in PB. Most things work, however I have a few interesting issues when I try to implement a queue:
1. My didFinishSpeaking function is only called if I don't use a synth function as a parameter to the loop. E.g. if I use isSpeaking, the program just exits or hangs.
2. If my didFinishSpeaking function in the delegate class gets called, the argument values do not mach. E.g. for 'success' I always get 69 and the synth argument's value is not the same as the synth class's I created.
Note:
For this to run, you will need Wilbert's Cocoa lib at http://www.purebasic.fr/english/viewtop ... 19&t=50688
At the moment, the code has a memory leak, as both the synth and the delegate will not be released.
Code: Select all
Global pbNSSpeechSynthesizerDelegateClass, synthDelegate , RunLoop
Procedure.i create(voice.s)
If voice<>""
ProcedureReturn CocoaMessage(0, CocoaMessage(0, 0, "NSSpeechSynthesizer alloc"), "initWithVoice:$", @voice)
Else
ProcedureReturn CocoaMessage(0, CocoaMessage(0, 0, "NSSpeechSynthesizer alloc"), "init")
EndIf
EndProcedure
Procedure Speak(synth.i, text.s)
CocoaMessage(0,synth, "startSpeakingString:$", @text)
EndProcedure
Procedure Speaking(synth.i)
ProcedureReturn CocoaMessage(0, synth, "isSpeaking")
EndProcedure
Procedure availableVoices (Array avArray.s(1))
Protected i, av.i, vc
av=CocoaMessage(0, 0, "NSSpeechSynthesizer availableVoices")
vc=CocoaMessage(0,av,"count")
If vc
ReDim avArray(vc-1)
For i = 0 To vc - 1
avArray(i)=PeekS(CocoaMessage(0, CocoaMessage(0, av, "objectAtIndex:", i), "UTF8String"), -1, #PB_UTF8)
Next i
ProcedureReturn 1
EndIf
ProcedureReturn 0
EndProcedure
ProcedureC didFinishSpeaking(synth.i,finishedSpeaking.a)
Debug "finished: "+Str(finishedSpeaking)+" synth is "+Str(synth)
EndProcedure
Procedure Init()
pbNSSpeechSynthesizerDelegateClass = oCreateClass(oClass("NSObject"), "pbNSSpeechSynthesizerDelegateClass")
oClassAddMethod(pbNSSpeechSynthesizerDelegateClass, oSel("speechSynthesizer:didFinishSpeaking:"), @didFinishSpeaking(), "v@:@@")
oRegisterClass(pbNSSpeechSynthesizerDelegateClass)
synthDelegate = CocoaMessage(0, 0, "pbNSSpeechSynthesizerDelegateClass new")
RunLoop=cocoaMessage(0,0,"NSRunLoop currentRunLoop")
EndProcedure
Procedure new(voice.s="")
Protected synth.i=create(voice)
If synth
cocoaMessage(0,synth,"setDelegate:@",@synthDelegate)
Debug Str(synth)
ProcedureReturn synth
EndIf
ProcedureReturn 0
EndProcedure
Procedure freeSynth(synth)
cocoaMessage(0,synth,"release")
EndProcedure
Procedure Free()
cocoaMessage(0,synthDelegate,"release")
cocoaMessage(0,pbNSSpeechSynthesizerDelegateClass,"release")
EndProcedure
Init()
synth=new()
Speak(synth,"This is a test.")
While 1
CocoaMessage(0, runloop, "run")
Delay(5)
Wend
Delay(2000)
freeSynth(synth)
Free()