Audio hooking
PlanetKit provides the audio hooking function, which allows you to get the audio data before Planet Cloud transmits audio to a peer. This function can be used to modify the local user's voice or use the audio data for speech recognition.
Overview
HookAudioExtension
provides methods to use the audio hooking function.
Enable audio hooking
To enable audio hooking, call enableHookMyAudio()
with the implementation of PlanetKitCallHookedAudioHandler
.
Modify audio data (Optional)
To modify the hooked audio data, use setData()
of PlanetKitHookedAudio
.
The setData()
method can only modify the raw audio data (data
), and metadata such as channel
, sampleCount
, sampleRate
, sampleType
, and seq
cannot be changed.
Put back the audio data
To put the hooked audio back for transmission, call putHookedMyAudioBack()
with a PlanetKitHookedAudio
object.
After enabling audio hooking, you must put back the audio regardless of whether the audio data has been modified. Otherwise, audio is not transmitted to the peer.
Disable audio hooking
To disable audio hooking, call disableHookMyAudio()
.
Sample code
The following sample code shows how to use the audio hooking function for voice modification.
func modifyVoice(PlanetKitCall call) async {
final audioClipper = PlanetKitCallHookedAudioHandler(
onHook: (call, audio) async {
if(await audio.setData(clipAudioData(audio.data, 0.01)) {
await call.putHookedMyAudioBack(audio);
}
},
);
final result = await call.enableHookMyAudio(audioClipper);
print("enableHookMyAudio result: $result")
}
Uint8List clipAudioData(Uint8List audioData, double threshold) {
var floatData = audioData.buffer.asFloat32List();
for (int i = 0; i < floatData.length; i++) {
double value = floatData[i];
if (value > threshold) {
floatData[i] = threshold;
} else if (value < -threshold) {
floatData[i] = -threshold;
}
}
return Float32List.fromList(floatData).buffer.asUint8List();
}
Related API
APIs related to the audio hooking function are as follows: