Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proper partial s2t for less latency on iOS #32

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,25 @@ - (void)startRecording {
inputNode = audioEngine.inputNode;

recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
recognitionRequest.shouldReportPartialResults = NO;
recognitionRequest.shouldReportPartialResults = YES;
recognitionTask =[speechRecognizer recognitionTaskWithRequest:recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error)
{
if (result) {
NSString *transcriptText = result.bestTranscription.formattedString;
NSLog(@"STARTRECORDING RESULT: %@", transcriptText);
if (result.isFinal) {
UnitySendMessage("SpeechToText", "onResults", [transcriptText UTF8String]);
}
}
else {
[audioEngine stop];
recognitionTask = nil;
recognitionRequest = nil;
UnitySendMessage("SpeechToText", "onResults", "nil");
NSLog(@"STARTRECORDING RESULT NULL");
}
}];

AVAudioFormat *format = [inputNode outputFormatForBus:0];

[inputNode installTapOnBus:0 bufferSize:1024 format:format block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
Expand All @@ -96,27 +114,15 @@ - (void)startRecording {
[audioEngine prepare];
NSError *error1;
[audioEngine startAndReturnError:&error1];
NSLog(@"errorAudioEngine.description: %@", error1.description);

if (error1.description) {
NSLog(@"errorAudioEngine.description: %@", error1.description);
}
}
}

- (void)stopRecording {
if (audioEngine.isRunning) {
recognitionTask =[speechRecognizer recognitionTaskWithRequest:recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error)
{
if (result != nil) {
NSString *transcriptText = result.bestTranscription.formattedString;
UnitySendMessage("SpeechToText", "onResults", [transcriptText UTF8String]);
NSLog(@"STOPRECORDING RESULT: %@", transcriptText);
}
else {
[audioEngine stop];
recognitionTask = nil;
recognitionRequest = nil;
UnitySendMessage("SpeechToText", "onResults", "nil");
NSLog(@"STOPRECORDING RESULT NULL");
}
}];
if (audioEngine.isRunning) {
[inputNode removeTapOnBus:0];
[audioEngine stop];
[recognitionRequest endAudio];
Expand Down