Capturing in-car audio allows developers to interact with users by requesting raw audio data provided to them from the car's microphones. In order to gather the raw audio from the vehicle, you must leverage the SDLPerformAudioPassThru RPC.
SDL does not support automatic speech cancellation detection, so if this feature is desired, it is up to the developer to implement. The user may press an "OK" or "Cancel" button, the dialog may timeout, or you may close the dialog with SDLEndAudioPassThru.
SDL does not support an open microphone. However, SDL is working on wake-word support in the future. You may implement a voice command and start an audio pass thru session when that voice command occurs.
To initiate audio capture, first construct a SDLPerformAudioPassThru request. You must use a sampling rate, bit rate, and audio type supported by the head unit. To get the head unit's supported audio capture capabilities, check the SDLSystemCapabilityManager.audioPassThruCapabilities after successfully connecting to the head unit.
| Audio Pass Thru Capability | Parameter Name | Description |
|---|---|---|
| Sampling Rate | samplingRate | The sampling rate |
| Bits Per Sample | bitsPerSample | The sample depth in bits |
| Audio Type | audioType | The audio type |
SDLPerformAudioPassThru *audioPassThru = [[SDLPerformAudioPassThru alloc] initWithInitialPrompt:@"<#A speech prompt when the dialog appears#>" audioPassThruDisplayText1:@"<#Ask me \"What's the weather?\"#>" audioPassThruDisplayText2:@"<#or \"What is 1 + 2?\"#>" samplingRate:SDLSamplingRate16KHZ bitsPerSample:SDLBitsPerSample16Bit audioType:SDLAudioTypePCM maxDuration:<#Time in milliseconds to keep the dialog open#> muteAudio:YES]; [self.sdlManager sendRequest:audioPassThru];
let audioPassThru = SDLPerformAudioPassThru(initialPrompt: "<#A speech prompt when the dialog appears#>", audioPassThruDisplayText1: "<#Ask me \"What's the weather?\"#>", audioPassThruDisplayText2: "<#or \"What is 1 + 2?\"#>", samplingRate: .rate16KHZ, bitsPerSample: .sample16Bit, audioType: .PCM, maxDuration: <#Time in milliseconds to keep the dialog open#>, muteAudio: true) sdlManager.send(audioPassThru)

SDL provides audio data as fast as it can gather it, and sends it to the developer in chunks. In order to retrieve this audio data, the developer must add a handler to the SDLPerformAudioPassThru.
This audio data is only the current chunk of audio data, so the developer must be in charge of managing previously retrieved audio data.
SDLPerformAudioPassThru *audioPassThru = [[SDLPerformAudioPassThru alloc] initWithInitialPrompt:@"<#A speech prompt when the dialog appears#>" audioPassThruDisplayText1:@"<#Ask me \"What's the weather?\"#>" audioPassThruDisplayText2:@"<#or \"What is 1 + 2?\"#>" samplingRate:SDLSamplingRate16KHZ bitsPerSample:SDLBitsPerSample16Bit audioType:SDLAudioTypePCM maxDuration:<#Time in milliseconds to keep the dialog open#> muteAudio:YES]; audioPassThru.audioDataHandler = ^(NSData * _Nullable audioData) { // Do something with current audio data. if (audioData.length == 0) { return; } <#code#> } [self.sdlManager sendRequest:audioPassThru];
let audioPassThru = SDLPerformAudioPassThru(initialPrompt: "<#A speech prompt when the dialog appears#>", audioPassThruDisplayText1: "<#Ask me \"What's the weather?\"#>", audioPassThruDisplayText2: "<#or \"What is 1 + 2?\"#>", samplingRate: .rate16KHZ, bitsPerSample: .sample16Bit, audioType: .PCM, maxDuration: <#Time in milliseconds to keep the dialog open#>, muteAudio: true) audioPassThru.audioDataHandler = { (data) in // Do something with current audio data. guard let audioData = data else { return } <#code#> } sdlManager.send(audioPassThru)
The format of audio data is described as follows:
- It does not include a header (such as a RIFF header) at the beginning.
- The audio sample is in linear PCM format.
- The audio data includes only one channel (i.e. monaural).
- For bit rates of 8 bits, the audio samples are unsigned. For bit rates of 16 bits, the audio samples are signed and are in little-endian.
SDLPerformAudioPassThru is a request that works in a different way than other RPCs. For most RPCs, a request is followed by an immediate response, with whether that RPC was successful or not. This RPC, however, will only send out the response when the audio pass thru has ended.
Audio capture can be ended in 4 ways:
Audio pass thru has timed out.
resultCode of SUCCESS. You should handle the audio pass thru though it was successful.Audio pass thru was closed due to user pressing "Cancel".
resultCode of ABORTED. You should ignore the audio pass thru.Audio pass thru was closed due to user pressing "Done".
resultCode of SUCCESS. You should handle the audio pass thru as though it was successful.Audio pass thru was ended due to the developer ending the request.
SDLEndAudioPassThru RPC. You will receive a resultCode of SUCCESS, and should handle the audio pass thru as though it was successful.SDLEndAudioPassThru *endAudioPassThru = [[SDLEndAudioPassThru alloc] init]; [self.sdlManager sendRequest:endAudioPassThru];
let endAudioPassThru = SDLEndAudioPassThru() sdlManager.send(endAudioPassThru)
To process the response received from an ended audio capture, use the withResponseHandler property in SDLManager's send(_ :) function.
[self.sdlManager sendRequest:audioPassThru withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) { if (error || ![response isKindOfClass:SDLPerformAudioPassThruResponse.class]) { return; } SDLPerformAudioPassThruResponse *audioPassThruResponse = (SDLPerformAudioPassThruResponse *)response; if (!response.success.boolValue) { <#Cancel any usage of the audio data#> return; } <#Process audio data#> }];
sdlManager.send(request: audioPassThru) { (request, response, error) in guard let response = response else { return } guard response?.success.boolValue == true else { <#Cancel any usage of the audio data#> return } <#Process audio data#> }