Skip to content

Commit

Permalink
Merge pull request #41 from goatchurchprime/audiogeneratorstereoslip
Browse files Browse the repository at this point in the history
stereo slip commit
  • Loading branch information
goatchurchprime authored Dec 14, 2024
2 parents d863c38 + 282a0a0 commit 1d8917a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
6 changes: 5 additions & 1 deletion example/radiomqtt/member.gd
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ func _process(delta):

else:
while audiostreamgeneratorplayback.get_frames_available() > audiosamplesize:
if len(audiopacketsbuffer) != 0:
if resampledpacketsbuffer != null and len(resampledpacketsbuffer) != 0:
var resampledaudiochunk = resampledpacketsbuffer.pop_front()
var audiochunk = audiostreamopuschunked.resample_chunk(resampledaudiochunk)
audiostreamgeneratorplayback.push_buffer(audiochunk)
elif len(audiopacketsbuffer) != 0:
audiostreamgeneratorplayback.push_buffer(audiopacketsbuffer.pop_front())
elif len(opuspacketsbuffer) != 0:
const fec = 0
Expand Down
6 changes: 5 additions & 1 deletion example/radiomqtt/member.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
[gd_scene load_steps=5 format=3 uid="uid://c1ibcb1toyerh"]
[gd_scene load_steps=6 format=3 uid="uid://c1ibcb1toyerh"]

[ext_resource type="Script" path="res://radiomqtt/member.gd" id="1_o3hks"]
[ext_resource type="Texture2D" uid="uid://cvyjsyigutxbh" path="res://icon.png" id="2_atirx"]

[sub_resource type="AudioStreamGenerator" id="AudioStreamGenerator_t0bqw"]
resource_local_to_scene = true

[sub_resource type="Shader" id="Shader_022wp"]
code = "// NOTE: Shader automatically converted from Godot Engine 4.3.beta's CanvasItemMaterial.

Expand Down Expand Up @@ -35,6 +38,7 @@ color = Color(0.423529, 0, 0.423529, 1)
script = ExtResource("1_o3hks")

[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]
stream = SubResource("AudioStreamGenerator_t0bqw")
bus = &"SpeechBus"

[node name="Label" type="Label" parent="."]
Expand Down
41 changes: 36 additions & 5 deletions example/radiomqtt/radiomqtt.gd
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ func _ready():

SelfMember.audiobufferregulationtime = 3600.0



func rechunkrecordedchunks(orgsamples, newsamplesize):
assert (newsamplesize > 0)
var res = [ ]
Expand Down Expand Up @@ -291,6 +293,8 @@ func _input(event):


func _process(_delta):
#Dmicrophonestreamplayback.mix()

var talking = $HBoxBigButtons/VBoxPTT/PTT.button_pressed
if talking:
$VBoxPlayback/HBoxPlaycount/GridContainer/TimeSecs.text = "%.1f" % ((Time.get_ticks_msec() - talkingstarttime)*0.001)
Expand Down Expand Up @@ -394,6 +398,33 @@ func endtalking():
$MQTTnetwork.transportaudiopacketjson({"framecount":len(recordedsamples)})
print("Talked for ", (Time.get_ticks_msec() - talkingstarttime)*0.001, " seconds")



func duplicatewithslippage(audiopackets, lframeslip):
var frameslip = abs(lframeslip)
var bleftside = (lframeslip >= 0)
var slippedaudiopackets = [ ]
for i in range(1, len(audiopackets)):
var prevpacket = audiopackets[i-1]
var packet = audiopackets[i]
var dpacket = packet.duplicate()
var n = len(packet)
if bleftside:
for j in range(n):
if j < frameslip:
dpacket[j].x = prevpacket[n - frameslip + j].x
else:
dpacket[j].x = packet[j - frameslip].x
else:
for j in range(n):
if j < frameslip:
dpacket[j].y = prevpacket[n - frameslip + j].y
else:
dpacket[j].y = packet[j - frameslip].y
slippedaudiopackets.push_back(dpacket)
return slippedaudiopackets


func _on_play_pressed():
if audioeffectpitchshift != null:
var speedup = $VBoxPlayback/HBoxStream/StreamSpeedup.value
Expand All @@ -408,11 +439,11 @@ func _on_play_pressed():
SelfMember.opuspacketsbuffer = recordedopuspackets.duplicate()
elif recordedresampledpackets != null:
SelfMember.processheaderpacket(h)
SelfMember.resampledpacketsbuffer = recordedresampledpackets.duplicate()
var resampledaudiochunk_blank = PackedVector2Array()
resampledaudiochunk_blank.resize(h["opusframesize"])
for i in range(5):
SelfMember.audiostreamopuschunked.resample_chunk(resampledaudiochunk_blank)
var stereoframeslip = $VBoxPlayback/HBoxStream/StereoFrameSlip.value
if stereoframeslip == 0:
SelfMember.resampledpacketsbuffer = recordedresampledpackets.duplicate()
else:
SelfMember.resampledpacketsbuffer = duplicatewithslippage(recordedresampledpackets, stereoframeslip)

elif recordedsamples and SelfMember.audiostreamgeneratorplayback != null:
SelfMember.audiosamplesize = audiosamplesize
Expand Down
10 changes: 8 additions & 2 deletions example/radiomqtt/radiomqtt.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ text = "Play"

[node name="LabelSamplerate" type="Label" parent="VBoxPlayback/HBoxStream"]
layout_mode = 2
text = "sample rate"
text = "sample"

[node name="OutSampleRate" type="SpinBox" parent="VBoxPlayback/HBoxStream"]
custom_minimum_size = Vector2(95, 0)
Expand All @@ -539,7 +539,7 @@ suffix = "Hz"

[node name="LabelMixrate" type="Label" parent="VBoxPlayback/HBoxStream"]
layout_mode = 2
text = "mix rate"
text = "mix"

[node name="MixRate" type="SpinBox" parent="VBoxPlayback/HBoxStream"]
custom_minimum_size = Vector2(95, 0)
Expand All @@ -564,6 +564,12 @@ max_value = 3.0
step = 0.1
value = 1.0

[node name="StereoFrameSlip" type="SpinBox" parent="VBoxPlayback/HBoxStream"]
layout_mode = 2
tooltip_text = "stereo frame slip"
min_value = -900.0
max_value = 900.0

[node name="HSeparator5" type="HSeparator" parent="."]
custom_minimum_size = Vector2(0, 20)
layout_mode = 2
Expand Down

0 comments on commit 1d8917a

Please sign in to comment.