Speech Integration

I am trying to convert speech to text and display it in a UILabel using a speech frame. I allowed the user to allow the use of a microphone.

Here is my code

- (void)startRecording {
if (_recognitionTask != nil) {
[_recognitionTask cancel];
_recognitionTask = nil;
}

NSError *error;

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:&error];
[audioSession setMode:AVAudioSessionModeMeasurement error:&error];
[audioSession setActive:YES error:&error];

_recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
_recognitionTask = [[SFSpeechRecognitionTask alloc] init];

AVAudioInputNode *inputNode = [_audioEngine inputNode];

_recognitionRequest.shouldReportPartialResults = YES;

_recognitionTask = [_speechRecognizer     recognitionTaskWithRequest:_recognitionRequest resultHandler:^(SFSpeechRecognitionResult *result, NSError  *error) {
BOOL isFinal = NO;

if (result != nil) {
   _textLabel.text = [[result bestTranscription] formattedString];
   isFinal = result.isFinal;
}

NSLog(@"%@", error);

if (error != nil || isFinal) {
  _textLabel.text = [NSString stringWithFormat:@"%@", error];
  [inputNode removeTapOnBus:0];
  [_audioEngine stop];
  _recognitionRequest = nil;
  _recognitionTask = nil;
}
}];

[_audioEngine prepare];
[_audioEngine startAndReturnError:nil];
}

During debugging, it enters the TaskWithRequest recognition block, but the result is zero, and I get the error as follows:

Domain Error = kAFAssistantErrorDomain Code = 203 "Corrupt" UserInfo = {NSUnderlyingError = 0x14651450 {Error Domain = SiriSpeechErrorDomain Code = 102 "(null)"}, NSLocalizedDescription = Corrupt}

+4
source share
1 answer

, , , , [_audioEngine prepare];

[_audioEngine.inputNode installTapOnBus:0 bufferSize:1024 format:[inputNode inputFormatForBus:0] block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when){
                //NSLog(@"Tapped");
                [self.recognitionRequest appendAudioPCMBuffer:buffer];
            }];

. , .

0

Source: https://habr.com/ru/post/1665686/


All Articles