From cc22cb99591a089c18e54e0e4a3fe1450f66abcc Mon Sep 17 00:00:00 2001 From: jan-weil <52463906+jan-weil@users.noreply.github.com> Date: Wed, 17 Jan 2024 08:42:18 +0100 Subject: [PATCH] Minor changes to the readme --- README.md | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 6c693aa..fe7f877 100644 --- a/README.md +++ b/README.md @@ -4,32 +4,41 @@ A GDExtension that adds to Godot4 support for encoding voice data using the Opus ## Overview -This extension adds a new singleton to Godot: `Opus` with two methods: `encode` and `decode`. +This extension adds a new node to Godot: `Opus` with three methods: `encode`, `decode` and `decode_and_play`. These can be used to compress audio obtained `AudioEffectCapture` and then to decode it so it's usable in Godot again. -Quick and dirty example (full demo coming soon): +Usage is best illustrated in this demo: https://github.com/microtaur/godot4-p2p-voip + +Most interesting part from the demo code illustrating how this works: ```GDScript -func _process_audio() -> void: - if has_data() and active: - call_deferred("rpc", "play_data", get_data()) +var _encoder := Opus.new() + +... + +func _audio_process(): + while true: + if has_data() and active: + var data = get_data() + call_deferred("rpc", "play_data", data) + else: + OS.delay_msec(10) ... func get_data() -> PackedFloat32Array: - var data = effect.get_buffer(BUFFER_SIZE) - return Opus.encode(data) + var data = effect.get_buffer(BUFFER_SIZE) + return _encoder.encode(data) ... -@rpc("any_peer", "call_remote", "unreliable_ordered") +@rpc("any_peer", "call_remote", "reliable") func play_data(data: PackedFloat32Array) -> void: - var id = client.multiplayer.get_remote_sender_id() - var decoded = Opus.decode(data) - for b in range(0, BUFFER_SIZE): - _get_generator(id).push_frame(decoded[b]) + var id = client.multiplayer.get_remote_sender_id() + _update_player_pool() + _get_opus_instance(id).decode_and_play(_get_generator(id), data) ```