daily pastebin goal
38%
SHARE
TWEET

Untitled

a guest Feb 14th, 2018 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @property (strong, nonatomic) AVAudioEngine * audioEngine;
  2. @property (strong, nonatomic) SFSpeechRecognizer * speechRecognizer;
  3. @property (strong, nonatomic) SFSpeechAudioBufferRecognitionRequest * request;
  4. @property (strong, nonatomic) SFSpeechRecognitionTask * recognitionTask;
  5. @property (strong, nonatomic) AVAudioInputNode * node;
  6.    
  7. - (void) initialization {
  8. self.audioEngine = [[AVAudioEngine alloc] init];
  9.  
  10. NSLocale * local = [[NSLocale alloc] initWithLocaleIdentifier:@"ru_RU"];
  11. self.speechRecognizer = [[SFSpeechRecognizer alloc] initWithLocale:local];
  12.  
  13. [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
  14.     dispatch_async(dispatch_get_main_queue(), ^{
  15.         switch (status) {
  16.             case SFSpeechRecognizerAuthorizationStatusNotDetermined:
  17.                 NSLog(@"SFSpeechRecognizerAuthorizationStatusNotDetermined");
  18.                 break;
  19.             case SFSpeechRecognizerAuthorizationStatusRestricted:
  20.                 NSLog(@"SFSpeechRecognizerAuthorizationStatusRestricted");
  21.                 break;
  22.             case SFSpeechRecognizerAuthorizationStatusDenied:
  23.                 NSLog(@"SFSpeechRecognizerAuthorizationStatusDenied");
  24.                 break;
  25.             case SFSpeechRecognizerAuthorizationStatusAuthorized:
  26.                 NSLog(@"SFSpeechRecognizerAuthorizationStatusAuthorized");
  27.                 break;
  28.         }
  29.     });
  30. }];
  31.    
  32. - (void) startRecognitionWithCompletion:(void(^)(NSString *recodingString))completion {
  33. if (self.request == nil) {
  34.     self.request = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
  35. }
  36. self.node = self.audioEngine.inputNode;
  37. AVAudioFormat * recognitionFormat = [self.node outputFormatForBus:0];
  38.  
  39. [self.node installTapOnBus:0 bufferSize:1024 format:recognitionFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
  40.     [self.request appendAudioPCMBuffer:buffer];
  41. }];
  42.  
  43. [self.audioEngine prepare];
  44. [self.audioEngine startAndReturnError:nil];
  45.  
  46.  
  47. self.recognitionTask = [self.speechRecognizer recognitionTaskWithRequest:self.request resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
  48.     if (result.bestTranscription != nil) {
  49.         dispatch_async(dispatch_get_main_queue(), ^{
  50.             completion([result.bestTranscription formattedString]);
  51.         });
  52.     } else {
  53.         NSLog(@"%@", [error localizedDescription]);
  54.     }
  55. }];
  56.    
  57. - (void)stopRecording {
  58. dispatch_async(dispatch_get_main_queue(), ^{
  59.     if(self.audioEngine.isRunning){
  60.         if (self.node == [self.audioEngine inputNode]) {
  61.             [self.node removeTapOnBus:0];
  62.             [self.node reset];
  63.         }
  64.         [self.audioEngine stop];
  65.         [self.request endAudio];
  66.         [self.recognitionTask cancel];
  67.         self.recognitionTask = nil;
  68.         self.request = nil;
  69.     }
  70. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top