A request to recognize speech provided in audio buffers.


For example, to recognize audio from the device's microphone, create a request using the SFSpeechAudioBufferRecognitionRequest class and provide a sequence of in-memory audio buffers. You can append audio buffers to the request as they become available. Listing 1 shows how to append audio buffers (see SpeakToMe: Using Speech Recognition with AVAudioEngine for the complete project).

Listing 1

Appending audio buffers to a request

// Get an instance of AVAudioEngine and set up an audio session (not shown).
// Create a recognition request.
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()

// Configure the request so that results are returned before audio recording is finished.
recognitionRequest.shouldReportPartialResults = true

// Set up a recognition task and enable a recording button (not shown).
// Set up to append buffers.
let recordingFormat = inputNode.outputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in

// Prepare and start the audio engine (not shown).


Appending Audio Buffers

func append(AVAudioPCMBuffer)

Appends audio in the PCM format to the end of the recognition request.

func appendAudioSampleBuffer(CMSampleBuffer)

Appends audio to the end of the recognition request.

func endAudio()

Indicates that the audio source is finished and no more audio will be appended to the recognition request.

Getting the Audio Format

var nativeAudioFormat: AVAudioFormat

The preferred audio format for optimal speech recognition.


Conforms To

See Also

Requesting Recognition and Monitoring Progress

class SFSpeechRecognitionRequest

A request to recognize speech from an audio source.

class SFSpeechURLRecognitionRequest

A request to recognize speech in a recorded audio file.

class SFSpeechRecognitionTask

A speech recognition task that lets you monitor recognition progress.