-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFMX.SpeechToText.pas
99 lines (81 loc) · 3.29 KB
/
FMX.SpeechToText.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
unit FMX.SpeechToText;
interface
uses
System.Messaging,
Androidapi.Helpers,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Speech,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.App;
type
TSpeechHandler = procedure(AResult: string) of object;
type
TSpeech_Text = class
private
const
FREQUEST_CODE_SPEECH = 205;
class var FProcResult: TSpeechHandler;
class procedure SetProcSpeechHandler(AHandler: TSpeechHandler);
class procedure HandleActivityMessage(const Sender: TObject; const M: TMessage);
class procedure HandleActivityResult(ARequestCode, AResultCode: Integer; AData: JIntent);
public
class procedure StartRecognition(ADisplay: string; AProcFinish: TSpeechHandler);
end;
implementation
{ TSpeech_Text }
class procedure TSpeech_Text.SetProcSpeechHandler(AHandler: TSpeechHandler);
begin
FProcResult := AHandler;
end;
class procedure TSpeech_Text.StartRecognition(ADisplay: string; AProcFinish: TSpeechHandler);
var
LRequestIntent: JIntent;
begin
SetProcSpeechHandler(AProcFinish);
TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification, HandleActivityMessage);
// Create an Intent object for speech recognition
LRequestIntent := TJIntent.Create;
LRequestIntent.setAction(TJRecognizerIntent.JavaClass.ACTION_RECOGNIZE_SPEECH);
// Set language model and number of expected results
LRequestIntent.putExtra(TJRecognizerIntent.JavaClass.EXTRA_LANGUAGE_MODEL, TJRecognizerIntent.JavaClass.LANGUAGE_MODEL_FREE_FORM);
LRequestIntent.putExtra(TJRecognizerIntent.JavaClass.EXTRA_LANGUAGE, StringToJString('pt-BR'));
LRequestIntent.putExtra(TJRecognizerIntent.JavaClass.EXTRA_MAX_RESULTS, 1);
// Set prompt message to be displayed to the user
LRequestIntent.putExtra(TJRecognizerIntent.JavaClass.EXTRA_PROMPT, StringToJString(ADisplay));
// Start activity for speech recognition
SharedActivity.StartActivityForResult(LRequestIntent, FREQUEST_CODE_SPEECH);
end;
class procedure TSpeech_Text.HandleActivityResult(ARequestCode, AResultCode: Integer; AData: JIntent);
var
LResults: JArrayList;
LResultText: string;
LResultObject: JObject;
begin
// Check if request code and result are correct
if (ARequestCode = FREQUEST_CODE_SPEECH) and (AResultCode = TJActivity.JavaClass.RESULT_OK) then
begin
// Get the results of speech recognition
LResults := AData.getStringArrayListExtra(TJRecognizerIntent.JavaClass.EXTRA_RESULTS);
// If there are results, get the first one (the most likely one)
if Assigned(LResults) and (LResults.size > 0) then
begin
// Convert JObject to JString
LResultObject := LResults.get(0);
if Assigned(lResultObject) then
begin
TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, HandleActivityMessage);
LResultText := JStringToString(TJString.Wrap(lResultObject));
// Do something with the recognized text (resultText)
if Assigned(FProcResult) then
FProcResult(LResultText);
end;
end;
end;
end;
class procedure TSpeech_Text.HandleActivityMessage(const Sender: TObject; const M: TMessage);
begin
if M is TMessageResultNotification then
HandleActivityResult(TMessageResultNotification(M).RequestCode, TMessageResultNotification(M).ResultCode,
TMessageResultNotification(M).Value);
end;
end.