【问题标题】:How to Auto stop speech recognition if user stop speaking如果用户停止说话,如何自动停止语音识别
【发布时间】:2018-03-02 09:40:03
【问题描述】:

我正在开发 Bot 应用程序,这里我有 2 个功能

  • 语音转文字
  • 文字转语音

两者都按预期工作,但我想检测到当用户停止说话时我想停止检测并将该数据发送到服务器。

有什么方法可以让用户不说话?

我正在使用以下代码进行语音检测:

// Starts an AVAudio Session
    NSError *error;
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    [audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];

    // Starts a recognition process, in the block it logs the input or stops the audio
    // process if there's an error.
    recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
    AVAudioInputNode *inputNode = audioEngine.inputNode;
    recognitionRequest.shouldReportPartialResults = YES;
    recognitionTask = [speechRecognizer recognitionTaskWithRequest:recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
        BOOL isFinal = NO;
        if (result) {
            // Whatever you say in the microphone after pressing the button should be being logged
            // in the console.
            NSLog(@"RESULT:%@",result.bestTranscription.formattedString);
            self.inputToolbar.contentView.textView.text = result.bestTranscription.formattedString;
            self.inputToolbar.contentView.rightBarButtonItem.enabled = YES;
            isFinal = !result.isFinal;
        }
        if (error) {
            if (audioEngine != NULL) {
                [audioEngine stop];
                [inputNode removeTapOnBus:0];
                recognitionRequest = nil;
                recognitionTask = nil;
            }
        }
    }];

    // Sets the recording format
    AVAudioFormat *recordingFormat = [inputNode outputFormatForBus:0]; //[[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100 channels:1];
    [inputNode installTapOnBus:0 bufferSize:1024 format:recordingFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
        [recognitionRequest appendAudioPCMBuffer:buffer];
    }];

    // Starts the audio engine, i.e. it starts listening.
    [audioEngine prepare];
    [audioEngine startAndReturnError:&error];
    NSLog(@"Say Something, I'm listening");

如果有人需要更多详细信息,请告诉我。

提前致谢。

【问题讨论】:

  • 您如何使用 Pushpendra 获得解决方案。 @CodeChanger

标签: ios objective-c speech-recognition speech-to-text


【解决方案1】:

尝试使用这个:

AVAudioRecorder *recorder;
NSTimer *levelTimer;
double lowPassResults;

-(void)configureRecorder{
    // AVAudioSession already set in your code, so no need for these 2 lines.
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];

    NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];

    NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                          [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                          [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                          [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                          nil];

    NSError *error;

    lowPassResults = 0;

    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

    if (recorder) {
        [recorder prepareToRecord];
        recorder.meteringEnabled = YES;
        [recorder record];
        levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.05 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
    } else
        NSLog(@"%@", [error description]);
    }
}


- (void)levelTimerCallback:(NSTimer *)timer {
    [recorder updateMeters];

    const double ALPHA = 0.05;
    double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
    lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;  

    NSLog(@"lowPassResults: %f",lowPassResults);

    // Use here a threshold value to stablish if there is silence or speech
    if (lowPassResults < 0.1) {
        NSLog(@"Silence");
    } else if(lowPassResults > 0.5){
        NSLog(@"Speech");
    }
}

参考: http://codedrago.com/q/200783/ios-objective-c-speech-recognition-how-to-detect-speech-start-on-ios-speech-api

【讨论】:

  • 它按预期工作,但我有一个问题会影响内存管理,因为我们正在使用语音到文本 API,所以它已经消耗了更多的内存和 CPU 用于网络调用和录音,所以给你宝贵的 cmets如果可能的话,顺便说一句谢谢你的代码。
  • 我正在使用此代码,并且它在我的应用程序中运行良好。您仅使用录音机来检测静音,所以我认为它不会消耗更多内存。管理计时器和记录器只需要一件事。当您的任务完成时,使计时器无效并停止记录器。
  • 是的,让我试试这段代码并上线看看会发生什么,但根据你的经验,我不认为它会消耗更多内存。感谢您的回复。
  • @Pushpendra 如何使用这个将语音转换为文本??
  • @Pushpendra 如何在开始监听时调用 configureRecorder。我无法弄清楚。你能帮我实现相同的语音到文本识别吗?我也使用与为语音识别编写的 CodeChanger 相同的代码。我会很感激你的。无论您提到什么链接都不起作用
猜你喜欢
  • 1970-01-01
  • 2017-05-16
  • 1970-01-01
  • 1970-01-01
  • 2012-04-05
  • 1970-01-01
  • 1970-01-01
  • 2013-04-18
  • 2020-10-03
相关资源
最近更新 更多