Klaus
Number of posts : 18 Age : 74 Localisation : France Registration date : 2013-03-23
| Subject: A speaking calculator Sun Mar 24, 2013 2:34 pm | |
| As an example for DLL usage, and especially the usage of my KGF.dll, I have created a simple speaking calculator. The calculator announces the calculated results (hitting the "=" key), and tells Good-Bye il the speaker is activated. Clear comments explain the main features. The program uses 4 out of the more than 250 functions of KGE.dll: 1. SetSpeaker to control switching the speaker ON and OFF 2. SpeakMode to force synchronous speak mode 3. SpeakVoices to load the list of installed voices into a selector 3. Speak to pronounce the messages As a side effect, the program shows some programming techniques, such as dynamic object number creation allowing easy object insertion without numbering conflicts. Please remember: download KGF.dll and KGF_SUB.bas frm my WebDav (link and identifiers are in the signature of my post) and installation instructions are in the comments of the source. Have fun ! - Code:
-
' speaking_calculator.bas ' ' This program is a simple demonstration of the usage of KGF.dll ' via the functions managing the SAPI features (Speak API). ' The program demonstrates the syntax and use of DLL function call ' via Panoramic's DLL_CALLx function, and how to obtain the same ' result using de KGF_SUB.bas module which is a wrapper arround the ' KGF.dll functions. ' ' Functionally, the program does the following: ' 1. a check box provides activation or desactivation of the speaker ' 2. a combo box allows the selection of one of the installes voices ' 3. the simple calculator can be used by clicking on the exposed signs ' or entering the corresponding keys ' 4. the result is spoken, as well a good-by message ' ' Technical note: ' The SpeakVoices function returns a string containing the names of all ' installed voices, separated by CR LF. But you cannot load this directly ' into a combo box because the whole string will be affected to element 1. ' Thus, the string is loaded into a memo which correctly generates multiple ' lines, and these lines are then copied into the combo box. ' ' Requirements: ' please download the following files: ' KGF.dll - the DLL ' KGF_SUB.bas - contains wrapper procedures arround the DLL functions ' and copy them into the folder where the present sorce module resides
' main program labels() constants() variables() form0() menu() GUI() initializations()
end
sub labels() label activate_speaker, change_voice, exit, help, byebye label number_0, number_1, number_2, number_3, number_4 label number_5, number_6, number_7, number_8, number_9 label key_CE, key_point, key_negate label key_add, key_subtract, key_multiply, key_divide label key_percent, key_C, key_result end_sub
sub constants() dim dll$ : dll$ = "KGF.dll" end_sub
sub variables() dim res%, no%, no1%, no2%, i%, txt$, do_exit% dim no_activate_speaker%, no_voice_selection% dim no_calc_x% dim voices$, selected_voice% dim digit, fact, reset% dim decimal_flag% : ' remember the decimal point has been entered dim calc_x : ' displayed calculator register dim calc_y : ' hidden calculator register dim calc_x$ : ' string containing the keys composing calc_x dim operation% : ' operation to execute: 1=+ 2=- 3=* 4=/ 5== dim sign% : ' 1 = positive -1 = negative end_sub
sub form0() on_close 0,byebye caption 0,"Speaking calculator" width 0,270 : height 0,330 left 0,(screen_x-width(0))/2 top 0,(screen_y-height(0))/2 end_sub
sub menu() no% = no% + 1 : main_menu no% : no1% = no% ' file menu no% = no% + 1 : sub_menu no% : parent no%,no1% : no2% = no% caption no%,"File" no% = no% + 1 : sub_menu no% : parent no%,no2% caption no%,"Exit" : on_click no%,exit ' help menu no% = no% + 1 : sub_menu no% : parent no%,no1% : no2% = no1% caption no%,"Help" : on_click no%,help end_sub
sub GUI() ' check box for speaker activation no% = no% + 1 : check no% : no_activate_speaker% = no% top no%,10 : left no%,10 : width no%,80 caption no%,"Speaker ON" : on_click no%,activate_speaker ' combo for voice selection no% = no% + 1 : combo no% : no_voice_selection% = no% top no%,10 : left no%,100 : width no%,150 : on_change no%,change_voice
' edit for calculator display displaying the calc_x register no% = no% + 1 : edit no% : no_calc_x% = no% : inactive no% top no%,40 : left no%,10 : width no%,240 font_size no%,2 : font_bold no% ' calculator numeric keys no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"7" top no%,70 : left no%,10 : font_size no%,12 : font_bold no% on_click no%,number_7
no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"8" top no%,70 : left no%,60 : font_size no%,12 : font_bold no% on_click no%,number_8
no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"9" top no%,70 : left no%,110 : font_size no%,12 : font_bold no% on_click no%,number_9
no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"4" top no%,120 : left no%,10 : font_size no%,12 : font_bold no% on_click no%,number_4
no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"5" top no%,120 : left no%,60 : font_size no%,12 : font_bold no% on_click no%,number_5
no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"6" top no%,120 : left no%,110 : font_size no%,12 : font_bold no% on_click no%,number_6
no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"1" top no%,170 : left no%,10 : font_size no%,12 : font_bold no% on_click no%,number_1
no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"2" top no%,170 : left no%,60 : font_size no%,12 : font_bold no% on_click no%,number_2
no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"3" top no%,170 : left no%,110 : font_size no%,12 : font_bold no% on_click no%,number_3
no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"0" top no%,220 : left no%,60 : font_size no%,12 : font_bold no% on_click no%,number_0
' decimal point no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"." top no%,220 : left no%,110 : font_size no%,12 : font_bold no% on_click no%,key_point ' CE key no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"CE" top no%,220 : left no%,10 : font_size no%,12 : font_bold no% on_click no%,key_CE
' + key no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"+" top no%,70 : left no%,160 : font_size no%,12 : font_bold no% on_click no%,key_add
' - key no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"-" top no%,120 : left no%,160 : font_size no%,12 : font_bold no% on_click no%,key_subtract
' * key no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"*" top no%,170 : left no%,160 : font_size no%,12 : font_bold no% on_click no%,key_multiply
' / key no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"/" top no%,220 : left no%,160 : font_size no%,12 : font_bold no% on_click no%,key_divide
' +/- key no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"+/-" top no%,70 : left no%,210 : font_size no%,12 : font_bold no% on_click no%,key_negate
' % key no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"%" top no%,120 : left no%,210 : font_size no%,12 : font_bold no% on_click no%,key_percent
' C key no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"C" top no%,170 : left no%,210 : font_size no%,12 : font_bold no% on_click no%,key_C
' = key no% = no% + 1 : button no% : width no%,40 : height no%,40 : caption no%,"=" top no%,220 : left no%,210 : font_size no%,12 : font_bold no% on_click no%,key_result
end_sub
sub initializations() ' initialize the DLL environment KGF_initialize(dll$) ' res% = DLL_call1("KGF_initialize",adr(dll$))
memo no%+1 : hide no%+1 : ' temporary object ' collect the list of installed voices SpeakVoices() item_add no%+1,SpeakVoices$ ' voices$ = string$(2000," ") ' res% = dll_call1("SpeakVoices",adr(voices$)) ' item_add no%+1,Voices$ for i%=1 to count(no%+1) item_add no_voice_selection%,item_read$(no%+1,i%) next i% text no_voice_selection%,item_read$(no%+1,1) delete no%+1 ' set synchronous speek mode SpeakMode(0) ' res% = dll_call1("SpeakMode",0)
do_exit% = 0 fact = 1 decimal_flag% = 0 calc_x = 0 : calc_x$ = "" : calc_y = 0 text no_calc_x%,str$(calc_x) operation% = 1 sign% = 1 reset% = 0 end_sub
' file menu, exit function exit: if message_confirmation_yes_no("Do you really want to close the calculator ?")=1 txt$ = "Good bye !" Speak(txt$,70,0,selected_voice%) ' res% = dll_call4("Speak",adr(txt$),70,0,selected_voice%) do_exit% = 1 terminate end_if return ' forced program termination byebye: if do_exit%=0 txt$ = "I am sorry to leave you !" Speak(txt$,70,0,selected_voice%) ' res% = dll_call4("Speak",adr(txt$),70,0,selected_voice%) end_if return ' help menu help: txt$ = "Please choose your voice from the selector"+chr$(13)+chr$(10) txt$ = txt$ + "and activate the speaker from the check box !" message txt$ return
' change the speaker activation (from check box) activate_speaker: SetSpeaker(checked(no_activate_speaker%)) return
' change the voice profile (used by the Speak function) change_voice: selected_voice% = item_index(no_voice_selection%) return ' a number is entered number_0: number(0) return number_1: number(1) return number_2: number(2) return number_3: number(3) return number_4: number(4) return number_5: number(5) return number_6: number(6) return number_7: number(7) return number_8: number(8) return number_9: number(9) return
' a function key is entered: key_CE: calc_x = 0 : calc_x$ = "" fact = 1 decimal_flag% = 0 sign% = 1 text no_calc_x%,str$(calc_x) return key_C: calc_x = 0 : calc_x$ = "" calc_y = 0 operation% = 1 fact = 1 decimal_flag% = 0 sign% = 1 text no_calc_x%,str$(calc_x) return
key_point: if decimal_flag%=0 decimal_flag% = 1 fact = 10 calc_x$ = calc_x$ + "." text no_calc_x%,calc_x$ end_if return key_add: do_operation(1) return key_subtract: do_operation(2) return
key_multiply: do_operation(3) return
key_divide: do_operation(4) return key_result: do_operation(5) txt$ = "<context ID = "+chr$(34)+"number_decimal"+chr$(34)+">"+str$(calc_y)+"</context>" display : ' wait for result display before announcing the result Speak(txt$,70,0,selected_voice%) ' res% = dll_call4("Speak",adr(txt$),70,0,selected_voice%) return
key_percent: calc_x = calc_y*calc_x/100 sign% = 0 decimal_flag% = 0 calc_x$ = "" fact = 1 reset% = 1 if calc_x=0 text no_calc_x%,"0" else text no_calc_x%,str$(calc_x) end_if return
key_negate: if sign%=1 sign% = -1 calc_x$ = "-" + calc_x$ else sign% = 1 calc_x$ = mid$(calc_x$,2,len(calc_x$)) end_if calc_x = 0 - calc_x text no_calc_x%,calc_x$ return
sub number(nb) if reset% = 1 calc_x = 0 calc_x$ = "" reset% = 0 end_if digit = nb if decimal_flag%=0 calc_x = calc_x*10 else digit = digit/fact fact = fact*10 end_if if sign%=1 if calc_x<0 then calc_x = 0 - calc_x else if calc_x>0 then calc_x = 0 - calc_x end_if calc_x$ = calc_x$ + str$(nb) calc_x = calc_x + digit text no_calc_x%,calc_x$ end_sub
sub do_operation(what%) select operation% case 1: calc_y = calc_y + calc_x case 2: calc_y = calc_y - calc_x case 3: calc_y = calc_y * calc_x case 4: calc_y = calc_y / calc_x case 5: operation% = 1 end_select operation% = what% calc_x$ = "" calc_x = 0 decimal_flag% = 0 fact = 1 text no_calc_x%,str$(calc_y) end_sub
#INCLUDE "KGF_SUB.bas"
| |
|