gpt4 book ai didi

ios - 如果用户停止说话,如何自动停止语音识别

转载 作者:行者123 更新时间:2023-12-01 17:47:20 24 4
gpt4 key购买 nike

我正在开发 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");

    让我知道是否有人需要更多详细信息。

    提前致谢。

    最佳答案

    尝试使用这个:

    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

    关于ios - 如果用户停止说话,如何自动停止语音识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46337097/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com