Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
909 views
in Technique[技术] by (71.8m points)

ios - Convert array of floats to AVAudioPCMBuffer for speed recognition

I have an array of floats that is raw audio data from a 3rd party source. I would like to pass this through to a Speech Recognition request via appendAudioPCMBuffer but that accepts an AVAudioPCMBuffer. How could I convert my NSMutableArray to AVAudioPCMBuffer?

For reference, this is how the buffer variable gets created before its passed to this function. It is written in C.

void CallNativePlugin( const float buffer[], int size ) {
    NSMutableArray *myArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < size; i++) {
        NSNumber *number = [[NSNumber alloc] initWithFloat:buffer[i]];
        [myArray addObject:number];
        [delegateObject recognizeSpeechFromBuffer:myArray ];
    }
}

Then the current code I have to take that buffer and pass is to the speech recognizer (objective-c):

-(void) recognizeSpeechFromBuffer: (NSMutableArray*) buffer {
    NSLog( @"Array length: %lu@", (unsigned long) buffer.count );
  
    recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
    recognitionRequest.shouldReportPartialResults = YES;
    recognitionTask = [speechRecognizer recognitionTaskWithRequest:recognitionRequest.resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
        BOOL isFinal = NO;
        if (result) {
            NSLog(@"RESULT:%@",result.bestTranscription.formattedString);
            isFinal = !result.isFinal;
        }
        if (error) {
            recognitionRequest = nil;
            recognitionTask = nil;
        }
    }];

    // Do something like [recognitionRequest appendAudioPCMBuffer:buffer];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since Objective-C is available in CallNativePlugin, you could rework it to create the AVAudioPCMBuffer there, assuming you know the audio sample rate and can adjust the delegate protocol.

// Assuming mono, sample rate ????
void CallNativePlugin( const float buffer[], int size ) {
    AVAudioFormat *fmt = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32 sampleRate:/* ?? */ channels:1 interleaved:YES];
    AVAudioPCMBuffer *buf = [[AVAudioPCMBuffer alloc] initWithPCMFormat:fmt frameCapacity:size];
    memcpy(buf.floatChannelData[0], buffer, sizeof(float) * size);
    buf.frameLength = size;
    [delegateObject recognizeSpeechFromPCMBuffer:buf];
}

If that isn't possible you can create an AVAudioPCMBuffer in -recognizeSpeechFromPCMBuffer: similarly and assign the floats individually.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...