Skip to content

Fix delay caused by python I/O buffering#38

Merged
gdzx merged 2 commits intogdzx:masterfrom
FarsadM:fix-buffering
Mar 10, 2026
Merged

Fix delay caused by python I/O buffering#38
gdzx merged 2 commits intogdzx:masterfrom
FarsadM:fix-buffering

Conversation

@FarsadM
Copy link
Copy Markdown
Contributor

@FarsadM FarsadM commented Feb 23, 2026

Fixes Issue #36

Copy link
Copy Markdown
Owner

@gdzx gdzx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR. Can you explain in detail what change addresses which issue? In particular, removing SETPIPE_SZ means that the pipe size is 65536 bytes, whereas before it was constrained to 4096 bytes, so this seems to add some buffering (hence latency that can build up over time until the pipe is full).

Comment thread audiosource Outdated
echo '[+] Loading PulseAudio module'

pactl load-module module-pipe-source source_name="$AUDIOSOURCE_NAME" channels=1 format=s16 rate=44100 file="/tmp/$AUDIOSOURCE_NAME"
pactl load-module module-pipe-source source_name="$AUDIOSOURCE_NAME" channels=1 format=s16 rate=48000 file="/tmp/$AUDIOSOURCE_NAME"
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't match what is requested by the app

private static final int DEFAULT_SAMPLE_RATE = 44100;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True I changed it back to the 44100 in the new PR update.
I tried to set it to 48000 since its the default Pipewire sample rate, though I noticed it was ruining the audio because I didn't change the app rate. thank you for pointing it out for me.
keeping it on 44100 is perfectly fine.

Comment thread audiosource Outdated
except BlockingIOError:
pass
os.write(out_fd, data)
except (BlockingIOError, BrokenPipeError):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it to hide the exception when disconnecting?

Comment thread audiosource Outdated
while True:
n = inp.recv_into(buf, BUF_SIZE, socket.MSG_WAITALL)
try:
data = inp.recv(BUF_SIZE)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this allocates a new buffer for each iteration.

@FarsadM
Copy link
Copy Markdown
Contributor Author

FarsadM commented Feb 24, 2026

Thank you for building this amazing project.
Sorry about the garbage PR I made earlier.
After reviewing the changes and the original implementation, I realized the changes I made earlier was unnecessary and could potentially cause more problems. so I've reverted them.

There was roughly a one second input delay, likely caused by the internal python I/O buffering in the line out.write(buf). so I used a direct system call os.write(out.fileno(), buf) instead which resolved the delay issue.

@FarsadM FarsadM changed the title Fix delay and cutoff by removing the buffering and optimizing audio s… Fix delay caused by python I/O buffering Feb 24, 2026
gdzx added a commit that referenced this pull request Feb 26, 2026
Co-authored-by: Farsad <far.sad.shift@gmail.com>
@gdzx gdzx merged commit 0ac29d6 into gdzx:master Mar 10, 2026
@gdzx
Copy link
Copy Markdown
Owner

gdzx commented Mar 10, 2026

@FarsadM Really appreciate you digging into this issue, great contribution, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants