Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
work with 3 notes
  • Loading branch information
dgruss committed Sep 21, 2025
commit 8efb081a50359e8af937df815c181a227c23eaa1
Binary file modified game/sounds/ping.wav
Binary file not shown.
69 changes: 62 additions & 7 deletions src/screens/UScreenOptionsRecord.pas
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ TScreenOptionsRecord = class(TMenu)
NextVolumePollTime: cardinal;

// Mic Delay measurement
PingTime: integer;
PingResponse: integer;
PingTime: integer;
PingNoteStarted: array[0..2] of integer;
PingNoteEnded: array[0..2] of integer;
PingLastValue: integer;
PingLastValueStarted: integer;
PingSustained: integer;

procedure StartPreview;
procedure StopPreview;
Expand Down Expand Up @@ -139,6 +143,7 @@ implementation
TextGL;

function TScreenOptionsRecord.ParseInput(PressedKey: cardinal; CharCode: UCS4Char; PressedDown: boolean): boolean;
var i: integer;
begin
Result := true;
if (PressedDown) then
Expand Down Expand Up @@ -177,7 +182,11 @@ function TScreenOptionsRecord.ParseInput(PressedKey: cardinal; CharCode: UCS4Cha
SDLK_W:
begin
SoundLib.Ping.Volume := 1.0;
PingResponse := 0;
for i := Low(PingNoteStarted) to High(PingNoteStarted) do
begin
PingNoteStarted[i] := 0;
PingNoteEnded[i] := 0;
end;
PingTime := SDL_GetTicks;
SoundLib.Ping.Play;
Exit;
Expand Down Expand Up @@ -261,11 +270,16 @@ constructor TScreenOptionsRecord.Create;
InputDevice: TAudioInputDevice;
InputDeviceCfg: PInputDeviceConfig;
WidgetYPos: integer;
i: integer;
begin
inherited Create;

PingTime := 0;
PingResponse := 0;
for i := Low(PingNoteStarted) to High(PingNoteStarted) do
begin
PingNoteStarted[i] := -1;
PingNoteEnded[i] := -1;
end;

LoadFromTheme(Theme.OptionsRecord);

Expand Down Expand Up @@ -795,12 +809,53 @@ procedure TScreenOptionsRecord.DrawDelay(const State: TDrawState; x, y, Width, H
var
DelayString: string;
DelayStringWidth, DelayStringMaxWidth, DelayStringCenterXOffset: real;
i: integer;
begin
PreviewChannel.AnalyzeBuffer();

if (PreviewChannel.ToneAbs = 48) and (PingResponse = 0) then
PingResponse := SDL_GetTicks - PingTime - 5; // 5 ms delay in the wav file
DelayString := 'Delay: ' + IntToStr(PingResponse) + ' ms';
if (PreviewChannel.ToneAbs = PingLastValue) then
begin
Inc(PingSustained);
if (PingSustained > 5) then
if (PingLastValue = 24) and (PingNoteStarted[0] = 0) then
PingNoteStarted[0] := PingLastValueStarted
else if (PingNoteStarted[0] > 0) and (PingLastValue = 31) and (PingNoteStarted[1] = 0) then
PingNoteStarted[1] := PingLastValueStarted
else if (PingNoteStarted[0] > 0) and (PingNoteStarted[1] > 0) and (PingLastValue = 40) and (PingNoteStarted[2] = 0) then
PingNoteStarted[2] := PingLastValueStarted;
if (PingNoteStarted[0] > 0) and (PingLastValue <> 24) and (PingNoteEnded[0] = 0) then
PingNoteEnded[0] := PingLastValueStarted
else if (PingNoteStarted[0] > 0) and (PingNoteStarted[1] > 0) and (PingLastValue <> 31) and (PingNoteEnded[1] = 0) then
PingNoteEnded[1] := PingLastValueStarted
else if (PingNoteStarted[0] > 0) and (PingNoteStarted[2] > 0) and (PingNoteStarted[3] > 0) and (PingLastValue <> 40) and (PingNoteEnded[2] = 0) then
PingNoteEnded[2] := PingLastValueStarted;
end
else
begin
PingSustained := 0;
PingLastValue := PreviewChannel.ToneAbs;
PingLastValueStarted := SDL_GetTicks;
end;

// calculate response time: the file is 0.5s per note, so PingResponse = actual mic delay + 0 for C4, actual mic delay + 0.5s for G4, actual mic delay + 1s for E5, and the notes end at actual mic delay + 1.5s this should give us 4 timestamps, and 4 different mic delays. let's print them all
if (PingNoteEnded[2] > 0) then
begin
Log.LogInfo('PingNoteStarted: ' + IntToStr(PingNoteStarted[0] - PingTime) + ' instead of 0ms', 'TScreenOptionsRecord.DrawDelay');
Log.LogInfo('PingNoteStarted: ' + IntToStr(PingNoteStarted[1] - PingTime) + ' instead of 900ms', 'TScreenOptionsRecord.DrawDelay');
Log.LogInfo('PingNoteStarted: ' + IntToStr(PingNoteStarted[2] - PingTime) + ' instead of 1800ms', 'TScreenOptionsRecord.DrawDelay');
Log.LogInfo('PingNoteEnded: ' + IntToStr(PingNoteEnded[0] - PingTime) + ' instead of 900ms', 'TScreenOptionsRecord.DrawDelay');
Log.LogInfo('PingNoteEnded: ' + IntToStr(PingNoteEnded[1] - PingTime) + ' instead of 1800ms', 'TScreenOptionsRecord.DrawDelay');
Log.LogInfo('PingNoteEnded: ' + IntToStr(PingNoteEnded[2] - PingTime) + ' instead of 2700ms', 'TScreenOptionsRecord.DrawDelay');
for i := Low(PingNoteStarted) to High(PingNoteStarted) do
begin
PingNoteStarted[i] := -1;
PingNoteEnded[i] := -1;
end;
end;

//if (PreviewChannel.ToneAbs = 48) and (PingResponse = 0) then
// PingResponse := SDL_GetTicks - PingTime - 5; // 5 ms delay in the wav file
//DelayString := 'Delay: ' + IntToStr(PingResponse) + ' ms';

SetFontSize(Height*2);

Expand Down