From cd9e504e8fe480e5fac22439da82ddca8eda039a Mon Sep 17 00:00:00 2001 From: Uladzimir Karpenka Date: Thu, 21 May 2026 18:31:48 +0300 Subject: [PATCH] Optimize web audio mic path: remove redundant copy and heap alloc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WebOnMic: drop intermediate Singles[] array, read PSingle directly. PushAudio: replace per-frame SetLength/array of Byte with pre-allocated FWsAudioBuf field — eliminates heap allocation from the DSP thread. Co-Authored-By: Claude Sonnet 4.6 --- MainForm.pas | 13 ++++++++----- WebServer.pas | 9 ++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/MainForm.pas b/MainForm.pas index 21b383b..99e71d3 100644 --- a/MainForm.pas +++ b/MainForm.pas @@ -6135,16 +6135,19 @@ end; procedure TMainForm.WebOnMic(Samples: PSingle; Count: Integer); var - Singles: array[0..5759] of Single; - Buf: array[0..5759] of Double; - i, n: Integer; + Buf: array[0..5759] of Double; + SP: PSingle; + i, n: Integer; begin if not FWDSPReady then Exit; n := Count; if n > Length(Buf) then n := Length(Buf); - Move(Samples^, Singles[0], n * SizeOf(Single)); + SP := Samples; for i := 0 to n - 1 do - Buf[i] := Singles[i]; + begin + Buf[i] := SP^; + Inc(SP); + end; FDSPEngine.PushTXMicSamplesD(Buf, n); end; diff --git a/WebServer.pas b/WebServer.pas index 2606ca8..6d86252 100644 --- a/WebServer.pas +++ b/WebServer.pas @@ -172,6 +172,7 @@ type FOpusBuf: array[0..OPUS_FRAME_SAMP-1] of Single; FOpusBufPos: Integer; FOpusOut: array[0..3999] of Byte; + FWsAudioBuf: array[0..4000] of Byte; // 1 байт типа + до 4000 байт Opus FOpusReady: Boolean; // Opus decoder — для RX TX-mic аудио от браузера FOpusDec: POpusDecoder; @@ -1429,7 +1430,6 @@ procedure TWebServer.PushAudio(const Samples: PSingle; Count: Integer); var HasWs: Boolean; i, n, Enc: Integer; - Pkt: array of Byte; begin if not FRunning then Exit; FClientLock.Enter; @@ -1454,10 +1454,9 @@ begin @FOpusOut[0], SizeOf(FOpusOut)); if Enc > 0 then begin - SetLength(Pkt, 1 + Enc); - Pkt[0] := WS_MSG_AUDIO; - Move(FOpusOut[0], Pkt[1], Enc); - BroadcastBinary(Pkt[0], Length(Pkt)); + FWsAudioBuf[0] := WS_MSG_AUDIO; + Move(FOpusOut[0], FWsAudioBuf[1], Enc); + BroadcastBinary(FWsAudioBuf[0], 1 + Enc); end; FOpusBufPos := 0; end;